79634ebcb1928d8b31de7a34f1f864b0e8dde201
[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.restclient;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.Map;
27 import java.util.Random;
28
29 import javax.ws.rs.GET;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.PUT;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.core.Response;
34
35 import com.google.gson.Gson;
36
37
38 @Path("/apex")
39 public class TestRESTClientEndpoint {
40
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
46     @Path("/event/Stats")
47     @GET
48     public Response serviceGetStats() {
49         statMessagesReceived++;
50         return Response.status(200).entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
51                 + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
52     }
53
54     @Path("/event/GetEvent")
55     @GET
56     public Response serviceGetEvent() {
57         final Random rand = new Random();
58         final int nextMatchCase = rand.nextInt(4);
59         final String nextEventName = "Event0" + rand.nextInt(2) + "00";
60
61         final String eventString = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.sample.events\",\n" + "\"name\": \""
62                 + nextEventName + "\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + getMessagesReceived
63                 + "\",\n" + "\"target\": \"apex\",\n" + "\"TestSlogan\": \"Test slogan for External Event0\",\n"
64                 + "\"TestMatchCase\": " + nextMatchCase + ",\n" + "\"TestTimestamp\": " + System.currentTimeMillis()
65                 + ",\n" + "\"TestTemperature\": 9080.866\n" + "}";
66
67         getMessagesReceived++;
68
69         return Response.status(200).entity(eventString).build();
70     }
71
72     @Path("/event/GetEmptyEvent")
73     @GET
74     public Response serviceGetEmptyEvent() {
75         return Response.status(200).build();
76     }
77
78     @Path("/event/GetEventBadResponse")
79     @GET
80     public Response serviceGetEventBadResponse() {
81         return Response.status(400).build();
82     }
83
84     @Path("/event/PostEvent")
85     @POST
86     public Response servicePostRequest(final String jsonString) {
87         postMessagesReceived++;
88
89         @SuppressWarnings("unchecked")
90         final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
91         assertTrue(jsonMap.containsKey("name"));
92         assertEquals("0.0.1", jsonMap.get("version"));
93         assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
94         assertEquals("Act", jsonMap.get("source"));
95         assertEquals("Outside", jsonMap.get("target"));
96
97         return Response.status(200).entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
98                 + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
99     }
100
101     @Path("/event/PostEventBadResponse")
102     @POST
103     public Response servicePostRequestBadResponse(final String jsonString) {
104         return Response.status(400).build();
105     }
106
107     @Path("/event/PutEvent")
108     @PUT
109     public Response servicePutRequest(final String jsonString) {
110         putMessagesReceived++;
111
112         @SuppressWarnings("unchecked")
113         final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
114         assertTrue(jsonMap.containsKey("name"));
115         assertEquals("0.0.1", jsonMap.get("version"));
116         assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
117         assertEquals("Act", jsonMap.get("source"));
118         assertEquals("Outside", jsonMap.get("target"));
119
120         return Response.status(200).entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
121                 + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
122     }
123 }