42ed4d3978b44f3397b3d1971efc7f8569ca428e
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
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
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.filesystem;
22
23 import static junit.framework.TestCase.fail;
24 import static org.mockito.MockitoAnnotations.initMocks;
25
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;
43
44 class WatcherEventProcessorTest {
45
46     @Mock
47     private WatchEvent watchEvent;
48     @Mock
49     private Path templatesDir;
50
51     private Storage<Template> storage;
52     private static Path jsonFilePath;
53
54     @BeforeAll
55     static void init() {
56         jsonFilePath = Paths.get("src/test/resources/org/onap/pnfsimulator/simulator/filesystem/test1.json");
57     }
58
59     @BeforeEach
60     void resetMocks() {
61         initMocks(this);
62         storage = new InMemoryTemplateStorage();
63         initStubs();
64     }
65
66     @Test
67     void shouldProcessCreatedEventTest() {
68         // when
69         Mockito.when(watchEvent.kind()).thenReturn(StandardWatchEventKinds.ENTRY_CREATE);
70         WatcherEventProcessor.process(watchEvent, storage, templatesDir);
71         // then
72         verifyPersistedValue();
73     }
74
75     @Test
76     void shouldProcessModifiedEventTest() {
77         //given
78         storage.persist(new Template("test1.json", new Document(Collections.emptyMap()), Instant.now().getNano()));
79         // when
80         Mockito.when(watchEvent.kind()).thenReturn(StandardWatchEventKinds.ENTRY_MODIFY);
81         WatcherEventProcessor.process(watchEvent, storage, templatesDir);
82         // then
83         verifyPersistedValue();
84     }
85
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");
99         } else {
100             fail();
101         }
102     }
103
104     @Test
105     void shouldProcessDeletedEventTest() {
106         //given
107         HashMap<String, Object> legacyObject = new HashMap<>();
108         legacyObject.put("field1", "value1");
109         legacyObject.put("field2", 2);
110
111         storage.persist(new Template("test1.json", new Document(legacyObject), Instant.now().getNano()));
112         // when
113         Mockito.when(watchEvent.kind()).thenReturn(StandardWatchEventKinds.ENTRY_DELETE);
114         WatcherEventProcessor.process(watchEvent, storage, templatesDir);
115         // then
116         Assertions.assertEquals(storage.getAll().size(), 0);
117     }
118
119     private void initStubs() {
120         Mockito.when(templatesDir.resolve(jsonFilePath)).thenReturn(jsonFilePath);
121         Mockito.when(watchEvent.context()).thenReturn(jsonFilePath);
122     }
123
124 }