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