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.experimental.categories.Category;
36 import org.junit.runner.RunWith;
37 import org.onap.so.bpmn.common.InjectionHelper;
38 import org.onap.so.bpmn.common.MockLoggerDelegate;
39 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
40 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupMapperLayer;
41 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
42 import org.onap.so.client.exception.ExceptionBuilder;
43 import org.onap.so.db.catalog.client.CatalogDbClient;
44 import org.onap.so.test.categories.SpringAware;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.beans.factory.annotation.Value;
47 import org.springframework.boot.context.embedded.LocalServerPort;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.boot.test.mock.mockito.MockBean;
50 import org.springframework.boot.test.mock.mockito.SpyBean;
51 import org.springframework.boot.test.web.client.TestRestTemplate;
52 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
53 import org.springframework.http.HttpHeaders;
54 import org.springframework.test.context.ActiveProfiles;
55 import org.springframework.test.context.ContextConfiguration;
56 import org.springframework.test.context.junit4.SpringRunner;
58 import com.fasterxml.jackson.databind.JsonNode;
59 import com.fasterxml.jackson.databind.ObjectMapper;
60 import com.github.tomakehurst.wiremock.client.WireMock;
62 @RunWith(SpringRunner.class)
63 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
64 @ActiveProfiles("test")
66 @AutoConfigureWireMock(port = 0)
67 @Category(SpringAware.class)
68 public abstract class BaseTest extends BuildingBlockTestDataSetup {
71 protected Map<String, Object> variables = new HashMap<>();
73 protected TestRestTemplate restTemplate = new TestRestTemplate();
75 protected HttpHeaders headers = new HttpHeaders();
79 protected RuntimeService runtimeService;
82 private RepositoryService repositoryService;
84 * Mocked for injection via autowiring
87 @Value("${mso.catalog.db.spring.endpoint}")
88 protected String endpoint;
90 @Value("${wiremock.server.port}")
91 protected String wireMockPort;
94 protected CatalogDbClient MOCK_catalogDbClient;
97 protected InjectionHelper MOCK_injectionHelper;
100 protected ExceptionBuilder exceptionUtil;
103 * Classes that cannot be simply mocked because they are both
104 * needed for testing another class, and must be autowired when
105 * being tested themselves....or classes with private methods that
106 * must be stubbed during testing
110 protected BBInputSetupMapperLayer SPY_bbInputSetupMapperLayer;
112 protected BBInputSetupUtils SPY_bbInputSetupUtils;
114 protected BBInputSetup SPY_bbInputSetup;
117 * Mocked for injection via the IntectionHelper
123 public void baseTestBefore() {
125 variables.put("gBuildingBlockExecution", execution);
131 protected String readFile(String path) throws IOException {
132 return readFile(path, Charset.defaultCharset());
135 protected String readFile(String path, Charset encoding) throws IOException {
136 byte[] encoded = Files.readAllBytes(Paths.get(path));
137 return new String(encoded, encoding);
140 protected String readJsonFileAsString(String fileLocation) throws IOException{
141 ObjectMapper mapper = new ObjectMapper();
142 JsonNode jsonNode = mapper.readTree(new File(fileLocation));
143 return jsonNode.asText();
146 protected String createURLWithPort(String uri) {
147 return "http://localhost:" + port + uri;
150 * Create and deploy a process model with one logger delegate as service task.
152 * @param origProcessKey
154 * @param mockProcessName
157 * file name without extension
159 protected void mockSubprocess(String origProcessKey, String mockProcessName, String fileName) {
160 BpmnModelInstance modelInstance = Bpmn.createExecutableProcess(origProcessKey).name(mockProcessName)
161 .startEvent().name("Start Point").serviceTask().name("Log Something for Test")
162 .camundaClass(MockLoggerDelegate.class.getName()).endEvent().name("End Point").done();
163 repositoryService.createDeployment().addModelInstance(fileName + ".bpmn", modelInstance).deploy();