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