Changes for checkstyle 8.32
[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 import java.util.Map;
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;
35
36 /**
37  * The Class TestRestRequestorEndpoint.
38  */
39 @Path("/apex")
40 public class SupportRestRequestorEndpoint {
41
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;
48
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" + "}";
52
53     /**
54      * Reset counters.
55      */
56     public static void resetCounters() {
57         postMessagesReceived = 0;
58         putMessagesReceived = 0;
59         statMessagesReceived = 0;
60         getMessagesReceived = 0;
61         deleteMessagesReceived = 0;
62     }
63
64     /**
65      * Service get stats.
66      *
67      * @return the response
68      */
69     @Path("/event/Stats")
70     @GET
71     public Response serviceGetStats() {
72         synchronized (counterLock) {
73             statMessagesReceived++;
74         }
75         return Response.status(200)
76                 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
77                         + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + ",\"DELETE\": "
78                         + deleteMessagesReceived + "}")
79                 .build();
80     }
81
82     /**
83      * Service get event.
84      *
85      * @return the response
86      */
87     @Path("/event/GetEvent")
88     @GET
89     public Response serviceGetEvent() {
90         synchronized (counterLock) {
91             getMessagesReceived++;
92         }
93
94         return Response.status(200).entity(EVENT_STRING).build();
95     }
96
97     /**
98      * Service get empty event.
99      *
100      * @return the response
101      */
102     @Path("/event/GetEmptyEvent")
103     @GET
104     public Response serviceGetEmptyEvent() {
105         return Response.status(200).build();
106     }
107
108     /**
109      * Service get event bad response.
110      *
111      * @return the response
112      */
113     @Path("/event/GetEventBadResponse")
114     @GET
115     public Response serviceGetEventBadResponse() {
116         return Response.status(400).build();
117     }
118
119     /**
120      * Service post request.
121      *
122      * @param jsonString the json string
123      * @return the response
124      */
125     @Path("/event/PostEvent")
126     @POST
127     public Response servicePostRequest(final String jsonString) {
128         synchronized (counterLock) {
129             postMessagesReceived++;
130         }
131
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"));
139
140         return Response.status(200).entity(EVENT_STRING).build();
141     }
142
143     /**
144      * Service post request bad response.
145      *
146      * @param jsonString the json string
147      * @return the response
148      */
149     @Path("/event/PostEventBadResponse")
150     @POST
151     public Response servicePostRequestBadResponse(final String jsonString) {
152         return Response.status(400).build();
153     }
154
155     /**
156      * Service put request.
157      *
158      * @param jsonString the json string
159      * @return the response
160      */
161     @Path("/event/PutEvent")
162     @PUT
163     public Response servicePutRequest(final String jsonString) {
164         synchronized (counterLock) {
165             putMessagesReceived++;
166         }
167
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"));
175
176         return Response.status(200).entity(EVENT_STRING).build();
177     }
178
179     /**
180      * Service delete request.
181      *
182      * @param jsonString the json string
183      * @return the response
184      */
185     @Path("/event/DeleteEvent")
186     @DELETE
187     public Response serviceDeleteRequest(final String jsonString) {
188         synchronized (counterLock) {
189             deleteMessagesReceived++;
190         }
191
192         return Response.status(200).entity(EVENT_STRING).build();
193     }
194
195     /**
196      * Service delete request bad response.
197      *
198      * @param jsonString the json string
199      * @return the response
200      */
201     @Path("/event/DeleteEventBadResponse")
202     @DELETE
203     public Response serviceDeleteRequestBadResponse(final String jsonString) {
204         return Response.status(400).build();
205     }
206 }