aboutsummaryrefslogtreecommitdiffstats
path: root/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/models/sim/dmaap/rest/DmaapSimRestControllerV1Test.java
blob: fd9397e340a0421aee317a9ff59370a69482d1ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*-
 * ============LICENSE_START=======================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
 * Modifications Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.models.sim.dmaap.rest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;

import jakarta.ws.rs.core.Response;
import java.io.File;
import java.util.Arrays;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
import org.onap.policy.models.sim.dmaap.provider.DmaapSimProvider;

public class DmaapSimRestControllerV1Test {
    private static final int LIMIT = 5;
    private static final String TOPIC = "my-topic";
    private static final String TOPIC2 = "my-topic-B";
    private static final String MESSAGE = "my-message";
    private static final String MESSAGE2 = "my-message-B";
    private static final String CONSUMER = "my-consumer";
    private static final String CONSUMER_ID = "my-id";

    private static final Coder coder = new StandardCoder();

    private DmaapSimRestControllerV1 rest;

    /**
     * Creates the controller.
     *
     * @throws CoderException if the parameters cannot be loaded
     */
    @Before
    public void setUp() throws CoderException {
        DmaapSimParameterGroup params = coder.decode(new File("src/test/resources/parameters/NormalParameters.json"),
                        DmaapSimParameterGroup.class);
        DmaapSimProvider.setInstance(new DmaapSimProvider(params));
        rest = new DmaapSimRestControllerV1();
    }

    @Test
    public void test() {
        Response resp = rest.getDmaapMessage(TOPIC, CONSUMER, CONSUMER_ID, LIMIT, 0);
        assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
        assertEquals("[]", resp.getEntity().toString());

        // add some messages
        resp = rest.postDmaapMessage(TOPIC, Arrays.asList(MESSAGE, MESSAGE2));
        assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
        assertEquals(2, getCount(resp));

        resp = rest.postDmaapMessage(TOPIC2, Arrays.asList(MESSAGE, MESSAGE2, MESSAGE));
        assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
        assertEquals(3, getCount(resp));

        // hadn't registered with topic 2 so nothing expected from there
        resp = rest.getDmaapMessage(TOPIC2, CONSUMER, CONSUMER_ID, LIMIT, 0);
        assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
        assertEquals("[]", resp.getEntity().toString());

        // now read from topic 1
        resp = rest.getDmaapMessage(TOPIC, CONSUMER, CONSUMER_ID, LIMIT, 0);
        assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
        assertEquals("[my-message, my-message-B]", resp.getEntity().toString());

        // verify getDmaapTopics
        resp = rest.getDmaapTopics();
        assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
        assertThat(resp.getEntity().toString()).contains("POLICY-PDP-PAP");
    }

    private int getCount(Response resp) {
        @SuppressWarnings("unchecked")
        Map<String, Object> map = (Map<String, Object>) resp.getEntity();

        return (int) map.get("count");
    }

}