2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.crud.service;
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;
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;
42 @RunWith(MockitoJUnitRunner.class)
43 public class CrudAsyncResponseConsumerTest {
45 private static final ArrayList<String> EVENTS = Lists.newArrayList("event_json1", "event_json2");
48 private EventConsumer eventConsumer;
50 private GraphEventUpdater graphEventUpdater;
52 private CrudAsyncResponseConsumer crudAsyncResponseConsumer;
56 crudAsyncResponseConsumer = new CrudAsyncResponseConsumer(eventConsumer, graphEventUpdater);
60 public void shouldCommitOnlyOffsetsWhenEventsCollectionIsEmpty() throws Exception {
62 when(eventConsumer.consume()).thenReturn(new ArrayList<>());
65 crudAsyncResponseConsumer.run();
68 verify(graphEventUpdater, never()).update(anyString());
69 verify(eventConsumer, times(1)).commitOffsets();
73 public void shouldCommitOnlyOffsetsWhenThereIsNoEventsToProcess() throws Exception {
75 when(eventConsumer.consume()).thenReturn(null);
78 crudAsyncResponseConsumer.run();
81 verify(graphEventUpdater, never()).update(anyString());
82 verify(eventConsumer, times(1)).commitOffsets();
86 public void shouldProcessEventsWhenConsumerProvidesListOfEvents() throws Exception {
88 when(eventConsumer.consume()).thenReturn(EVENTS);
91 crudAsyncResponseConsumer.run();
94 verify(graphEventUpdater, times(2)).update(anyString());
95 verify(eventConsumer, times(1)).commitOffsets();
99 public void shouldHandleAnyErrorCaseDuringCommitOffsets() throws Exception {
101 when(eventConsumer.consume()).thenReturn(EVENTS);
102 doThrow(OperationNotSupportedException.class).when(eventConsumer).commitOffsets();
106 crudAsyncResponseConsumer.run();
107 } catch (Exception e) {
108 fail("Any error reported by run method is wrong!");
112 verify(graphEventUpdater, times(2)).update(anyString());