Flesh out DMaaP simulator
[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  * ================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  */
18
19 package org.onap.policy.models.sim.dmaap.rest;
20
21 import static org.junit.Assert.assertEquals;
22
23 import java.io.File;
24 import java.util.Arrays;
25 import java.util.Map;
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;
34
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";
43
44     private static Coder coder = new StandardCoder();
45
46     private DmaapSimRestControllerV1 rest;
47
48     /**
49      * Creates the controller.
50      *
51      * @throws CoderException if the parameters cannot be loaded
52      */
53     @Before
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();
59     }
60
61     @Test
62     public void test() {
63         Response resp = rest.getDmaapMessage(TOPIC, CONSUMER, CONSUMER_ID, LIMIT, 0);
64         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
65         assertEquals("[]", resp.getEntity().toString());
66
67         // add some messages
68         resp = rest.postDmaapMessage(TOPIC, Arrays.asList(MESSAGE, MESSAGE2));
69         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
70         assertEquals(2, getCount(resp));
71
72         resp = rest.postDmaapMessage(TOPIC2, Arrays.asList(MESSAGE, MESSAGE2, MESSAGE));
73         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
74         assertEquals(3, getCount(resp));
75
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());
80
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());
85     }
86
87     private int getCount(Response resp) {
88         @SuppressWarnings("unchecked")
89         Map<String, Object> map = (Map<String, Object>) resp.getEntity();
90
91         return (int) map.get("count");
92     }
93
94 }