|
| 1 | +package io.securecodebox.persistence.defectdojo.http; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 5 | +import uk.org.webcompere.systemstubs.jupiter.SystemStub; |
| 6 | +import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; |
| 7 | +import uk.org.webcompere.systemstubs.properties.SystemProperties; |
| 8 | + |
| 9 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 10 | +import static org.hamcrest.Matchers.is; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests for {@link ProxyConfigFactory} |
| 15 | + */ |
| 16 | +@ExtendWith(SystemStubsExtension.class) |
| 17 | +class ProxyConfigFactoryTest { |
| 18 | + @SystemStub |
| 19 | + private SystemProperties restoreSystemProperties; |
| 20 | + private final ProxyConfigFactory sut = new ProxyConfigFactory(); |
| 21 | + |
| 22 | + @Test |
| 23 | + void create_throesExceptionIfUserNotSet() { |
| 24 | + System.clearProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_USER.getLiterat()); |
| 25 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password"); |
| 26 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host"); |
| 27 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242"); |
| 28 | + |
| 29 | + final var thrown = assertThrows( |
| 30 | + ProxyConfigFactory.MissingProxyConfigValue.class, |
| 31 | + sut::create); |
| 32 | + |
| 33 | + assertThat(thrown.getMessage(), is("Expected System property 'http.proxyUser' not set!")); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void create_throesExceptionIfPasswordNotSet() { |
| 38 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user"); |
| 39 | + System.clearProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat()); |
| 40 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host"); |
| 41 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242"); |
| 42 | + |
| 43 | + final var thrown = assertThrows( |
| 44 | + ProxyConfigFactory.MissingProxyConfigValue.class, |
| 45 | + sut::create); |
| 46 | + |
| 47 | + assertThat(thrown.getMessage(), is("Expected System property 'http.proxyPassword' not set!")); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void create_throesExceptionIfHostNotSet() { |
| 52 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user"); |
| 53 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password"); |
| 54 | + System.clearProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat()); |
| 55 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "4242"); |
| 56 | + |
| 57 | + final var thrown = assertThrows( |
| 58 | + ProxyConfigFactory.MissingProxyConfigValue.class, |
| 59 | + sut::create); |
| 60 | + |
| 61 | + assertThat(thrown.getMessage(), is("Expected System property 'http.proxyHost' not set!")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void create_throesExceptionIfPortNotSet() { |
| 66 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user"); |
| 67 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password"); |
| 68 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host"); |
| 69 | + System.clearProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat()); |
| 70 | + |
| 71 | + final var thrown = assertThrows( |
| 72 | + ProxyConfigFactory.MissingProxyConfigValue.class, |
| 73 | + sut::create); |
| 74 | + |
| 75 | + assertThat(thrown.getMessage(), is("Expected System property 'http.proxyPort' not set!")); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void create_throesExceptionIfPortIsNotInteger() { |
| 80 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_USER.getLiterat(), "user"); |
| 81 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PASSWORD.getLiterat(), "password"); |
| 82 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_HOST.getLiterat(), "host"); |
| 83 | + System.setProperty(ProxyConfigFactory.ProxyConfigNames.HTTP_PROXY_PORT.getLiterat(), "FUBAR"); |
| 84 | + |
| 85 | + final var thrown = assertThrows( |
| 86 | + IllegalArgumentException.class, |
| 87 | + sut::create); |
| 88 | + |
| 89 | + assertThat( |
| 90 | + thrown.getMessage(), |
| 91 | + is("Given port for proxy authentication configuration (property 'http.proxyPort') is not a valid number! Given value wa 'FUBAR'.")); |
| 92 | + } |
| 93 | +} |
0 commit comments