Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / java / org / onap / so / bpmn / infrastructure / pnf / delegate / ConfigCheckerDelegateTest.java
1 /*
2  * ============LICENSE_START======================================================= Copyright (C) 2019 Nordix
3  * Foundation. ================================================================================ Licensed under the
4  * Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
5  * obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
8  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
9  * either express or implied. See the License for the specific language governing permissions and limitations under the
10  * License.
11  *
12  * SPDX-License-Identifier: Apache-2.0 ============LICENSE_END=========================================================
13  */
14
15 package org.onap.so.bpmn.infrastructure.pnf.delegate;
16
17 import static org.assertj.core.api.Assertions.assertThat;
18 import static org.assertj.core.api.Assertions.assertThatThrownBy;
19 import static org.assertj.core.api.Assertions.fail;
20 import static org.mockito.BDDMockito.given;
21 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.MODEL_UUID;
22 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_BLUEPRINT_NAME;
23 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_BLUEPRINT_VERSION;
24 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_CUSTOMIZATION_UUID;
25 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_INSTANCE_NAME;
26 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.SERVICE_MODEL_INFO;
27 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.SKIP_POST_INSTANTIATION_CONFIGURATION;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import org.camunda.bpm.engine.ProcessEngineConfiguration;
32 import org.camunda.bpm.engine.delegate.BpmnError;
33 import org.camunda.bpm.engine.delegate.DelegateExecution;
34 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.onap.so.bpmn.core.WorkflowException;
39 import org.onap.so.client.exception.ExceptionBuilder;
40 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
41 import org.onap.so.db.catalog.client.CatalogDbClient;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.mock.mockito.MockBean;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
46
47 @RunWith(SpringJUnit4ClassRunner.class)
48 @ContextConfiguration(classes = {CatalogDbClient.class, ExceptionBuilder.class, ConfigCheckerDelegate.class,
49         ProcessEngineConfiguration.class})
50 public class ConfigCheckerDelegateTest {
51
52     private static String TEST_PROCESS_KEY = "processKey1";
53     private static String TEST_PNF_RESOURCE_INSTANCE_NAME = "PNF_demo_resource";
54     private static String TEST_PNF_RESOURCE_BLUEPRINT_NAME = "blueprintOnap";
55     private static String TEST_PNF_RESOURCE_BLUEPRINT_VERSION = "1.0.1";
56     private static String TEST_PNF_RESOURCE_CUSTOMIZATION_UUID = "9acb3a83-8a52-412c-9a45-901764938144";
57
58     /**
59      * Service model info json.
60      */
61     private static String TEST_SERVICE_MODEL_INFO = "{\n" + "      \"modelType\":\"service\",\n"
62             + "      \"modelInvariantUuid\":\"539b7a2f-9524-4dbf-9eee-f2e05521df3f\",\n"
63             + "      \"modelInvariantId\":\"539b7a2f-9524-4dbf-9eee-f2e05521df3f\",\n"
64             + "      \"modelUuid\":\"f2daaac6-5017-4e1e-96c8-6a27dfbe1421\",\n"
65             + "      \"modelName\":\"PNF_demo_resource\",\n" + "      \"modelVersion\":\"1.0\"\n" + "    }";
66
67     /**
68      * Testing model UUID, should be the same as specified in the TEST_SERVICE_MODEL_INFO.
69      */
70     private static String TEST_MODEL_UUID = "f2daaac6-5017-4e1e-96c8-6a27dfbe1421";
71
72     @MockBean
73     private CatalogDbClient catalogDbClient;
74
75     @MockBean
76     private ProcessEngineConfiguration processEngineConfiguration;
77
78     @Autowired
79     private ConfigCheckerDelegate configCheckerDelegate;
80
81     private DelegateExecution execution = new DelegateExecutionFake();
82
83     @Before
84     public void setUp() {
85         List<PnfResourceCustomization> pnfResourceCustomizations = new ArrayList<>();
86         pnfResourceCustomizations.add(buildPnfResourceCustomization());
87         given(catalogDbClient.getPnfResourceCustomizationByModelUuid(TEST_MODEL_UUID))
88                 .willReturn(pnfResourceCustomizations);
89         execution.setVariable("testProcessKey", TEST_PROCESS_KEY);
90         execution.setVariable(SERVICE_MODEL_INFO, TEST_SERVICE_MODEL_INFO);
91     }
92
93     private PnfResourceCustomization buildPnfResourceCustomization() {
94         PnfResourceCustomization pnfResourceCustomization = new PnfResourceCustomization();
95         pnfResourceCustomization.setSkipPostInstConf(true);
96         pnfResourceCustomization.setBlueprintName(TEST_PNF_RESOURCE_BLUEPRINT_NAME);
97         pnfResourceCustomization.setBlueprintVersion(TEST_PNF_RESOURCE_BLUEPRINT_VERSION);
98         pnfResourceCustomization.setModelInstanceName(TEST_PNF_RESOURCE_INSTANCE_NAME);
99         pnfResourceCustomization.setModelCustomizationUUID(TEST_PNF_RESOURCE_CUSTOMIZATION_UUID);
100         return pnfResourceCustomization;
101     }
102
103     @Test
104     public void testExecution_validCatalogdb_skipVariableSet() {
105         try {
106             configCheckerDelegate.execute(execution);
107             assertThat(execution.getVariable(MODEL_UUID)).isEqualTo(TEST_MODEL_UUID);
108             assertThat(execution.getVariable(SKIP_POST_INSTANTIATION_CONFIGURATION)).isEqualTo(Boolean.TRUE);
109             assertThat(execution.getVariable(PRC_BLUEPRINT_NAME)).isEqualTo(TEST_PNF_RESOURCE_BLUEPRINT_NAME);
110             assertThat(execution.getVariable(PRC_BLUEPRINT_VERSION)).isEqualTo(TEST_PNF_RESOURCE_BLUEPRINT_VERSION);
111             assertThat(execution.getVariable(PRC_CUSTOMIZATION_UUID)).isEqualTo(TEST_PNF_RESOURCE_CUSTOMIZATION_UUID);
112             assertThat(execution.getVariable(PRC_INSTANCE_NAME)).isEqualTo(TEST_PNF_RESOURCE_INSTANCE_NAME);
113         } catch (Exception e) {
114             e.printStackTrace();
115             fail("Exception thrown" + e.getMessage());
116         }
117     }
118
119     @Test
120     public void testExecution_EmptyPnfResourceCustomization_exceptionThrown() {
121         given(catalogDbClient.getPnfResourceCustomizationByModelUuid("f2daaac6-5017-4e1e-96c8-6a27dfbe1421"))
122                 .willReturn(Collections.EMPTY_LIST);
123
124         assertThatThrownBy(() -> configCheckerDelegate.execute(execution)).isInstanceOf(BpmnError.class);
125         assertThat(execution.getVariable("WorkflowExceptionErrorMessage")).asString()
126                 .contains("Unable to find the PNF resource customizations of model service UUID")
127                 .contains("f2daaac6-5017-4e1e-96c8-6a27dfbe1421");
128         assertThat(execution.getVariable("WorkflowException")).isInstanceOf(WorkflowException.class);
129     }
130
131     @Test
132     public void testExecution_NonExistingServiceModelInfo_exceptionThrown() {
133         execution.removeVariable("serviceModelInfo");
134         assertThatThrownBy(() -> configCheckerDelegate.execute(execution)).isInstanceOf(BpmnError.class);
135         assertThat(execution.getVariable("WorkflowExceptionErrorMessage")).asString()
136                 .contains("Unable to find parameter serviceModelInfo");
137         assertThat(execution.getVariable("WorkflowException")).isInstanceOf(WorkflowException.class);
138     }
139 }