2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.models.sim.dmaap.rest;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.junit.Assert.assertEquals;
26 import java.util.Arrays;
28 import javax.ws.rs.core.Response;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.common.utils.coder.Coder;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
35 import org.onap.policy.models.sim.dmaap.provider.DmaapSimProvider;
37 public class DmaapSimRestControllerV1Test {
38 private static final int LIMIT = 5;
39 private static final String TOPIC = "my-topic";
40 private static final String TOPIC2 = "my-topic-B";
41 private static final String MESSAGE = "my-message";
42 private static final String MESSAGE2 = "my-message-B";
43 private static final String CONSUMER = "my-consumer";
44 private static final String CONSUMER_ID = "my-id";
46 private static Coder coder = new StandardCoder();
48 private DmaapSimRestControllerV1 rest;
51 * Creates the controller.
53 * @throws CoderException if the parameters cannot be loaded
56 public void setUp() throws CoderException {
57 DmaapSimParameterGroup params = coder.decode(new File("src/test/resources/parameters/NormalParameters.json"),
58 DmaapSimParameterGroup.class);
59 DmaapSimProvider.setInstance(new DmaapSimProvider(params));
60 rest = new DmaapSimRestControllerV1();
65 Response resp = rest.getDmaapMessage(TOPIC, CONSUMER, CONSUMER_ID, LIMIT, 0);
66 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
67 assertEquals("[]", resp.getEntity().toString());
70 resp = rest.postDmaapMessage(TOPIC, Arrays.asList(MESSAGE, MESSAGE2));
71 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
72 assertEquals(2, getCount(resp));
74 resp = rest.postDmaapMessage(TOPIC2, Arrays.asList(MESSAGE, MESSAGE2, MESSAGE));
75 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
76 assertEquals(3, getCount(resp));
78 // hadn't registered with topic 2 so nothing expected from there
79 resp = rest.getDmaapMessage(TOPIC2, CONSUMER, CONSUMER_ID, LIMIT, 0);
80 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
81 assertEquals("[]", resp.getEntity().toString());
83 // now read from topic 1
84 resp = rest.getDmaapMessage(TOPIC, CONSUMER, CONSUMER_ID, LIMIT, 0);
85 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
86 assertEquals("[my-message, my-message-B]", resp.getEntity().toString());
88 // verify getDmaapTopics
89 resp = rest.getDmaapTopics();
90 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
91 assertThat(resp.getEntity().toString()).contains("POLICY-PDP-PAP");
94 private int getCount(Response resp) {
95 @SuppressWarnings("unchecked")
96 Map<String, Object> map = (Map<String, Object>) resp.getEntity();
98 return (int) map.get("count");