0f702fca2d208da523db49e35906a96339c4b607
[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.test.context.SpringBootTest;
45 import org.springframework.boot.test.mock.mockito.MockBean;
46 import org.springframework.boot.test.mock.mockito.SpyBean;
47 import org.springframework.boot.test.web.client.TestRestTemplate;
48 import org.springframework.boot.web.server.LocalServerPort;
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.WireMockServer;
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         @Autowired
82         protected WireMockServer wireMockServer;
83         /*
84          * Mocked for injection via autowiring
85          */
86         
87         @Value("${mso.catalog.db.spring.endpoint}")
88         protected String endpoint;
89         
90         @Value("${wiremock.server.port}")
91         protected String wireMockPort;
92         
93         @MockBean
94         protected CatalogDbClient MOCK_catalogDbClient;
95         
96         @SpyBean
97         protected InjectionHelper MOCK_injectionHelper;
98         
99         @SpyBean
100         protected ExceptionBuilder exceptionUtil;
101         
102         /*
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
107          */
108         
109         @SpyBean
110         protected BBInputSetupMapperLayer SPY_bbInputSetupMapperLayer;
111         @SpyBean
112         protected BBInputSetupUtils SPY_bbInputSetupUtils;
113         @SpyBean
114         protected BBInputSetup SPY_bbInputSetup;
115         
116         /*
117          *  Mocked for injection via the IntectionHelper
118          */
119         
120
121         
122         @Before
123         public void baseTestBefore() {
124                 wireMockServer.resetAll();
125                 variables.put("gBuildingBlockExecution", execution);
126         }
127
128         @LocalServerPort
129         private int port;
130         
131         protected String readFile(String path) throws IOException {
132                 return readFile(path, Charset.defaultCharset());
133         }
134         
135         protected String readFile(String path, Charset encoding) throws IOException {
136                 byte[] encoded = Files.readAllBytes(Paths.get(path));
137                 return new String(encoded, encoding);
138         }
139         
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();
144         }
145
146         protected String createURLWithPort(String uri) {
147                 return "http://localhost:" + port + uri;
148         }
149         
150 }