Springboot 2.0 upgrade
[so.git] / bpmn / so-bpmn-building-blocks / 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.camunda.bpm.model.bpmn.Bpmn;
33 import org.camunda.bpm.model.bpmn.BpmnModelInstance;
34 import org.junit.Before;
35 import org.junit.runner.RunWith;
36 import org.onap.so.bpmn.buildingblock.SniroHomingV2;
37 import org.onap.so.bpmn.common.DelegateExecutionImpl;
38 import org.onap.so.bpmn.common.InjectionHelper;
39 import org.onap.so.bpmn.infrastructure.aai.tasks.AAIFlagTasks;
40 import org.onap.so.bpmn.sdno.tasks.SDNOHealthCheckTasks;
41 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
42 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupMapperLayer;
43 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
44 import org.onap.so.client.aai.mapper.AAIObjectMapper;
45 import org.onap.so.client.adapter.network.NetworkAdapterClientImpl;
46 import org.onap.so.client.adapter.network.mapper.NetworkAdapterObjectMapper;
47 import org.onap.so.client.exception.ExceptionBuilder;
48 import org.onap.so.client.orchestration.AAIVnfResources;
49 import org.onap.so.client.orchestration.SDNOHealthCheckResources;
50 import org.onap.so.client.sdnc.SDNCClient;
51 import org.onap.so.client.sniro.SniroClient;
52 import org.onap.so.db.catalog.client.CatalogDbClient;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.beans.factory.annotation.Value;
55 import org.springframework.boot.web.server.LocalServerPort;
56 import org.springframework.boot.test.context.SpringBootTest;
57 import org.springframework.boot.test.mock.mockito.MockBean;
58 import org.springframework.boot.test.mock.mockito.SpyBean;
59 import org.springframework.boot.test.web.client.TestRestTemplate;
60 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
61 import org.springframework.http.HttpHeaders;
62 import org.springframework.test.context.ActiveProfiles;
63 import org.springframework.test.context.ContextConfiguration;
64 import org.springframework.test.context.junit4.SpringRunner;
65
66 import com.fasterxml.jackson.databind.JsonNode;
67 import com.fasterxml.jackson.databind.ObjectMapper;
68
69 @RunWith(SpringRunner.class)
70 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
71 @ActiveProfiles("test")
72 @ContextConfiguration
73 @AutoConfigureWireMock(port = 0)
74 public abstract class BaseTest extends BuildingBlockTestDataSetup {
75
76
77         protected Map<String, Object> variables = new HashMap<>();
78
79         protected TestRestTemplate restTemplate = new TestRestTemplate();
80
81         protected HttpHeaders headers = new HttpHeaders();
82
83         @Value("${wiremock.server.port}")
84         protected String wireMockPort;
85
86         @Autowired
87         protected RuntimeService runtimeService;
88
89         @Autowired
90         private RepositoryService repositoryService;
91         /*
92          * Mocked for injection via autowiring
93          */
94
95         @Value("${mso.catalog.db.spring.endpoint}")
96         protected String endpoint;
97
98         @MockBean
99         protected CatalogDbClient MOCK_catalogDbClient;
100
101         @SpyBean
102         protected InjectionHelper MOCK_injectionHelper;
103
104         @SpyBean
105         protected NetworkAdapterObjectMapper MOCK_networkAdapterObjectMapper;
106
107         @SpyBean
108         protected AAIObjectMapper MOCK_aaiObjectMapper;
109         @SpyBean
110         protected NetworkAdapterClientImpl MOCK_networkAdapterClient;
111         @SpyBean
112         protected SDNCClient MOCK_sdncClient;
113
114         @SpyBean
115         protected AAIFlagTasks aaiFlagTasks;
116
117         @SpyBean
118         protected AAIVnfResources aaiVnfResources;
119
120         @SpyBean
121         protected ExceptionBuilder exceptionUtil;
122
123         @SpyBean
124         protected SDNOHealthCheckResources MOCK_sdnoHealthCheckResources;
125
126
127
128
129
130
131         /*
132          *  Classes that cannot be simply mocked because they are both
133          *  needed for testing another class, and must be autowired when
134          *  being tested themselves....or classes with private methods that
135          *  must be stubbed during testing
136          */
137
138
139
140
141         @SpyBean
142         protected BBInputSetupMapperLayer SPY_bbInputSetupMapperLayer;
143         @SpyBean
144         protected BBInputSetupUtils SPY_bbInputSetupUtils;
145         @SpyBean
146         protected BBInputSetup SPY_bbInputSetup;
147         @SpyBean
148         protected SniroHomingV2 sniroHoming;
149
150         @SpyBean
151         protected SniroClient sniroClient;
152
153         @SpyBean
154         protected SDNOHealthCheckTasks sdnoHealthCheckTasks;
155
156         /*
157          *  Mocked for injection via the IntectionHelper
158          */
159
160
161
162         @Before
163         public void baseTestBefore() {
164                 variables.put("gBuildingBlockExecution", new DelegateExecutionImpl(new HashMap<>()));
165
166
167         }
168
169         @LocalServerPort
170         private int port;
171
172         protected String readFile(String path) throws IOException {
173                 return readFile(path, Charset.defaultCharset());
174         }
175
176         protected String readFile(String path, Charset encoding) throws IOException {
177                 byte[] encoded = Files.readAllBytes(Paths.get(path));
178                 return new String(encoded, encoding);
179         }
180
181         protected String readJsonFileAsString(String fileLocation) throws IOException{
182                 ObjectMapper mapper = new ObjectMapper();
183                 JsonNode jsonNode = mapper.readTree(new File(fileLocation));
184                 return jsonNode.asText();
185         }
186
187         protected String createURLWithPort(String uri) {
188                 return "http://localhost:" + port + uri;
189         }
190         /**
191          * Create and deploy a process model with one logger delegate as service task.
192          *
193          * @param origProcessKey
194          *            key to call
195          * @param mockProcessName
196          *            process name
197          * @param fileName
198          *            file name without extension
199          */
200         protected void mockSubprocess(String origProcessKey, String mockProcessName, String fileName) {
201                 BpmnModelInstance modelInstance = Bpmn.createExecutableProcess(origProcessKey).name(mockProcessName)
202                                 .startEvent().name("Start Point").serviceTask().name("Log Something for Test")
203                                 .camundaClass(MockLoggerDelegate.class.getName()).endEvent().name("End Point").done();
204                 repositoryService.createDeployment().addModelInstance(fileName + ".bpmn", modelInstance).deploy();
205         }
206
207 }