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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.restclient;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import com.google.gson.Gson;
29 import java.util.Random;
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;
38 * The Class TestRestClientEndpoint.
41 public class TestRestClientEndpoint {
43 private static int postMessagesReceived = 0;
44 private static int putMessagesReceived = 0;
45 private static int statMessagesReceived = 0;
46 private static int getMessagesReceived = 0;
51 * @return the response
55 public Response serviceGetStats() {
56 statMessagesReceived++;
57 return Response.status(200).entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
58 + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
64 * @return the response
66 @Path("/event/GetEvent")
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";
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" + "}";
79 getMessagesReceived++;
81 return Response.status(200).entity(eventString).build();
85 * Service get empty event.
87 * @return the response
89 @Path("/event/GetEmptyEvent")
91 public Response serviceGetEmptyEvent() {
92 return Response.status(200).build();
96 * Service get event bad response.
98 * @return the response
100 @Path("/event/GetEventBadResponse")
102 public Response serviceGetEventBadResponse() {
103 return Response.status(400).build();
107 * Service post request.
109 * @param jsonString the json string
110 * @return the response
112 @Path("/event/PostEvent")
114 public Response servicePostRequest(final String jsonString) {
115 postMessagesReceived++;
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"));
125 return Response.status(200).entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
126 + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
130 * Service post request bad response.
132 * @param jsonString the json string
133 * @return the response
135 @Path("/event/PostEventBadResponse")
137 public Response servicePostRequestBadResponse(final String jsonString) {
138 return Response.status(400).build();
142 * Service put request.
144 * @param jsonString the json string
145 * @return the response
147 @Path("/event/PutEvent")
149 public Response servicePutRequest(final String jsonString) {
150 putMessagesReceived++;
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"));
160 return Response.status(200).entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
161 + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();