2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Copyright (C) 2019, 2023 Nordix Foundation.
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.apex.testsuites.integration.uservice.adapt.restclient;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 import com.google.gson.Gson;
28 import jakarta.ws.rs.GET;
29 import jakarta.ws.rs.POST;
30 import jakarta.ws.rs.PUT;
31 import jakarta.ws.rs.Path;
32 import jakarta.ws.rs.core.Response;
34 import java.util.Random;
37 * The Class TestRestClientEndpoint.
40 public class TestRestClientEndpoint {
41 private static int postMessagesReceived = 0;
42 private static int putMessagesReceived = 0;
43 private static int statMessagesReceived = 0;
44 private static int getMessagesReceived = 0;
45 private static int tagUrlToProperUrl = 0;
50 * @return the response
54 public Response serviceGetStats() {
55 statMessagesReceived++;
56 return Response.status(200).entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
57 + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
63 * @return the response
65 @Path("/event/GetEvent")
67 public Response serviceGetEvent() {
68 final Random rand = new Random();
69 final int nextMatchCase = rand.nextInt(4);
70 final String nextEventName = "Event0" + rand.nextInt(2) + "00";
72 final String eventString = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.sample.events\",\n" + "\"name\": \""
73 + nextEventName + "\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + getMessagesReceived
74 + "\",\n" + "\"target\": \"apex\",\n" + "\"TestSlogan\": \"Test slogan for External Event0\",\n"
75 + "\"TestMatchCase\": " + nextMatchCase + ",\n" + "\"TestTimestamp\": " + System.currentTimeMillis()
76 + ",\n" + "\"TestTemperature\": 9080.866\n" + "}";
78 getMessagesReceived++;
80 return Response.status(200).entity(eventString).build();
84 * Service get empty event.
86 * @return the response
88 @Path("/event/GetEmptyEvent")
90 public Response serviceGetEmptyEvent() {
91 return Response.status(200).build();
95 * Service get event bad response.
97 * @return the response
99 @Path("/event/GetEventBadResponse")
101 public Response serviceGetEventBadResponse() {
102 return Response.status(400).build();
106 * Service post request.
108 * @param jsonString the json string
109 * @return the response
111 @Path("/event/PostEvent")
113 public Response servicePostRequest(final String jsonString) {
114 postMessagesReceived++;
116 @SuppressWarnings("unchecked")
117 final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
118 assertTrue(jsonMap.containsKey("name"));
119 assertEquals("0.0.1", jsonMap.get("version"));
120 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
121 assertEquals("Act", jsonMap.get("source"));
122 assertEquals("Outside", jsonMap.get("target"));
124 return Response.status(200).entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
125 + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
129 * Service post request bad response.
131 * @param jsonString the json string
132 * @return the response
134 @Path("/event/PostEventBadResponse")
136 public Response servicePostRequestBadResponse(final String jsonString) {
137 return Response.status(400).build();
141 * Service put request.
143 * @param jsonString the json string
144 * @return the response
146 @Path("/event/PutEvent")
148 public Response servicePutRequest(final String jsonString) {
149 putMessagesReceived++;
151 @SuppressWarnings("unchecked")
152 final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
153 assertTrue(jsonMap.containsKey("name"));
154 assertEquals("0.0.1", jsonMap.get("version"));
155 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
156 assertEquals("Act", jsonMap.get("source"));
157 assertEquals("Outside", jsonMap.get("target"));
159 return Response.status(200).entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
160 + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
164 * Service put request codeFilter Set.
166 * @return the response
168 @Path("/event/CodeFilterSet")
170 public Response serviceCodeFilterSet() {
171 putMessagesReceived++;
172 tagUrlToProperUrl = 1;
173 return Response.status(200)
174 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
175 + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "PostProperUrl"
176 + tagUrlToProperUrl + "}")
181 * Service put request codeFilter Default 200.
183 * @return the response
185 @Path("/event/CodeFilterDefault")
187 public Response serviceCodeFilterDefault() {
188 putMessagesReceived++;
189 tagUrlToProperUrl = 2;
190 return Response.status(200)
191 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
192 + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "PostProperUrl"
193 + tagUrlToProperUrl + "}")
198 * Service put request codeFilter Set.
200 * @return the response
202 @Path("/event/CodeFilterSet/3")
204 public Response serviceCodeFilterSetForMultiTag() {
205 putMessagesReceived++;
206 tagUrlToProperUrl = 3;
207 return Response.status(200)
208 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
209 + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "PostProperUrl"
210 + tagUrlToProperUrl + "}")
215 * Service get tagged Url request access status.
217 * @return the response
219 @Path("/event/GetProperUrl")
221 public Response serviceGetProperUrl() {
222 statMessagesReceived++;
223 if (tagUrlToProperUrl == 1) {
224 return Response.status(200).entity("{\"PostProperUrl\": " + tagUrlToProperUrl + "}").build();
226 return Response.status(500).entity("{\"PostProperUrl\": " + tagUrlToProperUrl + "}").build();
231 * Service fetch Http Code event.
233 * @return the response
235 @Path("/event/FetchHttpCode")
237 public Response serviceFetchHttpCode() {
238 statMessagesReceived++;
239 return Response.status(500).entity("{\"testToRun\": " + "FetchHttpCode" + "}").build();