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 public class TestRESTREequestorEndpoint {
40 private static Object counterLock = new Object();
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 deleteMessagesReceived = 0;
47 private static String EVENT_STRING = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"
48 + "\"name\": \"Event0100\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + getMessagesReceived
49 + "\",\n" + "\"target\": \"apex\",\n" + "\"TestSlogan\": \"Test slogan for External Event0\",\n"
50 + "\"TestMatchCase\": 2,\n" + "\"TestTimestamp\": " + System.currentTimeMillis() + ",\n"
51 + "\"TestTemperature\": 9080.866\n" + "}";
53 public static void resetCounters() {
54 postMessagesReceived = 0;
55 putMessagesReceived = 0;
56 statMessagesReceived = 0;
57 getMessagesReceived = 0;
58 deleteMessagesReceived = 0;
63 public Response serviceGetStats() {
64 synchronized (counterLock) {
65 statMessagesReceived++;
67 return Response.status(200)
68 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
69 + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + ",\"DELETE\": "
70 + deleteMessagesReceived + "}")
74 @Path("/event/GetEvent")
76 public Response serviceGetEvent() {
77 synchronized (counterLock) {
78 getMessagesReceived++;
81 return Response.status(200).entity(EVENT_STRING).build();
84 @Path("/event/GetEmptyEvent")
86 public Response serviceGetEmptyEvent() {
87 return Response.status(200).build();
90 @Path("/event/GetEventBadResponse")
92 public Response serviceGetEventBadResponse() {
93 return Response.status(400).build();
96 @Path("/event/PostEvent")
98 public Response servicePostRequest(final String jsonString) {
99 synchronized (counterLock) {
100 postMessagesReceived++;
103 @SuppressWarnings("unchecked")
104 final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
105 assertTrue(jsonMap.containsKey("name"));
106 assertEquals("0.0.1", jsonMap.get("version"));
107 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
108 assertEquals("Act", jsonMap.get("source"));
109 assertEquals("Outside", jsonMap.get("target"));
111 return Response.status(200).entity(EVENT_STRING).build();
114 @Path("/event/PostEventBadResponse")
116 public Response servicePostRequestBadResponse(final String jsonString) {
117 return Response.status(400).build();
120 @Path("/event/PutEvent")
122 public Response servicePutRequest(final String jsonString) {
123 synchronized (counterLock) {
124 putMessagesReceived++;
127 @SuppressWarnings("unchecked")
128 final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
129 assertTrue(jsonMap.containsKey("name"));
130 assertEquals("0.0.1", jsonMap.get("version"));
131 assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
132 assertEquals("Act", jsonMap.get("source"));
133 assertEquals("Outside", jsonMap.get("target"));
135 return Response.status(200).entity(EVENT_STRING).build();
138 @Path("/event/DeleteEvent")
140 public Response serviceDeleteRequest(final String jsonString) {
141 synchronized (counterLock) {
142 deleteMessagesReceived++;
145 return Response.status(200).entity(EVENT_STRING).build();
148 @Path("/event/DeleteEventBadResponse")
150 public Response serviceDeleteRequestBadResponse(final String jsonString) {
151 return Response.status(400).build();