2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
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.apex.apps.uservice.test.adapt.restrequestor;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import com.google.gson.Gson;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.core.Response;
38 * The Class TestRestRequestorEndpoint.
41 public class TestRestRequestorEndpoint {
43 private static Object counterLock = new Object();
44 private static int postMessagesReceived = 0;
45 private static int putMessagesReceived = 0;
46 private static int statMessagesReceived = 0;
47 private static int getMessagesReceived = 0;
48 private static int deleteMessagesReceived = 0;
50 private static String EVENT_STRING = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.events\",\n"
51 + "\"name\": \"ResponseEvent\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_"
52 + getMessagesReceived + "\",\n" + "\"target\": \"apex\",\n" + "\"intPar\": 9080\n" + "}";
57 public static void resetCounters() {
58 postMessagesReceived = 0;
59 putMessagesReceived = 0;
60 statMessagesReceived = 0;
61 getMessagesReceived = 0;
62 deleteMessagesReceived = 0;
68 * @return the response
72 public Response serviceGetStats() {
73 synchronized (counterLock) {
74 statMessagesReceived++;
76 return Response.status(200)
77 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
78 + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived
79 + ",\"DELETE\": " + deleteMessagesReceived + "}")
86 * @return the response
88 @Path("/event/GetEvent")
90 public Response serviceGetEvent() {
91 synchronized (counterLock) {
92 getMessagesReceived++;
95 return Response.status(200).entity(EVENT_STRING).build();
99 * Service get empty event.
101 * @return the response
103 @Path("/event/GetEmptyEvent")
105 public Response serviceGetEmptyEvent() {
106 return Response.status(200).build();
110 * Service get event bad response.
112 * @return the response
114 @Path("/event/GetEventBadResponse")
116 public Response serviceGetEventBadResponse() {
117 return Response.status(400).build();
121 * Service post request.
123 * @param jsonString the json string
124 * @return the response
126 @Path("/event/PostEvent")
128 public Response servicePostRequest(final String jsonString) {
129 synchronized (counterLock) {
130 postMessagesReceived++;
133 @SuppressWarnings("unchecked")
134 final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
135 assertTrue(jsonMap.containsKey("name"));
136 assertEquals("0.0.1", jsonMap.get("version"));
137 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
138 assertEquals("Act", jsonMap.get("source"));
139 assertEquals("Outside", jsonMap.get("target"));
141 return Response.status(200).entity(EVENT_STRING).build();
145 * Service post request bad response.
147 * @param jsonString the json string
148 * @return the response
150 @Path("/event/PostEventBadResponse")
152 public Response servicePostRequestBadResponse(final String jsonString) {
153 return Response.status(400).build();
157 * Service put request.
159 * @param jsonString the json string
160 * @return the response
162 @Path("/event/PutEvent")
164 public Response servicePutRequest(final String jsonString) {
165 synchronized (counterLock) {
166 putMessagesReceived++;
169 @SuppressWarnings("unchecked")
170 final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
171 assertTrue(jsonMap.containsKey("name"));
172 assertEquals("0.0.1", jsonMap.get("version"));
173 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
174 assertEquals("Act", jsonMap.get("source"));
175 assertEquals("Outside", jsonMap.get("target"));
177 return Response.status(200).entity(EVENT_STRING).build();
181 * Service delete request.
183 * @param jsonString the json string
184 * @return the response
186 @Path("/event/DeleteEvent")
188 public Response serviceDeleteRequest(final String jsonString) {
189 synchronized (counterLock) {
190 deleteMessagesReceived++;
193 return Response.status(200).entity(EVENT_STRING).build();
197 * Service delete request bad response.
199 * @param jsonString the json string
200 * @return the response
202 @Path("/event/DeleteEventBadResponse")
204 public Response serviceDeleteRequestBadResponse(final String jsonString) {
205 return Response.status(400).build();