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 * Modifications Copyright (C) 2023 Nordix Foundation.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.sim.dmaap.rest;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
26 import jakarta.ws.rs.core.Response;
28 import java.util.Arrays;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.utils.coder.Coder;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
36 import org.onap.policy.models.sim.dmaap.provider.DmaapSimProvider;
38 public class DmaapSimRestControllerV1Test {
39 private static final int LIMIT = 5;
40 private static final String TOPIC = "my-topic";
41 private static final String TOPIC2 = "my-topic-B";
42 private static final String MESSAGE = "my-message";
43 private static final String MESSAGE2 = "my-message-B";
44 private static final String CONSUMER = "my-consumer";
45 private static final String CONSUMER_ID = "my-id";
47 private static final Coder coder = new StandardCoder();
49 private DmaapSimRestControllerV1 rest;
52 * Creates the controller.
54 * @throws CoderException if the parameters cannot be loaded
57 public void setUp() throws CoderException {
58 DmaapSimParameterGroup params = coder.decode(new File("src/test/resources/parameters/NormalParameters.json"),
59 DmaapSimParameterGroup.class);
60 DmaapSimProvider.setInstance(new DmaapSimProvider(params));
61 rest = new DmaapSimRestControllerV1();
66 Response resp = rest.getDmaapMessage(TOPIC, CONSUMER, CONSUMER_ID, LIMIT, 0);
67 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
68 assertEquals("[]", resp.getEntity().toString());
71 resp = rest.postDmaapMessage(TOPIC, Arrays.asList(MESSAGE, MESSAGE2));
72 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
73 assertEquals(2, getCount(resp));
75 resp = rest.postDmaapMessage(TOPIC2, Arrays.asList(MESSAGE, MESSAGE2, MESSAGE));
76 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
77 assertEquals(3, getCount(resp));
79 // hadn't registered with topic 2 so nothing expected from there
80 resp = rest.getDmaapMessage(TOPIC2, CONSUMER, CONSUMER_ID, LIMIT, 0);
81 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
82 assertEquals("[]", resp.getEntity().toString());
84 // now read from topic 1
85 resp = rest.getDmaapMessage(TOPIC, CONSUMER, CONSUMER_ID, LIMIT, 0);
86 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
87 assertEquals("[my-message, my-message-B]", resp.getEntity().toString());
89 // verify getDmaapTopics
90 resp = rest.getDmaapTopics();
91 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
92 assertThat(resp.getEntity().toString()).contains("POLICY-PDP-PAP");
95 private int getCount(Response resp) {
96 @SuppressWarnings("unchecked")
97 Map<String, Object> map = (Map<String, Object>) resp.getEntity();
99 return (int) map.get("count");