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.event;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 import static org.mockito.MockitoAnnotations.initMocks;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonParser;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Optional;
37 import org.hamcrest.collection.IsIterableContainingInOrder;
38 import org.junit.jupiter.api.Test;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.mockito.ArgumentCaptor;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mock;
44 public class EventDataServiceTest {
47 private EventDataRepository repositoryMock;
50 private EventDataService service;
52 private static EventData sampleEventData(String id, String template,
53 String patched, String input, String keywords) {
54 return EventData.builder()
69 void persistEventDataJsonObjectTest() {
70 JsonParser parser = new JsonParser();
71 JsonObject template = parser.parse("{ \"bla1\": \"bla2\"}").getAsJsonObject();
72 JsonObject patched = parser.parse("{ \"bla3\": \"bla4\"}").getAsJsonObject();
73 JsonObject input = parser.parse("{ \"bla5\": \"bla6\"}").getAsJsonObject();
74 JsonObject keywords = parser.parse("{ \"bla7\": \"bla8\"}").getAsJsonObject();
75 ArgumentCaptor<EventData> argumentCaptor = ArgumentCaptor.forClass(EventData.class);
77 service.persistEventData(template, patched, input, keywords);
79 verify(repositoryMock).save(argumentCaptor.capture());
80 EventData captured = argumentCaptor.getValue();
82 assertEquals(captured.getTemplate(), template.toString());
83 assertEquals(captured.getPatched(), patched.toString());
84 assertEquals(captured.getInput(), input.toString());
85 assertEquals(captured.getKeywords(), keywords.toString());
89 void getAllEventsTest() {
91 List<EventData> eventDataList = new ArrayList<>();
92 EventData ed1 = sampleEventData("id1", "t1", "p1", "i1", "k1");
93 EventData ed2 = sampleEventData("id2", "t2", "p2", "i2", "k2");
94 eventDataList.add(ed1);
95 eventDataList.add(ed2);
97 when(repositoryMock.findAll()).thenReturn(eventDataList);
98 List<EventData> actualList = service.getAllEvents();
100 assertEquals(eventDataList.size(), actualList.size());
101 assertThat(actualList, IsIterableContainingInOrder.contains(ed1, ed2));
105 void findByIdPresentTest() {
106 String id = "some_object";
107 EventData eventData = sampleEventData(id, "template", "patched", "input", "keywords");
108 Optional<EventData> optional = Optional.of(eventData);
110 when(repositoryMock.findById(id)).thenReturn(optional);
112 Optional<EventData> actualOptional = service.getById(id);
113 assertTrue(actualOptional.isPresent());
114 EventData actualObject = actualOptional.get();
115 assertEquals(eventData.getId(), actualObject.getId());
116 assertEquals(eventData.getTemplate(), actualObject.getTemplate());
117 assertEquals(eventData.getPatched(), actualObject.getPatched());
118 assertEquals(eventData.getInput(), actualObject.getInput());
119 assertEquals(eventData.getKeywords(), actualObject.getKeywords());
124 void findByIdNotPresentTest() {
125 String id = "some_object";
126 Optional<EventData> optional = Optional.empty();
128 when(repositoryMock.findById(id)).thenReturn(optional);
130 Optional<EventData> actualOptional = service.getById(id);
131 assertTrue(!actualOptional.isPresent());