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.sample.events\",\n"
51 + "\"name\": \"Event0100\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + getMessagesReceived
52 + "\",\n" + "\"target\": \"apex\",\n" + "\"TestSlogan\": \"Test slogan for External Event0\",\n"
53 + "\"TestMatchCase\": 2,\n" + "\"TestTimestamp\": " + System.currentTimeMillis() + ",\n"
54 + "\"TestTemperature\": 9080.866\n" + "}";
59 public static void resetCounters() {
60 postMessagesReceived = 0;
61 putMessagesReceived = 0;
62 statMessagesReceived = 0;
63 getMessagesReceived = 0;
64 deleteMessagesReceived = 0;
70 * @return the response
74 public Response serviceGetStats() {
75 synchronized (counterLock) {
76 statMessagesReceived++;
78 return Response.status(200)
79 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
80 + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + ",\"DELETE\": "
81 + deleteMessagesReceived + "}")
88 * @return the response
90 @Path("/event/GetEvent")
92 public Response serviceGetEvent() {
93 synchronized (counterLock) {
94 getMessagesReceived++;
97 return Response.status(200).entity(EVENT_STRING).build();
101 * Service get empty event.
103 * @return the response
105 @Path("/event/GetEmptyEvent")
107 public Response serviceGetEmptyEvent() {
108 return Response.status(200).build();
112 * Service get event bad response.
114 * @return the response
116 @Path("/event/GetEventBadResponse")
118 public Response serviceGetEventBadResponse() {
119 return Response.status(400).build();
123 * Service post request.
125 * @param jsonString the json string
126 * @return the response
128 @Path("/event/PostEvent")
130 public Response servicePostRequest(final String jsonString) {
131 synchronized (counterLock) {
132 postMessagesReceived++;
135 @SuppressWarnings("unchecked")
136 final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
137 assertTrue(jsonMap.containsKey("name"));
138 assertEquals("0.0.1", jsonMap.get("version"));
139 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
140 assertEquals("Act", jsonMap.get("source"));
141 assertEquals("Outside", jsonMap.get("target"));
143 return Response.status(200).entity(EVENT_STRING).build();
147 * Service post request bad response.
149 * @param jsonString the json string
150 * @return the response
152 @Path("/event/PostEventBadResponse")
154 public Response servicePostRequestBadResponse(final String jsonString) {
155 return Response.status(400).build();
159 * Service put request.
161 * @param jsonString the json string
162 * @return the response
164 @Path("/event/PutEvent")
166 public Response servicePutRequest(final String jsonString) {
167 synchronized (counterLock) {
168 putMessagesReceived++;
171 @SuppressWarnings("unchecked")
172 final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
173 assertTrue(jsonMap.containsKey("name"));
174 assertEquals("0.0.1", jsonMap.get("version"));
175 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
176 assertEquals("Act", jsonMap.get("source"));
177 assertEquals("Outside", jsonMap.get("target"));
179 return Response.status(200).entity(EVENT_STRING).build();
183 * Service delete request.
185 * @param jsonString the json string
186 * @return the response
188 @Path("/event/DeleteEvent")
190 public Response serviceDeleteRequest(final String jsonString) {
191 synchronized (counterLock) {
192 deleteMessagesReceived++;
195 return Response.status(200).entity(EVENT_STRING).build();
199 * Service delete request bad response.
201 * @param jsonString the json string
202 * @return the response
204 @Path("/event/DeleteEventBadResponse")
206 public Response serviceDeleteRequestBadResponse(final String jsonString) {
207 return Response.status(400).build();