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