Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / BaseTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.so;
21
22 import java.io.File;
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;
28 import java.util.Map;
29 import org.camunda.bpm.engine.RepositoryService;
30 import org.camunda.bpm.engine.RuntimeService;
31 import org.junit.Before;
32 import org.junit.experimental.categories.Category;
33 import org.junit.runner.RunWith;
34 import org.onap.so.bpmn.common.InjectionHelper;
35 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
36 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupMapperLayer;
37 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
38 import org.onap.so.client.exception.ExceptionBuilder;
39 import org.onap.so.db.catalog.client.CatalogDbClient;
40 import org.onap.so.test.categories.SpringAware;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.beans.factory.annotation.Value;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.boot.test.mock.mockito.MockBean;
45 import org.springframework.boot.test.mock.mockito.SpyBean;
46 import org.springframework.boot.test.web.client.TestRestTemplate;
47 import org.springframework.boot.web.server.LocalServerPort;
48 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
49 import org.springframework.http.HttpHeaders;
50 import org.springframework.test.context.ActiveProfiles;
51 import org.springframework.test.context.ContextConfiguration;
52 import org.springframework.test.context.junit4.SpringRunner;
53 import com.fasterxml.jackson.databind.JsonNode;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55 import com.github.tomakehurst.wiremock.WireMockServer;
56
57 @RunWith(SpringRunner.class)
58 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
59 @ActiveProfiles("test")
60 @ContextConfiguration
61 @AutoConfigureWireMock(port = 0)
62 @Category(SpringAware.class)
63 public abstract class BaseTest extends BuildingBlockTestDataSetup {
64
65
66     protected Map<String, Object> variables = new HashMap<>();
67
68     protected TestRestTemplate restTemplate = new TestRestTemplate();
69
70     protected HttpHeaders headers = new HttpHeaders();
71
72
73     @Autowired
74     protected RuntimeService runtimeService;
75
76     @Autowired
77     private RepositoryService repositoryService;
78
79     @Autowired
80     protected WireMockServer wireMockServer;
81     /*
82      * Mocked for injection via autowiring
83      */
84
85     @Value("${mso.catalog.db.spring.endpoint}")
86     protected String endpoint;
87
88     @Value("${wiremock.server.port}")
89     protected String wireMockPort;
90
91     @MockBean
92     protected CatalogDbClient MOCK_catalogDbClient;
93
94     @SpyBean
95     protected InjectionHelper MOCK_injectionHelper;
96
97     @SpyBean
98     protected ExceptionBuilder exceptionUtil;
99
100     /*
101      * Classes that cannot be simply mocked because they are both needed for testing another class, and must be
102      * autowired when being tested themselves....or classes with private methods that must be stubbed during testing
103      */
104
105     @SpyBean
106     protected BBInputSetupMapperLayer SPY_bbInputSetupMapperLayer;
107     @SpyBean
108     protected BBInputSetupUtils SPY_bbInputSetupUtils;
109     @SpyBean
110     protected BBInputSetup SPY_bbInputSetup;
111
112     /*
113      * Mocked for injection via the IntectionHelper
114      */
115
116
117
118     @Before
119     public void baseTestBefore() {
120         wireMockServer.resetAll();
121         variables.put("gBuildingBlockExecution", execution);
122     }
123
124     @LocalServerPort
125     private int port;
126
127     protected String readFile(String path) throws IOException {
128         return readFile(path, Charset.defaultCharset());
129     }
130
131     protected String readFile(String path, Charset encoding) throws IOException {
132         byte[] encoded = Files.readAllBytes(Paths.get(path));
133         return new String(encoded, encoding);
134     }
135
136     protected String readJsonFileAsString(String fileLocation) throws IOException {
137         ObjectMapper mapper = new ObjectMapper();
138         JsonNode jsonNode = mapper.readTree(new File(fileLocation));
139         return jsonNode.asText();
140     }
141
142     protected String createURLWithPort(String uri) {
143         return "http://localhost:" + port + uri;
144     }
145
146 }