Clean up of Pair classes - models
[policy/models.git] / models-interactions / model-impl / vfc / src / test / java / org / onap / policy / vfc / VfcManagerTest.java
index 15534db..fbe29c3 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * vfc
  * ================================================================================
- * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019 Nordix Foundation.. All rights reserved.
+ * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2020 Nordix Foundation.. All rights reserved.
  * Modifications Copyright (C) 2018-2019 AT&T Corporation. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
 
 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.any;
 import static org.mockito.ArgumentMatchers.anyMap;
 import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.ArgumentMatchers.endsWith;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.ArgumentMatchers.startsWith;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
-
+import org.apache.commons.lang3.tuple.Pair;
 import org.junit.Before;
 import org.junit.Test;
 import org.onap.policy.rest.RestManager;
-import org.onap.policy.rest.RestManager.Pair;
 import org.onap.policy.vfc.VfcManager.VfcCallback;
 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;
-    private Pair<Integer, String> httpResponseGetOk;
     private Pair<Integer, String> httpResponseBadResponse;
     private Pair<Integer, String> httpResponseErr;
 
@@ -62,10 +64,9 @@ public class VfcManagerTest implements VfcCallback {
     public void setupMockedRest() {
         mockedRestManager   = mock(RestManager.class);
 
-        httpResponsePutOk       = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
-        httpResponseGetOk       = mockedRestManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
-        httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
-        httpResponseErr         = mockedRestManager.new Pair<>(200, null);
+        httpResponsePutOk       = Pair.of(202, Serialization.gsonPretty.toJson(response));
+        httpResponseBadResponse = Pair.of(202, Serialization.gsonPretty.toJson(null));
+        httpResponseErr         = Pair.of(200, null);
     }
 
     /**
@@ -89,7 +90,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,119 +111,93 @@ 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(),
             anyString()))
             .thenThrow(new RuntimeException("OzException"));
 
-        managerThread.join();
+        manager.run();
+
+        verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
     }
 
     @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();
+        manager.run();
+
+        verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
     }
 
     @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();
+        manager.run();
+
+        verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
     }
 
     @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();
+        manager.run();
+
+        verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
     }
 
     @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()))
-            .thenReturn(httpResponseGetOk);
+        manager.run();
 
-        managerThread.join();
+        verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
     }
 
     @Override