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.api.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.List;
28 import javax.servlet.http.HttpServletRequest;
29 import lombok.RequiredArgsConstructor;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.context.annotation.Profile;
33 import org.springframework.core.io.ClassPathResource;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Service;
38 @RequiredArgsConstructor
42 private static final Logger log = LoggerFactory.getLogger(StubUtils.class);
43 private final HttpServletRequest request;
44 private static final String ACCEPT = "Accept";
45 private static final String TOSCA_NODE_TEMPLATE_RESOURCE =
46 "nodetemplates/nodetemplates.metadatasets.input.tosca.json";
47 private static final Gson JSON_TRANSLATOR = new Gson();
49 <T> ResponseEntity<T> getStubbedResponse(Class<T> clazz) {
50 var accept = request.getHeader(ACCEPT);
51 if (accept != null && accept.contains("application/json")) {
52 final var resource = new ClassPathResource(TOSCA_NODE_TEMPLATE_RESOURCE);
53 try (var inputStream = resource.getInputStream()) {
54 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
55 var targetObject = JSON_TRANSLATOR.fromJson(string, clazz);
56 return new ResponseEntity<>(targetObject, HttpStatus.OK);
57 } catch (IOException e) {
58 log.error("Couldn't serialize response for content type application/json", e);
59 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
62 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
65 <T> ResponseEntity<List<T>> getStubbedResponseList(Class<T> clazz) {
66 var accept = request.getHeader(ACCEPT);
67 if (accept != null && accept.contains("application/json")) {
68 final var resource = new ClassPathResource(TOSCA_NODE_TEMPLATE_RESOURCE);
69 try (var inputStream = resource.getInputStream()) {
70 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
71 var targetObject = Arrays.asList(JSON_TRANSLATOR.fromJson(string, clazz));
72 return new ResponseEntity<>(targetObject, HttpStatus.OK);
73 } catch (IOException e) {
74 log.error("Couldn't serialize response for content type application/json", e);
75 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
78 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);