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