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