aboutsummaryrefslogtreecommitdiffstats
path: root/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/models/sim/dmaap/provider/TopicDataTest.java
blob: f37255acf40ac216656d43c6ecc14d7b69f4d6fd (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy Models
 * ================================================================================
 * Copyright (C) 2019-2020 AT&T Intellectual Property. 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.provider;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Collectors;
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.springframework.test.util.ReflectionTestUtils;

public class TopicDataTest {
    private static final String EXPECTED_EXCEPTION = "expected exception";
    private static final String GROUP1 = "group-A";
    private static final String GROUP2 = "group-B";
    private static final String GROUP3 = "group-C";

    private TopicData data;
    private ConsumerGroupData consgrp1;
    private ConsumerGroupData consgrp2;
    private ConsumerGroupData consgrp3;
    private List<ConsumerGroupData> groups;

    /**
     * Sets up mocks and the initial data object.
     *
     * @throws Exception if an error occurs
     */
    @Before
    public void setUp() throws Exception {
        consgrp1 = mock(ConsumerGroupData.class);
        consgrp2 = mock(ConsumerGroupData.class);
        consgrp3 = mock(ConsumerGroupData.class);

        when(consgrp1.read(anyInt(), anyLong())).thenReturn(Collections.emptyList());
        when(consgrp2.read(anyInt(), anyLong())).thenReturn(Collections.emptyList());
        when(consgrp3.read(anyInt(), anyLong())).thenReturn(Collections.emptyList());

        groups = new LinkedList<>(Arrays.asList(consgrp1, consgrp2, consgrp3));

        data = new TopicData("my-topic") {
            @Override
            protected ConsumerGroupData makeData(String consumerGroup) {
                return groups.remove(0);
            }
        };
    }

    @Test
    public void testRemoveIdleConsumers() throws Exception {
        // force two consumers into the map
        data.read(GROUP1, 0, 0);
        data.read(GROUP2, 0, 0);
        data.read(GROUP3, 0, 0);

        // indicate that one should be removed
        when(consgrp1.shouldRemove()).thenReturn(true);

        // sweep
        data.removeIdleConsumers();

        assertEquals("[group-B, group-C]", new TreeSet<>(getGroups().keySet()).toString());

        // indicate that the others should be removed
        when(consgrp2.shouldRemove()).thenReturn(true);
        when(consgrp3.shouldRemove()).thenReturn(true);

        // sweep
        data.removeIdleConsumers();

        assertTrue(getGroups().isEmpty());
    }

    @Test
    public void testRead() throws Exception {
        List<String> lst = Collections.emptyList();

        when(consgrp1.read(anyInt(), anyLong())).thenReturn(ConsumerGroupData.UNREADABLE_LIST)
                        .thenReturn(ConsumerGroupData.UNREADABLE_LIST).thenReturn(lst);

        assertSame(lst, data.read(GROUP1, 10, 20));

        // should have invoked three times
        verify(consgrp1, times(3)).read(anyInt(), anyLong());

        // should have used the given values
        verify(consgrp1, times(3)).read(10, 20);

        // should not have allocated more than one group
        assertEquals(2, groups.size());
    }

    @Test
    public void testRead_MultipleGroups() throws Exception {
        List<String> lst1 = Collections.emptyList();
        when(consgrp1.read(anyInt(), anyLong())).thenReturn(lst1);

        List<String> lst2 = Collections.emptyList();
        when(consgrp2.read(anyInt(), anyLong())).thenReturn(lst2);

        // one from each group
        assertSame(lst1, data.read(GROUP1, 0, 0));
        assertSame(lst2, data.read(GROUP2, 0, 0));

        // repeat
        assertSame(lst1, data.read(GROUP1, 0, 0));
        assertSame(lst2, data.read(GROUP2, 0, 0));

        // again
        assertSame(lst1, data.read(GROUP1, 0, 0));
        assertSame(lst2, data.read(GROUP2, 0, 0));

        // should still have group3 in the list
        assertEquals(1, groups.size());
    }

    @Test
    public void testWrite() throws Exception {
        // no groups yet
        List<Object> messages = Arrays.asList("hello", "world");
        data.write(messages);

        // add two groups
        data.read(GROUP1, 0, 0);
        data.read(GROUP2, 0, 0);

        data.write(messages);

        // should have been written to both groups
        List<String> strings = messages.stream().map(Object::toString).collect(Collectors.toList());
        verify(consgrp1).write(strings);
        verify(consgrp2).write(strings);
    }

    @Test
    public void testConvertMessagesToStrings() {
        assertEquals("[abc, 200]", data.convertMessagesToStrings(Arrays.asList("abc", null, 200)).toString());
    }

    @Test
    public void testConvertMessageToString() throws CoderException {
        Coder coder = new StandardCoder();

        assertNull(data.convertMessageToString(null, coder));
        assertEquals("text-msg", data.convertMessageToString("text-msg", coder));
        assertEquals("100", data.convertMessageToString(100, coder));

        coder = mock(Coder.class);
        when(coder.encode(any())).thenThrow(new CoderException(EXPECTED_EXCEPTION));
        assertNull(data.convertMessageToString(new TreeMap<String, Object>(), coder));
    }

    @Test
    public void testMakeData() throws Exception {
        // use real objects instead of mocks
        TopicData data2 = new TopicData("real-data-topic");

        // force a group into the topic
        data2.read(GROUP1, 0, 0);

        data2.write(Arrays.asList("abc", "def", "ghi"));

        assertEquals("[abc, def]", data2.read(GROUP1, 2, 0).toString());
    }

    /**
     * Gets the consumer group map from the topic data object.
     *
     * @return the topic's consumer group map
     */
    @SuppressWarnings("unchecked")
    private Map<String, ConsumerGroupData> getGroups() {
        return (Map<String, ConsumerGroupData>) ReflectionTestUtils.getField(data, "group2data");
    }
}