2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2020 Nordix
 
   4  *  ================================================================================
 
   5  *  Licensed under the Apache License, Version 2.0 (the "License");
 
   6  *  you may not use this file except in compliance with the License.
 
   7  *  You may obtain a copy of the License at
 
   9  *        http://www.apache.org/licenses/LICENSE-2.0
 
  10  *  Unless required by applicable law or agreed to in writing, software
 
  11  *  distributed under the License is distributed on an "AS IS" BASIS,
 
  12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  13  *  See the License for the specific language governing permissions and
 
  14  *  limitations under the License.
 
  16  *  SPDX-License-Identifier: Apache-2.0
 
  17  *  ============LICENSE_END=========================================================
 
  19 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
 
  21 import org.camunda.bpm.engine.delegate.DelegateExecution;
 
  22 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
 
  23 import org.junit.Before;
 
  24 import org.junit.ClassRule;
 
  25 import org.junit.Rule;
 
  26 import org.junit.Test;
 
  27 import org.junit.runner.RunWith;
 
  28 import org.junit.runners.Parameterized;
 
  29 import org.mockito.InjectMocks;
 
  30 import org.mockito.Mock;
 
  31 import org.onap.so.bpmn.BaseTaskTest;
 
  32 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerContext;
 
  33 import org.onap.so.client.cds.AbstractCDSProcessingBBUtils;
 
  34 import org.onap.so.client.cds.GeneratePayloadForCds;
 
  35 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
 
  36 import org.onap.so.client.exception.ExceptionBuilder;
 
  37 import org.skyscreamer.jsonassert.JSONAssert;
 
  38 import org.springframework.test.context.ContextConfiguration;
 
  39 import org.springframework.test.context.junit4.rules.SpringClassRule;
 
  40 import org.springframework.test.context.junit4.rules.SpringMethodRule;
 
  41 import java.util.Arrays;
 
  42 import java.util.Collection;
 
  43 import static org.assertj.core.api.Assertions.assertThat;
 
  44 import static org.assertj.core.api.Assertions.fail;
 
  45 import static org.junit.Assert.assertEquals;
 
  46 import static org.mockito.Mockito.doNothing;
 
  47 import static org.mockito.Mockito.doReturn;
 
  48 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.*;
 
  50 @RunWith(Parameterized.class)
 
  51 public class GenericPnfCDSProcessingDETest extends BaseTaskTest {
 
  54     public static final SpringClassRule springClassRule = new SpringClassRule();
 
  57     public final SpringMethodRule smr = new SpringMethodRule();
 
  60     private GenericPnfCDSProcessingDE controllerRunnable;
 
  63     private GeneratePayloadForCds generatePayloadForCds;
 
  66     private AbstractCDSProcessingBBUtils cdsDispather;
 
  68     private static final String PRECHECK_ACTION = "precheck";
 
  69     private static final String DOWNLOAD_ACTION = "downloadNESw";
 
  70     private static final String ACTIVATE_ACTION = "activateNESw";
 
  71     private static final String POSTCHECK_ACTION = "postcheck";
 
  73     private String description;
 
  74     private String action;
 
  76     private String expectedJson;
 
  78     public GenericPnfCDSProcessingDETest(String desc, String action, String scope, String expectedJson) {
 
  79         this.description = desc;
 
  82         this.expectedJson = expectedJson;
 
  86     @Parameterized.Parameters(name = "index {0}")
 
  87     public static Collection<String[]> data() {
 
  88         return Arrays.asList(new String[][] {
 
  89                 {"Test JSON for action:" + PRECHECK_ACTION + " scope:pnf", PRECHECK_ACTION, "pnf",
 
  90                         buildExpectedJson(PRECHECK_ACTION, "pnf")},
 
  91                 {"Test JSON for action:" + DOWNLOAD_ACTION + " scope:pnf", DOWNLOAD_ACTION, "pnf",
 
  92                         buildExpectedJson(DOWNLOAD_ACTION, "pnf")},
 
  93                 {"Test JSON for action:" + ACTIVATE_ACTION + " scope:pnf", ACTIVATE_ACTION, "pnf",
 
  94                         buildExpectedJson(ACTIVATE_ACTION, "pnf")},
 
  95                 {"Test JSON for action:" + POSTCHECK_ACTION + " scope:pnf", POSTCHECK_ACTION, "pnf",
 
  96                         buildExpectedJson(POSTCHECK_ACTION, "pnf")},});
 
  99     private static String buildExpectedJson(String action, String scope) {
 
 100         return "{\"" + action + "-request\":" + "{\"" + action + "-" + "properties\":"
 
 101                 + "{\"service-instance-id\":\"test_service_id\","
 
 102                 + "\"pnf-customization-uuid\":\"9acb3a83-8a52-412c-9a45-901764938144\","
 
 103                 + "\"pnf-id\":\"5df8b6de-2083-11e7-93ae-92361f002671\","
 
 104                 + "\"target-software-version\":\"demo-sw-ver2.0.0\"," + "\"pnf-name\":\"PNFDemo\","
 
 105                 + "\"service-model-uuid\":\"6bc0b04d-1873-4721-b53d-6615225b2a28\"}," + "\"resolution-key\":\"PNFDemo\""
 
 109     private DelegateExecution execution = new DelegateExecutionFake();
 
 112     public void testExecution_validPnf_action_executionObjectCreated() {
 
 116             ControllerContext controllerContext = new ControllerContext();
 
 117             controllerContext.setExecution(execution);
 
 118             controllerContext.setControllerActor("cds");
 
 119             controllerContext.setControllerAction(this.action);
 
 120             controllerContext.setControllerScope(this.scope);
 
 121             AbstractCDSPropertiesBean bean = new AbstractCDSPropertiesBean();
 
 122             doNothing().when(cdsDispather).constructExecutionServiceInputObject(execution);
 
 123             doNothing().when(cdsDispather).sendRequestToCDSClient(execution);
 
 124             doReturn(bean).when(generatePayloadForCds).buildCdsPropertiesBean(execution);
 
 127             Boolean isUnderstandable = controllerRunnable.understand(controllerContext);
 
 128             Boolean isReady = controllerRunnable.ready(controllerContext);
 
 129             controllerRunnable.prepare(controllerContext);
 
 130             controllerRunnable.run(controllerContext);
 
 133             assertEquals(isUnderstandable, true);
 
 134             assertEquals(isReady, true);
 
 135             Object executionObject = execution.getVariable(EXECUTION_OBJECT);
 
 136             assertThat(executionObject).isNotNull();
 
 137             assertThat(executionObject).isInstanceOf(AbstractCDSPropertiesBean.class);
 
 138         } catch (Exception e) {
 
 140             fail("Exception thrown" + e.getMessage());