Fix default homing 34/95734/4
authorkurczews <krzysztof.kurczewski@nokia.com>
Mon, 16 Sep 2019 10:55:00 +0000 (12:55 +0200)
committerkurczews <krzysztof.kurczewski@nokia.com>
Wed, 18 Sep 2019 05:13:54 +0000 (07:13 +0200)
Issue-ID: SO-2307
Change-Id: Iee2562c6f38a440006d3a6108ce6f6fd6dbedcf6
Signed-off-by: kurczews <krzysztof.kurczewski@nokia.com>
bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/buildingblock/HomingV2.java
bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/buildingblock/HomingV2Test.java [new file with mode: 0644]

index 4e74e5d..c223d3f 100644 (file)
@@ -2,14 +2,15 @@
  * ============LICENSE_START=======================================================
  * ONAP - SO
  * ================================================================================
- * Copyright (C) Copyright (C) 2018 Bell Canada.
+ * Copyright (C) 2018 Bell Canada.
+ * Copyright (C) 2019 Nokia.
  * ================================================================================
  * 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.
@@ -20,7 +21,6 @@
 
 package org.onap.so.bpmn.buildingblock;
 
-import java.util.Map;
 import org.onap.so.bpmn.common.BuildingBlockExecution;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -28,37 +28,37 @@ import org.springframework.stereotype.Component;
 @Component
 public class HomingV2 {
 
-    @Autowired
-    private OofHomingV2 oofHomingV2;
-    @Autowired
+    static final String HOMING_SNIRO = "sniro";
+    static final String HOMING_OOF = "oof";
+    static final String HOMING_SOLUTION = "Homing_Solution";
+
     private SniroHomingV2 sniroHomingV2;
+    private OofHomingV2 oofHomingV2;
 
-    private static final String HOMINGSOLUTION = "Homing_Solution";
+    @Autowired
+    public HomingV2(OofHomingV2 oofHomingV2, SniroHomingV2 sniroHomingV2) {
+        this.oofHomingV2 = oofHomingV2;
+        this.sniroHomingV2 = sniroHomingV2;
+    }
 
     public void callHoming(BuildingBlockExecution execution) {
-        if (isOof(execution)) {
-            oofHomingV2.callOof(execution);
-        } else {
+        if (isSniro(execution)) {
             sniroHomingV2.callSniro(execution);
+        } else {
+            oofHomingV2.callOof(execution);
         }
     }
 
     public void processSolution(BuildingBlockExecution execution, String asyncResponse) {
-        if (isOof(execution)) {
-            oofHomingV2.processSolution(execution, asyncResponse);
-        } else {
+        if (isSniro(execution)) {
             sniroHomingV2.processSolution(execution, asyncResponse);
+        } else {
+            oofHomingV2.processSolution(execution, asyncResponse);
         }
     }
 
-    // Default solution is SNIRO. OOF gets called only if specified.
-    private boolean isOof(BuildingBlockExecution execution) {
-        for (Map<String, Object> params : execution.getGeneralBuildingBlock().getRequestContext().getRequestParameters()
-                .getUserParams()) {
-            if (params.containsKey(HOMINGSOLUTION) && ("oof").equals(params.get(HOMINGSOLUTION))) {
-                return true;
-            }
-        }
-        return false;
+    private boolean isSniro(BuildingBlockExecution execution) {
+        return execution.getGeneralBuildingBlock().getRequestContext().getRequestParameters().getUserParams().stream()
+                .anyMatch(params -> HOMING_SNIRO.equals(params.get(HOMING_SOLUTION)));
     }
 }
diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/buildingblock/HomingV2Test.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/buildingblock/HomingV2Test.java
new file mode 100644 (file)
index 0000000..9dd0365
--- /dev/null
@@ -0,0 +1,149 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright (C) 2019 Nokia.
+ * ================================================================================
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.so.bpmn.buildingblock;
+
+import static org.mockito.BDDMockito.then;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.onap.so.bpmn.buildingblock.HomingV2.HOMING_OOF;
+import static org.onap.so.bpmn.buildingblock.HomingV2.HOMING_SNIRO;
+import static org.onap.so.bpmn.buildingblock.HomingV2.HOMING_SOLUTION;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.junit.Test;
+import org.onap.so.bpmn.common.BuildingBlockExecution;
+import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
+import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
+import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters;
+
+public class HomingV2Test {
+
+    @Test
+    public void whenHomingSolutionSetToSniroShouldCallSniro() {
+        HashMap<String, Object> userParams = new HashMap<>();
+        userParams.put(HOMING_SOLUTION, HOMING_SNIRO);
+        BuildingBlockExecution givenExecution = createBuildingBlockExecutionMock(userParams);
+
+        OofHomingV2 oofHoming = mock(OofHomingV2.class);
+        SniroHomingV2 sniroHoming = mock(SniroHomingV2.class);
+
+        HomingV2 homingV2 = new HomingV2(oofHoming, sniroHoming);
+        homingV2.callHoming(givenExecution);
+
+        then(sniroHoming).should().callSniro(givenExecution);
+        then(oofHoming).shouldHaveZeroInteractions();
+    }
+
+    @Test
+    public void whenHomingSolutionSetToSniroShouldProcessSniro() {
+        HashMap<String, Object> userParams = new HashMap<>();
+        userParams.put(HOMING_SOLUTION, HOMING_SNIRO);
+        BuildingBlockExecution givenExecution = createBuildingBlockExecutionMock(userParams);
+
+        OofHomingV2 oofHoming = mock(OofHomingV2.class);
+        SniroHomingV2 sniroHoming = mock(SniroHomingV2.class);
+
+        HomingV2 homingV2 = new HomingV2(oofHoming, sniroHoming);
+        homingV2.processSolution(givenExecution, "dummy");
+
+        then(sniroHoming).should().processSolution(givenExecution, "dummy");
+        then(oofHoming).shouldHaveZeroInteractions();
+    }
+
+    @Test
+    public void whenHomingSolutionSetToOofShouldCallOof() {
+        HashMap<String, Object> userParams = new HashMap<>();
+        userParams.put(HOMING_SOLUTION, HOMING_OOF);
+        BuildingBlockExecution givenExecution = createBuildingBlockExecutionMock(userParams);
+
+        OofHomingV2 oofHoming = mock(OofHomingV2.class);
+        SniroHomingV2 sniroHoming = mock(SniroHomingV2.class);
+
+        HomingV2 homingV2 = new HomingV2(oofHoming, sniroHoming);
+        homingV2.callHoming(givenExecution);
+
+        then(oofHoming).should().callOof(givenExecution);
+        then(sniroHoming).shouldHaveZeroInteractions();
+    }
+
+    @Test
+    public void whenHomingSolutionSetToOofShouldProcessOof() {
+        HashMap<String, Object> userParams = new HashMap<>();
+        userParams.put(HOMING_SOLUTION, HOMING_OOF);
+        BuildingBlockExecution givenExecution = createBuildingBlockExecutionMock(userParams);
+
+        OofHomingV2 oofHoming = mock(OofHomingV2.class);
+        SniroHomingV2 sniroHoming = mock(SniroHomingV2.class);
+
+        HomingV2 homingV2 = new HomingV2(oofHoming, sniroHoming);
+        homingV2.processSolution(givenExecution, "dummy");
+
+        then(oofHoming).should().processSolution(givenExecution, "dummy");
+        then(sniroHoming).shouldHaveZeroInteractions();
+    }
+
+    @Test
+    public void whenHomingSolutionNotSetShouldCallOof() {
+        BuildingBlockExecution givenExecution = createBuildingBlockExecutionMock(new HashMap<>());
+
+        OofHomingV2 oofHoming = mock(OofHomingV2.class);
+        SniroHomingV2 sniroHoming = mock(SniroHomingV2.class);
+
+        HomingV2 homingV2 = new HomingV2(oofHoming, sniroHoming);
+        homingV2.callHoming(givenExecution);
+
+        then(oofHoming).should().callOof(givenExecution);
+        then(sniroHoming).shouldHaveZeroInteractions();
+    }
+
+    @Test
+    public void whenHomingSolutionNotSetShouldProcessOof() {
+        BuildingBlockExecution givenExecution = createBuildingBlockExecutionMock(new HashMap<>());
+
+        OofHomingV2 oofHoming = mock(OofHomingV2.class);
+        SniroHomingV2 sniroHoming = mock(SniroHomingV2.class);
+
+        HomingV2 homingV2 = new HomingV2(oofHoming, sniroHoming);
+        homingV2.processSolution(givenExecution, "dummy");
+
+        then(oofHoming).should().processSolution(givenExecution, "dummy");
+        then(sniroHoming).shouldHaveZeroInteractions();
+    }
+
+    private BuildingBlockExecution createBuildingBlockExecutionMock(Map<String, Object> userParams) {
+        BuildingBlockExecution execution = mock(BuildingBlockExecution.class);
+        GeneralBuildingBlock generalBuildingBlock = mock(GeneralBuildingBlock.class);
+        RequestContext requestContext = mock(RequestContext.class);
+        RequestParameters requestParameters = mock(RequestParameters.class);
+        List<Map<String, Object>> userParamsList = new ArrayList<>();
+
+        userParamsList.add(userParams);
+
+        when(execution.getGeneralBuildingBlock()).thenReturn(generalBuildingBlock);
+        when(generalBuildingBlock.getRequestContext()).thenReturn(requestContext);
+        when(requestContext.getRequestParameters()).thenReturn(requestParameters);
+        when(requestParameters.getUserParams()).thenReturn(userParamsList);
+
+        return execution;
+    }
+}