119d3b1e2d113a43690110ffd2273ed55b58b971
[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.camunda.bpm.model.bpmn.Bpmn;
33 import org.camunda.bpm.model.bpmn.BpmnModelInstance;
34 import org.junit.Before;
35 import org.junit.experimental.categories.Category;
36 import org.junit.runner.RunWith;
37 import org.onap.so.bpmn.common.InjectionHelper;
38 import org.onap.so.bpmn.common.MockLoggerDelegate;
39 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
40 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupMapperLayer;
41 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
42 import org.onap.so.client.exception.ExceptionBuilder;
43 import org.onap.so.db.catalog.client.CatalogDbClient;
44 import org.onap.so.test.categories.SpringAware;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.beans.factory.annotation.Value;
47 import org.springframework.boot.context.embedded.LocalServerPort;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.boot.test.mock.mockito.MockBean;
50 import org.springframework.boot.test.mock.mockito.SpyBean;
51 import org.springframework.boot.test.web.client.TestRestTemplate;
52 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
53 import org.springframework.http.HttpHeaders;
54 import org.springframework.test.context.ActiveProfiles;
55 import org.springframework.test.context.ContextConfiguration;
56 import org.springframework.test.context.junit4.SpringRunner;
57
58 import com.fasterxml.jackson.databind.JsonNode;
59 import com.fasterxml.jackson.databind.ObjectMapper;
60 import com.github.tomakehurst.wiremock.client.WireMock;
61
62 @RunWith(SpringRunner.class)
63 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
64 @ActiveProfiles("test")
65 @ContextConfiguration
66 @AutoConfigureWireMock(port = 0)
67 @Category(SpringAware.class)
68 public abstract class BaseTest extends BuildingBlockTestDataSetup {
69         
70
71         protected Map<String, Object> variables = new HashMap<>();
72
73         protected TestRestTemplate restTemplate = new TestRestTemplate();
74
75         protected HttpHeaders headers = new HttpHeaders();
76
77         
78         @Autowired
79         protected RuntimeService runtimeService;
80
81         @Autowired
82         private RepositoryService repositoryService;
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                 WireMock.reset();
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          * Create and deploy a process model with one logger delegate as service task.
151          *
152          * @param origProcessKey
153          *            key to call
154          * @param mockProcessName
155          *            process name
156          * @param fileName
157          *            file name without extension
158          */
159         protected void mockSubprocess(String origProcessKey, String mockProcessName, String fileName) {
160                 BpmnModelInstance modelInstance = Bpmn.createExecutableProcess(origProcessKey).name(mockProcessName)
161                                 .startEvent().name("Start Point").serviceTask().name("Log Something for Test")
162                                 .camundaClass(MockLoggerDelegate.class.getName()).endEvent().name("End Point").done();
163                 repositoryService.createDeployment().addModelInstance(fileName + ".bpmn", modelInstance).deploy();
164         }
165         
166 }