From 29850fd4bbdec117aa40750667cb941d6ef708d5 Mon Sep 17 00:00:00 2001 From: IanHowell Date: Wed, 9 May 2018 16:02:39 -0500 Subject: Improve coverage of cadi-aaf Issue-ID: AAF-223 Change-Id: Iaf667bce71ac8beb1ba2a763d6ae243a83935e13 Signed-off-by: IanHowell --- .../org/onap/aaf/cadi/cm/test/JU_ArtifactDir.java | 1 - .../onap/aaf/cadi/oauth/test/JU_OAuth2HttpTaf.java | 85 ++++++++++++++++ .../aaf/cadi/oauth/test/JU_OAuth2HttpTafResp.java | 68 +++++++++++++ .../org/onap/aaf/cadi/oauth/test/JU_OAuth2Lur.java | 100 ++++++++++++++++++ .../aaf/cadi/oauth/test/JU_OAuth2Principal.java | 60 +++++++++++ .../onap/aaf/cadi/oauth/test/JU_TimedToken.java | 84 +++++++++++++++ .../org/onap/aaf/cadi/oauth/test/JU_TzHClient.java | 113 +++++++++++++++++++++ .../org/onap/aaf/cadi/persist/test/JU_Persist.java | 1 - 8 files changed, 510 insertions(+), 2 deletions(-) create mode 100644 cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2HttpTaf.java create mode 100644 cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2HttpTafResp.java create mode 100644 cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2Lur.java create mode 100644 cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2Principal.java create mode 100644 cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_TimedToken.java create mode 100644 cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_TzHClient.java diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_ArtifactDir.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_ArtifactDir.java index 5121448f..d0d67e23 100644 --- a/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_ArtifactDir.java +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_ArtifactDir.java @@ -23,7 +23,6 @@ package org.onap.aaf.cadi.cm.test; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.when; diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2HttpTaf.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2HttpTaf.java new file mode 100644 index 00000000..52b2beb4 --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2HttpTaf.java @@ -0,0 +1,85 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.cadi.oauth.test; + +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.any; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.LocatorException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.oauth.OAuth2HttpTaf; +import org.onap.aaf.cadi.oauth.OAuth2Principal; +import org.onap.aaf.cadi.oauth.TokenMgr; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.cadi.Taf.LifeForm; +import org.onap.aaf.cadi.client.Result; + +public class JU_OAuth2HttpTaf { + + private static final String authz = "Bearer John Doe"; + + @Mock private TokenMgr tmgrMock; + @Mock private HttpServletResponse respMock; + @Mock private HttpServletRequest reqMock; + @Mock private OAuth2Principal princMock; + + private PropAccess access; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + + access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]); + } + + @Test + public void test() throws APIException, CadiException, LocatorException { + OAuth2HttpTaf taf = new OAuth2HttpTaf(access, tmgrMock); + + taf.validate(LifeForm.CBLF, reqMock, respMock); + when(reqMock.getHeader("Authorization")).thenReturn(authz); + + doReturn(Result.ok(200, princMock)).when(tmgrMock).toPrincipal(anyString(), (byte[])any()); + taf.validate(LifeForm.CBLF, reqMock, respMock); + + when(reqMock.isSecure()).thenReturn(true); + + doReturn(Result.err(404, "not found")).when(tmgrMock).toPrincipal(anyString(), (byte[])any()); + taf.validate(LifeForm.CBLF, reqMock, respMock); + + taf.revalidate(null, null); + } + +} diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2HttpTafResp.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2HttpTafResp.java new file mode 100644 index 00000000..94737b0c --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2HttpTafResp.java @@ -0,0 +1,68 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.cadi.oauth.test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.io.IOException; + +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.oauth.OAuth2HttpTafResp; +import org.onap.aaf.cadi.oauth.OAuth2Principal; +import org.onap.aaf.cadi.principal.TrustPrincipal; +import org.onap.aaf.cadi.taf.TafResp.RESP; + +public class JU_OAuth2HttpTafResp { + + private static final String description = "description"; + + @Mock private TrustPrincipal princMock; + @Mock private OAuth2Principal oauthMock; + @Mock private HttpServletResponse respMock; + + private PropAccess access; + + private RESP status; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + status = RESP.NO_FURTHER_PROCESSING; + } + + @Test + public void test() throws IOException { + OAuth2HttpTafResp resp = new OAuth2HttpTafResp(access, princMock, description, status, respMock); + resp = new OAuth2HttpTafResp(access, oauthMock, description, status, respMock, true); + assertThat(resp.isFailedAttempt(), is(true)); + assertThat(resp.isAuthenticated(), is(status)); + assertThat(resp.authenticate(), is(RESP.HTTP_REDIRECT_INVOKED)); + } + +} diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2Lur.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2Lur.java new file mode 100644 index 00000000..853c4ae3 --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2Lur.java @@ -0,0 +1,100 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.cadi.oauth.test; + +import static org.mockito.Mockito.when; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.security.Principal; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aaf.cadi.Permission; +import org.onap.aaf.cadi.aaf.AAFPermission; +import org.onap.aaf.cadi.oauth.OAuth2Lur; +import org.onap.aaf.cadi.oauth.OAuth2Principal; +import org.onap.aaf.cadi.oauth.TokenMgr; +import org.onap.aaf.cadi.oauth.TokenPerm; +import org.onap.aaf.cadi.principal.BearerPrincipal; + +public class JU_OAuth2Lur { + + private List aafPerms; + private List perms; + + @Mock private TokenMgr tmMock; + @Mock private AAFPermission pondMock; + @Mock private Principal princMock; + @Mock private OAuth2Principal oauthPrincMock; + @Mock private BearerPrincipal bearPrincMock; + @Mock private TokenPerm tpMock; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void test() { + OAuth2Lur lur = new OAuth2Lur(tmMock); + lur.createPerm("testPerm"); + lur.createPerm("testPerm1|testPerm2|testPerm3"); + + assertThat(lur.fish(princMock, pondMock), is(false)); + assertThat(lur.fish(oauthPrincMock, pondMock), is(false)); + + when(oauthPrincMock.tokenPerm()).thenReturn(tpMock); + assertThat(lur.fish(oauthPrincMock, pondMock), is(false)); + + aafPerms = new ArrayList<>(); + aafPerms.add(pondMock); + aafPerms.add(pondMock); + when(tpMock.perms()).thenReturn(aafPerms); + when(pondMock.match(pondMock)).thenReturn(false).thenReturn(true); + assertThat(lur.fish(oauthPrincMock, pondMock), is(true)); + + perms = new ArrayList<>(); + perms.add(pondMock); + perms.add(pondMock); + lur.fishAll(oauthPrincMock, perms); + + when(oauthPrincMock.tokenPerm()).thenReturn(null); + lur.fishAll(oauthPrincMock, perms); + + assertThat(lur.handlesExclusively(pondMock), is(false)); + + assertThat(lur.handles(null), is(false)); + assertThat(lur.handles(princMock), is(false)); + assertThat(lur.handles(bearPrincMock), is(false)); + when(bearPrincMock.getBearer()).thenReturn("not null :)"); + assertThat(lur.handles(bearPrincMock), is(true)); + + lur.destroy(); + lur.clear(null, null); + } + +} diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2Principal.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2Principal.java new file mode 100644 index 00000000..45736949 --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_OAuth2Principal.java @@ -0,0 +1,60 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.cadi.oauth.test; + +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.is; +import static org.mockito.Mockito.when; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aaf.cadi.oauth.OAuth2Principal; +import org.onap.aaf.cadi.oauth.TokenPerm; + +public class JU_OAuth2Principal { + + @Mock TokenPerm tpMock; + + + private static final String username = "username"; + + private static final byte[] hash = "hashstring".getBytes(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + + when(tpMock.getUsername()).thenReturn(username); + } + + @Test + public void test() { + OAuth2Principal princ = new OAuth2Principal(tpMock, hash); + assertThat(princ.getName(), is(username)); + assertThat(princ.tokenPerm(), is(tpMock)); + assertThat(princ.tag(), is("OAuth")); + assertThat(princ.personalName(), is(username)); + } + +} diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_TimedToken.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_TimedToken.java new file mode 100644 index 00000000..775a0398 --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_TimedToken.java @@ -0,0 +1,84 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.cadi.oauth.test; + +import static org.hamcrest.CoreMatchers.is; +import static org.mockito.Mockito.when; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aaf.cadi.oauth.TimedToken; +import org.onap.aaf.cadi.persist.Persist; + +import aafoauth.v2_0.Token; + +public class JU_TimedToken { + + private static final byte[] hash = "hashstring".getBytes(); + + private static final int expires = 10000; + + private Path path; + + @Mock private Persist persistMock; + @Mock private Token tokenMock; + + @Before + public void setup() throws IOException { + MockitoAnnotations.initMocks(this); + + when(tokenMock.getExpiresIn()).thenReturn(expires); + path = Files.createTempFile("fake", ".txt"); + } + + @Test + public void test() { + int actuallyExpires = ((int)(System.currentTimeMillis() / 1000)) + expires; + TimedToken ttoken = new TimedToken(persistMock, tokenMock, hash, path); + + assertThat(ttoken.get(), is(tokenMock)); + assertThat(ttoken.checkSyncTime(), is(true)); + assertThat(ttoken.checkReloadable(), is(false)); + assertThat(ttoken.hasBeenTouched(), is(false)); + assertThat(Math.abs(ttoken.expires() - actuallyExpires) < 10, is(true)); + assertThat(ttoken.expired(), is(false)); + + assertThat(ttoken.match(hash), is(true)); + assertThat(ttoken.getHash(), is(hash)); + + assertThat(ttoken.path(), is(path)); + + assertThat(ttoken.count(), is(0)); + ttoken.inc(); + assertThat(ttoken.count(), is(1)); + ttoken.clearCount(); + assertThat(ttoken.count(), is(0)); + } + +} diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_TzHClient.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_TzHClient.java new file mode 100644 index 00000000..7febf51f --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_TzHClient.java @@ -0,0 +1,113 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.cadi.oauth.test; + +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.doReturn; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.net.HttpURLConnection; +import java.net.URI; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.Locator; +import org.onap.aaf.cadi.Locator.Item; +import org.onap.aaf.cadi.LocatorException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.client.Rcli; +import org.onap.aaf.cadi.client.Retryable; +import org.onap.aaf.cadi.oauth.TimedToken; +import org.onap.aaf.cadi.oauth.TzHClient; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.cadi.config.SecurityInfoC; + +public class JU_TzHClient { + + @Mock private Retryable retryableMock; + @Mock private TimedToken tokenMock; + @Mock private SecurityInfoC siMock; + @Mock private Locator locMock; + @Mock private Item itemMock; + @Mock private Rcli clientMock; + + private PropAccess access; + + private ByteArrayOutputStream errStream; + + private final static String client_id = "id"; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]); + access.setProperty(Config.CADI_LATITUDE, "38.62"); // St Louis approx lat + access.setProperty(Config.CADI_LONGITUDE, "90.19"); // St Louis approx lon } + + errStream = new ByteArrayOutputStream(); + System.setErr(new PrintStream(errStream)); + } + + @After + public void tearDown() { + System.setErr(System.err); + } + + @Test + public void test() throws CadiException, LocatorException, APIException, IOException { + TzHClient client = new TzHClient(access, "tag"); + try { + client.best(retryableMock); + fail("Should've thrown an exception"); + } catch (CadiException e) { + assertThat(e.getMessage(), is("OAuth2 Token has not been set")); + } + client.setToken(client_id, tokenMock); + when(tokenMock.expired()).thenReturn(true); + try { + client.best(retryableMock); + fail("Should've thrown an exception"); + } catch (CadiException e) { + assertThat(e.getMessage(), is("Expired Token")); + } + + client = new TzHClient(access, siMock, locMock); + when(tokenMock.expired()).thenReturn(false); + doReturn(clientMock).when(retryableMock).lastClient(); + + when(retryableMock.item()).thenReturn(itemMock); + client.setToken(client_id, tokenMock); + assertThat(client.best(retryableMock), is(nullValue())); + } + +} diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_Persist.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_Persist.java index 3fd65a73..f8d76a95 100644 --- a/cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_Persist.java +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/persist/test/JU_Persist.java @@ -33,7 +33,6 @@ import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.nio.file.Path; -import java.nio.file.Paths; import org.junit.After; import org.junit.Before; -- cgit 1.2.3-korg