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;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.core.Response;
37 * The Class TestRestRequestorEndpoint.
40 public class SupportRestRequestorEndpoint {
42 private static Object counterLock = new Object();
43 private static int postMessagesReceived = 0;
44 private static int putMessagesReceived = 0;
45 private static int statMessagesReceived = 0;
46 private static int getMessagesReceived = 0;
47 private static int deleteMessagesReceived = 0;
49 private static String EVENT_STRING = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.events\",\n"
50 + "\"name\": \"ResponseEvent\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_"
51 + getMessagesReceived + "\",\n" + "\"target\": \"apex\",\n" + "\"intPar\": 9080\n" + "}";
56 public static void resetCounters() {
57 postMessagesReceived = 0;
58 putMessagesReceived = 0;
59 statMessagesReceived = 0;
60 getMessagesReceived = 0;
61 deleteMessagesReceived = 0;
67 * @return the response
71 public Response serviceGetStats() {
72 synchronized (counterLock) {
73 statMessagesReceived++;
75 return Response.status(200)
76 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
77 + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + ",\"DELETE\": "
78 + deleteMessagesReceived + "}")
85 * @return the response
87 @Path("/event/GetEvent")
89 public Response serviceGetEvent() {
90 synchronized (counterLock) {
91 getMessagesReceived++;
94 return Response.status(200).entity(EVENT_STRING).build();
98 * Service get empty event.
100 * @return the response
102 @Path("/event/GetEmptyEvent")
104 public Response serviceGetEmptyEvent() {
105 return Response.status(200).build();
109 * Service get event bad response.
111 * @return the response
113 @Path("/event/GetEventBadResponse")
115 public Response serviceGetEventBadResponse() {
116 return Response.status(400).build();
120 * Service post request.
122 * @param jsonString the json string
123 * @return the response
125 @Path("/event/PostEvent")
127 public Response servicePostRequest(final String jsonString) {
128 synchronized (counterLock) {
129 postMessagesReceived++;
132 @SuppressWarnings("unchecked")
133 final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
134 assertTrue(jsonMap.containsKey("name"));
135 assertEquals("0.0.1", jsonMap.get("version"));
136 assertEquals("org.onap.policy.apex.events", jsonMap.get("nameSpace"));
137 assertEquals("apex", jsonMap.get("source"));
138 assertEquals("server", jsonMap.get("target"));
140 return Response.status(200).entity(EVENT_STRING).build();
144 * Service post request bad response.
146 * @param jsonString the json string
147 * @return the response
149 @Path("/event/PostEventBadResponse")
151 public Response servicePostRequestBadResponse(final String jsonString) {
152 return Response.status(400).build();
156 * Service put request.
158 * @param jsonString the json string
159 * @return the response
161 @Path("/event/PutEvent")
163 public Response servicePutRequest(final String jsonString) {
164 synchronized (counterLock) {
165 putMessagesReceived++;
168 @SuppressWarnings("unchecked")
169 final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
170 assertTrue(jsonMap.containsKey("name"));
171 assertEquals("0.0.1", jsonMap.get("version"));
172 assertEquals("org.onap.policy.apex.events", jsonMap.get("nameSpace"));
173 assertEquals("apex", jsonMap.get("source"));
174 assertEquals("server", jsonMap.get("target"));
176 return Response.status(200).entity(EVENT_STRING).build();
180 * Service delete request.
182 * @param jsonString the json string
183 * @return the response
185 @Path("/event/DeleteEvent")
187 public Response serviceDeleteRequest(final String jsonString) {
188 synchronized (counterLock) {
189 deleteMessagesReceived++;
192 return Response.status(200).entity(EVENT_STRING).build();
196 * Service delete request bad response.
198 * @param jsonString the json string
199 * @return the response
201 @Path("/event/DeleteEventBadResponse")
203 public Response serviceDeleteRequestBadResponse(final String jsonString) {
204 return Response.status(400).build();