diff options
Diffstat (limited to 'models-interactions/model-impl/sdnc')
4 files changed, 53 insertions, 71 deletions
diff --git a/models-interactions/model-impl/sdnc/src/main/java/org/onap/policy/sdnc/SdncManager.java b/models-interactions/model-impl/sdnc/src/main/java/org/onap/policy/sdnc/SdncManager.java index 3679625ec..7612eeb3f 100644 --- a/models-interactions/model-impl/sdnc/src/main/java/org/onap/policy/sdnc/SdncManager.java +++ b/models-interactions/model-impl/sdnc/src/main/java/org/onap/policy/sdnc/SdncManager.java @@ -49,6 +49,7 @@ public final class SdncManager implements Runnable { // The REST manager used for processing REST calls for this Sdnc manager private RestManager restManager; + @FunctionalInterface public interface SdncCallback { public void onCallback(SdncResponse response); } @@ -133,7 +134,7 @@ public final class SdncManager implements Runnable { logger.info(body); response.setRequestId(sdncRequest.getRequestId().toString()); - if (!response.getResponseOutput().getResponseCode().equals("200")) { + if (!"200".equals(response.getResponseOutput().getResponseCode())) { logger.info( "Sdnc Heal Restcall failed with http error code {} {}", httpDetails.first, httpDetails.second ); diff --git a/models-interactions/model-impl/sdnc/src/main/java/org/onap/policy/sdnc/util/Serialization.java b/models-interactions/model-impl/sdnc/src/main/java/org/onap/policy/sdnc/util/Serialization.java index cfaa46fd0..090b4f9b6 100644 --- a/models-interactions/model-impl/sdnc/src/main/java/org/onap/policy/sdnc/util/Serialization.java +++ b/models-interactions/model-impl/sdnc/src/main/java/org/onap/policy/sdnc/util/Serialization.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. @@ -23,11 +24,12 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; public final class Serialization { - private Serialization() { - } - + public static final Gson gsonPretty = new GsonBuilder().disableHtmlEscaping() .setPrettyPrinting() .create(); + private Serialization() { + + } } 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<Integer, String> 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); |