From 9315848e429a2af56dc34bc8ab44301d7e3002f0 Mon Sep 17 00:00:00 2001 From: IanHowell Date: Fri, 30 Mar 2018 12:14:55 -0500 Subject: [PATCH] Fix up several JUnits Issue-ID: AAF-128 Change-Id: I3644847671e08f0b6d57cce9674dc8a3d60fcbc4 Signed-off-by: IanHowell Get Test coverage for MultiGet to 100% Issue-ID: AAF-128 Change-Id: I5b591c0b7e0e7353dc669e75546961177ef8d152 Signed-off-by: IanHowell Get test coverage of UserChainManip to 100% Issue-ID: AAF-128 Change-Id: I41d6a2c3f405d8959507ae02c934fffff7903e32 Signed-off-by: IanHowell Get test coverage of SecurityInfoC to 100% Issue-ID: AAF-128 Change-Id: I04c088827915b14d1f597d5e1eeb7f8e9159e644 Signed-off-by: IanHowell Trial run at updating POMs for SONAR coverage Change-Id: Ib2a68773fafbc660266ee5d496ffe842c10e7931 Signed-off-by: IanHowell --- auth/auth-core/pom.xml | 110 +++--- auth/auth-deforg/pom.xml | 4 - cadi/cass/pom.xml | 119 ++++--- .../test/java/org/onap/aaf/cadi/test/JU_AES.java | 374 +++++++++++---------- .../onap/aaf/cadi/test/JU_CadiExceptionTest.java | 121 ------- .../org/onap/aaf/cadi/test/JU_CadiWrapTest.java | 155 --------- .../org/onap/aaf/cadi/test/config/JU_MultiGet.java | 68 ++++ .../aaf/cadi/test/config/JU_SecurityInfoC.java | 109 ++++++ .../onap/aaf/cadi/test/util/JU_UserChainManip.java | 57 ++-- 9 files changed, 516 insertions(+), 601 deletions(-) delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_CadiExceptionTest.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_CadiWrapTest.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_MultiGet.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_SecurityInfoC.java diff --git a/auth/auth-core/pom.xml b/auth/auth-core/pom.xml index 8e218632..0bfc0ffb 100644 --- a/auth/auth-core/pom.xml +++ b/auth/auth-core/pom.xml @@ -142,57 +142,65 @@ ecomp-staging - - org.jacoco - jacoco-maven-plugin - 0.7.7.201606060606 - - true - - org.onap.aaf.* - - - - - pre-unit-test - - prepare-agent - - - ${project.build.directory}/coverage-reports/jacoco.exec - - - - - pre-integration-test - pre-integration-test - - prepare-agent - - - ${project.build.directory}/coverage-reports/jacoco-it.exec - - - - - - merge - - post-integration-test - - - - ${project.build.directory}/coverage-reports - - *.exec - - - - ${project.build.directory}/jacoco-dev.exec - - - - + + org.jacoco + jacoco-maven-plugin + ${jacoco.version} + + + **/gen/** + **/generated-sources/** + **/yang-gen/** + **/pax/** + + + + + pre-unit-test + + prepare-agent + + + ${project.build.directory}/code-coverage/jacoco-ut.exec + surefireArgLine + + + + post-unit-test + test + + report + + + ${project.build.directory}/code-coverage/jacoco-ut.exec + ${project.reporting.outputDirectory}/jacoco-ut + + + + pre-integration-test + pre-integration-test + + prepare-agent + + + ${project.build.directory}/code-coverage/jacoco-it.exec + failsafeArgLine + + + + + post-integration-test + post-integration-test + + report + + + ${project.build.directory}/code-coverage/jacoco-it.exec + ${project.reporting.outputDirectory}/jacoco-it + + + + diff --git a/auth/auth-deforg/pom.xml b/auth/auth-deforg/pom.xml index cf86a783..f5162b7c 100644 --- a/auth/auth-deforg/pom.xml +++ b/auth/auth-deforg/pom.xml @@ -157,7 +157,6 @@ - pre-unit-test @@ -168,8 +167,6 @@ surefireArgLine - - post-unit-test test @@ -192,7 +189,6 @@ failsafeArgLine - post-integration-test post-integration-test diff --git a/cadi/cass/pom.xml b/cadi/cass/pom.xml index e40827e2..ae4d06af 100644 --- a/cadi/cass/pom.xml +++ b/cadi/cass/pom.xml @@ -129,69 +129,64 @@ - org.jacoco - jacoco-maven-plugin - ${jacoco.version} - - - **/gen/** - **/generated-sources/** - **/yang-gen/** - **/pax/** - - - - - - pre-unit-test - - prepare-agent - - - ${project.build.directory}/code-coverage/jacoco-ut.exec - surefireArgLine - - - - - - post-unit-test - test - - report - - - ${project.build.directory}/code-coverage/jacoco-ut.exec - ${project.reporting.outputDirectory}/jacoco-ut - - - - pre-integration-test - pre-integration-test - - prepare-agent - - - ${project.build.directory}/code-coverage/jacoco-it.exec - - failsafeArgLine - - + org.jacoco + jacoco-maven-plugin + ${jacoco.version} + + + **/gen/** + **/generated-sources/** + **/yang-gen/** + **/pax/** + + + + + pre-unit-test + + prepare-agent + + + ${project.build.directory}/code-coverage/jacoco-ut.exec + surefireArgLine + + + + post-unit-test + test + + report + + + ${project.build.directory}/code-coverage/jacoco-ut.exec + ${project.reporting.outputDirectory}/jacoco-ut + + + + pre-integration-test + pre-integration-test + + prepare-agent + + + ${project.build.directory}/code-coverage/jacoco-it.exec - - - post-integration-test - post-integration-test - - report - - - ${project.build.directory}/code-coverage/jacoco-it.exec - ${project.reporting.outputDirectory}/jacoco-it - - - - + failsafeArgLine + + + + post-integration-test + post-integration-test + + report + + + ${project.build.directory}/code-coverage/jacoco-it.exec + ${project.reporting.outputDirectory}/jacoco-it + + + + diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_AES.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_AES.java index 4fb92429..fc960be8 100644 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_AES.java +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_AES.java @@ -1,180 +1,194 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 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.test; - -import static org.junit.Assert.*; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.lang.reflect.Field; -import java.nio.file.Files; -import java.nio.file.Paths; - -import javax.crypto.CipherInputStream; -import javax.crypto.CipherOutputStream; -import javax.crypto.SecretKey; - -import org.junit.Test; -import org.onap.aaf.cadi.AES; -import org.onap.aaf.cadi.CadiException; -import org.onap.aaf.cadi.Symm; -import org.junit.Before; - -public class JU_AES { - private AES aes; - private ByteArrayInputStream baisEncrypt; - private ByteArrayInputStream baisDecrypt; - private ByteArrayOutputStream baosEncrypt; - private ByteArrayOutputStream baosDecrypt; - - @Before - public void setup() throws Exception { - byte[] keyBytes = new byte[AES.AES_KEY_SIZE/8]; - char[] codeset = Symm.base64.codeset; - int offset = (Math.abs(codeset[0])+47)%(codeset.length-keyBytes.length); - for(int i=0;i lur1 = new CachingLur() { - @Override public Permission createPerm(String p) { return null; } - @Override public boolean fish(Principal bait, Permission pond) { return true; } - @Override public void fishAll(Principal bait, List permissions) { } - @Override public void destroy() { } - @Override public boolean handlesExclusively(Permission pond) { return false; } - @Override public boolean handles(Principal principal) { return false; } - @Override public void remove(String user) { } - @Override public Resp reload(User user) { return null; } - @Override public void setDebug(String commaDelimIDsOrNull) { } - @Override public void clear(Principal p, StringBuilder sb) { } - }; - - MapPermConverter pc = new MapPermConverter(); - - CadiWrap wrap = new CadiWrap(request, tafResp, lur1, pc); - - assertNotNull(wrap.getUserPrincipal()); - assertNull(wrap.getRemoteUser()); - assertNull(wrap.getUser()); - - byte[] arr = {'1','2'}; - wrap.setCred(arr); - - assertEquals(arr, wrap.getCred()); - - wrap.setUser("User1"); - assertEquals("User1", wrap.getUser()); - - wrap.invalidate("1"); - wrap.setPermConverter(new MapPermConverter()); - - assertTrue(wrap.getLur() instanceof CachingLur); - assertTrue(wrap.isUserInRole("User1")); - - wrap.set(tafResp, lur); - assertFalse(wrap.isUserInRole("Perm1")); - } -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_MultiGet.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_MultiGet.java new file mode 100644 index 00000000..e678421c --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_MultiGet.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 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.test.config; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Get; +import org.onap.aaf.cadi.config.MultiGet; + +public class JU_MultiGet { + + private String defaultVal = "some default value"; + + private ByteArrayOutputStream outStream; + + private MultiGet multiGet; + private Get.AccessGet accessGet; + private PropAccess access; + + @Before + public void setup() throws IOException { + outStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outStream)); + + access = new PropAccess(); + access.setProperty("tag", "value"); + accessGet = new Get.AccessGet(access); + multiGet = new MultiGet(accessGet, Get.NULL); + } + + @After + public void tearDown() { + System.setOut(System.out); + } + + @Test + public void getTest() { + assertThat(multiGet.get("tag", defaultVal, false), is("value")); + assertThat(multiGet.get("not_a_tag", defaultVal, false), is(defaultVal)); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_SecurityInfoC.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_SecurityInfoC.java new file mode 100644 index 00000000..948bfad4 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_SecurityInfoC.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 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.test.config; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import org.junit.*; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.SecuritySetter; +import org.onap.aaf.cadi.config.SecurityInfoC; + +public class JU_SecurityInfoC { + + ByteArrayOutputStream outStream; + ByteArrayOutputStream errStream; + + @Before + public void setup() { + outStream = new ByteArrayOutputStream(); + errStream = new ByteArrayOutputStream(); + + System.setOut(new PrintStream(outStream)); + System.setErr(new PrintStream(errStream)); + } + + @After + public void tearDown() { + System.setOut(System.out); + System.setErr(System.err); + } + + @Test + public void instanceTest() throws CadiException, MalformedURLException { + SecurityInfoC si = SecurityInfoC.instance(new PropAccess(), HttpURLConnection.class); + assertThat(si.defSS.getID(), is(SecurityInfoC.DEF_ID)); + try { + si.defSS.setSecurity(new HttpURLConnectionStub()); + fail("Should have thrown an exception"); + } catch (CadiException e) { + assertTrue(e instanceof CadiException); + assertThat(e.getMessage(), is("No Client Credentials set.")); + } + assertThat(si.defSS.setLastResponse(0), is(0)); + + // Try it again for coverage + SecurityInfoC siClone = SecurityInfoC.instance(new PropAccess(), HttpURLConnection.class); + assertThat(siClone, is(si)); + } + + @Test + public void setTest() throws MalformedURLException, CadiException { + SecurityInfoC si = SecurityInfoC.instance(new PropAccess(), HttpURLConnection.class); + SecuritySetter ss = new SecuritySetterStub(); + assertThat(si.set(ss), is(si)); + assertThat(si.defSS.getID(), is("Example ID")); + try { + si.defSS.setSecurity(new HttpURLConnectionStub()); + fail("Should have thrown an exception"); + } catch (CadiException e) { + assertTrue(e instanceof CadiException); + assertThat(e.getMessage(), is("Example exception")); + } + assertThat(si.defSS.setLastResponse(0), is(0)); + assertThat(si.defSS.setLastResponse(1), is(1)); + assertThat(si.defSS.setLastResponse(-1), is(-1)); + } + + private class HttpURLConnectionStub extends HttpURLConnection { + public HttpURLConnectionStub() throws MalformedURLException { super(new URL("http://www.example.com")); } + @Override public void disconnect() { } + @Override public boolean usingProxy() { return false; } + @Override public void connect() throws IOException { } + } + + private class SecuritySetterStub implements SecuritySetter { + public String getID() { return "Example ID"; } + public void setSecurity(CT client) throws CadiException { throw new CadiException("Example exception"); } + public int setLastResponse(int respCode) { return respCode; } + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_UserChainManip.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_UserChainManip.java index d335edae..949d034b 100644 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_UserChainManip.java +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_UserChainManip.java @@ -6,9 +6,9 @@ * * 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. @@ -20,12 +20,10 @@ ******************************************************************************/ package org.onap.aaf.cadi.test.util; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; -import java.util.ArrayList; -import java.util.List; - -import org.junit.Test; import org.onap.aaf.cadi.UserChain; import org.onap.aaf.cadi.util.UserChainManip; @@ -34,33 +32,36 @@ public class JU_UserChainManip { @Test public void build(){ UserChain.Protocol baseAuth=UserChain.Protocol.BasicAuth; - assertEquals(UserChainManip.build(new StringBuilder(""), "app", "id", baseAuth, true).toString(), "app:id:BasicAuth:AS"); - } - + StringBuilder sb = UserChainManip.build(new StringBuilder(""), "app", "id", baseAuth, true); + assertThat(sb.toString(), is("app:id:BasicAuth:AS")); - @Test - public void idToNS(){ - assertEquals(UserChainManip.idToNS(null), ""); + // for coverage + sb = UserChainManip.build(sb, "app", "id", baseAuth, true); + assertThat(sb.toString(), is("app:id:BasicAuth:AS,app:id:BasicAuth")); + + sb = UserChainManip.build(new StringBuilder(""), "app", "id", baseAuth, false); + assertThat(sb.toString(), is("app:id:BasicAuth")); } - + @Test - public void idToNS1(){ - assertEquals(UserChainManip.idToNS("t@st"), "st"); + public void idToNSTEST() { + assertThat(UserChainManip.idToNS(null), is("")); + assertThat(UserChainManip.idToNS(""), is("")); + assertThat(UserChainManip.idToNS("something"), is("")); + assertThat(UserChainManip.idToNS("something@@"), is("")); + assertThat(UserChainManip.idToNS("something@@."), is("")); + assertThat(UserChainManip.idToNS("something@com"), is("com")); + assertThat(UserChainManip.idToNS("something@random.com"), is("com.random")); + assertThat(UserChainManip.idToNS("@random.com"), is("com.random")); + assertThat(UserChainManip.idToNS("something@random.com."), is("com.random")); + assertThat(UserChainManip.idToNS("something@..random...com..."), is("com.random")); + assertThat(UserChainManip.idToNS("something@this.random.com"), is("com.random.this")); } @Test - public void test() { - assertEquals("",UserChainManip.idToNS(null)); - assertEquals("",UserChainManip.idToNS("")); - assertEquals("",UserChainManip.idToNS("something")); - assertEquals("",UserChainManip.idToNS("something@@")); - assertEquals("",UserChainManip.idToNS("something@@.")); - assertEquals("com",UserChainManip.idToNS("something@com")); - assertEquals("com.random",UserChainManip.idToNS("something@random.com")); - assertEquals("com.random",UserChainManip.idToNS("@random.com")); - assertEquals("com.random",UserChainManip.idToNS("something@random.com.")); - assertEquals("com.random",UserChainManip.idToNS("something@..random...com...")); - assertEquals("com.random.this",UserChainManip.idToNS("something@this.random.com")); + public void coverageTest() { + @SuppressWarnings("unused") + UserChainManip ucm = new UserChainManip(); } } -- 2.16.6