2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. 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.
16 * ============LICENSE_END=========================================================
19 package org.onap.policy.models.sim.dmaap.rest;
21 import static org.junit.Assert.assertEquals;
24 import java.util.Arrays;
26 import javax.ws.rs.core.Response;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.policy.common.utils.coder.Coder;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
33 import org.onap.policy.models.sim.dmaap.provider.DmaapSimProvider;
35 public class DmaapSimRestControllerV1Test {
36 private static final int LIMIT = 5;
37 private static final String TOPIC = "my-topic";
38 private static final String TOPIC2 = "my-topic-B";
39 private static final String MESSAGE = "my-message";
40 private static final String MESSAGE2 = "my-message-B";
41 private static final String CONSUMER = "my-consumer";
42 private static final String CONSUMER_ID = "my-id";
44 private static Coder coder = new StandardCoder();
46 private DmaapSimRestControllerV1 rest;
49 * Creates the controller.
51 * @throws CoderException if the parameters cannot be loaded
54 public void setUp() throws CoderException {
55 DmaapSimParameterGroup params = coder.decode(new File("src/test/resources/parameters/NormalParameters.json"),
56 DmaapSimParameterGroup.class);
57 DmaapSimProvider.setInstance(new DmaapSimProvider(params));
58 rest = new DmaapSimRestControllerV1();
63 Response resp = rest.getDmaapMessage(TOPIC, CONSUMER, CONSUMER_ID, LIMIT, 0);
64 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
65 assertEquals("[]", resp.getEntity().toString());
68 resp = rest.postDmaapMessage(TOPIC, Arrays.asList(MESSAGE, MESSAGE2));
69 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
70 assertEquals(2, getCount(resp));
72 resp = rest.postDmaapMessage(TOPIC2, Arrays.asList(MESSAGE, MESSAGE2, MESSAGE));
73 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
74 assertEquals(3, getCount(resp));
76 // hadn't registered with topic 2 so nothing expected from there
77 resp = rest.getDmaapMessage(TOPIC2, CONSUMER, CONSUMER_ID, LIMIT, 0);
78 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
79 assertEquals("[]", resp.getEntity().toString());
81 // now read from topic 1
82 resp = rest.getDmaapMessage(TOPIC, CONSUMER, CONSUMER_ID, LIMIT, 0);
83 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
84 assertEquals("[my-message, my-message-B]", resp.getEntity().toString());
87 private int getCount(Response resp) {
88 @SuppressWarnings("unchecked")
89 Map<String, Object> map = (Map<String, Object>) resp.getEntity();
91 return (int) map.get("count");