Clean up dependencies for London Release
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / stub / StubUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.rest.stub;
22
23 import com.google.gson.Gson;
24 import java.io.IOException;
25 import java.nio.charset.StandardCharsets;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import javax.servlet.http.HttpServletRequest;
31 import lombok.RequiredArgsConstructor;
32 import org.onap.policy.models.pdp.concepts.PdpStatistics;
33 import org.onap.policy.pap.main.rest.PapRestControllerV1;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.context.annotation.Profile;
37 import org.springframework.core.io.ClassPathResource;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.stereotype.Service;
41
42 @RequiredArgsConstructor
43 @Service
44 @Profile("stub")
45 class StubUtils {
46     private static final Logger log = LoggerFactory.getLogger(StubUtils.class);
47     private final HttpServletRequest request;
48     private static final String ACCEPT = "Accept";
49     private static final String PAP_DB =
50             "/PapDb.json";
51     private static final Gson JSON_TRANSLATOR = new Gson();
52
53     <T> ResponseEntity<T> getStubbedResponse(Class<T> clazz) {
54         var accept = request.getHeader(ACCEPT);
55         if (accept != null && accept.contains("application/json")) {
56             final var resource = new ClassPathResource(PAP_DB);
57             try (var inputStream = resource.getInputStream()) {
58                 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
59                 var targetObject = JSON_TRANSLATOR.fromJson(string, clazz);
60                 return new ResponseEntity<>(targetObject, HttpStatus.OK);
61             } catch (IOException e) {
62                 log.error("Couldn't serialize response for content type application/json", e);
63                 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
64             }
65         }
66         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
67     }
68
69     <T> ResponseEntity<List<T>> getStubbedResponseList(Class<T> clazz) {
70         var accept = request.getHeader(ACCEPT);
71         if (accept != null && accept.contains("application/json")) {
72             final var resource = new ClassPathResource(PAP_DB);
73             try (var inputStream = resource.getInputStream()) {
74                 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
75                 var targetObject = Arrays.asList(JSON_TRANSLATOR.fromJson(string, clazz));
76                 return new ResponseEntity<>(targetObject, HttpStatus.OK);
77             } catch (IOException e) {
78                 log.error("Couldn't serialize response for content type application/json", e);
79                 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
80             }
81         }
82         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
83     }
84
85     ResponseEntity<Map<String, Object>> getStubbedResponseMap() {
86         var accept = request.getHeader(ACCEPT);
87         if (accept != null && accept.contains("application/json")) {
88             final var resource = new ClassPathResource(PAP_DB);
89             try (var inputStream = resource.getInputStream()) {
90                 Map<String, Object> map = new HashMap<>();
91                 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
92                 map.put(PapRestControllerV1.API_VERSION_NAME,
93                         JSON_TRANSLATOR.fromJson(string, Object.class));
94                 return new ResponseEntity<>(map, HttpStatus.OK);
95             } catch (IOException e) {
96                 log.error("Couldn't serialize response for content type application/json", e);
97                 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
98             }
99         }
100         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
101     }
102
103     ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> getStubbedResponseStatistics() {
104         var accept = request.getHeader(ACCEPT);
105         if (accept != null && accept.contains("application/json")) {
106             Map<String, Map<String, List<PdpStatistics>>> map = new HashMap<>();
107             return new ResponseEntity<>(map, HttpStatus.OK);
108         }
109         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
110     }
111
112 }