X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cadi%2Fclient%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Faaf%2Fcadi%2Fhttp%2Ftest%2FJU_HMangr.java;h=9d87d6993d16ccb974742c7b46c2bbc1633c323d;hb=4b5a7d721d994a49057e9bfb403c7bff1b376660;hp=b7415a5284ad2ccadec801c5347cec6d8b833498;hpb=6a93111c678a3970ab2f1667511d59d5fba458f6;p=aaf%2Fauthz.git diff --git a/cadi/client/src/test/java/org/onap/aaf/cadi/http/test/JU_HMangr.java b/cadi/client/src/test/java/org/onap/aaf/cadi/http/test/JU_HMangr.java index b7415a52..9d87d699 100644 --- a/cadi/client/src/test/java/org/onap/aaf/cadi/http/test/JU_HMangr.java +++ b/cadi/client/src/test/java/org/onap/aaf/cadi/http/test/JU_HMangr.java @@ -50,216 +50,216 @@ import org.onap.aaf.cadi.http.HMangr; import org.onap.aaf.misc.env.APIException; public class JU_HMangr { - - @Mock Locator locMock; - @Mock SecuritySetter ssMock; - @Mock Retryable retryableMock; - @Mock Retryable goodRetry; - @Mock Locator.Item itemMock; - @Mock Rcli clientMock; - - private PropAccess access; - private URI uri; - private final static String uriString = "http://example.com"; - - @Before - public void setup() throws URISyntaxException { - MockitoAnnotations.initMocks(this); - - access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]); - uri = new URI(uriString); - } - - @Test - public void sameTest() throws LocatorException, APIException, CadiException, ConnectException { - HMangr hman = new HMangr(access, locMock); - when(retryableMock.item()).thenReturn(itemMock); - when(locMock.get(itemMock)).thenReturn(uri); - assertThat(hman.same(ssMock, retryableMock), is(nullValue())); - - //coverage... - when(retryableMock.lastClient()).thenReturn(clientMock); - assertThat(hman.same(ssMock, retryableMock), is(nullValue())); - - CadiException cadiException; - - ConnectException connectException = new ConnectException(); - cadiException = new CadiException(connectException); - doThrow(cadiException).when(retryableMock).code(clientMock); - when(locMock.hasItems()).thenReturn(true).thenReturn(false); - assertThat(hman.same(ssMock, retryableMock), is(nullValue())); - - SocketException socketException = new SocketException(); - cadiException = new CadiException(socketException); - doThrow(cadiException).when(retryableMock).code(clientMock); - when(locMock.hasItems()).thenReturn(true).thenReturn(false); - assertThat(hman.same(ssMock, retryableMock), is(nullValue())); - - doThrow(connectException).when(retryableMock).code(clientMock); - assertThat(hman.same(ssMock, retryableMock), is(nullValue())); - - } - - @Test(expected = LocatorException.class) - public void throwsLocatorException1Test() throws LocatorException { - @SuppressWarnings("unused") - HMangr hman = new HMangr(access, null); - } - - @Test(expected = LocatorException.class) - public void throwsLocatorException2Test() throws LocatorException, APIException, CadiException { - HMangr hman = new HMangr(access, locMock); - hman.same(ssMock, retryableMock); - } - - @Test(expected = LocatorException.class) - public void throwsLocatorException3Test() throws LocatorException, APIException, CadiException { - HMangr hman = new HMangr(access, locMock); - when(locMock.best()).thenReturn(itemMock); - when(locMock.hasItems()).thenReturn(true).thenReturn(false); - hman.same(ssMock, retryableMock); - } - - @SuppressWarnings("unchecked") - @Test(expected = CadiException.class) - public void throwsCadiException1Test() throws LocatorException, APIException, CadiException, ConnectException { - HMangr hman = new HMangr(access, locMock); - when(retryableMock.item()).thenReturn(itemMock); - when(locMock.get(itemMock)).thenReturn(uri); - when(retryableMock.lastClient()).thenReturn(clientMock); - when(retryableMock.code(clientMock)).thenThrow(CadiException.class); - hman.same(ssMock, retryableMock); - } - - @Test(expected = CadiException.class) - public void throwsCadiException2Test() throws LocatorException, APIException, CadiException, ConnectException { - HMangr hman = new HMangr(access, locMock); - when(retryableMock.item()).thenReturn(itemMock); - when(locMock.get(itemMock)).thenReturn(uri); - when(retryableMock.lastClient()).thenReturn(clientMock); - - ConnectException connectException = new ConnectException(); - CadiException cadiException = new CadiException(connectException); - doThrow(cadiException).when(retryableMock).code(clientMock); - hman.same(ssMock, retryableMock); - } - - @Test(expected = CadiException.class) - public void throwsCadiException3Test() throws LocatorException, APIException, CadiException, ConnectException { - HMangr hman = new HMangr(access, locMock); - when(retryableMock.item()).thenReturn(itemMock); - when(locMock.get(itemMock)).thenReturn(uri); - when(retryableMock.lastClient()).thenReturn(clientMock); - - SocketException socketException = new SocketException(); - CadiException cadiException = new CadiException(socketException); - doThrow(cadiException).when(retryableMock).code(clientMock); - hman.same(ssMock, retryableMock); - } - - @Test(expected = CadiException.class) - public void throwsCadiException4Test() throws LocatorException, APIException, CadiException, ConnectException { - HMangr hman = new HMangr(access, locMock); - when(retryableMock.item()).thenReturn(itemMock); - when(locMock.get(itemMock)).thenReturn(uri); - when(retryableMock.lastClient()).thenReturn(clientMock); - - Exception e = new Exception(); - CadiException cadiException = new CadiException(e); - doThrow(cadiException).when(retryableMock).code(clientMock); - hman.same(ssMock, retryableMock); - } - - @Test - public void allTest() throws LocatorException, CadiException, APIException { - HManagerStub hman = new HManagerStub(access, locMock); - assertThat(hman.best(ssMock, retryableMock), is(nullValue())); - assertThat(hman.all(ssMock, retryableMock), is(nullValue())); - assertThat(hman.all(ssMock, retryableMock, true), is(nullValue())); - } - - @Test - public void oneOfTest() throws LocatorException, CadiException, APIException, ConnectException { - HMangr hman = new HMangr(access, locMock); - assertThat(hman.oneOf(ssMock, retryableMock, false, "host"), is(nullValue())); - - try { - hman.oneOf(ssMock, retryableMock, true, "host"); - fail("Should've thrown an exception"); - } catch (LocatorException e) { - } - - when(locMock.first()).thenReturn(itemMock); - when(locMock.get(itemMock)).thenReturn(uri); - - // Branching coverage... - assertThat(hman.oneOf(ssMock, retryableMock, false, null), is(nullValue())); - assertThat(hman.oneOf(ssMock, retryableMock, false, "host"), is(nullValue())); - - assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue())); - - CadiException cadiException; - - cadiException = new CadiException(new ConnectException()); - doThrow(cadiException).when(retryableMock).code((Rcli) any()); - assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue())); - - cadiException = new CadiException(new SSLHandshakeException(null)); - doThrow(cadiException).when(retryableMock).code((Rcli) any()); - assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue())); - - cadiException = new CadiException(new SocketException()); - doThrow(cadiException).when(retryableMock).code((Rcli) any()); - try { - hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)); - fail("Should've thrown an exception"); - } catch (CadiException e) { - } - - cadiException = new CadiException(new SocketException("java.net.SocketException: Connection reset")); - doThrow(cadiException).when(retryableMock).code((Rcli) any()); - try { - hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)); - fail("Should've thrown an exception"); - } catch (CadiException e) { - } - - cadiException = new CadiException(); - doThrow(cadiException).when(retryableMock).code((Rcli) any()); - try { - hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)); - fail("Should've thrown an exception"); - } catch (CadiException e) { - } - - doThrow(new ConnectException()).when(retryableMock).code((Rcli) any()); - assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue())); - - when(goodRetry.code((Rcli) any())).thenReturn(5); - assertThat(hman.oneOf(ssMock, goodRetry, false, uriString.substring(7)), is(5)); - } - - @Test - public void coverageTest() throws LocatorException { - HMangr hman = new HMangr(access, locMock); - hman.readTimeout(5); - assertThat(hman.readTimeout(), is(5)); - hman.connectionTimeout(5); - assertThat(hman.connectionTimeout(), is(5)); - hman.apiVersion("v1.0"); - assertThat(hman.apiVersion(), is("v1.0")); - hman.close(); - - } - - private class HManagerStub extends HMangr { - public HManagerStub(Access access, Locator loc) throws LocatorException { super(access, loc); } - @Override public RET same(SecuritySetter ss, Retryable retryable) { - return null; - } - @Override public RET oneOf(SecuritySetter ss, Retryable retryable, boolean notify, String host) { - return null; - } - } - + + @Mock Locator locMock; + @Mock SecuritySetter ssMock; + @Mock Retryable retryableMock; + @Mock Retryable goodRetry; + @Mock Locator.Item itemMock; + @Mock Rcli clientMock; + + private PropAccess access; + private URI uri; + private final static String uriString = "http://example.com"; + + @Before + public void setup() throws URISyntaxException { + MockitoAnnotations.initMocks(this); + + access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]); + uri = new URI(uriString); + } + + @Test + public void sameTest() throws LocatorException, APIException, CadiException, ConnectException { + HMangr hman = new HMangr(access, locMock); + when(retryableMock.item()).thenReturn(itemMock); + when(locMock.get(itemMock)).thenReturn(uri); + assertThat(hman.same(ssMock, retryableMock), is(nullValue())); + + //coverage... + when(retryableMock.lastClient()).thenReturn(clientMock); + assertThat(hman.same(ssMock, retryableMock), is(nullValue())); + + CadiException cadiException; + + ConnectException connectException = new ConnectException(); + cadiException = new CadiException(connectException); + doThrow(cadiException).when(retryableMock).code(clientMock); + when(locMock.hasItems()).thenReturn(true).thenReturn(false); + assertThat(hman.same(ssMock, retryableMock), is(nullValue())); + + SocketException socketException = new SocketException(); + cadiException = new CadiException(socketException); + doThrow(cadiException).when(retryableMock).code(clientMock); + when(locMock.hasItems()).thenReturn(true).thenReturn(false); + assertThat(hman.same(ssMock, retryableMock), is(nullValue())); + + doThrow(connectException).when(retryableMock).code(clientMock); + assertThat(hman.same(ssMock, retryableMock), is(nullValue())); + + } + + @Test(expected = LocatorException.class) + public void throwsLocatorException1Test() throws LocatorException { + @SuppressWarnings("unused") + HMangr hman = new HMangr(access, null); + } + + @Test(expected = LocatorException.class) + public void throwsLocatorException2Test() throws LocatorException, APIException, CadiException { + HMangr hman = new HMangr(access, locMock); + hman.same(ssMock, retryableMock); + } + + @Test(expected = LocatorException.class) + public void throwsLocatorException3Test() throws LocatorException, APIException, CadiException { + HMangr hman = new HMangr(access, locMock); + when(locMock.best()).thenReturn(itemMock); + when(locMock.hasItems()).thenReturn(true).thenReturn(false); + hman.same(ssMock, retryableMock); + } + + @SuppressWarnings("unchecked") + @Test(expected = CadiException.class) + public void throwsCadiException1Test() throws LocatorException, APIException, CadiException, ConnectException { + HMangr hman = new HMangr(access, locMock); + when(retryableMock.item()).thenReturn(itemMock); + when(locMock.get(itemMock)).thenReturn(uri); + when(retryableMock.lastClient()).thenReturn(clientMock); + when(retryableMock.code(clientMock)).thenThrow(CadiException.class); + hman.same(ssMock, retryableMock); + } + + @Test(expected = CadiException.class) + public void throwsCadiException2Test() throws LocatorException, APIException, CadiException, ConnectException { + HMangr hman = new HMangr(access, locMock); + when(retryableMock.item()).thenReturn(itemMock); + when(locMock.get(itemMock)).thenReturn(uri); + when(retryableMock.lastClient()).thenReturn(clientMock); + + ConnectException connectException = new ConnectException(); + CadiException cadiException = new CadiException(connectException); + doThrow(cadiException).when(retryableMock).code(clientMock); + hman.same(ssMock, retryableMock); + } + + @Test(expected = CadiException.class) + public void throwsCadiException3Test() throws LocatorException, APIException, CadiException, ConnectException { + HMangr hman = new HMangr(access, locMock); + when(retryableMock.item()).thenReturn(itemMock); + when(locMock.get(itemMock)).thenReturn(uri); + when(retryableMock.lastClient()).thenReturn(clientMock); + + SocketException socketException = new SocketException(); + CadiException cadiException = new CadiException(socketException); + doThrow(cadiException).when(retryableMock).code(clientMock); + hman.same(ssMock, retryableMock); + } + + @Test(expected = CadiException.class) + public void throwsCadiException4Test() throws LocatorException, APIException, CadiException, ConnectException { + HMangr hman = new HMangr(access, locMock); + when(retryableMock.item()).thenReturn(itemMock); + when(locMock.get(itemMock)).thenReturn(uri); + when(retryableMock.lastClient()).thenReturn(clientMock); + + Exception e = new Exception(); + CadiException cadiException = new CadiException(e); + doThrow(cadiException).when(retryableMock).code(clientMock); + hman.same(ssMock, retryableMock); + } + + @Test + public void allTest() throws LocatorException, CadiException, APIException { + HManagerStub hman = new HManagerStub(access, locMock); + assertThat(hman.best(ssMock, retryableMock), is(nullValue())); + assertThat(hman.all(ssMock, retryableMock), is(nullValue())); + assertThat(hman.all(ssMock, retryableMock, true), is(nullValue())); + } + + @Test + public void oneOfTest() throws LocatorException, CadiException, APIException, ConnectException { + HMangr hman = new HMangr(access, locMock); + assertThat(hman.oneOf(ssMock, retryableMock, false, "host"), is(nullValue())); + + try { + hman.oneOf(ssMock, retryableMock, true, "host"); + fail("Should've thrown an exception"); + } catch (LocatorException e) { + } + + when(locMock.first()).thenReturn(itemMock); + when(locMock.get(itemMock)).thenReturn(uri); + + // Branching coverage... + assertThat(hman.oneOf(ssMock, retryableMock, false, null), is(nullValue())); + assertThat(hman.oneOf(ssMock, retryableMock, false, "host"), is(nullValue())); + + assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue())); + + CadiException cadiException; + + cadiException = new CadiException(new ConnectException()); + doThrow(cadiException).when(retryableMock).code((Rcli) any()); + assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue())); + + cadiException = new CadiException(new SSLHandshakeException(null)); + doThrow(cadiException).when(retryableMock).code((Rcli) any()); + assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue())); + + cadiException = new CadiException(new SocketException()); + doThrow(cadiException).when(retryableMock).code((Rcli) any()); + try { + hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)); + fail("Should've thrown an exception"); + } catch (CadiException e) { + } + + cadiException = new CadiException(new SocketException("java.net.SocketException: Connection reset")); + doThrow(cadiException).when(retryableMock).code((Rcli) any()); + try { + hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)); + fail("Should've thrown an exception"); + } catch (CadiException e) { + } + + cadiException = new CadiException(); + doThrow(cadiException).when(retryableMock).code((Rcli) any()); + try { + hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)); + fail("Should've thrown an exception"); + } catch (CadiException e) { + } + + doThrow(new ConnectException()).when(retryableMock).code((Rcli) any()); + assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue())); + + when(goodRetry.code((Rcli) any())).thenReturn(5); + assertThat(hman.oneOf(ssMock, goodRetry, false, uriString.substring(7)), is(5)); + } + + @Test + public void coverageTest() throws LocatorException { + HMangr hman = new HMangr(access, locMock); + hman.readTimeout(5); + assertThat(hman.readTimeout(), is(5)); + hman.connectionTimeout(5); + assertThat(hman.connectionTimeout(), is(5)); + hman.apiVersion("v1.0"); + assertThat(hman.apiVersion(), is("v1.0")); + hman.close(); + + } + + private class HManagerStub extends HMangr { + public HManagerStub(Access access, Locator loc) throws LocatorException { super(access, loc); } + @Override public RET same(SecuritySetter ss, Retryable retryable) { + return null; + } + @Override public RET oneOf(SecuritySetter ss, Retryable retryable, boolean notify, String host) { + return null; + } + } + } \ No newline at end of file