From f10d38d4af0178d008c69ada7cd1a6abc6ec075a Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 13 Feb 2019 16:16:45 -0500 Subject: Sonar fixes Added test for Serializer to increase junit coverage. Added tests for TestTimeMulti to increase junit coverage. Sonar fixes for PropertyConfiguration: - use equalsIgnoreCase - use the exception Updated license dates. Some fixes for LoggerFactoryWrapper to address sonar issue - utility classes should typically have a private constructor. Change-Id: I8957e9673fe8371ecca7abbb7ece87b0d6f46c1a Issue-ID: POLICY-1519 Signed-off-by: Jim Hahn --- .../onap/policy/common/utils/io/Serializer.java | 6 +- .../policy/common/utils/time/TestTimeMulti.java | 14 ++--- .../policy/common/utils/io/SerializerTest.java | 28 +++++++++- .../common/utils/time/TestTimeMultiTest.java | 17 +++++- .../utils/properties/PropertyConfiguration.java | 64 +++++++++++----------- .../common/utils/slf4j/LoggerFactoryWrapper.java | 12 +++- .../utils/slf4j/LoggerFactoryWrapperTest.java | 4 +- 7 files changed, 93 insertions(+), 52 deletions(-) diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/io/Serializer.java b/utils-test/src/main/java/org/onap/policy/common/utils/io/Serializer.java index 600bbd39..9ab26d32 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/io/Serializer.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/io/Serializer.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine - Common Modules * ================================================================================ - * 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. @@ -126,7 +126,7 @@ public class Serializer { /** * Read the object. - * + * * @param ois input stream * @return the object * @throws IOException throws IO exception if cannot read @@ -134,7 +134,7 @@ public class Serializer { public Object readObject(ObjectInputStream ois) throws IOException { try { return ois.readObject(); - + } catch (ClassNotFoundException e) { throw new IOException(e); } diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTimeMulti.java b/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTimeMulti.java index 2782eb72..b37e49e0 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTimeMulti.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTimeMulti.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * Common Utils-Test * ================================================================================ - * 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. @@ -57,7 +57,7 @@ public class TestTimeMulti extends CurrentTime { /** * Constructor. - * + * * @param nthreads number of threads that will be sleeping simultaneously */ public TestTimeMulti(int nthreads) { @@ -99,7 +99,7 @@ public class TestTimeMulti extends CurrentTime { * Indicates that a thread has terminated or that it will no longer be invoking * {@link #sleep(long)}. Awakens the next sleeping thread, if the queue is full after * removing the terminated thread. - * + * * @throws IllegalStateException if the queue is already full */ public void threadCompleted() { @@ -159,7 +159,7 @@ public class TestTimeMulti extends CurrentTime { /** * Constructor. - * + * * @param awakenAtMs time, in milliseconds, at which the associated thread should * awaken */ @@ -181,7 +181,7 @@ public class TestTimeMulti extends CurrentTime { /** * Blocks the current thread until awakened (i.e., until its latch is * decremented). - * + * * @throws InterruptedException can be interrupted */ public void await() throws InterruptedException { diff --git a/utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java b/utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java index 36812b3b..ee66195c 100644 --- a/utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java +++ b/utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java @@ -33,6 +33,8 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import org.junit.AfterClass; import org.junit.Before; @@ -195,7 +197,7 @@ public class SerializerTest { public ByteArrayOutputStream makeByteArrayOutputStream() { return out; } - + @Override public ObjectOutputStream makeObjectOutputStream(ByteArrayOutputStream out) throws IOException { return oos; @@ -208,7 +210,7 @@ public class SerializerTest { }); assertThatThrownBy(() -> Serializer.serialize(new MyObject(130))).isEqualTo(ex2); - + } @Test @@ -268,6 +270,28 @@ public class SerializerTest { assertThatThrownBy(() -> Serializer.deserialize(MyObject.class, data)).isEqualTo(ex); } + @Test + public void testDeserialize_ObjectRead_ClassEx() throws Exception { + MyObject obj1 = new MyObject(200); + + // must use binary character set + Charset binary = StandardCharsets.ISO_8859_1; + + // serialize the object + String text = new String(Serializer.serialize(obj1), binary); + + /* + * Replace the class name with a bogus class name, which should cause + * ClassNotFoundException when we attempt to deserialize it. + */ + text = text.replace("MyObject", "AnObject"); + + byte[] data = text.getBytes(binary); + + assertThatThrownBy(() -> Serializer.deserialize(MyObject.class, data)).isInstanceOf(IOException.class) + .hasCauseInstanceOf(ClassNotFoundException.class); + } + @Test public void testDeserialize_ObjectCloseEx() throws Exception { IOException ex = new IOException("testDeserialize_ObjectCloseEx"); diff --git a/utils-test/src/test/java/org/onap/policy/common/utils/time/TestTimeMultiTest.java b/utils-test/src/test/java/org/onap/policy/common/utils/time/TestTimeMultiTest.java index 311e276d..f17235a2 100644 --- a/utils-test/src/test/java/org/onap/policy/common/utils/time/TestTimeMultiTest.java +++ b/utils-test/src/test/java/org/onap/policy/common/utils/time/TestTimeMultiTest.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * Common Utils-Test * ================================================================================ - * 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. @@ -20,6 +20,7 @@ package org.onap.policy.common.utils.time; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -72,6 +73,9 @@ public class TestTimeMultiTest { } assertTrue(ttm.getMillis() >= tbeg + NTIMES * MIN_SLEEP_MS); + + // something in the queue, but no threads remain -> exception + assertThatIllegalStateException().isThrownBy(() -> ttm.threadCompleted()); } private class MyThread extends Thread { @@ -95,6 +99,13 @@ public class TestTimeMultiTest { public void run() { try { for (int x = 0; x < NTIMES; ++x) { + // negative sleep should have no effect + texpected = ttm.getMillis(); + ttm.sleep(-1); + if ((tactual = ttm.getMillis()) != texpected) { + break; + } + texpected = ttm.getMillis() + sleepMs; ttm.sleep(sleepMs); diff --git a/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyConfiguration.java b/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyConfiguration.java index 46003bae..b41f3bc0 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyConfiguration.java +++ b/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyConfiguration.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine - Common Modules * ================================================================================ - * 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. @@ -41,7 +41,7 @@ import org.onap.policy.common.utils.properties.exception.PropertyMissingExceptio * subclass. The values of the fields are set via setXxx() methods. As a result, if * a field is annotated and there is no corresponding setXxx() method, then an * exception will be thrown. - * + * *

It is possible that an invalid defaultValue is specified via the * {@link Property} annotation. This could remain undetected until an optional property is * left out of the {@link Properties}. Consequently, this class will always validate a @@ -66,7 +66,7 @@ public class PropertyConfiguration { /** * Initializes each "@Property" field with its value, as found in the properties. - * + * * @param props properties from which to extract the values * @throws PropertyException if an error occurs */ @@ -77,7 +77,7 @@ public class PropertyConfiguration { /** * Walks the class hierarchy of "this" object, populating fields defined in each * class, using values extracted from the given property set. - * + * * @param props properties from which to extract the values * @throws PropertyException if an error occurs */ @@ -95,7 +95,7 @@ public class PropertyConfiguration { /** * Sets a field's value, within an object, based on what's in the properties. - * + * * @param field field whose value is to be set * @param props properties from which to get the value * @return {@code true} if the property's value was set, {@code false} otherwise @@ -121,7 +121,7 @@ public class PropertyConfiguration { /** * Sets a field's value from a particular property. - * + * * @param setter method to be used to set the field's value * @param field field whose value is to be set * @param props properties from which to get the value @@ -151,7 +151,7 @@ public class PropertyConfiguration { /** * Get the setter. - * + * * @param field field whose value is to be set * @param prop property of interest * @return the method to be used to set the field's value @@ -170,7 +170,7 @@ public class PropertyConfiguration { /** * Gets a property value, coercing it to the field's type. - * + * * @param field field whose value is to be set * @param props properties from which to get the value * @param prop property of interest @@ -205,7 +205,7 @@ public class PropertyConfiguration { /** * Verifies that the field can be modified, i.e., it's neither static, nor * final. - * + * * @param field field whose value is to be set * @param prop property of interest * @throws PropertyAccessException if the field is not modifiable @@ -224,7 +224,7 @@ public class PropertyConfiguration { /** * Verifies that the setter method is not static. - * + * * @param setter method to be checked * @param prop property of interest * @throws PropertyAccessException if the method is static @@ -239,7 +239,7 @@ public class PropertyConfiguration { /** * Gets a property value, coercing it to a String. - * + * * @param fieldName field whose value is to be set * @param props properties from which to get the value * @param prop property of interest @@ -258,7 +258,7 @@ public class PropertyConfiguration { /** * Gets a property value, coercing it to a Boolean. - * + * * @param fieldName field whose value is to be set * @param props properties from which to get the value * @param prop property of interest @@ -274,7 +274,7 @@ public class PropertyConfiguration { /** * Gets a property value, coercing it to an Integer. - * + * * @param fieldName field whose value is to be set * @param props properties from which to get the value * @param prop property of interest @@ -290,7 +290,7 @@ public class PropertyConfiguration { /** * Gets a property value, coercing it to a Long. - * + * * @param fieldName field whose value is to be set * @param props properties from which to get the value * @param prop property of interest @@ -306,7 +306,7 @@ public class PropertyConfiguration { /** * Gets a value from the property set. - * + * * @param fieldName field whose value is to be set * @param props properties from which to get the value * @param prop property of interest @@ -333,7 +333,7 @@ public class PropertyConfiguration { /** * Gets the property value, straight from the property set. - * + * * @param props properties from which to get the value * @param propnm name of the property of interest * @return the raw property value @@ -344,7 +344,7 @@ public class PropertyConfiguration { /** * Coerces a String value into a Boolean. - * + * * @param fieldName field whose value is to be set * @param prop property of interest * @param value value to be coerced @@ -352,10 +352,10 @@ public class PropertyConfiguration { * @throws PropertyInvalidException if the value does not represent a valid Boolean */ private Boolean makeBoolean(String fieldName, Property prop, String value) throws PropertyInvalidException { - if ("true".equals(value.toLowerCase())) { + if ("true".equalsIgnoreCase(value)) { return Boolean.TRUE; - } else if ("false".equals(value.toLowerCase())) { + } else if ("false".equalsIgnoreCase(value)) { return Boolean.FALSE; } else { @@ -365,7 +365,7 @@ public class PropertyConfiguration { /** * Coerces a String value into an Integer. - * + * * @param fieldName field whose value is to be set * @param prop property of interest * @param value value to be coerced @@ -383,7 +383,7 @@ public class PropertyConfiguration { /** * Coerces a String value into a Long. - * + * * @param fieldName field whose value is to be set * @param prop property of interest * @param value value to be coerced @@ -403,7 +403,7 @@ public class PropertyConfiguration { * Applies a function to check a property's default value. If the function throws an * exception about an invalid property, then it's re-thrown as an exception about an * invalid defaultValue. - * + * * @param fieldName name of the field being checked * @param prop property of interest * @param func function to invoke to check the default value @@ -416,15 +416,14 @@ public class PropertyConfiguration { func.apply(null); } catch (PropertyInvalidException ex) { - throw new PropertyInvalidException(ex.getPropertyName(), fieldName, "defaultValue is invalid", - ex.getCause()); + throw new PropertyInvalidException(ex.getPropertyName(), fieldName, "defaultValue is invalid", ex); } } } /** * Determines if a value is OK, even if it's empty. - * + * * @param prop property specifying what's acceptable * @param value value to be checked * @return {@code true} if the value is not empty or empty is allowed, {@code false} @@ -437,7 +436,7 @@ public class PropertyConfiguration { /** * Determines if a {@link Property}'s accept attribute includes the "empty" * option. - * + * * @param prop property whose accept attribute is to be examined * @return {@code true} if the accept attribute includes "empty" */ @@ -459,7 +458,7 @@ public class PropertyConfiguration { /** * Checks the default value. - * + * * @param arg always {@code null} * @throws PropertyInvalidException if an error occurs */ @@ -476,14 +475,14 @@ public class PropertyConfiguration { /** * Name of the property. - * + * * @return the property name */ public String name(); /** * Default value, used when the property does not exist. - * + * * @return the default value */ public String defaultValue() default ""; @@ -491,10 +490,9 @@ public class PropertyConfiguration { /** * Comma-separated options identifying what's acceptable. The word, "empty", * indicates that an empty string, "", is an acceptable value. - * + * * @return options identifying what's acceptable */ public String accept() default ""; - } } diff --git a/utils/src/main/java/org/onap/policy/common/utils/slf4j/LoggerFactoryWrapper.java b/utils/src/main/java/org/onap/policy/common/utils/slf4j/LoggerFactoryWrapper.java index 8f32b3bb..6f800015 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/slf4j/LoggerFactoryWrapper.java +++ b/utils/src/main/java/org/onap/policy/common/utils/slf4j/LoggerFactoryWrapper.java @@ -1,8 +1,9 @@ -/*- +/* * ============LICENSE_START======================================================= * ONAP Policy Engine - Common Modules * ================================================================================ * Copyright (C) 2019 Samsung Electronics. All rights reserved. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,10 +25,17 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Helper class to retrive particular types of loggers without storing the logger name everywhere in ths code. + * Helper class to retrieve particular types of loggers without storing the logger name everywhere in ths code. */ public class LoggerFactoryWrapper { + /** + * Constructs the object. + */ + private LoggerFactoryWrapper() { + super(); + } + /** * Get Network Logger. * diff --git a/utils/src/test/java/org/onap/policy/common/utils/slf4j/LoggerFactoryWrapperTest.java b/utils/src/test/java/org/onap/policy/common/utils/slf4j/LoggerFactoryWrapperTest.java index dbe2e671..4bf8db30 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/slf4j/LoggerFactoryWrapperTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/slf4j/LoggerFactoryWrapperTest.java @@ -3,6 +3,7 @@ * ONAP Policy Engine - Common Modules * ================================================================================ * Copyright (C) 2019 Samsung Electronics. All rights reserved. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +25,6 @@ package org.onap.policy.common.utils.slf4j; import static org.junit.Assert.assertSame; import static org.mockito.Mockito.mock; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -47,6 +47,6 @@ public class LoggerFactoryWrapperTest { PowerMockito.mockStatic(LoggerFactory.class); PowerMockito.when(LoggerFactory.getLogger(netLoggerName)).thenReturn(mockLogger); - assertSame(LoggerFactoryWrapper.getNetworkLogger(), mockLogger); + assertSame(mockLogger, LoggerFactoryWrapper.getNetworkLogger()); } } -- cgit 1.2.3-korg