2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.sim.dmaap.provider;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyInt;
29 import static org.mockito.ArgumentMatchers.anyLong;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.LinkedList;
38 import java.util.List;
40 import java.util.TreeMap;
41 import java.util.TreeSet;
42 import java.util.stream.Collectors;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.onap.policy.common.utils.coder.Coder;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.common.utils.coder.StandardCoder;
48 import org.powermock.reflect.Whitebox;
50 public class TopicDataTest {
51 private static final String EXPECTED_EXCEPTION = "expected exception";
52 private static final String GROUP1 = "group-A";
53 private static final String GROUP2 = "group-B";
54 private static final String GROUP3 = "group-C";
56 private TopicData data;
57 private ConsumerGroupData consgrp1;
58 private ConsumerGroupData consgrp2;
59 private ConsumerGroupData consgrp3;
60 private List<ConsumerGroupData> groups;
63 * Sets up mocks and the initial data object.
65 * @throws Exception if an error occurs
68 public void setUp() throws Exception {
69 consgrp1 = mock(ConsumerGroupData.class);
70 consgrp2 = mock(ConsumerGroupData.class);
71 consgrp3 = mock(ConsumerGroupData.class);
73 when(consgrp1.read(anyInt(), anyLong())).thenReturn(Collections.emptyList());
74 when(consgrp2.read(anyInt(), anyLong())).thenReturn(Collections.emptyList());
75 when(consgrp3.read(anyInt(), anyLong())).thenReturn(Collections.emptyList());
77 groups = new LinkedList<>(Arrays.asList(consgrp1, consgrp2, consgrp3));
79 data = new TopicData("my-topic") {
81 protected ConsumerGroupData makeData(String consumerGroup) {
82 return groups.remove(0);
88 public void testRemoveIdleConsumers() throws Exception {
89 // force two consumers into the map
90 data.read(GROUP1, 0, 0);
91 data.read(GROUP2, 0, 0);
92 data.read(GROUP3, 0, 0);
94 // indicate that one should be removed
95 when(consgrp1.shouldRemove()).thenReturn(true);
98 data.removeIdleConsumers();
100 assertEquals("[group-B, group-C]", new TreeSet<>(getGroups().keySet()).toString());
102 // indicate that the others should be removed
103 when(consgrp2.shouldRemove()).thenReturn(true);
104 when(consgrp3.shouldRemove()).thenReturn(true);
107 data.removeIdleConsumers();
109 assertTrue(getGroups().isEmpty());
113 public void testRead() throws Exception {
114 List<String> lst = Collections.emptyList();
116 when(consgrp1.read(anyInt(), anyLong())).thenReturn(ConsumerGroupData.UNREADABLE_LIST)
117 .thenReturn(ConsumerGroupData.UNREADABLE_LIST).thenReturn(lst);
119 assertSame(lst, data.read(GROUP1, 10, 20));
121 // should have invoked three times
122 verify(consgrp1, times(3)).read(anyInt(), anyLong());
124 // should have used the given values
125 verify(consgrp1, times(3)).read(10, 20);
127 // should not have allocated more than one group
128 assertEquals(2, groups.size());
132 public void testRead_MultipleGroups() throws Exception {
133 List<String> lst1 = Collections.emptyList();
134 when(consgrp1.read(anyInt(), anyLong())).thenReturn(lst1);
136 List<String> lst2 = Collections.emptyList();
137 when(consgrp2.read(anyInt(), anyLong())).thenReturn(lst2);
139 // one from each group
140 assertSame(lst1, data.read(GROUP1, 0, 0));
141 assertSame(lst2, data.read(GROUP2, 0, 0));
144 assertSame(lst1, data.read(GROUP1, 0, 0));
145 assertSame(lst2, data.read(GROUP2, 0, 0));
148 assertSame(lst1, data.read(GROUP1, 0, 0));
149 assertSame(lst2, data.read(GROUP2, 0, 0));
151 // should still have group3 in the list
152 assertEquals(1, groups.size());
156 public void testWrite() throws Exception {
158 List<Object> messages = Arrays.asList("hello", "world");
159 data.write(messages);
162 data.read(GROUP1, 0, 0);
163 data.read(GROUP2, 0, 0);
165 data.write(messages);
167 // should have been written to both groups
168 List<String> strings = messages.stream().map(Object::toString).collect(Collectors.toList());
169 verify(consgrp1).write(strings);
170 verify(consgrp2).write(strings);
174 public void testConvertMessagesToStrings() {
175 assertEquals("[abc, 200]", data.convertMessagesToStrings(Arrays.asList("abc", null, 200)).toString());
179 public void testConvertMessageToString() throws CoderException {
180 Coder coder = new StandardCoder();
182 assertNull(data.convertMessageToString(null, coder));
183 assertEquals("text-msg", data.convertMessageToString("text-msg", coder));
184 assertEquals("100", data.convertMessageToString(100, coder));
186 coder = mock(Coder.class);
187 when(coder.encode(any())).thenThrow(new CoderException(EXPECTED_EXCEPTION));
188 assertNull(data.convertMessageToString(new TreeMap<String, Object>(), coder));
192 public void testMakeData() throws Exception {
193 // use real objects instead of mocks
194 TopicData data2 = new TopicData("real-data-topic");
196 // force a group into the topic
197 data2.read(GROUP1, 0, 0);
199 data2.write(Arrays.asList("abc", "def", "ghi"));
201 assertEquals("[abc, def]", data2.read(GROUP1, 2, 0).toString());
205 * Gets the consumer group map from the topic data object.
207 * @return the topic's consumer group map
209 @SuppressWarnings("unchecked")
210 private Map<String, ConsumerGroupData> getGroups() {
211 return (Map<String, ConsumerGroupData>) Whitebox.getInternalState(data, "group2data");