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