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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.pnfsimulator.simulator;
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;
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;
37 public class IncrementProviderImplTest {
38 private IncrementProvider incrementProvider;
41 private EventDataRepository eventDataRepositoryMock;
45 eventDataRepositoryMock = mock(EventDataRepository.class);
46 incrementProvider = new IncrementProviderImpl(eventDataRepositoryMock);
50 public void getAndIncrementTest() {
51 ArgumentCaptor<EventData> eventDataArgumentCaptor = ArgumentCaptor.forClass(EventData.class);
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);
58 when(eventDataRepositoryMock.findById(eventId)).thenReturn(optional);
60 int value = incrementProvider.getAndIncrement(eventId);
62 verify(eventDataRepositoryMock).save(eventDataArgumentCaptor.capture());
64 assertThat(value).isEqualTo(expectedValue);
65 assertThat(eventDataArgumentCaptor.getValue().getIncrementValue()).isEqualTo(expectedValue);
70 public void shouldThrowOnNonExistingEvent() {
71 Optional<EventData> emptyOptional = Optional.empty();
72 String nonExistingEventId = "THIS_DOES_NOT_EXIST";
73 when(eventDataRepositoryMock.findById(nonExistingEventId)).thenReturn(emptyOptional);
75 assertThrows(EventNotFoundException.class,
76 () -> incrementProvider.getAndIncrement(nonExistingEventId));