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