6da65728e8131e0fe6a786cb4bbbac0918e5b5fe
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 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.netconfsimulator.netconfcore.configuration;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.when;
27 import static org.mockito.MockitoAnnotations.initMocks;
28
29 import com.tailf.jnc.Element;
30 import com.tailf.jnc.JNCException;
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.springframework.mock.web.MockMultipartFile;
39 import org.springframework.util.ResourceUtils;
40
41 class NetconfConfigurationServiceTest {
42
43     @Mock
44     NetconfConfigurationReader reader;
45
46     @Mock
47     NetconfConfigurationEditor editor;
48
49     @InjectMocks
50     NetconfConfigurationService service;
51
52   private static String CURRENT_CONFIG_XML_STRING =
53       "<config xmlns=\"http://onap.org/pnf-simulator\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
54           + "  <itemValue1>100</itemValue1>\n"
55           + "  <itemValue2>200</itemValue2>\n"
56           + "</config>\n";
57
58     @BeforeEach
59     void setUp() {
60         initMocks(this);
61     }
62
63     @Test
64     void testShouldReturnCorrectCurrentConfiguration() throws IOException, JNCException {
65         String expectedConfiguration = CURRENT_CONFIG_XML_STRING;
66         when(reader.getRunningConfig()).thenReturn(CURRENT_CONFIG_XML_STRING);
67
68         String actualCurrentConfiguration = service.getCurrentConfiguration();
69
70         assertThat(actualCurrentConfiguration).isEqualToIgnoringCase(expectedConfiguration);
71     }
72
73     @Test
74     void testShouldThrowExceptionWhenCurrentConfigurationDoesNotExists() throws IOException, JNCException{
75         when(reader.getRunningConfig()).thenThrow(JNCException.class);
76
77         assertThatThrownBy(() -> service.getCurrentConfiguration()).isInstanceOf(JNCException.class);
78     }
79
80     @Test
81     void testShouldEditConfigurationSuccessfully() throws IOException, JNCException{
82         byte[] bytes =
83                 Files.readAllBytes(ResourceUtils.getFile("classpath:updatedConfig.xml").toPath());
84         MockMultipartFile editConfigXmlContent = new MockMultipartFile("editConfigXml", bytes);
85         ArgumentCaptor<Element> elementCaptor = ArgumentCaptor.forClass(Element.class);
86         doNothing().when(editor).editConfig(elementCaptor.capture());
87
88         service.editCurrentConfiguration(editConfigXmlContent);
89
90         assertThat(elementCaptor.getValue().toXMLString()).isEqualTo(CURRENT_CONFIG_XML_STRING);
91     }
92
93     @Test
94     void testShouldRaiseExceptionWhenMultipartFileIsInvalidXmlFile() throws IOException {
95         byte[] bytes =
96                 Files.readAllBytes(ResourceUtils.getFile("classpath:invalidXmlFile.xml").toPath());
97         MockMultipartFile editConfigXmlContent = new MockMultipartFile("editConfigXml", bytes);
98
99         assertThatThrownBy(() -> service.editCurrentConfiguration(editConfigXmlContent)).isInstanceOf(JNCException.class);
100     }
101
102 }