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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.rest.stub;
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;
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;
43 @RequiredArgsConstructor
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 =
55 private static final Gson JSON_TRANSLATOR = new Gson();
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);
70 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
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);
86 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
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);
104 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
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);
113 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);