2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.filesystem;
23 import static junit.framework.TestCase.fail;
24 import static org.mockito.MockitoAnnotations.initMocks;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.nio.file.StandardWatchEventKinds;
29 import java.nio.file.WatchEvent;
30 import java.time.Instant;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.Optional;
34 import org.bson.Document;
35 import org.junit.jupiter.api.Assertions;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.onap.pnfsimulator.db.Storage;
42 import org.onap.pnfsimulator.template.Template;
44 class WatcherEventProcessorTest {
47 private WatchEvent watchEvent;
49 private Path templatesDir;
51 private Storage<Template> storage;
52 private static Path jsonFilePath;
56 jsonFilePath = Paths.get("src/test/resources/org/onap/pnfsimulator/simulator/filesystem/test1.json");
62 storage = new InMemoryTemplateStorage();
67 void shouldProcessCreatedEventTest() {
69 Mockito.when(watchEvent.kind()).thenReturn(StandardWatchEventKinds.ENTRY_CREATE);
70 WatcherEventProcessor.process(watchEvent, storage, templatesDir);
72 verifyPersistedValue();
76 void shouldProcessModifiedEventTest() {
78 storage.persist(new Template("test1.json", new Document(Collections.emptyMap()), Instant.now().getNano()));
80 Mockito.when(watchEvent.kind()).thenReturn(StandardWatchEventKinds.ENTRY_MODIFY);
81 WatcherEventProcessor.process(watchEvent, storage, templatesDir);
83 verifyPersistedValue();
86 private void verifyPersistedValue() {
87 Assertions.assertEquals(storage.getAll().size(), 1);
88 Optional<Template> templateFromStorage = storage.get("test1.json");
89 if (templateFromStorage.isPresent()) {
90 Template retrievedTemplate = templateFromStorage.get();
91 Document templateContent = retrievedTemplate.getContent();
92 Document flatContent = retrievedTemplate.getFlatContent();
93 Assertions.assertEquals(templateContent.getString("field1"), "value1");
94 Assertions.assertEquals(templateContent.getInteger("field2", 0), 2);
95 Assertions.assertEquals(flatContent.getInteger(":nested:key1[0]", 0), 1);
96 Assertions.assertEquals(flatContent.getInteger(":nested:key1[1]", 0), 2);
97 Assertions.assertEquals(flatContent.getInteger(":nested:key1[2]", 0), 3);
98 Assertions.assertEquals(flatContent.getString(":nested:key2"), "sampleValue2");
105 void shouldProcessDeletedEventTest() {
107 HashMap<String, Object> legacyObject = new HashMap<>();
108 legacyObject.put("field1", "value1");
109 legacyObject.put("field2", 2);
111 storage.persist(new Template("test1.json", new Document(legacyObject), Instant.now().getNano()));
113 Mockito.when(watchEvent.kind()).thenReturn(StandardWatchEventKinds.ENTRY_DELETE);
114 WatcherEventProcessor.process(watchEvent, storage, templatesDir);
116 Assertions.assertEquals(storage.getAll().size(), 0);
119 private void initStubs() {
120 Mockito.when(templatesDir.resolve(jsonFilePath)).thenReturn(jsonFilePath);
121 Mockito.when(watchEvent.context()).thenReturn(jsonFilePath);