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
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.netconfsimulator.netconfcore.configuration;
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;
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;
41 class NetconfConfigurationServiceTest {
44 NetconfConfigurationReader reader;
47 NetconfConfigurationEditor editor;
50 NetconfConfigurationService service;
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"
64 void testShouldReturnCorrectCurrentConfiguration() throws IOException, JNCException {
65 String expectedConfiguration = CURRENT_CONFIG_XML_STRING;
66 when(reader.getRunningConfig()).thenReturn(CURRENT_CONFIG_XML_STRING);
68 String actualCurrentConfiguration = service.getCurrentConfiguration();
70 assertThat(actualCurrentConfiguration).isEqualToIgnoringCase(expectedConfiguration);
74 void testShouldThrowExceptionWhenCurrentConfigurationDoesNotExists() throws IOException, JNCException{
75 when(reader.getRunningConfig()).thenThrow(JNCException.class);
77 assertThatThrownBy(() -> service.getCurrentConfiguration()).isInstanceOf(JNCException.class);
81 void testShouldEditConfigurationSuccessfully() throws IOException, JNCException{
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());
88 service.editCurrentConfiguration(editConfigXmlContent);
90 assertThat(elementCaptor.getValue().toXMLString()).isEqualTo(CURRENT_CONFIG_XML_STRING);
94 void testShouldRaiseExceptionWhenMultipartFileIsInvalidXmlFile() throws IOException {
96 Files.readAllBytes(ResourceUtils.getFile("classpath:invalidXmlFile.xml").toPath());
97 MockMultipartFile editConfigXmlContent = new MockMultipartFile("editConfigXml", bytes);
99 assertThatThrownBy(() -> service.editCurrentConfiguration(editConfigXmlContent)).isInstanceOf(JNCException.class);