170b305bb5ccd80b684d3f26bb31e42e14be6fed
[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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.apihandlerinfra;
24
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
27
28 import javax.transaction.Transactional;
29
30 import org.junit.After;
31 import org.junit.BeforeClass;
32 import org.junit.runner.RunWith;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.boot.test.context.SpringBootTest;
37 import org.springframework.boot.test.web.client.TestRestTemplate;
38 import org.springframework.boot.web.server.LocalServerPort;
39 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
40 import org.springframework.core.env.Environment;
41 import org.springframework.test.context.ActiveProfiles;
42 import org.springframework.test.context.ContextConfiguration;
43 import org.springframework.test.context.junit4.SpringRunner;
44
45 import com.github.tomakehurst.wiremock.WireMockServer;
46 import com.github.tomakehurst.wiremock.client.WireMock;
47
48 @RunWith(SpringRunner.class)
49 @SpringBootTest(classes = ApiHandlerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
50 @ActiveProfiles("test")
51 @ContextConfiguration
52 @Transactional
53 @AutoConfigureWireMock(port = 0)
54 public abstract class BaseTest {
55         protected Logger logger = LoggerFactory.getLogger(BaseTest.class);
56         protected TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
57
58         @Autowired
59         protected Environment env;
60
61         @LocalServerPort
62         private int port;
63         
64         @Autowired
65         protected WireMockServer wireMockServer;
66         
67         protected String createURLWithPort(String uri) {
68                 return "http://localhost:" + port + uri;
69         }
70         
71         protected String createURLWithPort(String uri, int iPort) {
72                 return "http://localhost:" + iPort + uri;
73         }       
74         
75         @After
76         public void tearDown(){
77                 wireMockServer.resetAll();
78         }
79
80         public static String getResponseTemplate;
81         public static String getResponseTemplateNoBody;
82         public static String infraActivePost;
83         @BeforeClass
84         public static void setupTest() throws Exception {
85                 getResponseTemplate = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/InfraActiveRequests/getInfraActiveRequest.json")));
86                 getResponseTemplateNoBody = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/InfraActiveRequests/getInfraActiveRequestNoBody.json")));
87                 infraActivePost = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/InfraActiveRequests/createInfraActiveRequests.json")));
88         }
89
90         public String getTestUrl(String requestId) {
91                 return "/infraActiveRequests/" + requestId;
92         }
93 }