|
| 1 | +package com.itasocialacademy.oitassist.envvar.service; |
| 2 | + |
| 3 | +import static com.itasocialacademy.oitassist.envvar.dao.enums.AccessMode.ALL; |
| 4 | +import static com.itasocialacademy.oitassist.envvar.dao.enums.AccessMode.BLACKLIST; |
| 5 | +import static com.itasocialacademy.oitassist.envvar.dao.enums.AccessMode.WHITELIST; |
| 6 | +import static org.assertj.core.api.Assertions.assertThat; |
| 7 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import com.itasocialacademy.oitassist.envvar.dao.enums.AccessMode; |
| 11 | +import com.itasocialacademy.oitassist.envvar.properties.EnvVariableProperties; |
| 12 | +import com.itasocialacademy.oitassist.envvar.provider.interfaces.EnvVariableProvider; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.Set; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 18 | +import org.mockito.Mock; |
| 19 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 20 | + |
| 21 | +@ExtendWith(MockitoExtension.class) |
| 22 | +class EnvVariableServiceImplTest { |
| 23 | + |
| 24 | + private static final String PUBLIC_KEY = "APP_NAME"; |
| 25 | + private static final String PUBLIC_VALUE = "oit-assist"; |
| 26 | + private static final String RESTRICTED_KEY = "JWT_SIGN_KEY"; |
| 27 | + private static final String RESTRICTED_VALUE = "sign-key-value"; |
| 28 | + private static final String ABSENT_KEY = "NOT_SET_IN_ENVIRONMENT"; |
| 29 | + |
| 30 | + @Mock |
| 31 | + private EnvVariableProvider envVariableProvider; |
| 32 | + |
| 33 | + @Test |
| 34 | + void getenv_ShouldReturnEveryVariable_WhenAccessModeIsAll() { |
| 35 | + when(envVariableProvider.getenv()).thenReturn(environment()); |
| 36 | + |
| 37 | + Map<String, String> result = createService(ALL, null, null).getenv(); |
| 38 | + |
| 39 | + assertThat(result).containsOnlyKeys(PUBLIC_KEY, RESTRICTED_KEY); |
| 40 | + assertThat(result).containsEntry(PUBLIC_KEY, PUBLIC_VALUE); |
| 41 | + assertThat(result).containsEntry(RESTRICTED_KEY, RESTRICTED_VALUE); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void getenv_ShouldReturnOnlyListedKeys_WhenAccessModeIsWhitelist() { |
| 46 | + when(envVariableProvider.getenv()).thenReturn(environment()); |
| 47 | + |
| 48 | + Map<String, String> result = createService(WHITELIST, Set.of(PUBLIC_KEY), null).getenv(); |
| 49 | + |
| 50 | + assertThat(result).containsOnlyKeys(PUBLIC_KEY); |
| 51 | + assertThat(result).containsEntry(PUBLIC_KEY, PUBLIC_VALUE); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void getenv_ShouldIgnoreListedKey_WhenItIsAbsentFromTheEnvironment() { |
| 56 | + when(envVariableProvider.getenv()).thenReturn(environment()); |
| 57 | + |
| 58 | + Map<String, String> result = |
| 59 | + createService(WHITELIST, Set.of(PUBLIC_KEY, ABSENT_KEY), null).getenv(); |
| 60 | + |
| 61 | + assertThat(result).containsOnlyKeys(PUBLIC_KEY); |
| 62 | + assertThat(result).doesNotContainKey(ABSENT_KEY); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void getenv_ShouldReturnNothing_WhenAccessModeIsWhitelistAndNoKeyIsListed() { |
| 67 | + when(envVariableProvider.getenv()).thenReturn(environment()); |
| 68 | + |
| 69 | + Map<String, String> result = createService(WHITELIST, null, null).getenv(); |
| 70 | + |
| 71 | + assertThat(result).isEmpty(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void getenv_ShouldExcludeListedKeys_WhenAccessModeIsBlacklist() { |
| 76 | + when(envVariableProvider.getenv()).thenReturn(environment()); |
| 77 | + |
| 78 | + Map<String, String> result = createService(BLACKLIST, null, Set.of(RESTRICTED_KEY)).getenv(); |
| 79 | + |
| 80 | + assertThat(result).containsOnlyKeys(PUBLIC_KEY); |
| 81 | + assertThat(result).doesNotContainKey(RESTRICTED_KEY); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void getenv_ShouldReturnEveryVariable_WhenAccessModeIsBlacklistAndNoKeyIsListed() { |
| 86 | + when(envVariableProvider.getenv()).thenReturn(environment()); |
| 87 | + |
| 88 | + Map<String, String> result = createService(BLACKLIST, null, null).getenv(); |
| 89 | + |
| 90 | + assertThat(result).containsOnlyKeys(PUBLIC_KEY, RESTRICTED_KEY); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void getenv_ShouldKeepTheKey_WhenItsValueIsNull() { |
| 95 | + Map<String, String> environment = new HashMap<>(); |
| 96 | + environment.put(PUBLIC_KEY, null); |
| 97 | + when(envVariableProvider.getenv()).thenReturn(environment); |
| 98 | + |
| 99 | + Map<String, String> result = createService(BLACKLIST, null, null).getenv(); |
| 100 | + |
| 101 | + assertThat(result).containsKey(PUBLIC_KEY); |
| 102 | + assertThat(result.get(PUBLIC_KEY)).isNull(); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void getenv_ShouldReturnUnmodifiableMap_WhenAccessModeIsAll() { |
| 107 | + when(envVariableProvider.getenv()).thenReturn(environment()); |
| 108 | + |
| 109 | + Map<String, String> result = createService(ALL, null, null).getenv(); |
| 110 | + |
| 111 | + assertThatThrownBy(() -> result.put(ABSENT_KEY, PUBLIC_VALUE)) |
| 112 | + .isInstanceOf(UnsupportedOperationException.class); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void getenv_ShouldReturnUnmodifiableMap_WhenFilteringIsApplied() { |
| 117 | + when(envVariableProvider.getenv()).thenReturn(environment()); |
| 118 | + |
| 119 | + Map<String, String> result = createService(BLACKLIST, null, Set.of(RESTRICTED_KEY)).getenv(); |
| 120 | + |
| 121 | + assertThatThrownBy(() -> result.put(RESTRICTED_KEY, RESTRICTED_VALUE)) |
| 122 | + .isInstanceOf(UnsupportedOperationException.class); |
| 123 | + } |
| 124 | + |
| 125 | + private EnvVariableServiceImpl createService(AccessMode accessMode, Set<String> whitelist, |
| 126 | + Set<String> blacklist) { |
| 127 | + return new EnvVariableServiceImpl( |
| 128 | + envVariableProvider, |
| 129 | + new EnvVariableProperties(accessMode, whitelist, blacklist)); |
| 130 | + } |
| 131 | + |
| 132 | + private Map<String, String> environment() { |
| 133 | + Map<String, String> environment = new HashMap<>(); |
| 134 | + environment.put(PUBLIC_KEY, PUBLIC_VALUE); |
| 135 | + environment.put(RESTRICTED_KEY, RESTRICTED_VALUE); |
| 136 | + return environment; |
| 137 | + } |
| 138 | +} |
0 commit comments