aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/test/java/org/onap/so/BaseTest.java
blob: d2f1186ad0d8082fc0c87713d8fd4e2fc417fce0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.so;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.onap.so.bpmn.common.InjectionHelper;
import org.onap.so.bpmn.common.MockLoggerDelegate;
import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupMapperLayer;
import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
import org.onap.so.client.exception.ExceptionBuilder;
import org.onap.so.db.catalog.client.CatalogDbClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
import org.springframework.http.HttpHeaders;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.client.WireMock;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@ContextConfiguration
@AutoConfigureWireMock(port = 0)
public abstract class BaseTest extends BuildingBlockTestDataSetup {
	

	protected Map<String, Object> variables = new HashMap<>();

	protected TestRestTemplate restTemplate = new TestRestTemplate();

	protected HttpHeaders headers = new HttpHeaders();

	
	@Autowired
	protected RuntimeService runtimeService;

	@Autowired
	private RepositoryService repositoryService;
	/*
	 * Mocked for injection via autowiring
	 */
	
	@Value("${mso.catalog.db.spring.endpoint}")
	protected String endpoint;
	
	@Value("${wiremock.server.port}")
	protected String wireMockPort;
	
	@MockBean
	protected CatalogDbClient MOCK_catalogDbClient;
	
	@SpyBean
	protected InjectionHelper MOCK_injectionHelper;
	
	@SpyBean
	protected ExceptionBuilder exceptionUtil;
	
	/*
	 *  Classes that cannot be simply mocked because they are both
	 *  needed for testing another class, and must be autowired when
	 *  being tested themselves....or classes with private methods that
	 *  must be stubbed during testing
	 */
	
	@SpyBean
	protected BBInputSetupMapperLayer SPY_bbInputSetupMapperLayer;
	@SpyBean
	protected BBInputSetupUtils SPY_bbInputSetupUtils;
	@SpyBean
	protected BBInputSetup SPY_bbInputSetup;
	
	/*
	 *  Mocked for injection via the IntectionHelper
	 */
	

	
	@Before
	public void baseTestBefore() {
		WireMock.reset();
		variables.put("gBuildingBlockExecution", execution);
	}

	@LocalServerPort
	private int port;
	
	protected String readFile(String path) throws IOException {
		return readFile(path, Charset.defaultCharset());
	}
	
	protected String readFile(String path, Charset encoding) throws IOException {
		byte[] encoded = Files.readAllBytes(Paths.get(path));
		return new String(encoded, encoding);
	}
	
	protected String readJsonFileAsString(String fileLocation) throws IOException{
		ObjectMapper mapper = new ObjectMapper();
		JsonNode jsonNode = mapper.readTree(new File(fileLocation));
		return jsonNode.asText();
	}

	protected String createURLWithPort(String uri) {
		return "http://localhost:" + port + uri;
	}
	/**
	 * Create and deploy a process model with one logger delegate as service task.
	 *
	 * @param origProcessKey
	 *            key to call
	 * @param mockProcessName
	 *            process name
	 * @param fileName
	 *            file name without extension
	 */
	protected void mockSubprocess(String origProcessKey, String mockProcessName, String fileName) {
		BpmnModelInstance modelInstance = Bpmn.createExecutableProcess(origProcessKey).name(mockProcessName)
				.startEvent().name("Start Point").serviceTask().name("Log Something for Test")
				.camundaClass(MockLoggerDelegate.class.getName()).endEvent().name("End Point").done();
		repositoryService.createDeployment().addModelInstance(fileName + ".bpmn", modelInstance).deploy();
	}
	
}