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