5ed51cc34a802aa8a36435679e2aaa893380be65
[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.event;
22
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;
29
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonParser;
32
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Optional;
36
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;
43
44 public class EventDataServiceTest {
45
46     @Mock
47     private EventDataRepository repositoryMock;
48
49     @InjectMocks
50     private EventDataService service;
51
52     private static EventData sampleEventData(String id, String template,
53                                              String patched, String input, String keywords) {
54         return EventData.builder()
55                 .id(id)
56                 .template(template)
57                 .patched(patched)
58                 .input(input)
59                 .keywords(keywords)
60                 .build();
61     }
62
63     @BeforeEach
64     void resetMocks() {
65         initMocks(this);
66     }
67
68     @Test
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);
76
77         service.persistEventData(template, patched, input, keywords);
78
79         verify(repositoryMock).save(argumentCaptor.capture());
80         EventData captured = argumentCaptor.getValue();
81
82         assertEquals(captured.getTemplate(), template.toString());
83         assertEquals(captured.getPatched(), patched.toString());
84         assertEquals(captured.getInput(), input.toString());
85         assertEquals(captured.getKeywords(), keywords.toString());
86     }
87
88     @Test
89     void getAllEventsTest() {
90
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);
96
97         when(repositoryMock.findAll()).thenReturn(eventDataList);
98         List<EventData> actualList = service.getAllEvents();
99
100         assertEquals(eventDataList.size(), actualList.size());
101         assertThat(actualList, IsIterableContainingInOrder.contains(ed1, ed2));
102     }
103
104     @Test
105     void findByIdPresentTest() {
106         String id = "some_object";
107         EventData eventData = sampleEventData(id, "template", "patched", "input", "keywords");
108         Optional<EventData> optional = Optional.of(eventData);
109
110         when(repositoryMock.findById(id)).thenReturn(optional);
111
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());
120
121     }
122
123     @Test
124     void findByIdNotPresentTest() {
125         String id = "some_object";
126         Optional<EventData> optional = Optional.empty();
127
128         when(repositoryMock.findById(id)).thenReturn(optional);
129
130         Optional<EventData> actualOptional = service.getById(id);
131         assertTrue(!actualOptional.isPresent());
132     }
133 }