2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.onap.dcae.common.validator;
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;
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;
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;
41 class BatchEventValidatorTest {
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";
49 void shouldThrowException_whenDomainFieldsHaveDifferentValues() throws IOException {
51 final List<VesEvent> eventList = prepareEventList("src/test/resources/ves7_batch_valid_two_different_domain.json", EVENT_LIST);
55 assertThrows(EventValidatorException.class, () -> executeBatchEventValidation(eventList));
59 void shouldNotThrowException_whenDomainFieldsHaveSameValues() throws IOException {
61 final List<VesEvent> eventList = prepareEventList("src/test/resources/ves7_batch_valid.json", EVENT_LIST);
65 assertDoesNotThrow(() -> executeBatchEventValidation(eventList));
69 void shouldThrowException_whenStndDefinedNamespaceFieldsHaveDifferentValuesAndDomainsAreStndDefined() throws IOException {
71 final List<VesEvent> eventList = prepareEventList("src/test/resources/ves7_batch_stdnDefined_withDifferentStndDefinedNamespace.json", EVENT_LIST);
75 assertThrows(EventValidatorException.class, () -> executeBatchEventValidation(eventList));
79 void shouldNotThrowException_whenStndDefinedNamespaceFieldsHaveSameValuesAndDomainsAreStndDefined() throws IOException {
81 final List<VesEvent> eventList = prepareEventList("src/test/resources/ves7_batch_stdnDefined_withSameStndDefinedNamespace.json", EVENT_LIST);
85 assertDoesNotThrow(() -> executeBatchEventValidation(eventList));
89 void shouldNotThrowException_whenSendValidNotBatchEvent() throws IOException {
91 final List<VesEvent> eventList = prepareEventList("src/test/resources/ves_stdnDefined_valid.json", EVENT);
95 assertDoesNotThrow(() -> executeBatchEventValidation(eventList));
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);
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));