2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.sim.dmaap.provider;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.anyInt;
30 import static org.mockito.ArgumentMatchers.anyLong;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.times;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.LinkedList;
39 import java.util.List;
41 import java.util.TreeMap;
42 import java.util.TreeSet;
43 import java.util.stream.Collectors;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.springframework.test.util.ReflectionTestUtils;
51 public class TopicDataTest {
52 private static final String EXPECTED_EXCEPTION = "expected exception";
53 private static final String GROUP1 = "group-A";
54 private static final String GROUP2 = "group-B";
55 private static final String GROUP3 = "group-C";
57 private TopicData data;
58 private ConsumerGroupData consgrp1;
59 private ConsumerGroupData consgrp2;
60 private ConsumerGroupData consgrp3;
61 private List<ConsumerGroupData> groups;
64 * Sets up mocks and the initial data object.
66 * @throws Exception if an error occurs
69 public void setUp() throws Exception {
70 consgrp1 = mock(ConsumerGroupData.class);
71 consgrp2 = mock(ConsumerGroupData.class);
72 consgrp3 = mock(ConsumerGroupData.class);
74 when(consgrp1.read(anyInt(), anyLong())).thenReturn(Collections.emptyList());
75 when(consgrp2.read(anyInt(), anyLong())).thenReturn(Collections.emptyList());
76 when(consgrp3.read(anyInt(), anyLong())).thenReturn(Collections.emptyList());
78 groups = new LinkedList<>(Arrays.asList(consgrp1, consgrp2, consgrp3));
80 data = new TopicData("my-topic") {
82 protected ConsumerGroupData makeData(String consumerGroup) {
83 return groups.remove(0);
89 public void testRemoveIdleConsumers() throws Exception {
90 // force two consumers into the map
91 data.read(GROUP1, 0, 0);
92 data.read(GROUP2, 0, 0);
93 data.read(GROUP3, 0, 0);
95 // indicate that one should be removed
96 when(consgrp1.shouldRemove()).thenReturn(true);
99 data.removeIdleConsumers();
101 assertEquals("[group-B, group-C]", new TreeSet<>(getGroups().keySet()).toString());
103 // indicate that the others should be removed
104 when(consgrp2.shouldRemove()).thenReturn(true);
105 when(consgrp3.shouldRemove()).thenReturn(true);
108 data.removeIdleConsumers();
110 assertTrue(getGroups().isEmpty());
114 public void testRead() throws Exception {
115 List<String> lst = Collections.emptyList();
117 when(consgrp1.read(anyInt(), anyLong())).thenReturn(ConsumerGroupData.UNREADABLE_LIST)
118 .thenReturn(ConsumerGroupData.UNREADABLE_LIST).thenReturn(lst);
120 assertSame(lst, data.read(GROUP1, 10, 20));
122 // should have invoked three times
123 verify(consgrp1, times(3)).read(anyInt(), anyLong());
125 // should have used the given values
126 verify(consgrp1, times(3)).read(10, 20);
128 // should not have allocated more than one group
129 assertEquals(2, groups.size());
133 public void testRead_MultipleGroups() throws Exception {
134 List<String> lst1 = Collections.emptyList();
135 when(consgrp1.read(anyInt(), anyLong())).thenReturn(lst1);
137 List<String> lst2 = Collections.emptyList();
138 when(consgrp2.read(anyInt(), anyLong())).thenReturn(lst2);
140 // one from each group
141 assertSame(lst1, data.read(GROUP1, 0, 0));
142 assertSame(lst2, data.read(GROUP2, 0, 0));
145 assertSame(lst1, data.read(GROUP1, 0, 0));
146 assertSame(lst2, data.read(GROUP2, 0, 0));
149 assertSame(lst1, data.read(GROUP1, 0, 0));
150 assertSame(lst2, data.read(GROUP2, 0, 0));
152 // should still have group3 in the list
153 assertEquals(1, groups.size());
157 public void testWrite() throws Exception {
159 List<Object> messages = Arrays.asList("hello", "world");
160 data.write(messages);
163 data.read(GROUP1, 0, 0);
164 data.read(GROUP2, 0, 0);
166 data.write(messages);
168 // should have been written to both groups
169 List<String> strings = messages.stream().map(Object::toString).collect(Collectors.toList());
170 verify(consgrp1).write(strings);
171 verify(consgrp2).write(strings);
175 public void testConvertMessagesToStrings() {
176 assertEquals("[abc, 200]", data.convertMessagesToStrings(Arrays.asList("abc", null, 200)).toString());
180 public void testConvertMessageToString() throws CoderException {
181 Coder coder = new StandardCoder();
183 assertNull(data.convertMessageToString(null, coder));
184 assertEquals("text-msg", data.convertMessageToString("text-msg", coder));
185 assertEquals("100", data.convertMessageToString(100, coder));
187 coder = mock(Coder.class);
188 when(coder.encode(any())).thenThrow(new CoderException(EXPECTED_EXCEPTION));
189 assertNull(data.convertMessageToString(new TreeMap<String, Object>(), coder));
193 public void testMakeData() throws Exception {
194 // use real objects instead of mocks
195 TopicData data2 = new TopicData("real-data-topic");
197 // force a group into the topic
198 data2.read(GROUP1, 0, 0);
200 data2.write(Arrays.asList("abc", "def", "ghi"));
202 assertEquals("[abc, def]", data2.read(GROUP1, 2, 0).toString());
206 * Gets the consumer group map from the topic data object.
208 * @return the topic's consumer group map
210 @SuppressWarnings("unchecked")
211 private Map<String, ConsumerGroupData> getGroups() {
212 return (Map<String, ConsumerGroupData>) ReflectionTestUtils.getField(data, "group2data");