Springboot 2.0 upgrade
[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
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;
54
55 import com.fasterxml.jackson.databind.JsonNode;
56 import com.fasterxml.jackson.databind.ObjectMapper;
57 import com.github.tomakehurst.wiremock.client.WireMock;
58
59 @RunWith(SpringRunner.class)
60 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
61 @ActiveProfiles("test")
62 @ContextConfiguration
63 @AutoConfigureWireMock(port = 0)
64 @Category(SpringAware.class)
65 public abstract class BaseTest extends BuildingBlockTestDataSetup {
66         
67
68         protected Map<String, Object> variables = new HashMap<>();
69
70         protected TestRestTemplate restTemplate = new TestRestTemplate();
71
72         protected HttpHeaders headers = new HttpHeaders();
73
74         
75         @Autowired
76         protected RuntimeService runtimeService;
77
78         @Autowired
79         private RepositoryService repositoryService;
80         /*
81          * Mocked for injection via autowiring
82          */
83         
84         @Value("${mso.catalog.db.spring.endpoint}")
85         protected String endpoint;
86         
87         @Value("${wiremock.server.port}")
88         protected String wireMockPort;
89         
90         @MockBean
91         protected CatalogDbClient MOCK_catalogDbClient;
92         
93         @SpyBean
94         protected InjectionHelper MOCK_injectionHelper;
95         
96         @SpyBean
97         protected ExceptionBuilder exceptionUtil;
98         
99         /*
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
104          */
105         
106         @SpyBean
107         protected BBInputSetupMapperLayer SPY_bbInputSetupMapperLayer;
108         @SpyBean
109         protected BBInputSetupUtils SPY_bbInputSetupUtils;
110         @SpyBean
111         protected BBInputSetup SPY_bbInputSetup;
112         
113         /*
114          *  Mocked for injection via the IntectionHelper
115          */
116         
117
118         
119         @Before
120         public void baseTestBefore() {
121                 WireMock.reset();
122                 variables.put("gBuildingBlockExecution", execution);
123         }
124
125         @LocalServerPort
126         private int port;
127         
128         protected String readFile(String path) throws IOException {
129                 return readFile(path, Charset.defaultCharset());
130         }
131         
132         protected String readFile(String path, Charset encoding) throws IOException {
133                 byte[] encoded = Files.readAllBytes(Paths.get(path));
134                 return new String(encoded, encoding);
135         }
136         
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();
141         }
142
143         protected String createURLWithPort(String uri) {
144                 return "http://localhost:" + port + uri;
145         }
146         
147 }