f82add13f04a64012fe5fb0dd829be7035d573a9
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020, 2023-2024 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 jakarta.ws.rs.DELETE;
29 import jakarta.ws.rs.GET;
30 import jakarta.ws.rs.POST;
31 import jakarta.ws.rs.PUT;
32 import jakarta.ws.rs.Path;
33 import jakarta.ws.rs.core.Response;
34 import java.util.Map;
35
36 /**
37  * The Class TestRestRequestorEndpoint.
38  */
39 @Path("/apex")
40 public class SupportRestRequestorEndpoint {
41
42     private static final 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 final 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") final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
133         assertTrue(jsonMap.containsKey("name"));
134         assertEquals("0.0.1", jsonMap.get("version"));
135         assertEquals("org.onap.policy.apex.events", jsonMap.get("nameSpace"));
136         assertEquals("apex", jsonMap.get("source"));
137         assertEquals("server", jsonMap.get("target"));
138
139         return Response.status(200).entity(EVENT_STRING).build();
140     }
141
142     /**
143      * Service post request bad response.
144      *
145      * @param jsonString the json string
146      * @return the response
147      */
148     @Path("/event/PostEventBadResponse")
149     @POST
150     public Response servicePostRequestBadResponse(final String jsonString) {
151         return Response.status(400).build();
152     }
153
154     /**
155      * Service put request.
156      *
157      * @param jsonString the json string
158      * @return the response
159      */
160     @Path("/event/PutEvent")
161     @PUT
162     public Response servicePutRequest(final String jsonString) {
163         synchronized (counterLock) {
164             putMessagesReceived++;
165         }
166
167         @SuppressWarnings("unchecked") final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
168         assertTrue(jsonMap.containsKey("name"));
169         assertEquals("0.0.1", jsonMap.get("version"));
170         assertEquals("org.onap.policy.apex.events", jsonMap.get("nameSpace"));
171         assertEquals("apex", jsonMap.get("source"));
172         assertEquals("server", jsonMap.get("target"));
173
174         return Response.status(200).entity(EVENT_STRING).build();
175     }
176
177     /**
178      * Service delete request.
179      *
180      * @param jsonString the json string
181      * @return the response
182      */
183     @Path("/event/DeleteEvent")
184     @DELETE
185     public Response serviceDeleteRequest(final String jsonString) {
186         synchronized (counterLock) {
187             deleteMessagesReceived++;
188         }
189
190         return Response.status(200).entity(EVENT_STRING).build();
191     }
192
193     /**
194      * Service delete request bad response.
195      *
196      * @param jsonString the json string
197      * @return the response
198      */
199     @Path("/event/DeleteEventBadResponse")
200     @DELETE
201     public Response serviceDeleteRequestBadResponse(final String jsonString) {
202         return Response.status(400).build();
203     }
204 }