Performance Improvements for Gizmo bulk API
[aai/gizmo.git] / src / test / java / org / onap / crud / service / CrudAsyncResponseConsumerTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2018 Nokia
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 package org.onap.crud.service;
22
23 import static junit.framework.TestCase.fail;
24 import static org.mockito.Matchers.anyString;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.never;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import com.google.common.collect.Lists;
32 import java.util.ArrayList;
33 import javax.naming.OperationNotSupportedException;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.runners.MockitoJUnitRunner;
39 import org.onap.aai.event.api.EventConsumer;
40
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class CrudAsyncResponseConsumerTest {
44
45     private static final ArrayList<String> EVENTS = Lists.newArrayList("event_json1", "event_json2");
46
47     @Mock
48     private EventConsumer eventConsumer;
49     @Mock
50     private GraphEventUpdater graphEventUpdater;
51
52     private CrudAsyncResponseConsumer crudAsyncResponseConsumer;
53
54     @Before
55     public void setUp() {
56         crudAsyncResponseConsumer = new CrudAsyncResponseConsumer(eventConsumer, graphEventUpdater);
57     }
58
59     @Test
60     public void shouldCommitOnlyOffsetsWhenEventsCollectionIsEmpty() throws Exception {
61         // given
62         when(eventConsumer.consume()).thenReturn(new ArrayList<>());
63
64         // when
65         crudAsyncResponseConsumer.run();
66
67         // then
68         verify(graphEventUpdater, never()).update(anyString());
69         verify(eventConsumer, times(1)).commitOffsets();
70     }
71
72     @Test
73     public void shouldCommitOnlyOffsetsWhenThereIsNoEventsToProcess() throws Exception {
74         // given
75         when(eventConsumer.consume()).thenReturn(null);
76
77         // when
78         crudAsyncResponseConsumer.run();
79
80         // then
81         verify(graphEventUpdater, never()).update(anyString());
82         verify(eventConsumer, times(1)).commitOffsets();
83     }
84
85     @Test
86     public void shouldProcessEventsWhenConsumerProvidesListOfEvents() throws Exception {
87         // given
88         when(eventConsumer.consume()).thenReturn(EVENTS);
89
90         // when
91         crudAsyncResponseConsumer.run();
92
93         // then
94         verify(graphEventUpdater, times(2)).update(anyString());
95         verify(eventConsumer, times(1)).commitOffsets();
96     }
97
98     @Test
99     public void shouldHandleAnyErrorCaseDuringCommitOffsets() throws Exception {
100         // given
101         when(eventConsumer.consume()).thenReturn(EVENTS);
102         doThrow(OperationNotSupportedException.class).when(eventConsumer).commitOffsets();
103
104         // when
105         try {
106             crudAsyncResponseConsumer.run();
107         } catch (Exception e) {
108             fail("Any error reported by run method is wrong!");
109         }
110
111         // then
112         verify(graphEventUpdater, times(2)).update(anyString());
113
114     }
115 }