From ff0842117fba4dfe675a8b87af3a1098cde023bd Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 29 Jun 2020 10:16:44 -0400 Subject: Fix issues in common for new sonar rules Addressed issues reported due to updates to the sonar rules: - invoke only one method in a junit lambda - complete the assertion - add DOCTYPE to html Issue-ID: POLICY-2650 Change-Id: Ib8b8a2e4736cc23849c0f7aef972ffa3365a3e00 Signed-off-by: Jim Hahn --- .../common/gson/internal/FieldSerializerTest.java | 6 ++++-- .../org/onap/policy/common/im/AllSeemsWellTest.java | 19 +++++++++++-------- .../onap/policy/common/im/IntegrityMonitorTest.java | 20 ++++++++++---------- .../onap/policy/common/im/StateManagementTest.java | 12 +++++------- .../event/comm/bus/NoopTopicFactoryTest.java | 6 +++--- .../http/server/test/YamlMessageBodyHandlerTest.java | 5 ++--- .../src/test/resources/webapps/alt-root/index.html | 4 +++- .../src/test/resources/webapps/root/index.html | 4 +++- .../policy/common/utils/gson/GsonTestUtilsTest.java | 9 +++++---- .../time/PseudoScheduledExecutorServiceTest.java | 12 +++++++----- .../policy/common/utils/time/PseudoTimerTest.java | 16 ++++++++++------ .../policy/common/utils/coder/StandardCoderTest.java | 5 ++++- 12 files changed, 67 insertions(+), 51 deletions(-) diff --git a/gson/src/test/java/org/onap/policy/common/gson/internal/FieldSerializerTest.java b/gson/src/test/java/org/onap/policy/common/gson/internal/FieldSerializerTest.java index cc5ef928..156e4ef3 100644 --- a/gson/src/test/java/org/onap/policy/common/gson/internal/FieldSerializerTest.java +++ b/gson/src/test/java/org/onap/policy/common/gson/internal/FieldSerializerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 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. @@ -89,7 +89,9 @@ public class FieldSerializerTest { text = "world"; - assertThatThrownBy(() -> ser.addToTree(this, new JsonObject())).isInstanceOf(JsonParseException.class) + JsonObject obj = new JsonObject(); + + assertThatThrownBy(() -> ser.addToTree(this, obj)).isInstanceOf(JsonParseException.class) .hasMessage(FieldSerializer.GET_ERR + FieldSerializerTest.class.getName() + ".text"); } } diff --git a/integrity-monitor/src/test/java/org/onap/policy/common/im/AllSeemsWellTest.java b/integrity-monitor/src/test/java/org/onap/policy/common/im/AllSeemsWellTest.java index 79cfe549..a46be4f0 100644 --- a/integrity-monitor/src/test/java/org/onap/policy/common/im/AllSeemsWellTest.java +++ b/integrity-monitor/src/test/java/org/onap/policy/common/im/AllSeemsWellTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Monitor * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 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. @@ -20,7 +20,7 @@ package org.onap.policy.common.im; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -177,16 +177,19 @@ public class AllSeemsWellTest extends IntegrityMonitorTestBase { } // Check for null parameters - assertThatThrownBy(() -> im.allSeemsWell(null, IntegrityMonitorProperties.ALLSEEMSWELL, ALL_SEEMS_WELL_MSG)); + assertThatIllegalArgumentException().isThrownBy( + () -> im.allSeemsWell(null, IntegrityMonitorProperties.ALLSEEMSWELL, ALL_SEEMS_WELL_MSG)); - assertThatThrownBy(() -> im.allSeemsWell("", IntegrityMonitorProperties.ALLSEEMSWELL, ALL_SEEMS_WELL_MSG)); + assertThatIllegalArgumentException().isThrownBy( + () -> im.allSeemsWell("", IntegrityMonitorProperties.ALLSEEMSWELL, ALL_SEEMS_WELL_MSG)); - assertThatThrownBy(() -> im.allSeemsWell(this.getClass().getName(), null, ALL_SEEMS_WELL_MSG)); + assertThatIllegalArgumentException().isThrownBy( + () -> im.allSeemsWell(this.getClass().getName(), null, ALL_SEEMS_WELL_MSG)); - assertThatThrownBy(() -> im.allSeemsWell(this.getClass().getName(), IntegrityMonitorProperties.ALLSEEMSWELL, - null)); + assertThatIllegalArgumentException().isThrownBy( + () -> im.allSeemsWell(this.getClass().getName(), IntegrityMonitorProperties.ALLSEEMSWELL, null)); - assertThatThrownBy( + assertThatIllegalArgumentException().isThrownBy( () -> im.allSeemsWell(this.getClass().getName(), IntegrityMonitorProperties.ALLSEEMSWELL, "")); logger.debug("\n\ntestAllSeemsWell: Exit\n\n"); diff --git a/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTest.java b/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTest.java index 1e194c3b..0def27e0 100644 --- a/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTest.java +++ b/integrity-monitor/src/test/java/org/onap/policy/common/im/IntegrityMonitorTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Monitor * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 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. @@ -192,7 +192,7 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase { waitCycles(DEPENDENCY_CHECK_CYCLES); final IntegrityMonitor im2 = im; - assertThatThrownBy(im2::evaluateSanity); + assertThatThrownBy(im2::evaluateSanity).isInstanceOf(IntegrityMonitorException.class); // undo dependency groups and jmx test properties settings myProp.put(IntegrityMonitorProperties.DEPENDENCY_GROUPS, ""); @@ -313,7 +313,7 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase { assertEquals(StateManagement.LOCKED, sm.getAdminState()); // test startTransaction. It should fail since it is locked - assertThatThrownBy(im::startTransaction); + assertThatThrownBy(im::startTransaction).isInstanceOf(IntegrityMonitorException.class); sm.unlock(); logger.debug("\n\nsm.unlock()\nAdminState = {}\nOpState() = {}\nAvailStatus = {}\nStandbyStatus = {}\n", @@ -374,7 +374,7 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase { assertEquals(StateManagement.HOT_STANDBY, sm.getStandbyStatus()); // test startTransaction. It should fail since it is standby - assertThatThrownBy(im::startTransaction); + assertThatThrownBy(im::startTransaction).isInstanceOf(IntegrityMonitorException.class); sm.promote(); @@ -400,7 +400,7 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase { // Test startTransaction. Should fail since standby status is cold // standby - assertThatThrownBy(im::startTransaction); + assertThatThrownBy(im::startTransaction).isInstanceOf(IntegrityMonitorException.class); sm.enableNoDependency(); @@ -410,7 +410,7 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase { assertEquals(StateManagement.FAILED, sm.getAvailStatus()); // Test startTransaction. Should fail since standby status is cold // standby - assertThatThrownBy(im::startTransaction); + assertThatThrownBy(im::startTransaction).isInstanceOf(IntegrityMonitorException.class); sm.disableDependency(); sm.enableNotFailed(); @@ -423,7 +423,7 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase { assertEquals(StateManagement.DEPENDENCY, sm.getAvailStatus()); // Test startTransaction. Should fail since standby status is cold // standby - assertThatThrownBy(im::startTransaction); + assertThatThrownBy(im::startTransaction).isInstanceOf(IntegrityMonitorException.class); sm.enableNoDependency(); logger.debug( @@ -432,7 +432,7 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase { assertEquals(StateManagement.ENABLED, sm.getOpState()); // test startTransaction. It should fail since standby status is hot // standby - assertThatThrownBy(im::startTransaction); + assertThatThrownBy(im::startTransaction).isInstanceOf(IntegrityMonitorException.class); logger.debug("\n\ntestIM: Exit\n\n"); } @@ -490,7 +490,7 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase { // to do it. logger.debug("\n\nIntegrityMonitor.testSanityState: calling im.dependencyCheck()\n\n"); im.dependencyCheck(); - assertThatThrownBy(im::evaluateSanity); + assertThatThrownBy(im::evaluateSanity).isInstanceOf(IntegrityMonitorException.class); logger.debug("\n\ntestSanityState: Exit\n\n"); } @@ -648,7 +648,7 @@ public class IntegrityMonitorTest extends IntegrityMonitorTestBase { */ waitCycles(DEPENDENCY_CHECK_CYCLES); - assertThatThrownBy(im::evaluateSanity); + assertThatThrownBy(im::evaluateSanity).isInstanceOf(IntegrityMonitorException.class); logger.debug("\n\ntestStateCheck: Exit\n\n"); } diff --git a/integrity-monitor/src/test/java/org/onap/policy/common/im/StateManagementTest.java b/integrity-monitor/src/test/java/org/onap/policy/common/im/StateManagementTest.java index 680a73e6..12671ec3 100644 --- a/integrity-monitor/src/test/java/org/onap/policy/common/im/StateManagementTest.java +++ b/integrity-monitor/src/test/java/org/onap/policy/common/im/StateManagementTest.java @@ -113,19 +113,17 @@ public class StateManagementTest extends IntegrityMonitorTestBase { // the standbystatus shall transition to coldstandby and a // StandbyStatusException shall be thrown logger.info("\n??? promote() test case P4"); - assertThatThrownBy(() -> { - sm.disableFailed(); - sm.lock(); + sm.disableFailed(); + sm.lock(); + assertThatThrownBy(sm::promote).isInstanceOf(IntegrityMonitorException.class); - sm.promote(); - }); assertEquals(LOCKED_DISABLED_FAILED_COLDSTANDBY, makeString(sm)); // P3 If promote() is called while standbyStatus is coldstandby, the // state shall not transition // and a StandbyStatusException shall be thrown logger.info("\n??? promote() test case P3"); - assertThatThrownBy(sm::promote); + assertThatThrownBy(sm::promote).isInstanceOf(IntegrityMonitorException.class); assertEquals(LOCKED_DISABLED_FAILED_COLDSTANDBY, makeString(sm)); // P2 If promote() is called while the standbyStatus is null and the @@ -217,7 +215,7 @@ public class StateManagementTest extends IntegrityMonitorTestBase { assertEquals(UNLOCKED_DISABLED_FAILED_COLDSTANDBY, makeString(stateChangeNotifier.getStateManagement())); logger.info("\n??? State change notification test case 6 - promote()"); - assertThatThrownBy(sm::promote); + assertThatThrownBy(sm::promote).isInstanceOf(IntegrityMonitorException.class); assertEquals(UNLOCKED_DISABLED_FAILED_COLDSTANDBY, makeString(sm)); } catch (final Exception ex) { diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/NoopTopicFactoryTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/NoopTopicFactoryTest.java index 5cb9bcbd..af7a4b29 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/NoopTopicFactoryTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/NoopTopicFactoryTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 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. @@ -20,7 +20,7 @@ package org.onap.policy.common.endpoints.event.comm.bus; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; @@ -136,7 +136,7 @@ public abstract class NoopTopicFactoryTest, T exte initFactory(); assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC) .setTopicProperty(PROPERTY_MANAGED_SUFFIX, "false").build()).size()); - assertThatThrownBy(() -> factory.get(MY_TOPIC)); + assertThatIllegalStateException().isThrownBy(() -> factory.get(MY_TOPIC)); // managed undefined - default to true initFactory(); diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlMessageBodyHandlerTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlMessageBodyHandlerTest.java index 70b620c0..4f2eb316 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlMessageBodyHandlerTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/YamlMessageBodyHandlerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 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. @@ -115,8 +115,7 @@ public class YamlMessageBodyHandlerTest { }; MyObject obj1 = new MyObject(10); - - assertThatThrownBy(() -> hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr)) + assertThatThrownBy(() -> hdlr.writeTo(obj1, MyObject.class, CLASS_OBJ, null, null, null, outstr)) .isInstanceOf(YAMLException.class); outstr.close(); diff --git a/policy-endpoints/src/test/resources/webapps/alt-root/index.html b/policy-endpoints/src/test/resources/webapps/alt-root/index.html index 0948cebd..8ef757e6 100644 --- a/policy-endpoints/src/test/resources/webapps/alt-root/index.html +++ b/policy-endpoints/src/test/resources/webapps/alt-root/index.html @@ -1,6 +1,7 @@ + Hello World @@ -25,4 +27,4 @@

Test Jetty Static Resources Alt-Root

- \ No newline at end of file + diff --git a/policy-endpoints/src/test/resources/webapps/root/index.html b/policy-endpoints/src/test/resources/webapps/root/index.html index a7cc120b..35f61013 100644 --- a/policy-endpoints/src/test/resources/webapps/root/index.html +++ b/policy-endpoints/src/test/resources/webapps/root/index.html @@ -1,6 +1,7 @@ + Hello World @@ -25,4 +27,4 @@

Test Jetty Static Resources Root

- \ No newline at end of file + diff --git a/utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsTest.java b/utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsTest.java index 11ee63c0..f16764c9 100644 --- a/utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsTest.java +++ b/utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsTest.java @@ -76,10 +76,11 @@ public class GsonTestUtilsTest { utils.compareGson(data, GsonTestUtilsTest.class); // file not found - assertThatThrownBy(() -> utils.compareGson(data, - new File(GsonTestUtilsTest.class.getSimpleName() + "-NotFound.json"))) - .isInstanceOf(JsonParseException.class) - .hasCauseInstanceOf(FileNotFoundException.class); + File file = new File(GsonTestUtilsTest.class.getSimpleName() + "-NotFound.json"); + + assertThatThrownBy(() -> utils.compareGson(data, file)) + .isInstanceOf(JsonParseException.class) + .hasCauseInstanceOf(FileNotFoundException.class); // force I/O error while reading file GsonTestUtils utils2 = new GsonTestUtils() { diff --git a/utils-test/src/test/java/org/onap/policy/common/utils/time/PseudoScheduledExecutorServiceTest.java b/utils-test/src/test/java/org/onap/policy/common/utils/time/PseudoScheduledExecutorServiceTest.java index 70820c44..745e989f 100644 --- a/utils-test/src/test/java/org/onap/policy/common/utils/time/PseudoScheduledExecutorServiceTest.java +++ b/utils-test/src/test/java/org/onap/policy/common/utils/time/PseudoScheduledExecutorServiceTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 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. @@ -27,6 +27,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.Collections; +import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.Future; import java.util.concurrent.ScheduledFuture; @@ -37,6 +38,7 @@ import org.junit.Test; public class PseudoScheduledExecutorServiceTest { private static final long DELAY_MS = 100L; private static final long PERIOD_MS = 200L; + private static final List> EMPTY_CALLABLES = Collections.emptyList(); private int ran; private int called; @@ -135,25 +137,25 @@ public class PseudoScheduledExecutorServiceTest { @Test public void testInvokeAllCollectionOfQextendsCallableOfT() { - assertThatThrownBy(() -> svc.invokeAll(Collections.emptyList())) + assertThatThrownBy(() -> svc.invokeAll(EMPTY_CALLABLES)) .isInstanceOf(UnsupportedOperationException.class); } @Test public void testInvokeAllCollectionOfQextendsCallableOfTLongTimeUnit() { - assertThatThrownBy(() -> svc.invokeAll(Collections.emptyList(), 1, TimeUnit.MILLISECONDS)) + assertThatThrownBy(() -> svc.invokeAll(EMPTY_CALLABLES, 1, TimeUnit.MILLISECONDS)) .isInstanceOf(UnsupportedOperationException.class); } @Test public void testInvokeAnyCollectionOfQextendsCallableOfT() { - assertThatThrownBy(() -> svc.invokeAny(Collections.emptyList())) + assertThatThrownBy(() -> svc.invokeAny(EMPTY_CALLABLES)) .isInstanceOf(UnsupportedOperationException.class); } @Test public void testInvokeAnyCollectionOfQextendsCallableOfTLongTimeUnit() { - assertThatThrownBy(() -> svc.invokeAny(Collections.emptyList(), 1, TimeUnit.MILLISECONDS)) + assertThatThrownBy(() -> svc.invokeAny(EMPTY_CALLABLES, 1, TimeUnit.MILLISECONDS)) .isInstanceOf(UnsupportedOperationException.class); } diff --git a/utils-test/src/test/java/org/onap/policy/common/utils/time/PseudoTimerTest.java b/utils-test/src/test/java/org/onap/policy/common/utils/time/PseudoTimerTest.java index 49710538..d7316c1f 100644 --- a/utils-test/src/test/java/org/onap/policy/common/utils/time/PseudoTimerTest.java +++ b/utils-test/src/test/java/org/onap/policy/common/utils/time/PseudoTimerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 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. @@ -83,8 +83,9 @@ public class PseudoTimerTest { @Test public void testScheduleTimerTaskDate() { - assertThatThrownBy(() -> timer.schedule(new MyTask(), new Date())) - .isInstanceOf(UnsupportedOperationException.class); + MyTask task = new MyTask(); + Date curdate = new Date(); + assertThatThrownBy(() -> timer.schedule(task, curdate)).isInstanceOf(UnsupportedOperationException.class); } @Test @@ -105,8 +106,9 @@ public class PseudoTimerTest { @Test public void testScheduleTimerTaskDateLong() { - assertThatThrownBy(() -> timer.schedule(new MyTask(), new Date(), 1L)) - .isInstanceOf(UnsupportedOperationException.class); + MyTask task = new MyTask(); + Date curdate = new Date(); + assertThatThrownBy(() -> timer.schedule(task, curdate, 1L)).isInstanceOf(UnsupportedOperationException.class); } @Test @@ -127,7 +129,9 @@ public class PseudoTimerTest { @Test public void testScheduleAtFixedRateTimerTaskDateLong() { - assertThatThrownBy(() -> timer.scheduleAtFixedRate(new MyTask(), new Date(), 1L)) + MyTask task = new MyTask(); + Date curdate = new Date(); + assertThatThrownBy(() -> timer.scheduleAtFixedRate(task, curdate, 1L)) .isInstanceOf(UnsupportedOperationException.class); } diff --git a/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java b/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java index ad4382ce..a468f0b4 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java @@ -31,6 +31,7 @@ import static org.mockito.Mockito.when; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; +import com.google.gson.JsonSyntaxException; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -319,7 +320,9 @@ public class StandardCoderTest { assertEquals(json, coder.toJson(sco)); // invalid json -> exception - assertThatThrownBy(() -> coder.fromJson(new StringReader("["), StandardCoderObject.class)); + StringReader rdr = new StringReader("["); + assertThatThrownBy(() -> coder.fromJson(rdr, StandardCoderObject.class)) + .isInstanceOf(JsonSyntaxException.class); } @Test -- cgit 1.2.3-korg