aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/pnfsimulator/event/EventDataServiceTest.java
blob: 5ed51cc34a802aa8a36435679e2aaa893380be65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * ============LICENSE_START=======================================================
 * PNF-REGISTRATION-HANDLER
 * ================================================================================
 * Copyright (C) 2018 Nokia. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.pnfsimulator.event;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.hamcrest.collection.IsIterableContainingInOrder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;

public class EventDataServiceTest {

    @Mock
    private EventDataRepository repositoryMock;

    @InjectMocks
    private EventDataService service;

    private static EventData sampleEventData(String id, String template,
                                             String patched, String input, String keywords) {
        return EventData.builder()
                .id(id)
                .template(template)
                .patched(patched)
                .input(input)
                .keywords(keywords)
                .build();
    }

    @BeforeEach
    void resetMocks() {
        initMocks(this);
    }

    @Test
    void persistEventDataJsonObjectTest() {
        JsonParser parser = new JsonParser();
        JsonObject template = parser.parse("{ \"bla1\": \"bla2\"}").getAsJsonObject();
        JsonObject patched = parser.parse("{ \"bla3\": \"bla4\"}").getAsJsonObject();
        JsonObject input = parser.parse("{ \"bla5\": \"bla6\"}").getAsJsonObject();
        JsonObject keywords = parser.parse("{ \"bla7\": \"bla8\"}").getAsJsonObject();
        ArgumentCaptor<EventData> argumentCaptor = ArgumentCaptor.forClass(EventData.class);

        service.persistEventData(template, patched, input, keywords);

        verify(repositoryMock).save(argumentCaptor.capture());
        EventData captured = argumentCaptor.getValue();

        assertEquals(captured.getTemplate(), template.toString());
        assertEquals(captured.getPatched(), patched.toString());
        assertEquals(captured.getInput(), input.toString());
        assertEquals(captured.getKeywords(), keywords.toString());
    }

    @Test
    void getAllEventsTest() {

        List<EventData> eventDataList = new ArrayList<>();
        EventData ed1 = sampleEventData("id1", "t1", "p1", "i1", "k1");
        EventData ed2 = sampleEventData("id2", "t2", "p2", "i2", "k2");
        eventDataList.add(ed1);
        eventDataList.add(ed2);

        when(repositoryMock.findAll()).thenReturn(eventDataList);
        List<EventData> actualList = service.getAllEvents();

        assertEquals(eventDataList.size(), actualList.size());
        assertThat(actualList, IsIterableContainingInOrder.contains(ed1, ed2));
    }

    @Test
    void findByIdPresentTest() {
        String id = "some_object";
        EventData eventData = sampleEventData(id, "template", "patched", "input", "keywords");
        Optional<EventData> optional = Optional.of(eventData);

        when(repositoryMock.findById(id)).thenReturn(optional);

        Optional<EventData> actualOptional = service.getById(id);
        assertTrue(actualOptional.isPresent());
        EventData actualObject = actualOptional.get();
        assertEquals(eventData.getId(), actualObject.getId());
        assertEquals(eventData.getTemplate(), actualObject.getTemplate());
        assertEquals(eventData.getPatched(), actualObject.getPatched());
        assertEquals(eventData.getInput(), actualObject.getInput());
        assertEquals(eventData.getKeywords(), actualObject.getKeywords());

    }

    @Test
    void findByIdNotPresentTest() {
        String id = "some_object";
        Optional<EventData> optional = Optional.empty();

        when(repositoryMock.findById(id)).thenReturn(optional);

        Optional<EventData> actualOptional = service.getById(id);
        assertTrue(!actualOptional.isPresent());
    }
}