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