2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. 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=========================================================
23 import java.io.IOException;
24 import java.nio.charset.Charset;
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
27 import java.util.HashMap;
30 import org.camunda.bpm.engine.RepositoryService;
31 import org.camunda.bpm.engine.RuntimeService;
32 import org.camunda.bpm.model.bpmn.Bpmn;
33 import org.camunda.bpm.model.bpmn.BpmnModelInstance;
34 import org.junit.Before;
35 import org.junit.runner.RunWith;
36 import org.onap.so.bpmn.buildingblock.SniroHomingV2;
37 import org.onap.so.bpmn.common.DelegateExecutionImpl;
38 import org.onap.so.bpmn.common.InjectionHelper;
39 import org.onap.so.bpmn.common.MockLoggerDelegate;
40 import org.onap.so.bpmn.infrastructure.aai.tasks.AAIFlagTasks;
41 import org.onap.so.bpmn.sdno.tasks.SDNOHealthCheckTasks;
42 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
43 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupMapperLayer;
44 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
45 import org.onap.so.client.aai.mapper.AAIObjectMapper;
46 import org.onap.so.client.adapter.network.NetworkAdapterClientImpl;
47 import org.onap.so.client.adapter.network.mapper.NetworkAdapterObjectMapper;
48 import org.onap.so.client.exception.ExceptionBuilder;
49 import org.onap.so.client.orchestration.AAIVnfResources;
50 import org.onap.so.client.orchestration.SDNOHealthCheckResources;
51 import org.onap.so.client.sdnc.SDNCClient;
52 import org.onap.so.client.sniro.SniroClient;
53 import org.onap.so.db.catalog.client.CatalogDbClient;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.beans.factory.annotation.Value;
56 import org.springframework.boot.context.embedded.LocalServerPort;
57 import org.springframework.boot.test.context.SpringBootTest;
58 import org.springframework.boot.test.mock.mockito.MockBean;
59 import org.springframework.boot.test.mock.mockito.SpyBean;
60 import org.springframework.boot.test.web.client.TestRestTemplate;
61 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
62 import org.springframework.http.HttpHeaders;
63 import org.springframework.test.context.ActiveProfiles;
64 import org.springframework.test.context.ContextConfiguration;
65 import org.springframework.test.context.junit4.SpringRunner;
67 import com.fasterxml.jackson.databind.JsonNode;
68 import com.fasterxml.jackson.databind.ObjectMapper;
70 @RunWith(SpringRunner.class)
71 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
72 @ActiveProfiles("test")
74 @AutoConfigureWireMock(port = 0)
75 public abstract class BaseTest extends BuildingBlockTestDataSetup {
78 protected Map<String, Object> variables = new HashMap<>();
80 protected TestRestTemplate restTemplate = new TestRestTemplate();
82 protected HttpHeaders headers = new HttpHeaders();
84 @Value("${wiremock.server.port}")
85 protected String wireMockPort;
88 protected RuntimeService runtimeService;
91 private RepositoryService repositoryService;
93 * Mocked for injection via autowiring
96 @Value("${mso.catalog.db.spring.endpoint}")
97 protected String endpoint;
100 protected CatalogDbClient MOCK_catalogDbClient;
103 protected InjectionHelper MOCK_injectionHelper;
106 protected NetworkAdapterObjectMapper MOCK_networkAdapterObjectMapper;
109 protected AAIObjectMapper MOCK_aaiObjectMapper;
111 protected NetworkAdapterClientImpl MOCK_networkAdapterClient;
113 protected SDNCClient MOCK_sdncClient;
116 protected AAIFlagTasks aaiFlagTasks;
119 protected AAIVnfResources aaiVnfResources;
122 protected ExceptionBuilder exceptionUtil;
125 protected SDNOHealthCheckResources MOCK_sdnoHealthCheckResources;
133 * Classes that cannot be simply mocked because they are both
134 * needed for testing another class, and must be autowired when
135 * being tested themselves....or classes with private methods that
136 * must be stubbed during testing
143 protected BBInputSetupMapperLayer SPY_bbInputSetupMapperLayer;
145 protected BBInputSetupUtils SPY_bbInputSetupUtils;
147 protected BBInputSetup SPY_bbInputSetup;
149 protected SniroHomingV2 sniroHoming;
152 protected SniroClient sniroClient;
155 protected SDNOHealthCheckTasks sdnoHealthCheckTasks;
158 * Mocked for injection via the IntectionHelper
164 public void baseTestBefore() {
165 variables.put("gBuildingBlockExecution", new DelegateExecutionImpl(new HashMap<>()));
173 protected String readFile(String path) throws IOException {
174 return readFile(path, Charset.defaultCharset());
177 protected String readFile(String path, Charset encoding) throws IOException {
178 byte[] encoded = Files.readAllBytes(Paths.get(path));
179 return new String(encoded, encoding);
182 protected String readJsonFileAsString(String fileLocation) throws IOException{
183 ObjectMapper mapper = new ObjectMapper();
184 JsonNode jsonNode = mapper.readTree(new File(fileLocation));
185 return jsonNode.asText();
188 protected String createURLWithPort(String uri) {
189 return "http://localhost:" + port + uri;
192 * Create and deploy a process model with one logger delegate as service task.
194 * @param origProcessKey
196 * @param mockProcessName
199 * file name without extension
201 protected void mockSubprocess(String origProcessKey, String mockProcessName, String fileName) {
202 BpmnModelInstance modelInstance = Bpmn.createExecutableProcess(origProcessKey).name(mockProcessName)
203 .startEvent().name("Start Point").serviceTask().name("Log Something for Test")
204 .camundaClass(MockLoggerDelegate.class.getName()).endEvent().name("End Point").done();
205 repositoryService.createDeployment().addModelInstance(fileName + ".bpmn", modelInstance).deploy();