From: varun gudisena Date: Fri, 13 Apr 2018 14:29:10 +0000 (+0000) Subject: Merge "Increased code coverage in auth cmd" X-Git-Tag: Beijing-2.1.1~112 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=e42027821142e47c1e52f0f314f7c915446b76c6;hp=905034c3e3e8c93eb1f0bb3347474ce214f02592;p=aaf%2Fauthz.git Merge "Increased code coverage in auth cmd" --- diff --git a/auth/pom.xml b/auth/pom.xml index 73720284..777480ea 100644 --- a/auth/pom.xml +++ b/auth/pom.xml @@ -23,20 +23,15 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - + authparent AAF Auth Parent pom - - org.onap.oparent - oparent - 1.1.0 - UTF-8 diff --git a/cadi/aaf/src/main/java/org/onap/aaf/cadi/aaf/AAFPermission.java b/cadi/aaf/src/main/java/org/onap/aaf/cadi/aaf/AAFPermission.java index b3f0c4bb..37fb859e 100644 --- a/cadi/aaf/src/main/java/org/onap/aaf/cadi/aaf/AAFPermission.java +++ b/cadi/aaf/src/main/java/org/onap/aaf/cadi/aaf/AAFPermission.java @@ -71,24 +71,28 @@ public class AAFPermission implements Permission { * If you want a simple field comparison, it is faster without REGEX */ public boolean match(Permission p) { + boolean rv; + String aafType; + String aafInstance; + String aafAction; if(p instanceof AAFPermission) { AAFPermission ap = (AAFPermission)p; // Note: In AAF > 1.0, Accepting "*" from name would violate multi-tenancy // Current solution is only allow direct match on Type. // 8/28/2014 Jonathan - added REGEX ability - if(type.equals(ap.getName())) - if(PermEval.evalInstance(instance,ap.getInstance())) - if(PermEval.evalAction(action,ap.getAction())) - return true; + aafType = ap.getName(); + aafInstance = ap.getInstance(); + aafAction = ap.getAction(); } else { // Permission is concatenated together: separated by | String[] aaf = p.getKey().split("[\\s]*\\|[\\s]*",3); - if(aaf.length>0 && type.equals(aaf[0])) - if(PermEval.evalInstance(instance,aaf.length>1?aaf[1]:"*")) - if(PermEval.evalAction(action,aaf.length>2?aaf[2]:"*")) - return true; - } - return false; + aafType = aaf[0]; + aafInstance = (aaf.length > 1) ? aaf[1] : "*"; + aafAction = (aaf.length > 2) ? aaf[2] : "*"; + } + return ((type.equals(aafType)) && + (PermEval.evalInstance(instance, aafInstance)) && + (PermEval.evalAction(action, aafAction))); } public String getName() { diff --git a/cadi/aaf/src/main/java/org/onap/aaf/cadi/aaf/PermEval.java b/cadi/aaf/src/main/java/org/onap/aaf/cadi/aaf/PermEval.java index aa65504d..75df4eab 100644 --- a/cadi/aaf/src/main/java/org/onap/aaf/cadi/aaf/PermEval.java +++ b/cadi/aaf/src/main/java/org/onap/aaf/cadi/aaf/PermEval.java @@ -7,9 +7,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. @@ -28,121 +28,113 @@ public class PermEval { public static final char START_REGEX_CHAR = '!'; public static final char START_INST_KEY_CHAR=':'; public static final char ALT_START_INST_KEY_CHAR='/'; - + public static final char LIST_SEP = ','; public static final String INST_KEY_REGEX = new StringBuilder().append(START_INST_KEY_CHAR).toString(); public static final String ASTERIX = "*"; - + /** * Evaluate Instance - * - * Instance can be more complex. It can be a string, a Regular Expression, or a ":" separated Key + * + * Instance can be more complex. It can be a string, a Regular Expression, or a ":" separated Key * who's parts can also be a String, Regular Expression. - * + * * sInst = Server's Instance * In order to prevent false matches, keys must be the same length to count as equal * Changing this will break existing users, like Cassandra. Jonathan 9-4-2015 */ - public static boolean evalInstance(String sInst, String pInst) { - if(sInst==null || pInst == null) { - return false; - } - if(ASTERIX.equals(sInst)) { - return true; // If Server's String is "*", then it accepts every Instance - } - char firstChar = pInst.charAt(0); - char startChar = firstChar==ALT_START_INST_KEY_CHAR?ALT_START_INST_KEY_CHAR:START_INST_KEY_CHAR; - switch(pInst.charAt(0)) { // First char - case START_REGEX_CHAR: // Evaluate as Regular Expression - String pItem = pInst.substring(1); - for(String sItem : Split.split(LIST_SEP,sInst)) { // allow for "," definition in Action - return sItem.matches(pItem); - } - - case START_INST_KEY_CHAR: // Evaluate a special Key field, i.e.:xyz:*:!df.* - case ALT_START_INST_KEY_CHAR: // Also allow '/' as special Key Field, i.e. /xyz/*/!.* - if(sInst.charAt(0)==startChar) { // To compare key-to-key, both strings must be keys - String[] skeys=Split.split(startChar,sInst); - String[] pkeys=Split.split(startChar,pInst); - if(skeys.length!=pkeys.length) return false; - - boolean pass = true; - for(int i=1;pass && i future; + + private ByteArrayOutputStream errStream; @Before public void setUp() throws Exception { @@ -89,14 +94,15 @@ public class JU_ErrMessageTest { error.setText("Error Text"); errMessage = new ErrMessage(env); - + errStream = new ByteArrayOutputStream(); } @Test public void testPrintErrMessage() throws APIException { when(errDF.newData().in(TYPE.JSON).load(attErrJson).asObject()).thenReturn(error); - errMessage.printErr(System.out, attErrJson); + errMessage.printErr(new PrintStream(errStream), attErrJson); + assertEquals("Error Message Id Error Text\n", errStream.toString()); } @Test diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/aaf/test/JU_AAFPermission.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/aaf/test/JU_AAFPermission.java new file mode 100644 index 00000000..10958a23 --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/aaf/test/JU_AAFPermission.java @@ -0,0 +1,140 @@ +/******************************************************************************* + * ============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.aaf.test; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; +import org.onap.aaf.cadi.Permission; +import org.onap.aaf.cadi.aaf.AAFPermission; + +public class JU_AAFPermission { + + private final static String type = "type"; + private final static String instance = "instance"; + private final static String action = "action"; + private final static String key = type + '|' + instance + '|' + action; + private final static String role = "role"; + + private static List roles; + + @Before + public void setup() { + roles = new ArrayList(); + roles.add(role); + } + + @Test + public void constructor1Test() { + AAFPermission perm = new AAFPermission(type, instance, action); + assertThat(perm.getName(), is(type)); + assertThat(perm.getInstance(), is(instance)); + assertThat(perm.getAction(), is(action)); + assertThat(perm.getKey(), is(key)); + assertThat(perm.permType(), is("AAF")); + assertThat(perm.roles().size(), is(0)); + assertThat(perm.toString(), is("AAFPermission:\n\tType: " + type + + "\n\tInstance: " + instance + + "\n\tAction: " + action + + "\n\tKey: " + key)); + } + + @Test + public void constructor2Test() { + AAFPermission perm; + + perm = new AAFPermission(type, instance, action, null); + assertThat(perm.getName(), is(type)); + assertThat(perm.getInstance(), is(instance)); + assertThat(perm.getAction(), is(action)); + assertThat(perm.getKey(), is(key)); + assertThat(perm.permType(), is("AAF")); + assertThat(perm.roles().size(), is(0)); + assertThat(perm.toString(), is("AAFPermission:\n\tType: " + type + + "\n\tInstance: " + instance + + "\n\tAction: " + action + + "\n\tKey: " + key)); + + perm = new AAFPermission(type, instance, action, roles); + assertThat(perm.getName(), is(type)); + assertThat(perm.getInstance(), is(instance)); + assertThat(perm.getAction(), is(action)); + assertThat(perm.getKey(), is(key)); + assertThat(perm.permType(), is("AAF")); + assertThat(perm.roles().size(), is(1)); + assertThat(perm.roles().get(0), is(role)); + assertThat(perm.toString(), is("AAFPermission:\n\tType: " + type + + "\n\tInstance: " + instance + + "\n\tAction: " + action + + "\n\tKey: " + key)); + } + + @Test + public void matchTest() { + final AAFPermission controlPermission = new AAFPermission(type, instance, action); + PermissionStub perm; + AAFPermission aafperm; + + aafperm = new AAFPermission(type, instance, action); + assertThat(controlPermission.match(aafperm), is(true)); + + perm = new PermissionStub(key); + assertThat(controlPermission.match(perm), is(true)); + + // Coverage tests + perm = new PermissionStub("not a valid key"); + assertThat(controlPermission.match(perm), is(false)); + perm = new PermissionStub("type"); + assertThat(controlPermission.match(perm), is(false)); + perm = new PermissionStub("type|instance|badAction"); + assertThat(controlPermission.match(perm), is(false)); + } + + @Test + public void coverageTest() { + AAFPermissionStub aafps = new AAFPermissionStub(); + assertThat(aafps.getName(), is(nullValue())); + assertThat(aafps.getInstance(), is(nullValue())); + assertThat(aafps.getAction(), is(nullValue())); + assertThat(aafps.getKey(), is(nullValue())); + assertThat(aafps.permType(), is("AAF")); + assertThat(aafps.roles().size(), is(0)); + } + + private class PermissionStub implements Permission { + private String key; + + public PermissionStub(String key) { this.key = key; } + @Override public String permType() { return null; } + @Override public String getKey() { return key; } + @Override public boolean match(Permission p) { return false; } + } + + private class AAFPermissionStub extends AAFPermission { + + } +} diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/aaf/test/JU_PermEval.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/aaf/test/JU_PermEval.java index 29ce7412..9433cef1 100644 --- a/cadi/aaf/src/test/java/org/onap/aaf/cadi/aaf/test/JU_PermEval.java +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/aaf/test/JU_PermEval.java @@ -7,9 +7,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. @@ -22,40 +22,131 @@ package org.onap.aaf.cadi.aaf.test; import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; -import org.junit.AfterClass; -import org.junit.Test; import org.onap.aaf.cadi.aaf.PermEval; public class JU_PermEval { - @AfterClass - public static void tearDownAfterClass() throws Exception { + @Test + public void instanceNullTest() { + assertThat(PermEval.evalInstance(null, null), is(false)); + assertThat(PermEval.evalInstance(null, "test"), is(false)); + assertThat(PermEval.evalInstance("test", null), is(false)); } @Test - public void test() { - // TRUE - assertTrue(PermEval.evalAction("fred","fred")); - assertTrue(PermEval.evalAction("fred,wilma","fred")); - assertTrue(PermEval.evalAction("barney,betty,fred,wilma","fred")); - assertTrue(PermEval.evalAction("*","fred")); - - assertTrue(PermEval.evalInstance("fred","fred")); - assertTrue(PermEval.evalInstance("fred,wilma","fred")); - assertTrue(PermEval.evalInstance("barney,betty,fred,wilma","fred")); + public void instanceEmptyTest() { + assertThat(PermEval.evalInstance("", ""), is(false)); + assertThat(PermEval.evalInstance("", "test"), is(false)); + assertThat(PermEval.evalInstance("test", ""), is(false)); + } + + @Test + public void instanceAsterixTest() { + assertThat(PermEval.evalInstance("*", "*"), is(true)); assertTrue(PermEval.evalInstance("*","fred")); - + } + + @Test + public void instanceRegexTest() { + assertThat(PermEval.evalInstance("test", "!test"), is(true)); + assertThat(PermEval.evalInstance(",", "!"), is(true)); + assertThat(PermEval.evalInstance("test,test", "!test"), is(true)); + + assertThat(PermEval.evalInstance("test", "!"), is(false)); + assertThat(PermEval.evalInstance("test", "!mismatch"), is(false)); + assertThat(PermEval.evalInstance("test,mismatch", "!mismatch"), is(false)); + } + + @Test + public void instanceKeyTest() { + // Reject non-keys + assertThat(PermEval.evalInstance("fred", ":fred"), is(false)); + + // Reject differing number of keys + assertThat(PermEval.evalInstance(":fred:barney", ":fred"), is(false)); + assertThat(PermEval.evalInstance(":fred", ":fred:barney"), is(false)); + + // Accept all wildcard keys + assertThat(PermEval.evalInstance(":*", ":fred"), is(true)); + + // Accept matching empty keys + assertThat(PermEval.evalInstance(":", ":"), is(true)); + + // Reject non-matching empty keys + assertThat(PermEval.evalInstance(":fred", ":"), is(false)); + + // Accept matches starting with a wildcard + assertThat(PermEval.evalInstance(":!.*ed", ":fred"), is(true)); + + // Reject non-matches starting with a wildcard + assertThat(PermEval.evalInstance(":!.*arney", ":fred"), is(false)); + + // Accept matches ending with a wildcard + assertThat(PermEval.evalInstance(":fr*", ":fred"), is(true)); + + // Reject non-matches ending with a wildcard + assertThat(PermEval.evalInstance(":bar*", ":fred"), is(false)); + + // Accept exact keys + assertThat(PermEval.evalInstance(":fred", ":fred"), is(true)); + + // Reject mismatched keys + assertThat(PermEval.evalInstance(":fred", ":barney"), is(false)); + + // Check using alt-start character + assertThat(PermEval.evalInstance("/fred", "/fred"), is(true)); + assertThat(PermEval.evalInstance("/barney", "/fred"), is(false)); + } + + @Test + public void instanceDirectTest() { + assertThat(PermEval.evalInstance("fred","fred"), is(true)); + assertThat(PermEval.evalInstance("fred,wilma","fred"), is(true)); + assertThat(PermEval.evalInstance("barney,betty,fred,wilma","fred"), is(true)); + assertThat(PermEval.evalInstance("barney,betty,wilma","fred"), is(false)); + + assertThat(PermEval.evalInstance("fr*","fred"), is(true)); + assertThat(PermEval.evalInstance("freddy*","fred"), is(false)); + assertThat(PermEval.evalInstance("ba*","fred"), is(false)); + } + + @Test + public void actionTest() { + // Accept server * + assertThat(PermEval.evalAction("*", ""), is(true)); + assertThat(PermEval.evalAction("*", "literally anything"), is(true)); + + // Reject empty actions + assertThat(PermEval.evalAction("literally anything", ""), is(false)); + + // Accept match as regex + assertThat(PermEval.evalAction("action", "!action"), is(true)); + + // Reject non-match as regex + assertThat(PermEval.evalAction("action", "!nonaction"), is(false)); + + // Accept exact match + assertThat(PermEval.evalAction("action", "action"), is(true)); + + // Reject non-match + assertThat(PermEval.evalAction("action", "nonaction"), is(false)); + } + + @Test + public void redundancyTest() { + // TRUE assertTrue(PermEval.evalInstance(":fred:fred",":fred:fred")); assertTrue(PermEval.evalInstance(":fred:fred,wilma",":fred:fred")); assertTrue(PermEval.evalInstance(":fred:barney,betty,fred,wilma",":fred:fred")); - assertTrue(PermEval.evalInstance("*","fred")); assertTrue(PermEval.evalInstance(":*:fred",":fred:fred")); assertTrue(PermEval.evalInstance(":fred:*",":fred:fred")); assertTrue(PermEval.evalInstance(":!f.*:fred",":fred:fred")); assertTrue(PermEval.evalInstance(":fred:!f.*",":fred:fred")); - - /// FALSE + + // FALSE assertFalse(PermEval.evalInstance("fred","wilma")); assertFalse(PermEval.evalInstance("fred,barney,betty","wilma")); assertFalse(PermEval.evalInstance(":fred:fred",":fred:wilma")); @@ -73,10 +164,10 @@ public class JU_PermEval { assertTrue(PermEval.evalInstance("/v1/services/features/*","/v1/services/features/api1")); assertTrue(PermEval.evalInstance(":v1:services:features:*",":v1:services:features:api2")); // MSO - Xue Gao - assertTrue(PermEval.evalInstance(":v1:requests:*",":v1:requests:test0-service")); + assertTrue(PermEval.evalInstance(":v1:requests:*",":v1:requests:test0-service")); + - // Same tests, with Slashes assertTrue(PermEval.evalInstance("/fred/fred","/fred/fred")); assertTrue(PermEval.evalInstance("/fred/fred,wilma","/fred/fred")); @@ -86,8 +177,8 @@ public class JU_PermEval { assertTrue(PermEval.evalInstance("/fred/*","/fred/fred")); assertTrue(PermEval.evalInstance("/!f.*/fred","/fred/fred")); assertTrue(PermEval.evalInstance("/fred/!f.*","/fred/fred")); - - /// FALSE + + // FALSE assertFalse(PermEval.evalInstance("fred","wilma")); assertFalse(PermEval.evalInstance("fred,barney,betty","wilma")); assertFalse(PermEval.evalInstance("/fred/fred","/fred/wilma")); @@ -98,7 +189,7 @@ public class JU_PermEval { assertFalse(PermEval.evalInstance("/!f.*/!w.*","/fred/fred")); assertFalse(PermEval.evalInstance("/fred/!x.*","/fred/fred")); - + assertTrue(PermEval.evalInstance(":!com.att.*:role:write",":com.att.temp:role:write")); // CPFSF-431 Group needed help with Wild Card @@ -113,7 +204,10 @@ public class JU_PermEval { ":!topic.com.att.ecomp_test.crm.pre.*", ":topic.com.att.ecomp_test.crm.predemo100" )); - + + // coverage + @SuppressWarnings("unused") + PermEval pe = new PermEval(); } } diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_CertException.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_CertException.java new file mode 100644 index 00000000..aa12d7c6 --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_CertException.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * ============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.cm.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import org.onap.aaf.cadi.cm.CertException; + +public class JU_CertException { + + private static final String message = "The message associated with the exception"; + + @Test(expected = CertException.class) + public void test() throws CertException { + CertException except; + + except = new CertException(message); + assertThat(except.getMessage(), is(message)); + + except = new CertException(new Exception(message)); + assertThat(except.getMessage(), is("java.lang.Exception: " + message)); + + except = new CertException(message, new Exception(message)); + assertThat(except.getMessage(), is(message)); + + throw new CertException(); + } + +} diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_Factory.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_Factory.java new file mode 100644 index 00000000..e4eaf7fb --- /dev/null +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_Factory.java @@ -0,0 +1,112 @@ +/******************************************************************************* + * ============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.cm.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import static org.mockito.Mockito.*; +import org.junit.*; +import org.mockito.*; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.security.KeyPair; +import java.security.PublicKey; + +import javax.crypto.Cipher; + +import org.onap.aaf.cadi.cm.Factory; +import org.onap.aaf.cadi.cm.Factory.StripperInputStream; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.LogTarget; +import org.onap.aaf.misc.env.TimeTaken; +import org.onap.aaf.misc.env.Trans; + +public class JU_Factory { + + @Mock + Trans transMock; + + @Mock + TimeTaken timeTakenMock; + + @Mock + LogTarget logTargetMock; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + + when(transMock.start(anyString(), anyInt())).thenReturn(timeTakenMock); + when(transMock.debug()).thenReturn(logTargetMock); + } + + @Test + public void generateKeyPairTest() throws Exception { + String message = "The quick brown fox jumps over the lazy dog."; + + Cipher encryptor = Cipher.getInstance(Factory.KEY_ALGO); + Cipher decryptor = Cipher.getInstance(Factory.KEY_ALGO); + + KeyPair kp1 = Factory.generateKeyPair(transMock); + encryptor.init(Cipher.ENCRYPT_MODE, kp1.getPublic()); + decryptor.init(Cipher.DECRYPT_MODE, kp1.getPrivate()); + byte[] encrypedMessage1 = encryptor.doFinal(message.getBytes(StandardCharsets.UTF_8)); + String output1 = new String(decryptor.doFinal(encrypedMessage1)); + assertThat(output1, is(message)); + + // coverage + when(transMock.start("Generate KeyPair", Env.SUB)).thenReturn(null); + KeyPair kp2 = Factory.generateKeyPair(transMock); + encryptor.init(Cipher.ENCRYPT_MODE, kp2.getPublic()); + decryptor.init(Cipher.DECRYPT_MODE, kp2.getPrivate()); + byte[] encrypedMessage2 = encryptor.doFinal(message.getBytes(StandardCharsets.UTF_8)); + String output2 = new String(decryptor.doFinal(encrypedMessage2)); + assertThat(output2, is(message)); + + KeyPair kp3 = Factory.generateKeyPair(null); + encryptor.init(Cipher.ENCRYPT_MODE, kp3.getPublic()); + decryptor.init(Cipher.DECRYPT_MODE, kp3.getPrivate()); + byte[] encrypedMessage3 = encryptor.doFinal(message.getBytes(StandardCharsets.UTF_8)); + String output3 = new String(decryptor.doFinal(encrypedMessage3)); + assertThat(output3, is(message)); + } + + @Test + public void keyToStringTest() throws IOException { + KeyPair kp = Factory.generateKeyPair(transMock); + + String publicKeyString = Factory.toString(transMock, kp.getPublic()); + String privateKeyString = Factory.toString(transMock, kp.getPrivate()); + + String[] publicKeyLines = publicKeyString.split("\n", 0); + assertThat(publicKeyLines.length, is(9)); + assertThat(publicKeyLines[0], is("-----BEGIN PUBLIC KEY-----")); + assertThat(publicKeyLines[8], is("-----END PUBLIC KEY-----")); + + String[] privateKeyLines = privateKeyString.split("\n", 0); + assertThat(privateKeyLines.length, is(28)); + assertThat(privateKeyLines[0], is("-----BEGIN PRIVATE KEY-----")); + assertThat(privateKeyLines[27], is("-----END PRIVATE KEY-----")); + } +} diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_FastPerms.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_TokenPerm.java similarity index 72% rename from cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_FastPerms.java rename to cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_TokenPerm.java index 523bbc58..861e32e0 100644 --- a/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_FastPerms.java +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/oauth/test/JU_TokenPerm.java @@ -21,37 +21,74 @@ package org.onap.aaf.cadi.oauth.test; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import org.junit.*; + import java.io.StringReader; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; import org.onap.aaf.cadi.Permission; import org.onap.aaf.cadi.oauth.TokenPerm.LoadPermissions; import org.onap.aaf.misc.rosetta.ParseException; -public class JU_FastPerms { +public class JU_TokenPerm { - @BeforeClass - public static void setUpBeforeClass() throws Exception { - } + @Test + public void test() throws ParseException { + String json; + LoadPermissions lp; + Permission p; - @AfterClass - public static void tearDownAfterClass() throws Exception { - } + json = "{\"perm\":[" + + " {\"type\":\"com.access\",\"instance\":\"*\",\"action\":\"read,approve\"}," + + "]}"; - @Before - public void setUp() throws Exception { - } + lp = new LoadPermissions(new StringReader(json)); + assertThat(lp.perms.size(), is(1)); + + p = lp.perms.get(0); + assertThat(p.getKey(), is("com.access|*|read,approve")); + assertThat(p.permType(), is("AAF")); + + // Extra closing braces for coverage + json = "{\"perm\":[" + + " {\"type\":\"com.access\",\"instance\":\"*\",\"action\":\"read,approve\"}}," + + "]]}"; + + lp = new LoadPermissions(new StringReader(json)); + assertThat(lp.perms.size(), is(1)); - @After - public void tearDown() throws Exception { + p = lp.perms.get(0); + assertThat(p.getKey(), is("com.access|*|read,approve")); + assertThat(p.permType(), is("AAF")); + + // Test without a type + json = "{\"perm\":[" + + " {\"instance\":\"*\",\"action\":\"read,approve\"}," + + "]}"; + + lp = new LoadPermissions(new StringReader(json)); + assertThat(lp.perms.size(), is(0)); + + // Test without an instance + json = "{\"perm\":[" + + " {\"type\":\"com.access\",\"action\":\"read,approve\"}," + + "]}"; + + lp = new LoadPermissions(new StringReader(json)); + assertThat(lp.perms.size(), is(0)); + + // Test without an action + json = "{\"perm\":[" + + " {\"type\":\"com.access\",\"instance\":\"*\"}," + + "]}"; + + lp = new LoadPermissions(new StringReader(json)); + assertThat(lp.perms.size(), is(0)); } @Test - public void test() { + public void redundancyTest() { String json = "{\"perm\":[" + " {\"type\":\"com.access\",\"instance\":\"*\",\"action\":\"read,approve\"}," + " {\"type\":\"org.osaaf.aaf.access\",\"instance\":\"*\",\"action\":\"*\"}," + @@ -88,19 +125,12 @@ public class JU_FastPerms { " {\"type\":\"com.test.access\",\"instance\":\"*\",\"action\":\"read\"}," + " {\"type\":\"com.test.access\",\"instance\":\"*\",\"action\":\"read\"}" + "]}"; - try { LoadPermissions lp = new LoadPermissions(new StringReader(json)); - for(Permission p : lp.perms) { - System.out.println(p.toString()); - } - System.out.println("done"); + assertThat(lp.perms.size(), is(34)); } catch (ParseException e) { - System.err.println(e); + fail(e.getMessage()); } - - } - } diff --git a/cadi/client/src/test/java/org/onap/aaf/cadi/locator/test/JU_DNSLocator.java b/cadi/client/src/test/java/org/onap/aaf/cadi/locator/test/JU_DNSLocator.java index b97667d5..614cafbb 100644 --- a/cadi/client/src/test/java/org/onap/aaf/cadi/locator/test/JU_DNSLocator.java +++ b/cadi/client/src/test/java/org/onap/aaf/cadi/locator/test/JU_DNSLocator.java @@ -19,7 +19,7 @@ * */ -package org.onap.aaf.cadi.client.test; +package org.onap.aaf.cadi.locator.test; import java.net.URI; import java.net.URL; diff --git a/cadi/pom.xml b/cadi/pom.xml index a15d8132..6c2dd15f 100644 --- a/cadi/pom.xml +++ b/cadi/pom.xml @@ -21,11 +21,11 @@ --> 4.0.0 - + org.onap.aaf.authz cadiparent AAF CADI Parent (Code, Access, Data, Identity) @@ -36,11 +36,6 @@ pom - - org.onap.oparent - oparent - 1.1.0 - diff --git a/misc/env/pom.xml b/misc/env/pom.xml index 76de25f9..d1e3ad95 100644 --- a/misc/env/pom.xml +++ b/misc/env/pom.xml @@ -174,6 +174,12 @@ maven-surefire-plugin 2.17 + false + + **/JU*.java + + + @@ -288,6 +294,32 @@ log4j compile + + org.mockito + mockito-all + 1.9.5 + test + + + + org.powermock + powermock-module-junit4 + ${powermock.version} + test + + + org.powermock + powermock-api-mockito + ${powermock.version} + test + + + + junit + junit + 4.10 + test + diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/JU_APIExceptionTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_APIExceptionTest.java new file mode 100644 index 00000000..b0c60878 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_APIExceptionTest.java @@ -0,0 +1,71 @@ +/** + * ============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.misc.env; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +public class JU_APIExceptionTest { + + private static final String EXCEPTION_MESSAGE = "New API Exception for test"; + + @Before + public void setUp() throws Exception { + } + + @Test + public void testNewAPIExceptionWithMessage() { + APIException exception = new APIException(EXCEPTION_MESSAGE); + + assertEquals(exception.getMessage(), EXCEPTION_MESSAGE); + } + + @Test + public void testNewAPIExceptionCreatedWithMessageAndThrowable() { + Throwable throwable = new Throwable(); + APIException exception = new APIException(EXCEPTION_MESSAGE, throwable); + + assertEquals(exception.getMessage(), EXCEPTION_MESSAGE); + assertEquals(exception.getCause(), throwable); + } + + @Test + public void testNewAPIExceptionCreatedWithThrowable() { + Throwable throwable = new Throwable(); + APIException exception = new APIException(throwable); + + assertEquals(exception.getCause(), throwable); + } + + @Test + public void testPayloadSetter() { + Throwable throwable = new Throwable(); + Object payload = new Object(); + + APIException exception = new APIException(throwable); + + exception.setPayload(payload); + + assertEquals(exception.getPayload(), payload); + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/JU_BasicTransTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_BasicTransTest.java new file mode 100644 index 00000000..6a090167 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_BasicTransTest.java @@ -0,0 +1,109 @@ +/** + * ============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.misc.env; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.aaf.misc.env.impl.BasicTrans; + +@RunWith(MockitoJUnitRunner.class) +public class JU_BasicTransTest { + + BasicTrans trans = null; + + @Mock + private EnvJAXB env; + + @Mock + private TimeTaken timeTaken; + + @Before + public void setUp() throws Exception { + trans = new BasicTrans(env); + } + + @Test + public void testSlot() { + Slot slot = new Slot(1, "XML"); + when(env.slot("XML")).thenReturn(slot); + + Slot outputSlot = trans.slot("XML"); + Object[] state = new Object[2]; + + slot.put(state, "JSON"); + + assertEquals(slot.get(state), "JSON"); + assertEquals(slot.getKey(), outputSlot.getKey()); + assertEquals(slot.toString(), outputSlot.toString()); + } + + @Test + public void testGetStaticSlot() { + StaticSlot staticSlot = new StaticSlot(1, "XML"); + when(env.get(staticSlot)).thenReturn(staticSlot.toString()); + + assertEquals(staticSlot.toString(), trans.get(staticSlot)); + } + + @Test + public void testGetStaticSlotWithT() { + StaticSlot staticSlot = new StaticSlot(1, "XML"); + when(env.get(staticSlot, "XML")).thenReturn(staticSlot.getKey()); + + assertEquals(staticSlot.getKey(), trans.get(staticSlot, "XML")); + } + + @Test + public void testSetProperty() { + String tag = "tag"; + String value = "value"; + String defltValue = "diffValue"; + when(env.setProperty(tag, value)).thenReturn(value); + when(env.getProperty(tag)).thenReturn(value); + when(env.getProperty(tag, defltValue)).thenReturn(defltValue); + + assertEquals(value, trans.setProperty(tag, value)); + assertEquals(value, trans.getProperty(tag)); + assertEquals(defltValue, trans.getProperty(tag, defltValue)); + } + + @Test + public void testDecryptor() { + when(env.decryptor()).thenReturn(Decryptor.NULL); + + assertEquals(Decryptor.NULL, trans.decryptor()); + assertEquals("tag", trans.decryptor().decrypt("tag")); + } + + @Test + public void testEncryptor() { + when(env.encryptor()).thenReturn(Encryptor.NULL); + + assertEquals(Encryptor.NULL, trans.encryptor()); + assertEquals("tag", trans.encryptor().encrypt("tag")); + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_EnvFactoryTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_EnvFactoryTest.java new file mode 100644 index 00000000..f6c6912d --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_EnvFactoryTest.java @@ -0,0 +1,79 @@ +/** + * ============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.misc.env.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; +import org.onap.aaf.misc.env.EnvJAXB; +import org.onap.aaf.misc.env.TransCreate; +import org.onap.aaf.misc.env.TransJAXB; + +public class JU_EnvFactoryTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testSingleton() { + BasicEnv singleton = EnvFactory.singleton(); + + assertEquals(EnvFactory.singleton, singleton); + } + + @Test + public void testSetSingleton() { + String[] str = { "argument1" }; + BasicEnv env = new BasicEnv("tag", str); + EnvFactory.setSingleton(env); + + assertEquals(EnvFactory.singleton(), env); + } + + @Test + public void testNewTrans() { + TransJAXB newTrans = EnvFactory.newTrans(); + + assertTrue(newTrans instanceof BasicTrans); + } + + @Test + public void testNewTransEnvJAXB() { + EnvJAXB env = new BasicEnv(""); + + TransJAXB trans = EnvFactory.newTrans(env); + + assertTrue(trans instanceof BasicTrans); + } + + @Test + public void testTransCreator() { + TransCreate transCreator = EnvFactory.transCreator(); + + TransJAXB newTrans = transCreator.newTrans(); + + assertTrue(newTrans instanceof BasicTrans); + } + +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JU_JAXBDataTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JU_JAXBDataTest.java new file mode 100644 index 00000000..80de9b7b --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JU_JAXBDataTest.java @@ -0,0 +1,180 @@ +/** + * ============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.misc.env.jaxb; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.EnvJAXB; +import org.onap.aaf.misc.env.IOStringifier; +import org.onap.aaf.misc.env.old.Objectifier; +import org.onap.aaf.misc.env.old.Stringifier; + +public class JU_JAXBDataTest { + + @Mock + private Objectifier objfr; + + private String object = "Text"; + + @Mock + private Stringifier strfr; + + @Mock + private IOStringifier ioStrfr; + + @Mock + private JAXBDF df; + + @Mock + private Env env; + + @Mock + private Class typeClass; + + @Mock + private OutputStream os; + + @Mock + private Writer writer; + + @Mock + private EnvJAXB env1; + + @Before + public void setUp() throws Exception { + writer = mock(Writer.class); + os = mock(OutputStream.class); + strfr = mock(Stringifier.class); + ioStrfr = mock(IOStringifier.class); + objfr = mock(Objectifier.class); + env1 = mock(EnvJAXB.class); + } + + @Test + public void testJAXBDataEnv() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object, typeClass); + + when(objfr.objectify(env, object)).thenReturn("String1"); + + jaxb.to(os); + jaxb.to(writer); + + verify(writer).write(object); + verify(os).write(object.getBytes()); + + assertEquals(jaxb.asString(), object); + assertEquals(jaxb.asString(null), object); + assertEquals(jaxb.toString(), object); + assertEquals(jaxb.getTypeClass(), typeClass); + assertEquals(jaxb.out(null), jaxb); + assertEquals(jaxb.in(null), jaxb); + assertTrue(jaxb.getInputStream() instanceof ByteArrayInputStream); + assertEquals(jaxb.asObject(), "String1"); + assertEquals(jaxb.asObject(env1), "String1"); + assertEquals(jaxb.toString(), object); + } + + @Test + public void testJAXBDataEnvForObjectifier() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object, typeClass); + + when(objfr.objectify(env1, object)).thenReturn("String1"); + + assertEquals(jaxb.asObject(env1), "String1"); + } + + @Test + public void testJAXBDataEnvWithObject() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object); + + when(strfr.stringify(env, object, new boolean[] { false, false })).thenReturn(object); + + jaxb.to(os); + + verify(os).write(object.getBytes()); + + assertEquals(jaxb.asString(), object); + assertEquals(jaxb.asString(null), object); + assertEquals(jaxb.toString(), object); + } + + @Test + public void testJAXBDataEnvForWriter() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object); + + when(strfr.stringify(env, object, new boolean[] { false, false })).thenReturn(object); + + jaxb.to(writer); + + verify(writer).write(object); + + assertEquals(jaxb.asString(), object); + assertEquals(jaxb.asString(null), object); + assertEquals(jaxb.toString(), object); + assertEquals(jaxb.asObject(), object); + assertEquals(jaxb.asObject(null), object); + } + + @Test + public void testAsStringWithNullString() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object); + + when(strfr.stringify(env, object, new boolean[] { false, false })).thenReturn(object); + + assertEquals(jaxb.asString(), object); + } + + @Test + public void testAsStringWithNullStringWithEnv() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object); + + when(strfr.stringify(env1, object)).thenReturn(object); + + assertEquals(jaxb.asString(env1), object); + } + + @Test + public void testToWithIOStrifier() throws APIException, IOException { + JAXBData jaxb = new JAXBData(env, df, strfr, objfr, object); + + jaxb.option(0); + + when(strfr.stringify(env1, object)).thenReturn(object); + when(strfr.stringify(env, object, new boolean[] { false, false })).thenReturn(object); + + assertTrue(jaxb.getInputStream() instanceof ByteArrayInputStream); + assertEquals(jaxb.asString(env1), object); + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_DoubleOutputStreamTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_DoubleOutputStreamTest.java new file mode 100644 index 00000000..4b8c9dce --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_DoubleOutputStreamTest.java @@ -0,0 +1,104 @@ +/** + * ============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.misc.env.util; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.only; +import static org.mockito.Mockito.verify; + +import java.io.IOException; +import java.io.OutputStream; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +public class JU_DoubleOutputStreamTest { + + @Mock + private OutputStream stream1; + + @Mock + private OutputStream stream2; + + private DoubleOutputStream doubleOutputStream; + + @Before + public void setup() { + stream1 = mock(OutputStream.class); + stream2 = mock(OutputStream.class); + } + + @Test + public void testWriteInt() throws IOException { + doubleOutputStream = new DoubleOutputStream(stream1, true, stream2, true); + + doubleOutputStream.write(123); + + verify(stream1, only()).write(123); + verify(stream2, only()).write(123); + } + + @Test + public void testWriteByteArray() throws IOException { + doubleOutputStream = new DoubleOutputStream(stream1, true, stream2, true); + + byte[] bytes = { 1, 2, 3, 4 }; + + doubleOutputStream.write(bytes); + + verify(stream1, only()).write(bytes); + verify(stream2, only()).write(bytes); + + } + + @Test + public void testWriteByteArrayWithOffset() throws IOException { + doubleOutputStream = new DoubleOutputStream(stream1, true, stream2, true); + + byte[] bytes = { 1, 2, 3, 4 }; + + doubleOutputStream.write(bytes, 1, 3); + verify(stream1, only()).write(bytes, 1, 3); + verify(stream2, only()).write(bytes, 1, 3); + } + + @Test + public void testFlush() throws IOException { + doubleOutputStream = new DoubleOutputStream(stream1, true, stream2, true); + + doubleOutputStream.flush(); + + verify(stream1, only()).flush(); + verify(stream2, only()).flush(); + } + + @Test + public void testClose() throws IOException { + doubleOutputStream = new DoubleOutputStream(stream1, true, stream2, false); + + doubleOutputStream.close(); + + verify(stream1, only()).close(); + verify(stream2, never()).close(); + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_IndentPrintWriterTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_IndentPrintWriterTest.java new file mode 100644 index 00000000..b54026f1 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_IndentPrintWriterTest.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.misc.env.util; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +public class JU_IndentPrintWriterTest { + + @Mock + private OutputStream stream; + + @Mock + private Writer writer; + + @Before + public void setUp() throws Exception { + stream = mock(OutputStream.class); + writer = mock(Writer.class); + } + + @Test + public void testWriteInt() throws IOException { + IndentPrintWriter indentWriter = new IndentPrintWriter(writer); + + indentWriter.write(123); + + verify(writer).write(123); + + assertEquals(indentWriter.getIndent(), 0); + } + + @Test + public void testWriteIntWithNewLineCharacter() throws IOException { + IndentPrintWriter indentWriter = new IndentPrintWriter(writer); + + indentWriter.setIndent(12); + + indentWriter.println(); + + indentWriter.write("123", 1, 2); + + verify(writer).write('\n'); + verify(writer).write('2'); + verify(writer).write('3'); + assertEquals(indentWriter.getIndent(), 12); + } + + @Test + public void testWriteString() throws IOException { + IndentPrintWriter indentWriter = new IndentPrintWriter(writer); + + indentWriter.inc(); + + indentWriter.write("123"); + + verify(writer).write('1'); + verify(writer).write('2'); + verify(writer).write('3'); + assertEquals(indentWriter.getIndent(), 1); + } + + @Test + public void testSetIndent() throws IOException { + IndentPrintWriter indentWriter = new IndentPrintWriter(stream); + + indentWriter.setIndent(12); + indentWriter.dec(); + + assertEquals(indentWriter.getIndent(), 11); + } + + @Test + public void testToCol() throws IOException { + IndentPrintWriter indentWriter = new IndentPrintWriter(writer); + + indentWriter.toCol(5); + char[] chars = { 'a', 'b', 'c' }; + indentWriter.write(chars, 1, 2); + + verify(writer, times(5)).write(' '); + verify(writer).write('c'); + verify(writer).write('b'); + } +} \ No newline at end of file diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderOutputStreamTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderOutputStreamTest.java new file mode 100644 index 00000000..377a2891 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderOutputStreamTest.java @@ -0,0 +1,135 @@ +/** + * ============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.misc.env.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.io.IOException; + +import org.junit.Before; +import org.junit.Test; + +public class JU_StringBuilderOutputStreamTest { + + StringBuilderOutputStream streamBuilder; + + StringBuilder builder = new StringBuilder(); + + @Before + public void setUp() throws Exception { + streamBuilder = new StringBuilderOutputStream(builder); + } + + @Test + public void testWriteIntAndReset() { + streamBuilder.write(123); + + assertEquals("123", streamBuilder.toString()); + streamBuilder.reset(); + assertEquals("", streamBuilder.toString()); + } + + @Test + public void testWriteByteArrayWithoutException() throws IOException { + byte[] bytes = { 1, 2, 3, 4 }; + streamBuilder.write(bytes); + assertEquals(4, streamBuilder.getBuffer().length()); + + streamBuilder.write(bytes, 1, 2); + assertEquals(6, streamBuilder.getBuffer().length()); + + streamBuilder.write(bytes, 1, 0); + assertEquals(6, streamBuilder.getBuffer().length()); + + streamBuilder.append(bytes[0]); + assertEquals(7, streamBuilder.getBuffer().length()); + } + + @Test + public void testWriteByteArrayWithIndexOutOfBoundException() { + byte[] bytes = { 1, 2, 3, 4 }; + + try { + streamBuilder.write(bytes, -1, 2); + fail("This is supposed to throw IndexOutOfBounds Excetpion"); + } catch (IndexOutOfBoundsException e) { + } catch (Exception e) { + fail("This should throw only IndexOutOfBounds Exception"); + } + assertEquals(0, streamBuilder.getBuffer().length()); + + } + + @Test + public void testDefaultConstructor() throws IOException { + StringBuilderOutputStream stream = new StringBuilderOutputStream(); + + assertNotNull(stream.getBuffer()); + stream.close(); + } + + @Test + public void testConstructorWithPositiveDefaultCapacity() throws IOException { + StringBuilderOutputStream stream = new StringBuilderOutputStream(10); + + assertNotNull(stream.getBuffer()); + assertEquals(10, stream.getBuffer().capacity()); + stream.close(); + } + + @Test + public void testConstructorWithNegativeCapacityException() { + try { + StringBuilderOutputStream stream = new StringBuilderOutputStream(-1); + fail("This should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + } catch (Exception e) { + fail("This should throw only IllegalArgumentException"); + } + } + + @Test + public void testWriteString() { + streamBuilder.write("1234"); + + assertEquals("1234", streamBuilder.toString()); + + streamBuilder.write("1234", 1, 2); + assertEquals("12342", streamBuilder.toString()); + } + + @Test + public void testAppendCharSequence() { + streamBuilder.append("1234"); + assertEquals("1234", streamBuilder.toString()); + + streamBuilder.append(null); + assertEquals("1234null", streamBuilder.toString()); + + streamBuilder.append("1234", 1, 2); + assertEquals("1234null2", streamBuilder.toString()); + + streamBuilder.append(null, 1, 2); + assertEquals("1234null2u", streamBuilder.toString()); + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderWriterTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderWriterTest.java new file mode 100644 index 00000000..6a06e866 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderWriterTest.java @@ -0,0 +1,135 @@ +/** + * ============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.misc.env.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.io.IOException; + +import org.junit.Before; +import org.junit.Test; + +public class JU_StringBuilderWriterTest { + + StringBuilderWriter streamWriter; + + StringBuilder builder = new StringBuilder(); + + @Before + public void setUp() throws Exception { + streamWriter = new StringBuilderWriter(builder); + } + + @Test + public void testWriteIntAndReset() { + streamWriter.write(1); + + assertEquals(1, streamWriter.getBuffer().length()); + streamWriter.reset(); + assertEquals("", streamWriter.toString()); + } + + @Test + public void testWriteByteArrayWithoutException() throws IOException { + char[] bytes = { 1, 2, 3, 4 }; + streamWriter.write(bytes); + assertEquals(4, streamWriter.getBuffer().length()); + + streamWriter.write(bytes, 1, 2); + assertEquals(6, streamWriter.getBuffer().length()); + + streamWriter.write(bytes, 1, 0); + assertEquals(6, streamWriter.getBuffer().length()); + + streamWriter.append(bytes[0]); + assertEquals(7, streamWriter.getBuffer().length()); + } + + @Test + public void testWriteByteArrayWithIndexOutOfBoundException() { + char[] bytes = { 1, 2, 3, 4 }; + + try { + streamWriter.write(bytes, -1, 2); + fail("This is supposed to throw IndexOutOfBounds Excetpion"); + } catch (IndexOutOfBoundsException e) { + } catch (Exception e) { + fail("This should throw only IndexOutOfBounds Exception"); + } + assertEquals(0, streamWriter.getBuffer().length()); + + } + + @Test + public void testDefaultConstructor() throws IOException { + StringBuilderWriter stream = new StringBuilderWriter(); + + assertNotNull(stream.getBuffer()); + stream.close(); + } + + @Test + public void testConstructorWithPositiveDefaultCapacity() throws IOException { + StringBuilderWriter stream = new StringBuilderWriter(10); + + assertNotNull(stream.getBuffer()); + assertEquals(10, stream.getBuffer().capacity()); + stream.close(); + } + + @Test + public void testConstructorWithNegativeCapacityException() { + try { + StringBuilderWriter stream = new StringBuilderWriter(-1); + fail("This should throw IllegalArgumentException"); + } catch (IllegalArgumentException e) { + } catch (Exception e) { + fail("This should throw only IllegalArgumentException"); + } + } + + @Test + public void testWriteString() { + streamWriter.write("1234"); + + assertEquals("1234", streamWriter.toString()); + + streamWriter.write("1234", 1, 2); + assertEquals("123423", streamWriter.toString()); + } + + @Test + public void testAppendCharSequence() { + streamWriter.append("1234"); + assertEquals("1234", streamWriter.toString()); + + streamWriter.append(null); + assertEquals("1234null", streamWriter.toString()); + + streamWriter.append("1234", 1, 2); + assertEquals("1234null2", streamWriter.toString()); + + streamWriter.append(null, 1, 2); + assertEquals("1234null2u", streamWriter.toString()); + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_IPValidator.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_IPValidator.java index 01acf218..3976718f 100644 --- a/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_IPValidator.java +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_IPValidator.java @@ -1,70 +1,67 @@ -/** - * ============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.misc.env.util.test; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; - -import org.junit.Test; -import org.onap.aaf.misc.env.util.IPValidator; - -public class JU_IPValidator { - - @Test - public void test() { - assertTrue(IPValidator.ipv4("10.10.10.10")); - assertTrue(IPValidator.ipv4("127.0.0.0")); - assertFalse(IPValidator.ipv4("10")); - assertFalse(IPValidator.ipv4("10.10.10")); - assertFalse(IPValidator.ipv4("10.10.10.")); - assertFalse(IPValidator.ipv4("10.10.10.10.")); - assertFalse(IPValidator.ipv4("10.10.10.10.10")); - assertFalse(IPValidator.ipv4("something10.10.10.10")); - assertTrue(IPValidator.ipv4("0.10.10.10")); - assertTrue(IPValidator.ipv4("0.0.0.0")); - assertTrue(IPValidator.ipv4("0.10.10.10")); - assertFalse(IPValidator.ipv4("011.255.255.255")); - assertFalse(IPValidator.ipv4("255.01.255.255")); - assertFalse(IPValidator.ipv4("255.255.255.256")); - assertFalse(IPValidator.ipv4("255.299.255.255")); - - - assertTrue(IPValidator.ipv6("0000:0000:0000:0000:0000:0000:0000:0000")); - assertTrue(IPValidator.ipv6("0:0:0:0:0:0:0:0")); - assertTrue(IPValidator.ipv6("2001:08DB:0000:0000:0023:F422:FE3B:AC10")); - assertTrue(IPValidator.ipv6("2001:8DB:0:0:23:F422:FE3B:AC10")); - assertTrue(IPValidator.ipv6("2001:8DB::23:F422:FE3B:AC10")); - assertTrue(IPValidator.ipv6("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); - assertTrue(IPValidator.ipv6("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF")); - assertFalse(IPValidator.ipv6("2001:8DB::23:G422:FE3B:AC10")); - assertFalse(IPValidator.ipv6("2001:8DB::23:G422:FE3B:AC10")); - // more than one Double Colons - assertFalse(IPValidator.ipv6("0000:0000:0000::0000::0000")); - assertFalse(IPValidator.ipv6("2001:8DB::23:G422:FE3B:AC10:FFFF")); - - - - assertTrue(IPValidator.ip("2001:08DB:0000:0000:0023:F422:FE3B:AC10")); - assertTrue(IPValidator.ip("192.168.7.2")); - } - -} +/** + * ============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.misc.env.util.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.aaf.misc.env.util.IPValidator; + +public class JU_IPValidator { + + @Test + public void test() { + assertTrue(IPValidator.ipv4("10.10.10.10")); + assertTrue(IPValidator.ipv4("127.0.0.0")); + assertFalse(IPValidator.ipv4("10")); + assertFalse(IPValidator.ipv4("10.10.10")); + assertFalse(IPValidator.ipv4("10.10.10.")); + assertFalse(IPValidator.ipv4("10.10.10.10.")); + assertFalse(IPValidator.ipv4("10.10.10.10.10")); + assertFalse(IPValidator.ipv4("something10.10.10.10")); + assertTrue(IPValidator.ipv4("0.10.10.10")); + assertTrue(IPValidator.ipv4("0.0.0.0")); + assertTrue(IPValidator.ipv4("0.10.10.10")); + assertFalse(IPValidator.ipv4("011.255.255.255")); + assertFalse(IPValidator.ipv4("255.01.255.255")); + assertFalse(IPValidator.ipv4("255.255.255.256")); + assertFalse(IPValidator.ipv4("255.299.255.255")); + + assertTrue(IPValidator.ipv6("0000:0000:0000:0000:0000:0000:0000:0000")); + assertTrue(IPValidator.ipv6("0:0:0:0:0:0:0:0")); + assertTrue(IPValidator.ipv6("2001:08DB:0000:0000:0023:F422:FE3B:AC10")); + assertTrue(IPValidator.ipv6("2001:8DB:0:0:23:F422:FE3B:AC10")); + assertTrue(IPValidator.ipv6("2001:8DB::23:F422:FE3B:AC10")); + assertTrue(IPValidator.ipv6("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); + assertTrue(IPValidator.ipv6("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF")); + assertFalse(IPValidator.ipv6("2001:8DB::23:G422:FE3B:AC10")); + assertFalse(IPValidator.ipv6("2001:8DB::23:G422:FE3B:AC10")); + // more than one Double Colons + assertFalse(IPValidator.ipv6("0000:0000:0000::0000::0000")); + assertFalse(IPValidator.ipv6("2001:8DB::23:G422:FE3B:AC10:FFFF")); + + assertTrue(IPValidator.ip("2001:08DB:0000:0000:0023:F422:FE3B:AC10")); + assertTrue(IPValidator.ip("192.168.7.2")); + } + +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_PoolTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_PoolTest.java new file mode 100644 index 00000000..e3f90de1 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/util/test/JU_PoolTest.java @@ -0,0 +1,81 @@ +/** + * ============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.misc.env.util.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.LogTarget; +import org.onap.aaf.misc.env.util.Pool; + +public class JU_PoolTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void test() { + Pool pool = new Pool(new Pool.Creator() { + + Integer content = 0; + + @Override + public Integer create() throws APIException { + return content++; + } + + @Override + public void destroy(Integer t) { + + } + + @Override + public boolean isValid(Integer t) { + return t == content; + } + + @Override + public void reuse(Integer t) { + content = t; + } + }); + try { + // pool.drain(); + assertEquals("Should return intial value", 0, pool.get().content); + // pooled.toss(); + pool.prime(LogTarget.SYSOUT, 23); + assertEquals("Should Return 23 as added at last prime", 23, pool.get(LogTarget.SYSOUT).content); + pool.prime(LogTarget.SYSERR, 13); + assertEquals("Should add another 13 from SysErr and remove 1", 35, pool.get(LogTarget.SYSERR).content); + assertEquals("Create a new creator with create method", 1, pool.get().content); + assertEquals("Create a new creator with create method", 2, pool.get().content); + assertEquals("Should remove last from pool", 34, pool.get(LogTarget.SYSOUT).content); + + pool.drain(); + assertEquals("Should remove last from pool", 17, pool.get(LogTarget.SYSOUT).content); + } catch (APIException e) { + } + } + +} diff --git a/misc/log4j/pom.xml b/misc/log4j/pom.xml index c945b739..bcd2d257 100644 --- a/misc/log4j/pom.xml +++ b/misc/log4j/pom.xml @@ -71,7 +71,7 @@ - true + 0.7.7.201606060606 3.2 jacoco @@ -174,6 +174,12 @@ maven-surefire-plugin 2.17 + false + + **/JU*.java + + + diff --git a/misc/pom.xml b/misc/pom.xml index e6c955fd..d7aa53ac 100644 --- a/misc/pom.xml +++ b/misc/pom.xml @@ -22,11 +22,11 @@ 4.0.0 - + + org.onap.aaf.authz + parent + 2.1.0-SNAPSHOT + org.onap.aaf.authz miscparent AAF Misc Parent @@ -34,13 +34,6 @@ pom - - - org.onap.oparent - oparent - 1.1.0 - - @@ -127,6 +120,13 @@ + + env + xgen + rosetta + log4j + + @@ -205,6 +205,12 @@ maven-surefire-plugin 2.17 + false + + **/JU*.java + + + @@ -319,12 +325,7 @@ - - env - xgen - rosetta - log4j - +