diff options
Diffstat (limited to 'models-interactions/model-impl/vfc/src')
3 files changed, 48 insertions, 59 deletions
diff --git a/models-interactions/model-impl/vfc/src/main/java/org/onap/policy/vfc/VfcManager.java b/models-interactions/model-impl/vfc/src/main/java/org/onap/policy/vfc/VfcManager.java index 850f37552..406e35d33 100644 --- a/models-interactions/model-impl/vfc/src/main/java/org/onap/policy/vfc/VfcManager.java +++ b/models-interactions/model-impl/vfc/src/main/java/org/onap/policy/vfc/VfcManager.java @@ -47,6 +47,7 @@ public final class VfcManager implements Runnable { // The REST manager used for processing REST calls for this VFC manager private RestManager restManager; + @FunctionalInterface public interface VfcCallback { void onResponse(VfcResponse responseError); } @@ -155,9 +156,11 @@ public final class VfcManager implements Runnable { } Thread.sleep(20000); } - if ((attemptsLeft <= 0) && (responseGet != null) && (responseGet.getResponseDescriptor() != null) - && (responseGet.getResponseDescriptor().getStatus() != null) - && (!responseGet.getResponseDescriptor().getStatus().isEmpty())) { + boolean isTimeout = (attemptsLeft <= 0) && (responseGet != null) + && (responseGet.getResponseDescriptor() != null); + isTimeout = isTimeout && (responseGet.getResponseDescriptor().getStatus() != null) + && (!responseGet.getResponseDescriptor().getStatus().isEmpty()); + if (isTimeout) { logger.debug("VFC timeout. Status: ({})", responseGet.getResponseDescriptor().getStatus()); this.callback.onResponse(responseGet); } diff --git a/models-interactions/model-impl/vfc/src/test/java/org/onap/policy/vfc/DemoTest.java b/models-interactions/model-impl/vfc/src/test/java/org/onap/policy/vfc/DemoTest.java index 4d060a713..1c530c797 100644 --- a/models-interactions/model-impl/vfc/src/test/java/org/onap/policy/vfc/DemoTest.java +++ b/models-interactions/model-impl/vfc/src/test/java/org/onap/policy/vfc/DemoTest.java @@ -21,11 +21,13 @@ package org.onap.policy.vfc; import java.util.LinkedList; - import org.junit.Test; import org.onap.policy.vfc.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() { @@ -44,13 +46,13 @@ public class DemoTest { request.getHealRequest().getAdditionalParams().getActionInfo().setVmname("xgw-smp11"); String body = Serialization.gsonPretty.toJson(request); - System.out.println(body); + logger.info("{}", body); VfcResponse response = new VfcResponse(); response.setJobId("1"); body = Serialization.gsonPretty.toJson(response); - System.out.println(body); + logger.info("{}", body); response.setResponseDescriptor(new VfcResponseDescriptor()); response.getResponseDescriptor().setProgress("40"); @@ -59,7 +61,7 @@ public class DemoTest { response.getResponseDescriptor().setErrorCode(null); response.getResponseDescriptor().setResponseId("42"); body = Serialization.gsonPretty.toJson(response); - System.out.println(body); + logger.info("{}", body); VfcResponseDescriptor responseDescriptor = new VfcResponseDescriptor(); responseDescriptor.setProgress("20"); @@ -72,11 +74,11 @@ public class DemoTest { response.getResponseDescriptor().getResponseHistoryList().add(responseDescriptor); body = Serialization.gsonPretty.toJson(response); - System.out.println(body); + logger.info("{}", body); response = Serialization.gsonPretty.fromJson(body, VfcResponse.class); body = Serialization.gsonPretty.toJson(response); - System.out.println(body); + logger.info("{}", body); } } diff --git a/models-interactions/model-impl/vfc/src/test/java/org/onap/policy/vfc/VfcManagerTest.java b/models-interactions/model-impl/vfc/src/test/java/org/onap/policy/vfc/VfcManagerTest.java index 15534dbbb..7874d2514 100644 --- a/models-interactions/model-impl/vfc/src/test/java/org/onap/policy/vfc/VfcManagerTest.java +++ b/models-interactions/model-impl/vfc/src/test/java/org/onap/policy/vfc/VfcManagerTest.java @@ -22,8 +22,7 @@ package org.onap.policy.vfc; -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.ArrayList; import java.util.List; import java.util.UUID; - import org.junit.Before; import org.junit.Test; import org.onap.policy.rest.RestManager; @@ -45,6 +43,10 @@ import org.onap.policy.vfc.util.Serialization; public class VfcManagerTest implements VfcCallback { + private static final String SOME_URL = "http://somewhere.over.the.rainbow"; + + private static final String DOROTHY = "Dorothy"; + private RestManager mockedRestManager; private Pair<Integer, String> httpResponsePutOk; @@ -89,7 +91,7 @@ public class VfcManagerTest implements VfcCallback { final UUID requestId = UUID.randomUUID(); request = new VfcRequest(); request.setHealRequest(healRequest); - request.setNsInstanceId("Dorothy"); + request.setNsInstanceId(DOROTHY); request.setRequestId(requestId); List<VfcResponseDescriptor> responseHistoryList = new ArrayList<>();; @@ -110,49 +112,31 @@ public class VfcManagerTest implements VfcCallback { @Test public void testVfcInitiation() { - try { - new VfcManager(null, null, null, null, null); - fail("test should throw an exception here"); - } - catch (IllegalArgumentException e) { - assertEquals("the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null", - e.getMessage()); - } - - try { - new VfcManager(this, null, null, null, null); - fail("test should throw an exception here"); - } - catch (IllegalArgumentException e) { - assertEquals("the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null", - e.getMessage()); - } - - try { - new VfcManager(this, request, null, null, null); - fail("test should throw an exception here"); - } - catch (IllegalArgumentException e) { - assertEquals("the \"url\" parameter on the VfcManager constructor may not be null", - e.getMessage()); - } - - new VfcManager(this, request, "http://somewhere.over.the.rainbow", null, null); - - new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Toto"); + assertThatIllegalArgumentException().isThrownBy(() -> new VfcManager(null, null, null, null, null)).withMessage( + "the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null"); + + assertThatIllegalArgumentException().isThrownBy(() -> new VfcManager(this, null, null, null, null)).withMessage( + "the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null"); + + assertThatIllegalArgumentException().isThrownBy(() -> new VfcManager(this, request, null, null, null)) + .withMessage("the \"url\" parameter on the VfcManager constructor may not be null"); + + new VfcManager(this, request, SOME_URL, null, null); + + new VfcManager(this, request, SOME_URL, DOROTHY, "Toto"); } @Test public void testVfcExecutionException() throws InterruptedException { - VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Exception"); + VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Exception"); manager.setRestManager(mockedRestManager); Thread managerThread = new Thread(manager); managerThread.start(); when(mockedRestManager.post( - startsWith("http://somewhere.over.the.rainbow"), - eq("Dorothy"), + startsWith(SOME_URL), + eq(DOROTHY), eq("Exception"), anyMap(), anyString(), @@ -164,14 +148,14 @@ public class VfcManagerTest implements VfcCallback { @Test public void testVfcExecutionNull() throws InterruptedException { - VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Null"); + VfcManager manager = new VfcManager(this, request, SOME_URL, 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())) + when(mockedRestManager.post(startsWith(SOME_URL), + eq(DOROTHY), eq("Null"), anyMap(), anyString(), anyString())) .thenReturn(null); managerThread.join(); @@ -179,14 +163,14 @@ public class VfcManagerTest implements VfcCallback { @Test public void testVfcExecutionError0() throws InterruptedException { - VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Error0"); + VfcManager manager = new VfcManager(this, request, SOME_URL, 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())) + when(mockedRestManager.post(startsWith(SOME_URL), + eq(DOROTHY), eq("Error0"), anyMap(), anyString(), anyString())) .thenReturn(httpResponseErr); managerThread.join(); @@ -194,14 +178,14 @@ public class VfcManagerTest implements VfcCallback { @Test public void testVfcExecutionBadResponse() throws InterruptedException { - VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "BadResponse"); + VfcManager manager = new VfcManager(this, request, SOME_URL, 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())) + when(mockedRestManager.post(startsWith(SOME_URL), + eq(DOROTHY), eq("OK"), anyMap(), anyString(), anyString())) .thenReturn(httpResponseBadResponse); managerThread.join(); @@ -209,17 +193,17 @@ public class VfcManagerTest implements VfcCallback { @Test public void testVfcExecutionOk() throws InterruptedException { - VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Ok"); + VfcManager manager = new VfcManager(this, request, SOME_URL, DOROTHY, "Ok"); 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())) + when(mockedRestManager.post(startsWith(SOME_URL), + 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); managerThread.join(); |