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 jakarta.servlet.http.HttpServletRequest;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.util.HashMap;
29 import java.util.List;
31 import lombok.RequiredArgsConstructor;
32 import org.onap.policy.pap.main.rest.PapRestControllerV1;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.context.annotation.Profile;
36 import org.springframework.core.io.ClassPathResource;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Service;
41 @RequiredArgsConstructor
45 private static final Logger log = LoggerFactory.getLogger(StubUtils.class);
46 private static final String APPLICATION_JSON = "application/json";
47 private static final String SERIALIZE_RESPONSE_FAILURE_MSG =
48 "Couldn't serialize response for content type application/json";
49 private final HttpServletRequest request;
50 private static final String ACCEPT = "Accept";
51 private static final String PAP_DB =
53 private static final Gson JSON_TRANSLATOR = new Gson();
55 <T> ResponseEntity<T> getStubbedResponse(Class<T> clazz) {
56 var accept = request.getHeader(ACCEPT);
57 if (accept != null && accept.contains(APPLICATION_JSON)) {
58 final var resource = new ClassPathResource(PAP_DB);
59 try (var inputStream = resource.getInputStream()) {
60 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
61 var targetObject = JSON_TRANSLATOR.fromJson(string, clazz);
62 return new ResponseEntity<>(targetObject, HttpStatus.OK);
63 } catch (IOException e) {
64 log.error(SERIALIZE_RESPONSE_FAILURE_MSG, e);
65 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
68 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
71 <T> ResponseEntity<List<T>> getStubbedResponseList(Class<T> clazz) {
72 var accept = request.getHeader(ACCEPT);
73 if (accept != null && accept.contains(APPLICATION_JSON)) {
74 final var resource = new ClassPathResource(PAP_DB);
75 try (var inputStream = resource.getInputStream()) {
76 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
77 var targetObject = List.of(JSON_TRANSLATOR.fromJson(string, clazz));
78 return new ResponseEntity<>(targetObject, HttpStatus.OK);
79 } catch (IOException e) {
80 log.error(SERIALIZE_RESPONSE_FAILURE_MSG, e);
81 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
84 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
87 ResponseEntity<Map<String, Object>> getStubbedResponseMap() {
88 var accept = request.getHeader(ACCEPT);
89 if (accept != null && accept.contains(APPLICATION_JSON)) {
90 final var resource = new ClassPathResource(PAP_DB);
91 try (var inputStream = resource.getInputStream()) {
92 Map<String, Object> map = new HashMap<>();
93 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
94 map.put(PapRestControllerV1.API_VERSION_NAME,
95 JSON_TRANSLATOR.fromJson(string, Object.class));
96 return new ResponseEntity<>(map, HttpStatus.OK);
97 } catch (IOException e) {
98 log.error(SERIALIZE_RESPONSE_FAILURE_MSG, e);
99 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
102 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);