6c7f7db19dfb52ecd8265398afda717b5294d3e7
[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 @Path("/sim")
39 public class AAIAndGuardSimEndpoint {
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("/pdp/api/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("/pdp/api/getDecision")
55     @POST
56     public Response serviceGuardPostRequest(final String jsonString) {
57         postMessagesReceived++;
58
59         if (postMessagesReceived % 2 == 0) {
60             return Response.status(200).entity("{\"decision\": \"PERMIT\", \"details\": \"Decision Permit. OK!\"}")
61                     .build();
62         } else {
63             return Response.status(200).entity("{\"decision\": \"DENY\", \"details\": \"Decision Denied. NOK :-(\"}")
64                     .build();
65         }
66     }
67
68     @Path("/event/GetEvent")
69     @GET
70     public Response serviceGetEvent() {
71         final Random rand = new Random();
72         final int nextMatchCase = rand.nextInt(4);
73         final String nextEventName = "Event0" + rand.nextInt(2) + "00";
74
75         final String eventString = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.sample.events\",\n" + "\"name\": \""
76                 + nextEventName + "\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + getMessagesReceived
77                 + "\",\n" + "\"target\": \"apex\",\n" + "\"TestSlogan\": \"Test slogan for External Event0\",\n"
78                 + "\"TestMatchCase\": " + nextMatchCase + ",\n" + "\"TestTimestamp\": " + System.currentTimeMillis()
79                 + ",\n" + "\"TestTemperature\": 9080.866\n" + "}";
80
81         getMessagesReceived++;
82
83         return Response.status(200).entity(eventString).build();
84     }
85
86     @Path("/event/GetEmptyEvent")
87     @GET
88     public Response serviceGetEmptyEvent() {
89         return Response.status(200).build();
90     }
91
92     @Path("/event/GetEventBadResponse")
93     @GET
94     public Response serviceGetEventBadResponse() {
95         return Response.status(400).build();
96     }
97
98     @Path("/event/PostEvent")
99     @POST
100     public Response servicePostRequest(final String jsonString) {
101         postMessagesReceived++;
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("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
112                 + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
113     }
114
115     @Path("/event/PostEventBadResponse")
116     @POST
117     public Response servicePostRequestBadResponse(final String jsonString) {
118         return Response.status(400).build();
119     }
120
121     @Path("/event/PutEvent")
122     @PUT
123     public Response servicePutRequest(final String jsonString) {
124         putMessagesReceived++;
125
126         @SuppressWarnings("unchecked")
127         final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class);
128         assertTrue(jsonMap.containsKey("name"));
129         assertEquals("0.0.1", jsonMap.get("version"));
130         assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
131         assertEquals("Act", jsonMap.get("source"));
132         assertEquals("Outside", jsonMap.get("target"));
133
134         return Response.status(200).entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
135                 + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
136     }
137 }