Replace cambria with DmaaP client
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / common / validator / BatchEventValidatorTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * VES Collector
4  * ================================================================================
5  * Copyright (C) 2021 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 package org.onap.dcae.common.validator;
21
22 import org.json.JSONObject;
23 import org.junit.jupiter.api.Test;
24 import org.onap.dcae.ApplicationSettings;
25 import org.onap.dcae.common.EventUpdater;
26 import org.onap.dcae.common.model.VesEvent;
27 import org.onap.dcae.restapi.EventValidatorException;
28
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.List;
34 import java.util.UUID;
35
36 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
37 import static org.junit.jupiter.api.Assertions.assertThrows;
38 import static org.mockito.Mockito.mock;
39 import static org.onap.dcae.common.validator.BatchEventValidator.executeBatchEventValidation;
40
41 class BatchEventValidatorTest {
42
43     private final ApplicationSettings settings = mock(ApplicationSettings.class);
44     private final EventUpdater eventUpdater = new EventUpdater(settings);
45     private static final String EVENT = "event";
46     private static final String EVENT_LIST = "eventList";
47
48     @Test
49     void shouldThrowException_whenDomainFieldsHaveDifferentValues() throws IOException {
50         //given
51         final List<VesEvent> eventList = prepareEventList("src/test/resources/ves7_batch_valid_two_different_domain.json", EVENT_LIST);
52
53         //when
54         //then
55         assertThrows(EventValidatorException.class, () -> executeBatchEventValidation(eventList));
56     }
57
58     @Test
59     void shouldNotThrowException_whenDomainFieldsHaveSameValues() throws IOException {
60         //given
61         final List<VesEvent> eventList = prepareEventList("src/test/resources/ves7_batch_valid.json", EVENT_LIST);
62
63         //when
64         //then
65         assertDoesNotThrow(() -> executeBatchEventValidation(eventList));
66     }
67
68     @Test
69     void shouldThrowException_whenStndDefinedNamespaceFieldsHaveDifferentValuesAndDomainsAreStndDefined() throws IOException {
70         //given
71         final List<VesEvent> eventList = prepareEventList("src/test/resources/ves7_batch_stdnDefined_withDifferentStndDefinedNamespace.json", EVENT_LIST);
72
73         //when
74         //then
75         assertThrows(EventValidatorException.class, () -> executeBatchEventValidation(eventList));
76     }
77
78     @Test
79     void shouldNotThrowException_whenStndDefinedNamespaceFieldsHaveSameValuesAndDomainsAreStndDefined() throws IOException {
80         //given
81         final List<VesEvent> eventList = prepareEventList("src/test/resources/ves7_batch_stdnDefined_withSameStndDefinedNamespace.json", EVENT_LIST);
82
83         //when
84         //then
85         assertDoesNotThrow(() -> executeBatchEventValidation(eventList));
86     }
87
88     @Test
89     void shouldNotThrowException_whenSendValidNotBatchEvent() throws IOException {
90         //given
91         final List<VesEvent> eventList = prepareEventList("src/test/resources/ves_stdnDefined_valid.json", EVENT);
92
93         //when
94         //then
95         assertDoesNotThrow(() -> executeBatchEventValidation(eventList));
96     }
97
98     private List<VesEvent> prepareEventList(String pathToFile, String eventType) throws IOException {
99         final VesEvent vesEventFromJson = createVesEventFromJson(pathToFile);
100         return eventUpdater.convert(vesEventFromJson, "v7", UUID.randomUUID(), eventType);
101     }
102
103     private VesEvent createVesEventFromJson(String pathToFile) throws IOException {
104         Path path = Paths.get(pathToFile);
105         final List<String> lines = Files.readAllLines(path);
106         String str = String.join("", lines);
107         return new VesEvent(new JSONObject(str));
108     }
109
110 }