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.junit.Before;
33 import org.junit.experimental.categories.Category;
34 import org.junit.runner.RunWith;
35 import org.onap.so.bpmn.common.InjectionHelper;
36 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
37 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupMapperLayer;
38 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
39 import org.onap.so.client.exception.ExceptionBuilder;
40 import org.onap.so.db.catalog.client.CatalogDbClient;
41 import org.onap.so.test.categories.SpringAware;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Value;
44 import org.springframework.boot.web.server.LocalServerPort;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.boot.test.mock.mockito.MockBean;
47 import org.springframework.boot.test.mock.mockito.SpyBean;
48 import org.springframework.boot.test.web.client.TestRestTemplate;
49 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
50 import org.springframework.http.HttpHeaders;
51 import org.springframework.test.context.ActiveProfiles;
52 import org.springframework.test.context.ContextConfiguration;
53 import org.springframework.test.context.junit4.SpringRunner;
55 import com.fasterxml.jackson.databind.JsonNode;
56 import com.fasterxml.jackson.databind.ObjectMapper;
57 import com.github.tomakehurst.wiremock.client.WireMock;
59 @RunWith(SpringRunner.class)
60 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
61 @ActiveProfiles("test")
63 @AutoConfigureWireMock(port = 0)
64 @Category(SpringAware.class)
65 public abstract class BaseTest extends BuildingBlockTestDataSetup {
68 protected Map<String, Object> variables = new HashMap<>();
70 protected TestRestTemplate restTemplate = new TestRestTemplate();
72 protected HttpHeaders headers = new HttpHeaders();
76 protected RuntimeService runtimeService;
79 private RepositoryService repositoryService;
81 * Mocked for injection via autowiring
84 @Value("${mso.catalog.db.spring.endpoint}")
85 protected String endpoint;
87 @Value("${wiremock.server.port}")
88 protected String wireMockPort;
91 protected CatalogDbClient MOCK_catalogDbClient;
94 protected InjectionHelper MOCK_injectionHelper;
97 protected ExceptionBuilder exceptionUtil;
100 * Classes that cannot be simply mocked because they are both
101 * needed for testing another class, and must be autowired when
102 * being tested themselves....or classes with private methods that
103 * must be stubbed during testing
107 protected BBInputSetupMapperLayer SPY_bbInputSetupMapperLayer;
109 protected BBInputSetupUtils SPY_bbInputSetupUtils;
111 protected BBInputSetup SPY_bbInputSetup;
114 * Mocked for injection via the IntectionHelper
120 public void baseTestBefore() {
122 variables.put("gBuildingBlockExecution", execution);
128 protected String readFile(String path) throws IOException {
129 return readFile(path, Charset.defaultCharset());
132 protected String readFile(String path, Charset encoding) throws IOException {
133 byte[] encoded = Files.readAllBytes(Paths.get(path));
134 return new String(encoded, encoding);
137 protected String readJsonFileAsString(String fileLocation) throws IOException{
138 ObjectMapper mapper = new ObjectMapper();
139 JsonNode jsonNode = mapper.readTree(new File(fileLocation));
140 return jsonNode.asText();
143 protected String createURLWithPort(String uri) {
144 return "http://localhost:" + port + uri;