Clean up of Pair classes - models
[policy/models.git] / models-interactions / model-impl / vfc / src / test / java / org / onap / policy / vfc / VfcManagerTest.java
index 504af4d..fbe29c3 100644 (file)
@@ -2,16 +2,16 @@
  * ============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");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
 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.drools.core.WorkingMemory;
-import org.junit.After;
+import org.apache.commons.lang3.tuple.Pair;
 import org.junit.Before;
-import org.junit.BeforeClass;
 import org.junit.Test;
-import org.onap.policy.drools.system.PolicyEngine;
 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 {
-    private static WorkingMemory mockedWorkingMemory;
+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;
 
     private VfcRequest  request;
     private VfcResponse response;
 
-    @BeforeClass
-    public static void beforeTestVfcManager() {
-        mockedWorkingMemory = mock(WorkingMemory.class);
-    }
-
     /**
      * Set up the mocked REST manager.
      */
@@ -72,10 +64,9 @@ public class VfcManagerTest {
     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);
     }
 
     /**
@@ -99,7 +90,7 @@ public class VfcManagerTest {
         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<>();;
@@ -118,177 +109,101 @@ public class VfcManagerTest {
         response.setResponseDescriptor(responseDescriptor);
     }
 
-    /**
-     * Remove the environnment.
-     */
-    @After
-    public void tearDown() {
-        PolicyEngine.manager.getEnvironment().remove("vfc.password");
-        PolicyEngine.manager.getEnvironment().remove("vfc.username");
-        PolicyEngine.manager.getEnvironment().remove("vfc.url");
-    }
-
     @Test
     public void testVfcInitiation() {
-        try {
-            new VfcManager(null, null);
-            fail("test should throw an exception here");
-        }
-        catch (IllegalArgumentException e) {
-            assertEquals("the parameters \"wm\" and \"request\" on the VfcManager constructor may not be null", 
-                    e.getMessage());
-        }
-
-        try {
-            new VfcManager(mockedWorkingMemory, null);
-            fail("test should throw an exception here");
-        }
-        catch (IllegalArgumentException e) {
-            assertEquals("the parameters \"wm\" and \"request\" on the VfcManager constructor may not be null", 
-                    e.getMessage());
-        }
-
-        try {
-            new VfcManager(mockedWorkingMemory, request);
-            fail("test should throw an exception here");
-        }
-        catch (IllegalArgumentException e) {
-            assertEquals("The value of policy engine manager environment property \"vfc.url\" may not be null", 
-                    e.getMessage());
-        }
-
-        // add url; username & password are not required
-        PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
-        new VfcManager(mockedWorkingMemory, request);
-
-        // url & username, but no password
-        PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
-
-        // url, username, and password
-        PolicyEngine.manager.getEnvironment().put("vfc.password", "Toto");
-        new VfcManager(mockedWorkingMemory, request);
+        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 {
-        PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
-        PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
-        PolicyEngine.manager.getEnvironment().put("vfc.password", "Exception");
-
-        VfcManager manager = new VfcManager(mockedWorkingMemory, request);
+        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();
 
-        PolicyEngine.manager.getEnvironment().remove("vfc.password");
-        PolicyEngine.manager.getEnvironment().remove("vfc.username");
-        PolicyEngine.manager.getEnvironment().remove("vfc.url");
+        verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
     }
 
     @Test
     public void testVfcExecutionNull() throws InterruptedException {
-        PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
-        PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
-        PolicyEngine.manager.getEnvironment().put("vfc.password", "Null");
-
-        VfcManager manager = new VfcManager(mockedWorkingMemory, request);
+        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();
 
-        PolicyEngine.manager.getEnvironment().remove("vfc.password");
-        PolicyEngine.manager.getEnvironment().remove("vfc.username");
-        PolicyEngine.manager.getEnvironment().remove("vfc.url");
+        verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
     }
 
     @Test
     public void testVfcExecutionError0() throws InterruptedException {
-        PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
-        PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
-        PolicyEngine.manager.getEnvironment().put("vfc.password", "Error0");
-
-        VfcManager manager = new VfcManager(mockedWorkingMemory, request);
+        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();
 
-        PolicyEngine.manager.getEnvironment().remove("vfc.password");
-        PolicyEngine.manager.getEnvironment().remove("vfc.username");
-        PolicyEngine.manager.getEnvironment().remove("vfc.url");
+        verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
     }
 
     @Test
     public void testVfcExecutionBadResponse() throws InterruptedException {
-        PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
-        PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
-        PolicyEngine.manager.getEnvironment().put("vfc.password", "BadResponse");
-
-        VfcManager manager = new VfcManager(mockedWorkingMemory, request);
+        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();
 
-        PolicyEngine.manager.getEnvironment().remove("vfc.password");
-        PolicyEngine.manager.getEnvironment().remove("vfc.username");
-        PolicyEngine.manager.getEnvironment().remove("vfc.url");
+        verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
     }
 
     @Test
     public void testVfcExecutionOk() throws InterruptedException {
-        PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
-        PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
-        PolicyEngine.manager.getEnvironment().put("vfc.password", "OK");
-
-        VfcManager manager = new VfcManager(mockedWorkingMemory, request);
+        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());
+    }
 
-        PolicyEngine.manager.getEnvironment().remove("vfc.password");
-        PolicyEngine.manager.getEnvironment().remove("vfc.username");
-        PolicyEngine.manager.getEnvironment().remove("vfc.url");
+    @Override
+    public void onResponse(VfcResponse responseError) {
+        //
+        // Nothing needs to be done
+        //
     }
 }