Remove unused method
[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.JsonObject;
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.model.JobInfo;
43 import org.oransc.portal.nonrtric.controlpanel.model.ProducerInfo;
44 import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
50 import org.springframework.boot.web.server.LocalServerPort;
51 import org.springframework.context.ApplicationContext;
52 import org.springframework.http.HttpStatus;
53 import org.springframework.http.ResponseEntity;
54 import org.springframework.test.context.junit.jupiter.SpringExtension;
55
56 @ExtendWith(SpringExtension.class)
57 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
58 class RestApiTest {
59
60     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61
62     private static Gson gson = new GsonBuilder().setPrettyPrinting().create();
63
64     @Autowired
65     ApplicationContext context;
66
67     @LocalServerPort
68     private int port;
69
70     @Test
71     void createApiDoc() throws FileNotFoundException {
72         String url = "/v2/api-docs";
73         ResponseEntity<String> resp = restClient().getForEntity(url).block();
74         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
75         JsonObject jsonElement = JsonParser.parseString(resp.getBody()).getAsJsonObject();
76         jsonElement.remove("host");
77         String indented = gson.toJson(jsonElement);
78         try (PrintStream out = new PrintStream(new FileOutputStream("../docs/api.json"))) {
79             out.println(indented);
80         }
81     }
82
83     @Test
84     void getJobs() throws FileNotFoundException {
85         String url = "/api/enrichment/eijobs";
86         ResponseEntity<String> resp = restClient().getForEntity(url).block();
87         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
88
89         JsonArray jobs = JsonParser.parseString(resp.getBody()).getAsJsonArray();
90         JobInfo wantedJobInfo = JobInfo.builder() //
91             .id("job1") //
92             .typeId("type1") //
93             .jobData(getStringFromFile("job-1.json")) //
94             .targetUri("http://example.com/") //
95             .owner("owner") //
96             .build();
97         JobInfo wantedJobInfo2 = JobInfo.builder() //
98             .id("job2") //
99             .typeId("type2") //
100             .jobData(getStringFromFile("job-2.json")) //
101             .targetUri("http://example.com/") //
102             .owner("owner") //
103             .build();
104         assertThat(jobs).hasSize(6) //
105             .contains(gson.toJsonTree(wantedJobInfo), //
106             gson.toJsonTree(wantedJobInfo2));
107     }
108
109     @Test
110     void getProducers() {
111         String url = "/api/enrichment/eiproducers";
112         ResponseEntity<String> resp = restClient().getForEntity(url).block();
113         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
114
115         JsonArray producers = JsonParser.parseString(resp.getBody()).getAsJsonArray();
116
117         ProducerInfo wantedProducerInfo = ProducerInfo.builder() //
118             .id("prod-1") //
119             .types(new String[] {"type1", "type2"}) //
120             .status("ENABLED") //
121             .build();
122         ProducerInfo wantedProducerInfo2 = ProducerInfo.builder() //
123             .id("prod-2") //
124             .types(new String[] {"type1"}) //
125             .status("DISABLED") //
126             .build();
127         ProducerInfo wantedProducerInfo3 = ProducerInfo.builder() //
128             .id("3-prod") //
129             .types(new String[] {"type1", "type2"}) //
130             .status("ENABLED") //
131             .build();
132         assertThat(producers).hasSize(3) //
133             .contains(gson.toJsonTree(wantedProducerInfo), //
134             gson.toJsonTree(wantedProducerInfo2), //
135             gson.toJsonTree(wantedProducerInfo3));
136     }
137
138     private AsyncRestClient restClient() {
139         return new AsyncRestClient(baseUrl());
140     }
141
142     private String baseUrl() {
143         return "https://localhost:" + this.port;
144     }
145
146     private String getStringFromFile(String path) {
147         try {
148             InputStream inputStream = MethodHandles.lookup().lookupClass().getClassLoader().getResourceAsStream(path);
149             return new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
150         } catch (Exception e) {
151             logger.error("Cannot read file :" + path, e);
152             return "";
153         }
154     }
155 }