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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.rest.stub;
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;
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;
42 @RequiredArgsConstructor
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 =
51 private static final Gson JSON_TRANSLATOR = new Gson();
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);
66 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
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);
82 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
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);
100 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
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);
109 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);