Uplift from master
[portal/nonrtric-controlpanel.git] / webapp-backend / src / test / java / org / oransc / portal / nonrtric / controlpanel / RestApiTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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.oransc.portal.nonrtric.controlpanel;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import com.google.gson.JsonArray;
28 import com.google.gson.JsonElement;
29 import com.google.gson.JsonParser;
30
31 import java.io.BufferedReader;
32 import java.io.FileNotFoundException;
33 import java.io.FileOutputStream;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.io.PrintStream;
37 import java.lang.invoke.MethodHandles;
38 import java.util.stream.Collectors;
39
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.extension.ExtendWith;
42 import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
48 import org.springframework.boot.web.server.LocalServerPort;
49 import org.springframework.context.ApplicationContext;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.test.context.junit.jupiter.SpringExtension;
53
54 @ExtendWith(SpringExtension.class)
55 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
56 class RestApiTest {
57
58     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59
60     private static Gson gson = new GsonBuilder().setPrettyPrinting().create();
61
62     @Autowired
63     ApplicationContext context;
64
65     @LocalServerPort
66     private int port;
67
68     @Test
69     void createApiDoc() throws FileNotFoundException {
70         String url = "/v2/api-docs";
71         ResponseEntity<String> resp = restClient().getForEntity(url).block();
72         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
73         JsonElement jsonElement = JsonParser.parseString(resp.getBody());
74         String indented = gson.toJson(jsonElement);
75         try (PrintStream out = new PrintStream(new FileOutputStream("../docs/api.json"))) {
76             out.println(indented);
77         }
78     }
79
80     @Test
81     void getJobs() throws FileNotFoundException {
82         String url = "/api/enrichment/eijobs";
83         ResponseEntity<String> resp = restClient().getForEntity(url).block();
84         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
85
86         JsonArray jobs = JsonParser.parseString(resp.getBody()).getAsJsonArray();
87
88         assertThat(jobs).hasSize(6);
89         assertThat(resp.getBody()).contains("job2");
90         assertThat(resp.getBody()).contains("job1");
91     }
92
93     @Test
94     void getProducers() {
95         String url = "/api/enrichment/eiproducers";
96         ResponseEntity<String> resp = restClient().getForEntity(url).block();
97         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
98
99         JsonArray producers = JsonParser.parseString(resp.getBody()).getAsJsonArray();
100
101         assertThat(producers).hasSize(3);
102     }
103
104     private AsyncRestClient restClient() {
105         return new AsyncRestClient(baseUrl());
106     }
107
108     private String baseUrl() {
109         return "https://localhost:" + this.port;
110     }
111
112     private String getStringFromFile(String path) {
113         try {
114             InputStream inputStream = MethodHandles.lookup().lookupClass().getClassLoader().getResourceAsStream(path);
115             return new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
116         } catch (Exception e) {
117             logger.error("Cannot read file :" + path, e);
118             return "";
119         }
120     }
121 }