b731a6fce87296c44bcdfb425c7ed785cd4577e0
[policy/models.git] / models-sim / models-sim-dmaap / src / test / java / org / onap / policy / models / sim / dmaap / provider / TopicDataTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.models.sim.dmaap.provider;
22
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;
34
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.LinkedList;
38 import java.util.List;
39 import java.util.Map;
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;
49
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";
55
56     private TopicData data;
57     private ConsumerGroupData consgrp1;
58     private ConsumerGroupData consgrp2;
59     private ConsumerGroupData consgrp3;
60     private List<ConsumerGroupData> groups;
61
62     /**
63      * Sets up mocks and the initial data object.
64      *
65      * @throws Exception if an error occurs
66      */
67     @Before
68     public void setUp() throws Exception {
69         consgrp1 = mock(ConsumerGroupData.class);
70         consgrp2 = mock(ConsumerGroupData.class);
71         consgrp3 = mock(ConsumerGroupData.class);
72
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());
76
77         groups = new LinkedList<>(Arrays.asList(consgrp1, consgrp2, consgrp3));
78
79         data = new TopicData("my-topic") {
80             @Override
81             protected ConsumerGroupData makeData(String consumerGroup) {
82                 return groups.remove(0);
83             }
84         };
85     }
86
87     @Test
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);
93
94         // indicate that one should be removed
95         when(consgrp1.shouldRemove()).thenReturn(true);
96
97         // sweep
98         data.removeIdleConsumers();
99
100         assertEquals("[group-B, group-C]", new TreeSet<>(getGroups().keySet()).toString());
101
102         // indicate that the others should be removed
103         when(consgrp2.shouldRemove()).thenReturn(true);
104         when(consgrp3.shouldRemove()).thenReturn(true);
105
106         // sweep
107         data.removeIdleConsumers();
108
109         assertTrue(getGroups().isEmpty());
110     }
111
112     @Test
113     public void testRead() throws Exception {
114         List<String> lst = Collections.emptyList();
115
116         when(consgrp1.read(anyInt(), anyLong())).thenReturn(ConsumerGroupData.UNREADABLE_LIST)
117                         .thenReturn(ConsumerGroupData.UNREADABLE_LIST).thenReturn(lst);
118
119         assertSame(lst, data.read(GROUP1, 10, 20));
120
121         // should have invoked three times
122         verify(consgrp1, times(3)).read(anyInt(), anyLong());
123
124         // should have used the given values
125         verify(consgrp1, times(3)).read(10, 20);
126
127         // should not have allocated more than one group
128         assertEquals(2, groups.size());
129     }
130
131     @Test
132     public void testRead_MultipleGroups() throws Exception {
133         List<String> lst1 = Collections.emptyList();
134         when(consgrp1.read(anyInt(), anyLong())).thenReturn(lst1);
135
136         List<String> lst2 = Collections.emptyList();
137         when(consgrp2.read(anyInt(), anyLong())).thenReturn(lst2);
138
139         // one from each group
140         assertSame(lst1, data.read(GROUP1, 0, 0));
141         assertSame(lst2, data.read(GROUP2, 0, 0));
142
143         // repeat
144         assertSame(lst1, data.read(GROUP1, 0, 0));
145         assertSame(lst2, data.read(GROUP2, 0, 0));
146
147         // again
148         assertSame(lst1, data.read(GROUP1, 0, 0));
149         assertSame(lst2, data.read(GROUP2, 0, 0));
150
151         // should still have group3 in the list
152         assertEquals(1, groups.size());
153     }
154
155     @Test
156     public void testWrite() throws Exception {
157         // no groups yet
158         List<Object> messages = Arrays.asList("hello", "world");
159         data.write(messages);
160
161         // add two groups
162         data.read(GROUP1, 0, 0);
163         data.read(GROUP2, 0, 0);
164
165         data.write(messages);
166
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);
171     }
172
173     @Test
174     public void testConvertMessagesToStrings() {
175         assertEquals("[abc, 200]", data.convertMessagesToStrings(Arrays.asList("abc", null, 200)).toString());
176     }
177
178     @Test
179     public void testConvertMessageToString() throws CoderException {
180         Coder coder = new StandardCoder();
181
182         assertNull(data.convertMessageToString(null, coder));
183         assertEquals("text-msg", data.convertMessageToString("text-msg", coder));
184         assertEquals("100", data.convertMessageToString(100, coder));
185
186         coder = mock(Coder.class);
187         when(coder.encode(any())).thenThrow(new CoderException(EXPECTED_EXCEPTION));
188         assertNull(data.convertMessageToString(new TreeMap<String, Object>(), coder));
189     }
190
191     @Test
192     public void testMakeData() throws Exception {
193         // use real objects instead of mocks
194         TopicData data2 = new TopicData("real-data-topic");
195
196         // force a group into the topic
197         data2.read(GROUP1, 0, 0);
198
199         data2.write(Arrays.asList("abc", "def", "ghi"));
200
201         assertEquals("[abc, def]", data2.read(GROUP1, 2, 0).toString());
202     }
203
204     /**
205      * Gets the consumer group map from the topic data object.
206      *
207      * @return the topic's consumer group map
208      */
209     @SuppressWarnings("unchecked")
210     private Map<String, ConsumerGroupData> getGroups() {
211         return (Map<String, ConsumerGroupData>) Whitebox.getInternalState(data, "group2data");
212     }
213 }