5b9040f95220e96862ff986968f196347b8b8734
[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.domains.onap.vcpe;
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 import java.util.Random;
30
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 /**
38  * The Class AaiAndGuardSimEndpoint.
39  */
40 @Path("/sim")
41 public class AaiAndGuardSimEndpointTest {
42
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
48     /**
49      * Service get stats.
50      *
51      * @return the response
52      */
53     @Path("/pdp/api/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 guard post request.
63      *
64      * @param jsonString the json string
65      * @return the response
66      */
67     @Path("/pdp/api/getDecision")
68     @POST
69     public Response serviceGuardPostRequest(final String jsonString) {
70         postMessagesReceived++;
71
72         if (postMessagesReceived % 2 == 0) {
73             return Response.status(200).entity("{\"decision\": \"PERMIT\", \"details\": \"Decision Permit. OK!\"}")
74                     .build();
75         } else {
76             return Response.status(200).entity("{\"decision\": \"DENY\", \"details\": \"Decision Denied. NOK :-(\"}")
77                     .build();
78         }
79     }
80
81     /**
82      * Service get event.
83      *
84      * @return the response
85      */
86     @Path("/event/GetEvent")
87     @GET
88     public Response serviceGetEvent() {
89         final Random rand = new Random();
90         final int nextMatchCase = rand.nextInt(4);
91         final String nextEventName = "Event0" + rand.nextInt(2) + "00";
92
93         final String eventString = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.sample.events\",\n" + "\"name\": \""
94                 + nextEventName + "\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + getMessagesReceived
95                 + "\",\n" + "\"target\": \"apex\",\n" + "\"TestSlogan\": \"Test slogan for External Event0\",\n"
96                 + "\"TestMatchCase\": " + nextMatchCase + ",\n" + "\"TestTimestamp\": " + System.currentTimeMillis()
97                 + ",\n" + "\"TestTemperature\": 9080.866\n" + "}";
98
99         getMessagesReceived++;
100
101         return Response.status(200).entity(eventString).build();
102     }
103
104     /**
105      * Service get empty event.
106      *
107      * @return the response
108      */
109     @Path("/event/GetEmptyEvent")
110     @GET
111     public Response serviceGetEmptyEvent() {
112         return Response.status(200).build();
113     }
114
115     /**
116      * Service get event bad response.
117      *
118      * @return the response
119      */
120     @Path("/event/GetEventBadResponse")
121     @GET
122     public Response serviceGetEventBadResponse() {
123         return Response.status(400).build();
124     }
125
126     /**
127      * Service post request.
128      *
129      * @param jsonString the json string
130      * @return the response
131      */
132     @Path("/event/PostEvent")
133     @POST
134     public Response servicePostRequest(final String jsonString) {
135         postMessagesReceived++;
136
137         @SuppressWarnings("unchecked")
138         final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
139         assertTrue(jsonMap.containsKey("name"));
140         assertEquals("0.0.1", jsonMap.get("version"));
141         assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
142         assertEquals("Act", jsonMap.get("source"));
143         assertEquals("Outside", jsonMap.get("target"));
144
145         return Response.status(200).entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
146                 + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
147     }
148
149     /**
150      * Service post request bad response.
151      *
152      * @param jsonString the json string
153      * @return the response
154      */
155     @Path("/event/PostEventBadResponse")
156     @POST
157     public Response servicePostRequestBadResponse(final String jsonString) {
158         return Response.status(400).build();
159     }
160
161     /**
162      * Service put request.
163      *
164      * @param jsonString the json string
165      * @return the response
166      */
167     @Path("/event/PutEvent")
168     @PUT
169     public Response servicePutRequest(final String jsonString) {
170         putMessagesReceived++;
171
172         @SuppressWarnings("unchecked")
173         final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
174         assertTrue(jsonMap.containsKey("name"));
175         assertEquals("0.0.1", jsonMap.get("version"));
176         assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
177         assertEquals("Act", jsonMap.get("source"));
178         assertEquals("Outside", jsonMap.get("target"));
179
180         return Response.status(200).entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
181                 + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
182     }
183 }