2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 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.plugins.event.carrier.restrequestor;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 import com.google.gson.Gson;
31 import javax.ws.rs.DELETE;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.PUT;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.core.Response;
39 * The Class TestRestRequestorEndpoint.
42 public class SupportRestRequestorEndpoint {
44 private static Object counterLock = new Object();
45 private static int postMessagesReceived = 0;
46 private static int putMessagesReceived = 0;
47 private static int statMessagesReceived = 0;
48 private static int getMessagesReceived = 0;
49 private static int deleteMessagesReceived = 0;
51 private static String EVENT_STRING = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.events\",\n"
52 + "\"name\": \"ResponseEvent\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_"
53 + getMessagesReceived + "\",\n" + "\"target\": \"apex\",\n" + "\"intPar\": 9080\n" + "}";
58 public static void resetCounters() {
59 postMessagesReceived = 0;
60 putMessagesReceived = 0;
61 statMessagesReceived = 0;
62 getMessagesReceived = 0;
63 deleteMessagesReceived = 0;
69 * @return the response
73 public Response serviceGetStats() {
74 synchronized (counterLock) {
75 statMessagesReceived++;
77 return Response.status(200)
78 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
79 + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + ",\"DELETE\": "
80 + deleteMessagesReceived + "}")
87 * @return the response
89 @Path("/event/GetEvent")
91 public Response serviceGetEvent() {
92 synchronized (counterLock) {
93 getMessagesReceived++;
96 return Response.status(200).entity(EVENT_STRING).build();
100 * Service get empty event.
102 * @return the response
104 @Path("/event/GetEmptyEvent")
106 public Response serviceGetEmptyEvent() {
107 return Response.status(200).build();
111 * Service get event bad response.
113 * @return the response
115 @Path("/event/GetEventBadResponse")
117 public Response serviceGetEventBadResponse() {
118 return Response.status(400).build();
122 * Service post request.
124 * @param jsonString the json string
125 * @return the response
127 @Path("/event/PostEvent")
129 public Response servicePostRequest(final String jsonString) {
130 synchronized (counterLock) {
131 postMessagesReceived++;
134 @SuppressWarnings("unchecked")
135 final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
136 assertTrue(jsonMap.containsKey("name"));
137 assertEquals("0.0.1", jsonMap.get("version"));
138 assertEquals("org.onap.policy.apex.events", jsonMap.get("nameSpace"));
139 assertEquals("apex", jsonMap.get("source"));
140 assertEquals("server", jsonMap.get("target"));
142 return Response.status(200).entity(EVENT_STRING).build();
146 * Service post request bad response.
148 * @param jsonString the json string
149 * @return the response
151 @Path("/event/PostEventBadResponse")
153 public Response servicePostRequestBadResponse(final String jsonString) {
154 return Response.status(400).build();
158 * Service put request.
160 * @param jsonString the json string
161 * @return the response
163 @Path("/event/PutEvent")
165 public Response servicePutRequest(final String jsonString) {
166 synchronized (counterLock) {
167 putMessagesReceived++;
170 @SuppressWarnings("unchecked")
171 final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
172 assertTrue(jsonMap.containsKey("name"));
173 assertEquals("0.0.1", jsonMap.get("version"));
174 assertEquals("org.onap.policy.apex.events", jsonMap.get("nameSpace"));
175 assertEquals("apex", jsonMap.get("source"));
176 assertEquals("server", jsonMap.get("target"));
178 return Response.status(200).entity(EVENT_STRING).build();
182 * Service delete request.
184 * @param jsonString the json string
185 * @return the response
187 @Path("/event/DeleteEvent")
189 public Response serviceDeleteRequest(final String jsonString) {
190 synchronized (counterLock) {
191 deleteMessagesReceived++;
194 return Response.status(200).entity(EVENT_STRING).build();
198 * Service delete request bad response.
200 * @param jsonString the json string
201 * @return the response
203 @Path("/event/DeleteEventBadResponse")
205 public Response serviceDeleteRequestBadResponse(final String jsonString) {
206 return Response.status(400).build();