d9569b1ed59249666fa0afedee36d8d8453a9226
[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 @Path("/apex")
38 public class TestRESTREequestorEndpoint {
39
40     private static Object counterLock = new Object();
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 deleteMessagesReceived = 0;
46
47     private static String EVENT_STRING = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"
48             + "\"name\": \"Event0100\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + getMessagesReceived
49             + "\",\n" + "\"target\": \"apex\",\n" + "\"TestSlogan\": \"Test slogan for External Event0\",\n"
50             + "\"TestMatchCase\": 2,\n" + "\"TestTimestamp\": " + System.currentTimeMillis() + ",\n"
51             + "\"TestTemperature\": 9080.866\n" + "}";
52
53     public static void resetCounters() {
54         postMessagesReceived = 0;
55         putMessagesReceived = 0;
56         statMessagesReceived = 0;
57         getMessagesReceived = 0;
58         deleteMessagesReceived = 0;
59     }
60
61     @Path("/event/Stats")
62     @GET
63     public Response serviceGetStats() {
64         synchronized (counterLock) {
65             statMessagesReceived++;
66         }
67         return Response.status(200)
68                 .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
69                         + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + ",\"DELETE\": "
70                         + deleteMessagesReceived + "}")
71                 .build();
72     }
73
74     @Path("/event/GetEvent")
75     @GET
76     public Response serviceGetEvent() {
77         synchronized (counterLock) {
78             getMessagesReceived++;
79         }
80
81         return Response.status(200).entity(EVENT_STRING).build();
82     }
83
84     @Path("/event/GetEmptyEvent")
85     @GET
86     public Response serviceGetEmptyEvent() {
87         return Response.status(200).build();
88     }
89
90     @Path("/event/GetEventBadResponse")
91     @GET
92     public Response serviceGetEventBadResponse() {
93         return Response.status(400).build();
94     }
95
96     @Path("/event/PostEvent")
97     @POST
98     public Response servicePostRequest(final String jsonString) {
99         synchronized (counterLock) {
100             postMessagesReceived++;
101         }
102
103         @SuppressWarnings("unchecked")
104         final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
105         assertTrue(jsonMap.containsKey("name"));
106         assertEquals("0.0.1", jsonMap.get("version"));
107         assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
108         assertEquals("Act", jsonMap.get("source"));
109         assertEquals("Outside", jsonMap.get("target"));
110
111         return Response.status(200).entity(EVENT_STRING).build();
112     }
113
114     @Path("/event/PostEventBadResponse")
115     @POST
116     public Response servicePostRequestBadResponse(final String jsonString) {
117         return Response.status(400).build();
118     }
119
120     @Path("/event/PutEvent")
121     @PUT
122     public Response servicePutRequest(final String jsonString) {
123         synchronized (counterLock) {
124             putMessagesReceived++;
125         }
126
127         @SuppressWarnings("unchecked")
128         final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
129         assertTrue(jsonMap.containsKey("name"));
130         assertEquals("0.0.1", jsonMap.get("version"));
131         assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
132         assertEquals("Act", jsonMap.get("source"));
133         assertEquals("Outside", jsonMap.get("target"));
134
135         return Response.status(200).entity(EVENT_STRING).build();
136     }
137
138     @Path("/event/DeleteEvent")
139     @DELETE
140     public Response serviceDeleteRequest(final String jsonString) {
141         synchronized (counterLock) {
142             deleteMessagesReceived++;
143         }
144
145         return Response.status(200).entity(EVENT_STRING).build();
146     }
147
148     @Path("/event/DeleteEventBadResponse")
149     @DELETE
150     public Response serviceDeleteRequestBadResponse(final String jsonString) {
151         return Response.status(400).build();
152     }
153 }