53f02da0e9117978f069587f9c90c5a6ae5ee67d
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. 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.pnfsimulator.simulator;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import java.util.Optional;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Mock;
34 import org.onap.pnfsimulator.event.EventData;
35 import org.onap.pnfsimulator.event.EventDataRepository;
36
37 public class IncrementProviderImplTest {
38   private IncrementProvider incrementProvider;
39
40   @Mock
41   private EventDataRepository eventDataRepositoryMock;
42
43   @BeforeEach
44   void setUp() {
45     eventDataRepositoryMock = mock(EventDataRepository.class);
46     incrementProvider = new IncrementProviderImpl(eventDataRepositoryMock);
47   }
48
49   @Test
50   public void getAndIncrementTest() {
51     ArgumentCaptor<EventData> eventDataArgumentCaptor = ArgumentCaptor.forClass(EventData.class);
52     String eventId = "1";
53     int initialIncrementValue = 0;
54     int expectedValue = initialIncrementValue + 1;
55     EventData eventData = EventData.builder().id(eventId).incrementValue(initialIncrementValue).build();
56     Optional<EventData> optional = Optional.of(eventData);
57
58     when(eventDataRepositoryMock.findById(eventId)).thenReturn(optional);
59
60     int value = incrementProvider.getAndIncrement(eventId);
61
62     verify(eventDataRepositoryMock).save(eventDataArgumentCaptor.capture());
63
64     assertThat(value).isEqualTo(expectedValue);
65     assertThat(eventDataArgumentCaptor.getValue().getIncrementValue()).isEqualTo(expectedValue);
66
67   }
68
69   @Test
70     public void shouldThrowOnNonExistingEvent() {
71     Optional<EventData> emptyOptional = Optional.empty();
72     String nonExistingEventId = "THIS_DOES_NOT_EXIST";
73     when(eventDataRepositoryMock.findById(nonExistingEventId)).thenReturn(emptyOptional);
74
75     assertThrows(EventNotFoundException.class,
76         () -> incrementProvider.getAndIncrement(nonExistingEventId));
77   }
78 }