Java 17 Upgrade
[policy/models.git] / models-sim / models-sim-dmaap / src / test / java / org / onap / policy / models / sim / dmaap / rest / DmaapSimRestControllerV1Test.java
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.models.sim.dmaap.rest;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25
26 import jakarta.ws.rs.core.Response;
27 import java.io.File;
28 import java.util.Arrays;
29 import java.util.Map;
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;
37
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";
46
47     private static final Coder coder = new StandardCoder();
48
49     private DmaapSimRestControllerV1 rest;
50
51     /**
52      * Creates the controller.
53      *
54      * @throws CoderException if the parameters cannot be loaded
55      */
56     @Before
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();
62     }
63
64     @Test
65     public void test() {
66         Response resp = rest.getDmaapMessage(TOPIC, CONSUMER, CONSUMER_ID, LIMIT, 0);
67         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
68         assertEquals("[]", resp.getEntity().toString());
69
70         // add some messages
71         resp = rest.postDmaapMessage(TOPIC, Arrays.asList(MESSAGE, MESSAGE2));
72         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
73         assertEquals(2, getCount(resp));
74
75         resp = rest.postDmaapMessage(TOPIC2, Arrays.asList(MESSAGE, MESSAGE2, MESSAGE));
76         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
77         assertEquals(3, getCount(resp));
78
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());
83
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());
88
89         // verify getDmaapTopics
90         resp = rest.getDmaapTopics();
91         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
92         assertThat(resp.getEntity().toString()).contains("POLICY-PDP-PAP");
93     }
94
95     private int getCount(Response resp) {
96         @SuppressWarnings("unchecked")
97         Map<String, Object> map = (Map<String, Object>) resp.getEntity();
98
99         return (int) map.get("count");
100     }
101
102 }