diff options
author | Jim Hahn <jrh3@att.com> | 2019-06-12 14:32:25 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-06-12 17:36:59 -0400 |
commit | ea262e6da52fd4da0733f02998f87aebaf502ddb (patch) | |
tree | a7cded567521f0141f31d9f8c185eccdaf2a8979 /utils/src/test/java | |
parent | e671e9fa5d09ec696cb24aeefaf10ddbc7c705d7 (diff) |
Apply simple sonar fixes
Note: A number of these were identified, by SonarLint, in the Test
classes, which are not typically scanned by Sonar.
Removed unnecessary imports.
Removed unneeded "throws Xxx".
Replaced lambda with method references.
Replaced duplicate strings with constants.
Replaced try-fail-catch with assert-j methods to eliminate sonar
complaints about duplicate failure messages.
Added missing @Override annotations.
Use map.computeIfAbsent() where appropriate.
Also fixed some minor checkstyle issues.
Removed unneeded "volatile" declarations.
Replaced some if-else constructs with "?:" construct, per sonar.
Replaced Object.wait() with CountDownLatch.await(); according to sonar
(and javadocs), Object.wait() can return due to "spurious wakeups".
Fixed issue whereby CryptoUtilsTest wouldn't run in my Eclipse.
Change-Id: Ib6b71ed65662cfd6209400dac57ed69279bf29ec
Issue-ID: POLICY-1791
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'utils/src/test/java')
8 files changed, 167 insertions, 162 deletions
diff --git a/utils/src/test/java/org/onap/policy/common/utils/jpa/EntityMgrCloserTest.java b/utils/src/test/java/org/onap/policy/common/utils/jpa/EntityMgrCloserTest.java index e791e084..589d0924 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/jpa/EntityMgrCloserTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/jpa/EntityMgrCloserTest.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * Common Utils * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 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. @@ -36,7 +36,7 @@ public class EntityMgrCloserTest { @Before - public void setUp() throws Exception { + public void setUp() { mgr = mock(EntityManager.class); } diff --git a/utils/src/test/java/org/onap/policy/common/utils/jpa/EntityMgrFactoryCloserTest.java b/utils/src/test/java/org/onap/policy/common/utils/jpa/EntityMgrFactoryCloserTest.java index 8f2c0159..ca2b7220 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/jpa/EntityMgrFactoryCloserTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/jpa/EntityMgrFactoryCloserTest.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * Common Utils * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 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. @@ -36,7 +36,7 @@ public class EntityMgrFactoryCloserTest { @Before - public void setUp() throws Exception { + public void setUp() { factory = mock(EntityManagerFactory.class); } diff --git a/utils/src/test/java/org/onap/policy/common/utils/jpa/EntityTransCloserTest.java b/utils/src/test/java/org/onap/policy/common/utils/jpa/EntityTransCloserTest.java index 9a499e49..d764e9d0 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/jpa/EntityTransCloserTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/jpa/EntityTransCloserTest.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * Common Utils * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 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. @@ -40,7 +40,7 @@ public class EntityTransCloserTest { * Set up EntityTransaction mock. */ @Before - public void setUp() throws Exception { + public void setUp() { trans = mock(EntityTransaction.class); when(trans.isActive()).thenReturn(true); diff --git a/utils/src/test/java/org/onap/policy/common/utils/properties/BeanConfiguratorTest.java b/utils/src/test/java/org/onap/policy/common/utils/properties/BeanConfiguratorTest.java index beca88f0..07e0795f 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/properties/BeanConfiguratorTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/properties/BeanConfiguratorTest.java @@ -39,6 +39,11 @@ import org.onap.policy.common.utils.properties.exception.PropertyMissingExceptio * Test class for PropertyConfiguration. */ public class BeanConfiguratorTest { + private static final String EXPECTED_EXCEPTION = "expected exception"; + private static final String FALSE_STRING = "false"; + private static final String A_VALUE = "a.value"; + private static final String NUMBER_STRING_LONG = "20000"; + private static final String NUMBER_STRING = "200"; /** * Property used for most of the simple configuration subclasses. @@ -141,7 +146,7 @@ public class BeanConfiguratorTest { // now a different set of values props.setProperty(THE_VALUE, STRING_VALUE + "x"); props.setProperty("parent.value", "50001"); - props.setProperty("grandparent.value", "false"); + props.setProperty("grandparent.value", FALSE_STRING); beancfg.configureFromProperties(cfg, props); assertEquals(STRING_VALUE + "x", cfg.value); @@ -240,7 +245,7 @@ public class BeanConfiguratorTest { beancfg = new BeanConfigurator() { @Override protected Object getValue(Field field, Properties props, Property prop) { - throw new IllegalArgumentException("expected exception"); + throw new IllegalArgumentException(EXPECTED_EXCEPTION); } }; @@ -253,7 +258,7 @@ public class BeanConfiguratorTest { @Override public void setValue(String value) { - throw new IllegalArgumentException("expected exception"); + throw new IllegalArgumentException(EXPECTED_EXCEPTION); } } @@ -384,11 +389,11 @@ public class BeanConfiguratorTest { } } - props.setProperty("string", "a string"); + props.setProperty("string", STRING_VALUE); props.setProperty("boolean.true", "true"); - props.setProperty("boolean.false", "false"); + props.setProperty("boolean.false", FALSE_STRING); props.setProperty("primitive.boolean.true", "true"); - props.setProperty("primitive.boolean.false", "false"); + props.setProperty("primitive.boolean.false", FALSE_STRING); props.setProperty("integer", "100"); props.setProperty("primitive.integer", "101"); props.setProperty("long", "10000"); @@ -397,7 +402,7 @@ public class BeanConfiguratorTest { Config cfg = new Config(); beancfg.configureFromProperties(cfg, props); - assertEquals("a string", cfg.stringValue); + assertEquals(STRING_VALUE, cfg.stringValue); assertEquals(true, cfg.boolTrueValue); assertEquals(false, cfg.boolFalseValue); assertEquals(true, cfg.primBoolTrueValue); @@ -553,7 +558,7 @@ public class BeanConfiguratorTest { assertEquals(true, cfg.value); // try again, with the property defined as false - props.setProperty(THE_VALUE, "false"); + props.setProperty(THE_VALUE, FALSE_STRING); cfg = new Config(); beancfg.configureFromProperties(cfg, props); assertEquals(false, cfg.value); @@ -563,7 +568,7 @@ public class BeanConfiguratorTest { public void testGetBooleanValue_ValidDefault_False() throws PropertyException { class Config { - @Property(name = THE_VALUE, defaultValue = "false") + @Property(name = THE_VALUE, defaultValue = FALSE_STRING) private Boolean value; @SuppressWarnings("unused") @@ -584,7 +589,7 @@ public class BeanConfiguratorTest { assertEquals(true, cfg.value); // try again, with the property defined as false - props.setProperty(THE_VALUE, "false"); + props.setProperty(THE_VALUE, FALSE_STRING); cfg = new Config(); beancfg.configureFromProperties(cfg, props); assertEquals(false, cfg.value); @@ -603,7 +608,7 @@ public class BeanConfiguratorTest { } } - props.setProperty(THE_VALUE, "200"); + props.setProperty(THE_VALUE, NUMBER_STRING); Config cfg = new Config(); beancfg.configureFromProperties(cfg, props); @@ -623,7 +628,7 @@ public class BeanConfiguratorTest { } } - props.setProperty(THE_VALUE, "200"); + props.setProperty(THE_VALUE, NUMBER_STRING); beancfg.configureFromProperties(new Config(), props); } @@ -646,7 +651,7 @@ public class BeanConfiguratorTest { assertEquals(201, cfg.value.intValue()); // try again, with the property defined - props.setProperty(THE_VALUE, "200"); + props.setProperty(THE_VALUE, NUMBER_STRING); cfg = new Config(); beancfg.configureFromProperties(cfg, props); assertEquals(200, cfg.value.intValue()); @@ -665,7 +670,7 @@ public class BeanConfiguratorTest { } } - props.setProperty(THE_VALUE, "20000"); + props.setProperty(THE_VALUE, NUMBER_STRING_LONG); Config cfg = new Config(); beancfg.configureFromProperties(cfg, props); @@ -685,7 +690,7 @@ public class BeanConfiguratorTest { } } - props.setProperty(THE_VALUE, "20000"); + props.setProperty(THE_VALUE, NUMBER_STRING_LONG); beancfg.configureFromProperties(new Config(), props); } @@ -708,7 +713,7 @@ public class BeanConfiguratorTest { assertEquals(20001L, cfg.value.longValue()); // try again, with the property defined - props.setProperty(THE_VALUE, "20000"); + props.setProperty(THE_VALUE, NUMBER_STRING_LONG); cfg = new Config(); beancfg.configureFromProperties(cfg, props); assertEquals(20000L, cfg.value.longValue()); @@ -869,7 +874,7 @@ public class BeanConfiguratorTest { @Test public void testMakeBoolean_False() throws PropertyException { - props.setProperty(THE_VALUE, "false"); + props.setProperty(THE_VALUE, FALSE_STRING); PlainBooleanConfig cfg = new PlainBooleanConfig(); beancfg.configureFromProperties(cfg, props); @@ -1095,7 +1100,7 @@ public class BeanConfiguratorTest { beancfg.addToProperties(cfg, props, "the", "a"); assertEquals("1010", props.getProperty("a.parent.value")); - assertEquals(STRING_VALUE, props.getProperty("a.value")); + assertEquals(STRING_VALUE, props.getProperty(A_VALUE)); assertEquals("other", props.getProperty("a.other.value")); // original prefix is empty @@ -1104,7 +1109,7 @@ public class BeanConfiguratorTest { // original prefix is ends with "." beancfg.addToProperties(cfg, props, "the.", "a"); - assertEquals(STRING_VALUE, props.getProperty("a.value")); + assertEquals(STRING_VALUE, props.getProperty(A_VALUE)); // new prefix is empty beancfg.addToProperties(cfg, props, "", ""); @@ -1162,7 +1167,7 @@ public class BeanConfiguratorTest { beancfg.addToProperties(cfg, props, "the", "a"); assertFalse(props.contains("noAnnotation")); - assertEquals(STRING_VALUE, props.getProperty("a.value")); + assertEquals(STRING_VALUE, props.getProperty(A_VALUE)); assertFalse(props.contains("a.null.value")); assertEquals("some other value", props.getProperty("some.other.prefix")); } @@ -1232,8 +1237,8 @@ public class BeanConfiguratorTest { beancfg.addToProperties(cfg, props, "", ""); assertEquals("true", props.getProperty("plain.bool")); - assertEquals("false", props.getProperty("prim.bool")); - assertEquals("false", props.getProperty("plain.bool.get")); + assertEquals(FALSE_STRING, props.getProperty("prim.bool")); + assertEquals(FALSE_STRING, props.getProperty("plain.bool.get")); assertEquals("true", props.getProperty("prim.bool.get")); assertEquals("1100", props.getProperty("int")); assertEquals(STRING_VALUE, props.getProperty("string")); @@ -1301,8 +1306,8 @@ public class BeanConfiguratorTest { beancfg = new BeanConfigurator() { @Override - protected Method getGetter(Field field, String methodName) throws SecurityException { - throw new SecurityException("expected exception"); + protected Method getGetter(Field field, String methodName) { + throw new SecurityException(EXPECTED_EXCEPTION); } }; @@ -1319,7 +1324,7 @@ public class BeanConfiguratorTest { @SuppressWarnings("unused") public String getValue() { - throw new RuntimeException("expected exception"); + throw new RuntimeException(EXPECTED_EXCEPTION); } } @@ -1411,7 +1416,7 @@ public class BeanConfiguratorTest { private String value; public static void setValue(String value) { - + // do nothing } } diff --git a/utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java index e5a79bc3..4b2b007b 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/resources/ResourceUtilsTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 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. @@ -48,11 +49,12 @@ public class ResourceUtilsTest { private String jarDirResource = null; private String jarFileResource = null; - private final String pathDirResource = "testdir"; - private final String pathFileResource = "testdir/testfile.xml"; + private static final String RESOURCES_PATH = "src/test/resources/"; + private static final String PATH_DIR_RESOURCE = "testdir"; + private static final String PATH_FILE_RESOURCE = "testdir/testfile.xml"; - private final String nonExistantResource = "somewhere/over/the/rainbow"; - private final String invalidResource = "@%%%\\\\_:::DESD"; + private static final String NON_EXISTENT_RESOURCE = "somewhere/over/the/rainbow"; + private static final String INVALID_RESOURCE = "@%%%\\\\_:::DESD"; /** * Setup resource utils test. @@ -68,9 +70,9 @@ public class ResourceUtilsTest { jarDirResource = "META-INF"; jarFileResource = "META-INF/MANIFEST.MF"; - final FileWriter fileWriter = new FileWriter(tmpUsedFile); - fileWriter.write("Bluebirds fly over the rainbow"); - fileWriter.close(); + try (final FileWriter fileWriter = new FileWriter(tmpUsedFile)) { + fileWriter.write("Bluebirds fly over the rainbow"); + } } /** @@ -93,25 +95,25 @@ public class ResourceUtilsTest { theUrl = ResourceUtils.getUrlResource(jarFileResource); assertNotNull(theUrl); - theUrl = ResourceUtils.getUrlResource(pathDirResource); + theUrl = ResourceUtils.getUrlResource(PATH_DIR_RESOURCE); assertNotNull(theUrl); - theUrl = ResourceUtils.getUrlResource(pathFileResource); + theUrl = ResourceUtils.getUrlResource(PATH_FILE_RESOURCE); assertNotNull(theUrl); - theUrl = ResourceUtils.getUrlResource("file:///" + pathDirResource); + theUrl = ResourceUtils.getUrlResource("file:///" + PATH_DIR_RESOURCE); assertNotNull(theUrl); - theUrl = ResourceUtils.getLocalFile("src/test/resources/" + pathDirResource); + theUrl = ResourceUtils.getLocalFile(RESOURCES_PATH + PATH_DIR_RESOURCE); assertNotNull(theUrl); - theUrl = ResourceUtils.getLocalFile("src/test/resources/" + pathFileResource); + theUrl = ResourceUtils.getLocalFile(RESOURCES_PATH + PATH_FILE_RESOURCE); assertNotNull(theUrl); - theUrl = ResourceUtils.getUrlResource(nonExistantResource); + theUrl = ResourceUtils.getUrlResource(NON_EXISTENT_RESOURCE); assertNull(theUrl); - theUrl = ResourceUtils.getUrlResource(invalidResource); + theUrl = ResourceUtils.getUrlResource(INVALID_RESOURCE); assertNull(theUrl); theUrl = ResourceUtils.getUrlResource(null); @@ -138,22 +140,22 @@ public class ResourceUtilsTest { theUrl = ResourceUtils.getLocalFile(jarFileResource); assertNull(theUrl); - theUrl = ResourceUtils.getLocalFile(pathDirResource); + theUrl = ResourceUtils.getLocalFile(PATH_DIR_RESOURCE); assertNull(theUrl); - theUrl = ResourceUtils.getLocalFile(pathFileResource); + theUrl = ResourceUtils.getLocalFile(PATH_FILE_RESOURCE); assertNull(theUrl); - theUrl = ResourceUtils.getLocalFile("src/test/resources/" + pathDirResource); + theUrl = ResourceUtils.getLocalFile(RESOURCES_PATH + PATH_DIR_RESOURCE); assertNotNull(theUrl); - theUrl = ResourceUtils.getLocalFile("src/test/resources/" + pathFileResource); + theUrl = ResourceUtils.getLocalFile(RESOURCES_PATH + PATH_FILE_RESOURCE); assertNotNull(theUrl); - theUrl = ResourceUtils.getLocalFile(nonExistantResource); + theUrl = ResourceUtils.getLocalFile(NON_EXISTENT_RESOURCE); assertNull(theUrl); - theUrl = ResourceUtils.getLocalFile(invalidResource); + theUrl = ResourceUtils.getLocalFile(INVALID_RESOURCE); assertNull(theUrl); theUrl = ResourceUtils.getLocalFile("file:///"); @@ -186,29 +188,29 @@ public class ResourceUtilsTest { theStream = ResourceUtils.getResourceAsStream(jarFileResource); assertNotNull(theStream); - theStream = ResourceUtils.getResourceAsStream(pathDirResource); + theStream = ResourceUtils.getResourceAsStream(PATH_DIR_RESOURCE); assertNotNull(theStream); - theStream = ResourceUtils.getResourceAsStream(pathFileResource); + theStream = ResourceUtils.getResourceAsStream(PATH_FILE_RESOURCE); assertNotNull(theStream); - theStream = ResourceUtils.getResourceAsStream("src/test/resources/" + pathDirResource); + theStream = ResourceUtils.getResourceAsStream(RESOURCES_PATH + PATH_DIR_RESOURCE); assertNotNull(theStream); - theStream = ResourceUtils.getResourceAsStream("src/test/resources/" + pathFileResource); + theStream = ResourceUtils.getResourceAsStream(RESOURCES_PATH + PATH_FILE_RESOURCE); assertNotNull(theStream); - theStream = ResourceUtils.getResourceAsStream(nonExistantResource); + theStream = ResourceUtils.getResourceAsStream(NON_EXISTENT_RESOURCE); assertNull(theStream); - theStream = ResourceUtils.getResourceAsStream(invalidResource); + theStream = ResourceUtils.getResourceAsStream(INVALID_RESOURCE); assertNull(theStream); theStream = ResourceUtils.getResourceAsStream(null); - assertNull(null); + assertNull(theStream); theStream = ResourceUtils.getResourceAsStream(""); - assertNull(null); + assertNotNull(theStream); } /** @@ -228,22 +230,22 @@ public class ResourceUtilsTest { theString = ResourceUtils.getResourceAsString(jarFileResource); assertNotNull(theString); - theString = ResourceUtils.getResourceAsString(pathDirResource); + theString = ResourceUtils.getResourceAsString(PATH_DIR_RESOURCE); assertNotNull(theString); - theString = ResourceUtils.getResourceAsString(pathFileResource); + theString = ResourceUtils.getResourceAsString(PATH_FILE_RESOURCE); assertNotNull(theString); - theString = ResourceUtils.getResourceAsString("src/test/resources/" + pathDirResource); + theString = ResourceUtils.getResourceAsString(RESOURCES_PATH + PATH_DIR_RESOURCE); assertNotNull(theString); - theString = ResourceUtils.getResourceAsString("src/test/resources/" + pathFileResource); + theString = ResourceUtils.getResourceAsString(RESOURCES_PATH + PATH_FILE_RESOURCE); assertNotNull(theString); - theString = ResourceUtils.getResourceAsString(nonExistantResource); + theString = ResourceUtils.getResourceAsString(NON_EXISTENT_RESOURCE); assertNull(theString); - theString = ResourceUtils.getResourceAsString(invalidResource); + theString = ResourceUtils.getResourceAsString(INVALID_RESOURCE); assertNull(theString); theString = ResourceUtils.getResourceAsString(null); @@ -272,22 +274,22 @@ public class ResourceUtilsTest { theUrl = ResourceUtils.getUrl4Resource(jarFileResource); assertNotNull(theUrl); - theUrl = ResourceUtils.getUrl4Resource(pathDirResource); + theUrl = ResourceUtils.getUrl4Resource(PATH_DIR_RESOURCE); assertNotNull(theUrl); - theUrl = ResourceUtils.getUrl4Resource(pathFileResource); + theUrl = ResourceUtils.getUrl4Resource(PATH_FILE_RESOURCE); assertNotNull(theUrl); - theUrl = ResourceUtils.getUrl4Resource("src/test/resources/" + pathDirResource); + theUrl = ResourceUtils.getUrl4Resource(RESOURCES_PATH + PATH_DIR_RESOURCE); assertNotNull(theUrl); - theUrl = ResourceUtils.getUrl4Resource("src/test/resources/" + pathFileResource); + theUrl = ResourceUtils.getUrl4Resource(RESOURCES_PATH + PATH_FILE_RESOURCE); assertNotNull(theUrl); - theUrl = ResourceUtils.getUrl4Resource(nonExistantResource); + theUrl = ResourceUtils.getUrl4Resource(NON_EXISTENT_RESOURCE); assertNull(theUrl); - theUrl = ResourceUtils.getUrl4Resource(invalidResource); + theUrl = ResourceUtils.getUrl4Resource(INVALID_RESOURCE); assertNull(theUrl); } diff --git a/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java index fd3daee8..9a88918d 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java @@ -24,7 +24,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import java.security.GeneralSecurityException; - import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,86 +34,88 @@ import org.slf4j.LoggerFactory; public class CryptoUtilsTest { private static Logger logger = LoggerFactory.getLogger(CryptoUtilsTest.class); - private final String pass = "HelloWorld"; - private final String secretKey = "12345678901234567890123456789012"; - private final String encryptedPass = "enc:8XxseP5W5ODxzPrReNKd9JBYLv0iiAzy9BHnMKau5yg="; + private static final String PASS = "HelloWorld"; + private static final String SECRET_KEY = "MTIzNDU2Nzg5MDEyMzQ1Ng=="; + private static final String ENCRYPTED_PASS = "enc:hcI2XVX+cxPz/6rlbebkWpCFF6WPbBtT7iJRr2VHUkA="; + private static final String DECRYPTED_MSG = "encrypted value: {} decrypted value : {}"; + private static final String ENCRYPTED_MSG = "original value : {} encrypted value: {}"; @Test public void testEncrypt() throws GeneralSecurityException { logger.info("testEncrypt:"); - CryptoUtils cryptoUtils = new CryptoUtils(secretKey); - String encryptedValue = cryptoUtils.encrypt(pass); - logger.info("original value : " + pass + " encrypted value: " + encryptedValue); + CryptoUtils cryptoUtils = new CryptoUtils(SECRET_KEY); + String encryptedValue = cryptoUtils.encrypt(PASS); + logger.info(ENCRYPTED_MSG, PASS, encryptedValue); String decryptedValue = cryptoUtils.decrypt(encryptedValue); - logger.info("encrypted value: " + encryptedValue + " decrypted value : " + decryptedValue); - assertEquals(pass, decryptedValue); + logger.info(DECRYPTED_MSG, encryptedValue, decryptedValue); + assertEquals(PASS, decryptedValue); } @Test public void testDecrypt() throws GeneralSecurityException { logger.info("testDecrypt:"); - CryptoUtils cryptoUtils = new CryptoUtils(secretKey); - String decryptedValue = cryptoUtils.decrypt(encryptedPass); - logger.info("encrypted value: " + encryptedPass + " decrypted value : " + decryptedValue); - assertEquals(pass, decryptedValue); + CryptoUtils cryptoUtils = new CryptoUtils(SECRET_KEY); + String decryptedValue = cryptoUtils.decrypt(ENCRYPTED_PASS); + logger.info(DECRYPTED_MSG, ENCRYPTED_PASS, decryptedValue); + assertEquals(PASS, decryptedValue); } @Test public void testStaticEncrypt() { logger.info("testStaticEncrypt:"); - String encryptedValue = CryptoUtils.encrypt(pass, secretKey); - logger.info("original value : " + pass + " encrypted value: " + encryptedValue); + String encryptedValue = CryptoUtils.encrypt(PASS, SECRET_KEY); + logger.info(ENCRYPTED_MSG, PASS, encryptedValue); - String decryptedValue = CryptoUtils.decrypt(encryptedValue, secretKey); - logger.info("encrypted value: " + encryptedValue + " decrypted value : " + decryptedValue); - assertEquals(pass, decryptedValue); + String decryptedValue = CryptoUtils.decrypt(encryptedValue, SECRET_KEY); + logger.info(DECRYPTED_MSG, encryptedValue, decryptedValue); + assertEquals(PASS, decryptedValue); } @Test public void testStaticDecrypt() { logger.info("testStaticDecrypt:"); - String decryptedValue = CryptoUtils.decrypt(encryptedPass, secretKey); - logger.info("encrypted value: " + encryptedPass + " decrypted value : " + decryptedValue); - assertEquals(pass, decryptedValue); + String decryptedValue = CryptoUtils.decrypt(ENCRYPTED_PASS, SECRET_KEY); + logger.info(DECRYPTED_MSG, ENCRYPTED_PASS, decryptedValue); + assertEquals(PASS, decryptedValue); } @Test public void testBadInputs() { - String badKey = CryptoUtils.encrypt(pass, "test"); - assertEquals(pass, badKey); + String badKey = CryptoUtils.encrypt(PASS, "test"); + assertEquals(PASS, badKey); - String badDecrypt = CryptoUtils.decrypt(encryptedPass, ""); - assertEquals(encryptedPass, badDecrypt); + String badDecrypt = CryptoUtils.decrypt(ENCRYPTED_PASS, ""); + assertEquals(ENCRYPTED_PASS, badDecrypt); - String emptyValue = CryptoUtils.encrypt(new String(), secretKey); + String emptyValue = CryptoUtils.encrypt("", SECRET_KEY); assertEquals("", emptyValue); - String emptyDecrypt = CryptoUtils.decrypt(new String(), secretKey); + String emptyDecrypt = CryptoUtils.decrypt("", SECRET_KEY); assertEquals("", emptyDecrypt); - String nullValue = CryptoUtils.encrypt(null, secretKey); + String nullValue = CryptoUtils.encrypt(null, SECRET_KEY); assertNull(nullValue); - String nullDecrypt = CryptoUtils.decrypt(null, secretKey); + String nullDecrypt = CryptoUtils.decrypt(null, SECRET_KEY); assertNull(nullDecrypt); } @Test public void testAll() { logger.info("testAll:"); - String encryptedValue = CryptoUtils.encrypt(pass, secretKey); - logger.info("original value : " + pass + " encrypted value: " + encryptedValue); + String encryptedValue = CryptoUtils.encrypt(PASS, SECRET_KEY); + logger.info(ENCRYPTED_MSG, PASS, encryptedValue); - String encryptedAgain = CryptoUtils.encrypt(encryptedValue, secretKey); + String encryptedAgain = CryptoUtils.encrypt(encryptedValue, SECRET_KEY); assertEquals(encryptedValue, encryptedAgain); - String decryptedValue = CryptoUtils.decrypt(encryptedAgain, secretKey); - logger.info("encrypted value: " + encryptedAgain + " decrypted value : " + decryptedValue); - assertEquals(pass, decryptedValue); + String decryptedValue = CryptoUtils.decrypt(encryptedAgain, SECRET_KEY); + logger.info(DECRYPTED_MSG, encryptedAgain, decryptedValue); + assertEquals(PASS, decryptedValue); - String decryptedAgain = CryptoUtils.decrypt(decryptedValue, secretKey); + String decryptedAgain = CryptoUtils.decrypt(decryptedValue, SECRET_KEY); assertEquals(decryptedValue, decryptedAgain); } }
\ No newline at end of file diff --git a/utils/src/test/java/org/onap/policy/common/utils/services/ServiceManagerTest.java b/utils/src/test/java/org/onap/policy/common/utils/services/ServiceManagerTest.java index b7774a5e..83b2629c 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/services/ServiceManagerTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/services/ServiceManagerTest.java @@ -90,7 +90,7 @@ public class ServiceManagerTest { } @Test - public void testAddStartable() throws Exception { + public void testAddStartable() { Startable start1 = mock(Startable.class); svcmgr.addService("first startable", start1); @@ -115,7 +115,7 @@ public class ServiceManagerTest { } @Test - public void testStart() throws Exception { + public void testStart() { Startable start1 = mock(Startable.class); svcmgr.addService("test start", start1); @@ -201,7 +201,7 @@ public class ServiceManagerTest { } @Test - public void testStop() throws Exception { + public void testStop() { Startable start1 = mock(Startable.class); svcmgr.addService("first stop", start1); @@ -245,7 +245,7 @@ public class ServiceManagerTest { } @Test - public void testShutdown() throws Exception { + public void testShutdown() { Startable start1 = mock(Startable.class); svcmgr.addService("first stop", start1); @@ -265,7 +265,7 @@ public class ServiceManagerTest { } @Test - public void testRewind() throws Exception { + public void testRewind() { RunnableWithEx starter = mock(RunnableWithEx.class); LinkedList<String> lst = new LinkedList<>(); diff --git a/utils/src/test/java/org/onap/policy/common/utils/validation/AssertionsTest.java b/utils/src/test/java/org/onap/policy/common/utils/validation/AssertionsTest.java index e39058bd..05d4c5c3 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/validation/AssertionsTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/validation/AssertionsTest.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019 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. @@ -21,6 +22,7 @@ package org.onap.policy.common.utils.validation; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -32,67 +34,62 @@ import org.junit.Test; * @author Liam Fallon (liam.fallon@ericsson.com) */ public class AssertionsTest { + private static final String HELLO = "Hello"; + private static final String IT_IS_OK = "it is OK"; + private static final String IT_IS_NULL = "it is null"; + private static final String IT_IS_TRUE = "it is true"; + private static final String IT_IS_FALSE = "it is false"; + @Test public void testAssertions() { - Assertions.argumentNotFalse(true, "it is true"); + Assertions.argumentNotFalse(true, IT_IS_TRUE); + + assertThatIllegalArgumentException().isThrownBy(() -> Assertions.argumentNotFalse(false, IT_IS_FALSE)) + .withMessage(IT_IS_FALSE); + + + Assertions.argumentOfClassNotFalse(true, ArithmeticException.class, IT_IS_TRUE); - try { - Assertions.argumentNotFalse(false, "it is false"); - } catch (IllegalArgumentException e) { - assertEquals("it is false", e.getMessage()); - } + assertThatIllegalArgumentException().isThrownBy( + () -> Assertions.argumentOfClassNotFalse(false, ArithmeticException.class, IT_IS_FALSE)) + .withMessage(IT_IS_FALSE); - Assertions.argumentOfClassNotFalse(true, ArithmeticException.class, "it is true"); - try { - Assertions.argumentOfClassNotFalse(false, ArithmeticException.class, "it is false"); - } catch (Exception e) { - assertEquals("it is false", e.getMessage()); - } + Assertions.argumentNotNull(HELLO, IT_IS_OK); - Assertions.argumentNotNull("Hello", "it is OK"); + assertThatIllegalArgumentException().isThrownBy(() -> Assertions.argumentNotNull(null, IT_IS_NULL)) + .withMessage(IT_IS_NULL); - try { - Assertions.argumentNotNull(null, "it is null"); - } catch (IllegalArgumentException e) { - assertEquals("it is null", e.getMessage()); - } - Assertions.argumentOfClassNotNull(true, ArithmeticException.class, "it is OK"); + Assertions.argumentOfClassNotNull(true, ArithmeticException.class, IT_IS_OK); + + assertThatIllegalArgumentException().isThrownBy( + () -> Assertions.argumentOfClassNotNull(null, ArithmeticException.class, IT_IS_NULL)) + .withMessage(IT_IS_NULL); - try { - Assertions.argumentOfClassNotNull(null, ArithmeticException.class, "it is null"); - } catch (Exception e) { - assertEquals("it is null", e.getMessage()); - } Assertions.assignableFrom(java.util.TreeMap.class, java.util.Map.class); - try { - Assertions.assignableFrom(java.util.Map.class, java.util.TreeMap.class); - } catch (IllegalArgumentException e) { - assertEquals("java.util.Map is not an instance of java.util.TreeMap", e.getMessage()); - } + assertThatIllegalArgumentException() + .isThrownBy(() -> Assertions.assignableFrom(java.util.Map.class, java.util.TreeMap.class)) + .withMessage("java.util.Map is not an instance of java.util.TreeMap"); + - Assertions.instanceOf("Hello", String.class); + Assertions.instanceOf(HELLO, String.class); + + assertThatIllegalArgumentException().isThrownBy(() -> Assertions.instanceOf(100, String.class)) + .withMessage("java.lang.Integer is not an instance of java.lang.String"); - try { - Assertions.instanceOf(100, String.class); - } catch (IllegalArgumentException e) { - assertEquals("java.lang.Integer is not an instance of java.lang.String", e.getMessage()); - } Assertions.validateStringParameter("name", "MyName", "^M.*e$"); - try { - Assertions.validateStringParameter("name", "MyName", "^M.*f$"); - } catch (IllegalArgumentException e) { - assertEquals("parameter \"name\": value \"MyName\", does not match regular expression \"^M.*f$\"", - e.getMessage()); - } + assertThatIllegalArgumentException() + .isThrownBy(() -> Assertions.validateStringParameter("name", "MyName", "^M.*f$")) + .withMessage("parameter \"name\": value \"MyName\", does not match regular expression \"^M.*f$\""); + - assertNull(Assertions.getStringParameterValidationMessage("Greeting", "Hello", "^H.*o$")); + assertNull(Assertions.getStringParameterValidationMessage("Greeting", HELLO, "^H.*o$")); assertEquals("parameter Greeting with value Hello does not match regular expression Goodbye", - Assertions.getStringParameterValidationMessage("Greeting", "Hello", "Goodbye")); + Assertions.getStringParameterValidationMessage("Greeting", HELLO, "Goodbye")); } } |