3895455420ad21bfa50fc374b4667f56f0dc130a
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Copyright (C) 2019, 2023 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.testsuites.integration.uservice.adapt.restclient;
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.GET;
29 import jakarta.ws.rs.POST;
30 import jakarta.ws.rs.PUT;
31 import jakarta.ws.rs.Path;
32 import jakarta.ws.rs.core.Response;
33 import java.util.Map;
34 import java.util.Random;
35
36 /**
37  * The Class TestRestClientEndpoint.
38  */
39 @Path("/apex")
40 public class TestRestClientEndpoint {
41     private static int postMessagesReceived = 0;
42     private static int putMessagesReceived = 0;
43     private static int statMessagesReceived = 0;
44     private static int getMessagesReceived = 0;
45     private static int tagUrlToProperUrl = 0;
46
47     /**
48      * Service get stats.
49      *
50      * @return the response
51      */
52     @Path("/event/Stats")
53     @GET
54     public Response serviceGetStats() {
55         statMessagesReceived++;
56         return Response.status(200).entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
57                 + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
58     }
59
60     /**
61      * Service get event.
62      *
63      * @return the response
64      */
65     @Path("/event/GetEvent")
66     @GET
67     public Response serviceGetEvent() {
68         final Random rand = new Random();
69         final int nextMatchCase = rand.nextInt(4);
70         final String nextEventName = "Event0" + rand.nextInt(2) + "00";
71
72         final String eventString = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.sample.events\",\n" + "\"name\": \""
73                 + nextEventName + "\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + getMessagesReceived
74                 + "\",\n" + "\"target\": \"apex\",\n" + "\"TestSlogan\": \"Test slogan for External Event0\",\n"
75                 + "\"TestMatchCase\": " + nextMatchCase + ",\n" + "\"TestTimestamp\": " + System.currentTimeMillis()
76                 + ",\n" + "\"TestTemperature\": 9080.866\n" + "}";
77
78         getMessagesReceived++;
79
80         return Response.status(200).entity(eventString).build();
81     }
82
83     /**
84      * Service get empty event.
85      *
86      * @return the response
87      */
88     @Path("/event/GetEmptyEvent")
89     @GET
90     public Response serviceGetEmptyEvent() {
91         return Response.status(200).build();
92     }
93
94     /**
95      * Service get event bad response.
96      *
97      * @return the response
98      */
99     @Path("/event/GetEventBadResponse")
100     @GET
101     public Response serviceGetEventBadResponse() {
102         return Response.status(400).build();
103     }
104
105     /**
106      * Service post request.
107      *
108      * @param jsonString the json string
109      * @return the response
110      */
111     @Path("/event/PostEvent")
112     @POST
113     public Response servicePostRequest(final String jsonString) {
114         postMessagesReceived++;
115
116         @SuppressWarnings("unchecked")
117         final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
118         assertTrue(jsonMap.containsKey("name"));
119         assertEquals("0.0.1", jsonMap.get("version"));
120         assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
121         assertEquals("Act", jsonMap.get("source"));
122         assertEquals("Outside", jsonMap.get("target"));
123
124         return Response.status(200).entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
125                 + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
126     }
127
128     /**
129      * Service post request bad response.
130      *
131      * @param jsonString the json string
132      * @return the response
133      */
134     @Path("/event/PostEventBadResponse")
135     @POST
136     public Response servicePostRequestBadResponse(final String jsonString) {
137         return Response.status(400).build();
138     }
139
140     /**
141      * Service put request.
142      *
143      * @param jsonString the json string
144      * @return the response
145      */
146     @Path("/event/PutEvent")
147     @PUT
148     public Response servicePutRequest(final String jsonString) {
149         putMessagesReceived++;
150
151         @SuppressWarnings("unchecked")
152         final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
153         assertTrue(jsonMap.containsKey("name"));
154         assertEquals("0.0.1", jsonMap.get("version"));
155         assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
156         assertEquals("Act", jsonMap.get("source"));
157         assertEquals("Outside", jsonMap.get("target"));
158
159         return Response.status(200).entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
160                 + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
161     }
162
163     /**
164      * Service put request codeFilter Set.
165      *
166      * @return the response
167      */
168     @Path("/event/CodeFilterSet")
169     @PUT
170     public Response serviceCodeFilterSet() {
171         putMessagesReceived++;
172         tagUrlToProperUrl = 1;
173         return Response.status(200)
174                 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
175                         + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "PostProperUrl"
176                         + tagUrlToProperUrl + "}")
177                 .build();
178     }
179
180     /**
181      * Service put request codeFilter Default 200.
182      *
183      * @return the response
184      */
185     @Path("/event/CodeFilterDefault")
186     @PUT
187     public Response serviceCodeFilterDefault() {
188         putMessagesReceived++;
189         tagUrlToProperUrl = 2;
190         return Response.status(200)
191                 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
192                         + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "PostProperUrl"
193                         + tagUrlToProperUrl + "}")
194                 .build();
195     }
196
197     /**
198      * Service put request codeFilter Set.
199      *
200      * @return the response
201      */
202     @Path("/event/CodeFilterSet/3")
203     @PUT
204     public Response serviceCodeFilterSetForMultiTag() {
205         putMessagesReceived++;
206         tagUrlToProperUrl = 3;
207         return Response.status(200)
208                 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
209                         + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "PostProperUrl"
210                         + tagUrlToProperUrl + "}")
211                 .build();
212     }
213
214     /**
215      * Service get tagged Url request access status.
216      *
217      * @return the response
218      */
219     @Path("/event/GetProperUrl")
220     @GET
221     public Response serviceGetProperUrl() {
222         statMessagesReceived++;
223         if (tagUrlToProperUrl == 1) {
224             return Response.status(200).entity("{\"PostProperUrl\": " + tagUrlToProperUrl + "}").build();
225         } else {
226             return Response.status(500).entity("{\"PostProperUrl\": " + tagUrlToProperUrl + "}").build();
227         }
228     }
229
230     /**
231      * Service fetch Http Code event.
232      *
233      * @return the response
234      */
235     @Path("/event/FetchHttpCode")
236     @GET
237     public Response serviceFetchHttpCode() {
238         statMessagesReceived++;
239         return Response.status(500).entity("{\"testToRun\": " + "FetchHttpCode" + "}").build();
240     }
241 }