From ba6267f5bd58dd7a3a0d6363e7ac4c27705e9f40 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 14 Jun 2019 19:20:55 -0400 Subject: Fix sonar issues in models: sdc to vfc sdc sdnc snr so vfc Still haven't done the "rest" project yet, as still awaiting removal of www.examples.org. Change-Id: Icc4948799e2423bd4568232ee5629b88b7ecb996 Issue-ID: POLICY-1791 Signed-off-by: Jim Hahn --- .../test/java/org/onap/policy/sdnc/DemoTest.java | 20 +++-- .../java/org/onap/policy/sdnc/SdncManagerTest.java | 93 ++++++++-------------- 2 files changed, 46 insertions(+), 67 deletions(-) (limited to 'models-interactions/model-impl/sdnc/src/test') diff --git a/models-interactions/model-impl/sdnc/src/test/java/org/onap/policy/sdnc/DemoTest.java b/models-interactions/model-impl/sdnc/src/test/java/org/onap/policy/sdnc/DemoTest.java index 1c18d9c8f..41f07c3c7 100644 --- a/models-interactions/model-impl/sdnc/src/test/java/org/onap/policy/sdnc/DemoTest.java +++ b/models-interactions/model-impl/sdnc/src/test/java/org/onap/policy/sdnc/DemoTest.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Huawei. 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,8 +22,11 @@ package org.onap.policy.sdnc; import org.junit.Test; import org.onap.policy.sdnc.util.Serialization; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DemoTest { + private static final Logger logger = LoggerFactory.getLogger(DemoTest.class); @Test public void test() { @@ -37,34 +41,34 @@ public class DemoTest { request.getHealRequest().setRequestInfo(new SdncHealRequestInfo()); request.getHealRequest().getRequestInfo().setRequestAction("request-action"); - + request.getHealRequest().setServiceInfo(new SdncHealServiceInfo()); request.getHealRequest().getServiceInfo().setServiceInstanceId("service-instance-01"); - + request.getHealRequest().setNetworkInfo(new SdncHealNetworkInfo()); request.getHealRequest().getNetworkInfo().setNetworkId("network-5555"); - + String body = Serialization.gsonPretty.toJson(request); - System.out.println(body); + logger.info("{}", body); SdncResponse response = new SdncResponse(); body = Serialization.gsonPretty.toJson(response); - System.out.println(body); + logger.info("{}", body); response.setRequestId("request-01"); response.setResponseOutput(new SdncResponseOutput()); response.getResponseOutput().setSvcRequestId("service-req-01"); response.getResponseOutput().setResponseCode("200"); response.getResponseOutput().setAckFinalIndicator("final-indicator-00"); - + body = Serialization.gsonPretty.toJson(response); - System.out.println(body); + logger.info("{}", body); response = Serialization.gsonPretty.fromJson(body, SdncResponse.class); body = Serialization.gsonPretty.toJson(response); - System.out.println(body); + logger.info("{}", body); } } diff --git a/models-interactions/model-impl/sdnc/src/test/java/org/onap/policy/sdnc/SdncManagerTest.java b/models-interactions/model-impl/sdnc/src/test/java/org/onap/policy/sdnc/SdncManagerTest.java index 2a1cc6984..c069f1c3b 100644 --- a/models-interactions/model-impl/sdnc/src/test/java/org/onap/policy/sdnc/SdncManagerTest.java +++ b/models-interactions/model-impl/sdnc/src/test/java/org/onap/policy/sdnc/SdncManagerTest.java @@ -23,8 +23,7 @@ package org.onap.policy.sdnc; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.mockito.ArgumentMatchers.anyMap; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.endsWith; @@ -35,7 +34,6 @@ import static org.mockito.Mockito.when; import java.util.UUID; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.rest.RestManager; import org.onap.policy.rest.RestManager.Pair; @@ -43,6 +41,10 @@ import org.onap.policy.sdnc.SdncManager.SdncCallback; import org.onap.policy.sdnc.util.Serialization; public class SdncManagerTest implements SdncCallback { + private static final String SOMEWHERE_OVER_THE_RAINBOW = "http://somewhere.over.the.rainbow"; + + private static final String DOROTHY = "Dorothy"; + private RestManager mockedRestManager; private Pair httpResponsePutOk; @@ -53,10 +55,6 @@ public class SdncManagerTest implements SdncCallback { private SdncRequest request; private SdncResponse response; - @BeforeClass - public static void beforeTestSdncManager() { - } - /** * Set up the mocked REST manager. */ @@ -90,7 +88,7 @@ public class SdncManagerTest implements SdncCallback { request = new SdncRequest(); request.setRequestId(requestId); request.setHealRequest(healRequest); - request.setNsInstanceId("Dorothy"); + request.setNsInstanceId(DOROTHY); SdncResponseOutput responseDescriptor = new SdncResponseOutput(); responseDescriptor.setSvcRequestId("1234"); @@ -103,53 +101,30 @@ public class SdncManagerTest implements SdncCallback { } @Test - public void testSdncInitiation() throws InterruptedException { - try { - new SdncManager(null, null, null, null, null); - fail("test should throw an exception here"); - } - catch (IllegalArgumentException e) { - assertEquals( - "the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null", - e.getMessage() - ); - } - - try { - new SdncManager(this, null, null, null, null); - fail("test should throw an exception here"); - } - catch (IllegalArgumentException e) { - assertEquals( - "the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null", - e.getMessage() - ); - } - - try { - new SdncManager(this, request, null, null, null); - fail("test should throw an exception here"); - } - catch (IllegalArgumentException e) { - assertEquals( - "the \"url\" parameter on the SdncManager constructor may not be null", - e.getMessage() - ); - } - - new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Toto"); + public void testSdncInitiation() { + + assertThatIllegalArgumentException().isThrownBy(() -> new SdncManager(null, null, null, null, null)) + .withMessage("the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null"); + + assertThatIllegalArgumentException().isThrownBy(() -> new SdncManager(this, null, null, null, null)) + .withMessage("the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null"); + + assertThatIllegalArgumentException().isThrownBy(() -> new SdncManager(this, request, null, null, null)) + .withMessage("the \"url\" parameter on the SdncManager constructor may not be null"); + + new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Toto"); } @Test public void testSdncExecutionException() throws InterruptedException { - SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Exception"); + SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Exception"); manager.setRestManager(mockedRestManager); Thread managerThread = new Thread(manager); managerThread.start(); - when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Exception"), anyMap(), anyString(), anyString())) - .thenThrow(new RuntimeException("OzException")); + when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("Exception"), anyMap(), + anyString(), anyString())).thenThrow(new RuntimeException("OzException")); managerThread.join(100); @@ -157,14 +132,14 @@ public class SdncManagerTest implements SdncCallback { @Test public void testSdncExecutionNull() throws InterruptedException { - SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Null"); + SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Null"); manager.setRestManager(mockedRestManager); Thread managerThread = new Thread(manager); managerThread.start(); - when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Null"), anyMap(), anyString(), anyString())) - .thenReturn(null); + when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("Null"), anyMap(), + anyString(), anyString())).thenReturn(null); managerThread.join(100); } @@ -172,44 +147,44 @@ public class SdncManagerTest implements SdncCallback { @Test public void testSdncExecutionError0() throws InterruptedException { - SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Error0"); + SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Error0"); manager.setRestManager(mockedRestManager); Thread managerThread = new Thread(manager); managerThread.start(); - when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Error0"), anyMap(), anyString(), anyString())) - .thenReturn(httpResponseErr); + when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("Error0"), anyMap(), + anyString(), anyString())).thenReturn(httpResponseErr); managerThread.join(100); } @Test public void testSdncExecutionBadResponse() throws InterruptedException { - SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "BadResponse"); + SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "BadResponse"); manager.setRestManager(mockedRestManager); Thread managerThread = new Thread(manager); managerThread.start(); - when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString())) - .thenReturn(httpResponseBadResponse); + when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("OK"), anyMap(), + anyString(), anyString())).thenReturn(httpResponseBadResponse); managerThread.join(100); } @Test public void testSdncExecutionOk() throws InterruptedException { - SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "OOK"); + SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "OOK"); manager.setRestManager(mockedRestManager); Thread managerThread = new Thread(manager); managerThread.start(); - when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString())) - .thenReturn(httpResponsePutOk); + when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("OK"), anyMap(), + anyString(), anyString())).thenReturn(httpResponsePutOk); - when(mockedRestManager.get(endsWith("1234"), eq("Dorothy"), eq("OK"), anyMap())) + when(mockedRestManager.get(endsWith("1234"), eq(DOROTHY), eq("OK"), anyMap())) .thenReturn(httpResponseGetOk); -- cgit 1.2.3-korg