2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2018 Nokia. All rights reserved.
 
   6  * =============================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  20 package org.onap.appc.flow.controller.node;
 
  22 import static org.mockito.Mockito.mock;
 
  23 import static org.mockito.Mockito.when;
 
  24 import static org.onap.appc.flow.controller.node.InputParamsCollector.SDNC_CONFIG_DIR_VAR;
 
  25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.ACTION_LEVEL;
 
  26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.PAYLOAD;
 
  27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REQUEST_ACTION;
 
  28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNFC_NAME;
 
  29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_ID;
 
  30 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VSERVER_ID;
 
  32 import com.fasterxml.jackson.core.JsonProcessingException;
 
  33 import com.fasterxml.jackson.databind.ObjectMapper;
 
  34 import java.util.ArrayList;
 
  35 import java.util.HashMap;
 
  36 import java.util.List;
 
  38 import org.junit.Assert;
 
  39 import org.junit.Before;
 
  40 import org.junit.Test;
 
  41 import org.onap.appc.flow.controller.data.Transaction;
 
  42 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
 
  43 import org.onap.appc.flow.controller.interfaceData.DependencyInfo;
 
  44 import org.onap.appc.flow.controller.interfaceData.Vnfcs;
 
  45 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  47 public class InputParamsCollectorTest {
 
  49   private SvcLogicContext ctx;
 
  50   private FlowControlDBService dbService;
 
  51   private EnvVariables envVariables;
 
  52   private InputParamsCollector inputParamsCollector;
 
  57     ctx = mock(SvcLogicContext.class);
 
  58     dbService = mock(FlowControlDBService.class);
 
  59     envVariables = mock(EnvVariables.class);
 
  61     when(envVariables.getenv(SDNC_CONFIG_DIR_VAR)).thenReturn("./src/test/resources");
 
  63     inputParamsCollector = new InputParamsCollector(envVariables, dbService);
 
  67   public void should_collect_input_params() throws Exception {
 
  69     when(ctx.getAttribute(VNF_ID)).thenReturn("some-vnf-id");
 
  70     when(ctx.getAttribute(REQUEST_ACTION)).thenReturn("some-request-action");
 
  71     when(ctx.getAttribute(ACTION_LEVEL)).thenReturn("some-action-level");
 
  72     when(ctx.getAttribute(PAYLOAD)).thenReturn("some-payload");
 
  73     when(ctx.getAttribute(VSERVER_ID)).thenReturn("some-vserver-id");
 
  74     when(ctx.getAttribute(VNFC_NAME)).thenReturn("some-vnfc-name");
 
  76     when(dbService.getCapabilitiesData(ctx)).thenReturn(
 
  77         "{'vnf':['vnf-1', 'vnf-2'],'vf-module':['vf-module-1', 'vf-module-2'],'vnfc':['vnfc-1', 'vnfc-2'],'vm':['vm-1', 'vm-2']}"
 
  78             .replaceAll("'", "\""));
 
  79     when(dbService.getDependencyInfo(ctx)).thenReturn(dependencyInfoPayload());
 
  81     Transaction transaction = inputParamsCollector.collectInputParams(ctx);
 
  84         "{\"input\":{\"request-info\":{\"action\":\"some-request-action\",\"payload\":\"some-payload\",\"action-level\":\"some-action-level\",\"action-identifier\":{\"vnf-id\":\"some-vnf-id\",\"vserver-id\":\"some-vserver-id\",\"vnfc-name\":\"some-vnfc-name\"}},\"inventory-info\":{\"vnf-info\":{\"vnf-id\":\"some-vnf-id\",\"vm\":[]}},\"dependency-info\":{\"vnfcs\":[{\"vnfc-type\":\"some-type\",\"mandatory\":\"some-mandatory\",\"resilience\":\"some-resilience\",\"parents\":[]},{\"vnfc-type\":\"some-type\",\"mandatory\":\"some-mandatory\",\"resilience\":\"some-resilience\",\"parents\":[]}]},\"capabilities\":{\"vnf\":[\"vnf-1\",\"vnf-2\"],\"vm\":[\"vm-1\",\"vm-2\"],\"vnfc\":[\"vnfc-1\",\"vnfc-2\"],\"vf-module\":[\"vf-module-1\",\"vf-module-2\"]}}}",
 
  85         transaction.getPayload());
 
  86     Assert.assertEquals("POST", transaction.getExecutionRPC());
 
  87     Assert.assertEquals("seq-generator-uid", transaction.getuId());
 
  88     Assert.assertEquals("some-pswd", transaction.getPswd());
 
  89     Assert.assertEquals("exec-endpoint", transaction.getExecutionEndPoint());
 
  93   public void should_handle_dependency_config() throws Exception {
 
  95     Vnfcs vnfcs = new Vnfcs();
 
  96     vnfcs.setVnfcType("some-type");
 
  97     vnfcs.setResilience("some-resilience");
 
  98     vnfcs.setMandatory("some-mandatory");
 
  99     Map<String, List<Vnfcs>> input = new HashMap<>();
 
 100     List<Vnfcs> list = new ArrayList<>();
 
 103     input.put("vnfcs", list);
 
 105     String jsonPayload = new ObjectMapper().writeValueAsString(input);
 
 107     when(dbService.getDependencyInfo(ctx)).thenReturn(jsonPayload);
 
 109     DependencyInfo dependencyInfo = inputParamsCollector.getDependencyInfo(ctx);
 
 112         "DependencyInfo [vnfcs=[Vnfcs [vnfcType=some-type, mandatory=some-mandatory, resilience=some-resilience, parents=[]], Vnfcs [vnfcType=some-type, mandatory=some-mandatory, resilience=some-resilience, parents=[]]]]",
 
 113         dependencyInfo.toString());
 
 116   private String dependencyInfoPayload() throws JsonProcessingException {
 
 117     Vnfcs vnfcs = new Vnfcs();
 
 118     vnfcs.setVnfcType("some-type");
 
 119     vnfcs.setResilience("some-resilience");
 
 120     vnfcs.setMandatory("some-mandatory");
 
 121     Map<String, List<Vnfcs>> input = new HashMap<>();
 
 122     List<Vnfcs> list = new ArrayList<>();
 
 125     input.put("vnfcs", list);
 
 127     return new ObjectMapper().writeValueAsString(input);