99cae00dfd910b1ad5e7238bee839bd83c90bd24
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. 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.netconfmonitor;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.never;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import com.tailf.jnc.JNCException;
29 import java.io.IOException;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.pnfsimulator.netconfmonitor.netconf.NetconfConfigurationCache;
35 import org.onap.pnfsimulator.netconfmonitor.netconf.NetconfConfigurationReader;
36 import org.onap.pnfsimulator.netconfmonitor.netconf.NetconfConfigurationWriter;
37
38 class NetconfConfigurationCheckingTaskTest {
39
40     private NetconfConfigurationCheckingTask checkingTask;
41
42     @Mock
43     private NetconfConfigurationReader reader;
44     @Mock
45     private NetconfConfigurationWriter writer;
46     @Mock
47     private NetconfConfigurationCache cache;
48
49     @BeforeEach
50     void setup() {
51         MockitoAnnotations.initMocks(this);
52         checkingTask = new NetconfConfigurationCheckingTask(reader, writer, cache);
53     }
54
55     @Test
56     void run_should_update_configuration_when_changed() throws IOException, JNCException {
57         String configuration = "newConfiguration";
58         when(reader.read()).thenReturn(configuration);
59         when(cache.getConfiguration()).thenReturn("oldConfiguration");
60
61         checkingTask.run();
62
63         verify(reader).read();
64         verify(cache).getConfiguration();
65         verify(writer).writeToFile(configuration);
66         verify(cache).update(configuration);
67     }
68
69     @Test
70     void run_should_not_update_configuration_when_same() throws IOException, JNCException {
71         String configuration = "configuration";
72         when(reader.read()).thenReturn(configuration);
73         when(cache.getConfiguration()).thenReturn("configuration");
74
75         checkingTask.run();
76
77         verify(reader).read();
78         verify(cache).getConfiguration();
79         verify(writer, never()).writeToFile(configuration);
80         verify(cache, never()).update(configuration);
81     }
82
83     @Test
84     void run_should_not_take_any_action_when_failed_to_read_configuration() throws IOException, JNCException {
85         when(reader.read()).thenThrow(new IOException());
86
87         checkingTask.run();
88
89         verify(reader).read();
90         verify(cache, never()).getConfiguration();
91         verify(writer, never()).writeToFile(any());
92         verify(cache, never()).update(any());
93     }
94 }