Containerization feature of SO
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / 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
21 package org.onap.so.apihandlerinfra;
22
23 import java.io.File;
24 import java.io.IOException;
25
26 import javax.transaction.Transactional;
27
28 import org.junit.After;
29 import org.junit.runner.RunWith;
30 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
31 import org.onap.so.logger.MsoLogger;
32 import org.onap.so.logger.MsoLogger.Catalog;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.boot.context.embedded.LocalServerPort;
35 import org.springframework.boot.test.context.SpringBootTest;
36 import org.springframework.boot.test.web.client.TestRestTemplate;
37 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
38 import org.springframework.core.env.Environment;
39 import org.springframework.http.HttpHeaders;
40 import org.springframework.test.context.ActiveProfiles;
41 import org.springframework.test.context.ContextConfiguration;
42 import org.springframework.test.context.jdbc.Sql;
43 import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
44 import org.springframework.test.context.junit4.SpringRunner;
45
46 import com.fasterxml.jackson.core.JsonParseException;
47 import com.fasterxml.jackson.databind.JsonMappingException;
48 import com.fasterxml.jackson.databind.JsonNode;
49 import com.fasterxml.jackson.databind.ObjectMapper;
50 import com.github.tomakehurst.wiremock.client.WireMock;
51
52 @RunWith(SpringRunner.class)
53 @SpringBootTest(classes = ApiHandlerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
54 @ActiveProfiles("test")
55 @ContextConfiguration
56 @Transactional
57 @Sql(executionPhase=ExecutionPhase.AFTER_TEST_METHOD,scripts="classpath:InfraActiveRequestsReset.sql")
58 @AutoConfigureWireMock(port = 0)
59 public abstract class BaseTest {
60         
61         protected MsoLogger logger = MsoLogger.getMsoLogger(Catalog.GENERAL, BaseTest.class);
62         protected TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
63
64         protected HttpHeaders headers = new HttpHeaders();      
65
66         @Autowired
67         protected Environment env;
68         
69         @Autowired
70         protected InfraActiveRequestsRepository iar;
71         
72         @LocalServerPort
73         private int port;
74         
75         protected String readJsonFileAsString(String fileLocation) throws JsonParseException, JsonMappingException, IOException{
76                 ObjectMapper mapper = new ObjectMapper();
77                 JsonNode jsonNode = mapper.readTree(new File(fileLocation));
78                 return jsonNode.asText();
79         }
80         
81         protected String createURLWithPort(String uri) {
82                 return "http://localhost:" + port + uri;
83         }
84         
85         @After
86         public void tearDown(){
87                 iar.deleteAll();
88                 WireMock.reset();
89         }
90 }