fb59bad2ae62ff4f09d0417b86a5a47351e412f0
[policy/apex-pdp.git] / plugins / plugins-event / plugins-event-carrier / plugins-event-carrier-restrequestor / src / test / java / org / onap / policy / apex / plugins / event / carrier / restrequestor / SupportRestRequestorEndpoint.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.event.carrier.restrequestor;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import com.google.gson.Gson;
28
29 import java.util.Map;
30
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;
37
38 /**
39  * The Class TestRestRequestorEndpoint.
40  */
41 @Path("/apex")
42 public class SupportRestRequestorEndpoint {
43
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;
50
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" + "}";
54
55     /**
56      * Reset counters.
57      */
58     public static void resetCounters() {
59         postMessagesReceived = 0;
60         putMessagesReceived = 0;
61         statMessagesReceived = 0;
62         getMessagesReceived = 0;
63         deleteMessagesReceived = 0;
64     }
65
66     /**
67      * Service get stats.
68      *
69      * @return the response
70      */
71     @Path("/event/Stats")
72     @GET
73     public Response serviceGetStats() {
74         synchronized (counterLock) {
75             statMessagesReceived++;
76         }
77         return Response.status(200)
78                 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
79                         + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + ",\"DELETE\": "
80                         + deleteMessagesReceived + "}")
81                 .build();
82     }
83
84     /**
85      * Service get event.
86      *
87      * @return the response
88      */
89     @Path("/event/GetEvent")
90     @GET
91     public Response serviceGetEvent() {
92         synchronized (counterLock) {
93             getMessagesReceived++;
94         }
95
96         return Response.status(200).entity(EVENT_STRING).build();
97     }
98
99     /**
100      * Service get empty event.
101      *
102      * @return the response
103      */
104     @Path("/event/GetEmptyEvent")
105     @GET
106     public Response serviceGetEmptyEvent() {
107         return Response.status(200).build();
108     }
109
110     /**
111      * Service get event bad response.
112      *
113      * @return the response
114      */
115     @Path("/event/GetEventBadResponse")
116     @GET
117     public Response serviceGetEventBadResponse() {
118         return Response.status(400).build();
119     }
120
121     /**
122      * Service post request.
123      *
124      * @param jsonString the json string
125      * @return the response
126      */
127     @Path("/event/PostEvent")
128     @POST
129     public Response servicePostRequest(final String jsonString) {
130         synchronized (counterLock) {
131             postMessagesReceived++;
132         }
133
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"));
141
142         return Response.status(200).entity(EVENT_STRING).build();
143     }
144
145     /**
146      * Service post request bad response.
147      *
148      * @param jsonString the json string
149      * @return the response
150      */
151     @Path("/event/PostEventBadResponse")
152     @POST
153     public Response servicePostRequestBadResponse(final String jsonString) {
154         return Response.status(400).build();
155     }
156
157     /**
158      * Service put request.
159      *
160      * @param jsonString the json string
161      * @return the response
162      */
163     @Path("/event/PutEvent")
164     @PUT
165     public Response servicePutRequest(final String jsonString) {
166         synchronized (counterLock) {
167             putMessagesReceived++;
168         }
169
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"));
177
178         return Response.status(200).entity(EVENT_STRING).build();
179     }
180
181     /**
182      * Service delete request.
183      *
184      * @param jsonString the json string
185      * @return the response
186      */
187     @Path("/event/DeleteEvent")
188     @DELETE
189     public Response serviceDeleteRequest(final String jsonString) {
190         synchronized (counterLock) {
191             deleteMessagesReceived++;
192         }
193
194         return Response.status(200).entity(EVENT_STRING).build();
195     }
196
197     /**
198      * Service delete request bad response.
199      *
200      * @param jsonString the json string
201      * @return the response
202      */
203     @Path("/event/DeleteEventBadResponse")
204     @DELETE
205     public Response serviceDeleteRequestBadResponse(final String jsonString) {
206         return Response.status(400).build();
207     }
208 }