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;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.never;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 import static org.mockito.MockitoAnnotations.initMocks;
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
32 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
34 import com.tailf.jnc.JNCException;
35 import java.io.IOException;
36 import java.nio.file.Files;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.onap.netconfsimulator.netconfcore.configuration.NetconfConfigurationService;
42 import org.onap.netconfsimulator.netconfcore.model.LoadModelResponse;
43 import org.onap.netconfsimulator.netconfcore.model.NetconfModelLoaderService;
44 import org.springframework.mock.web.MockMultipartFile;
45 import org.springframework.test.web.servlet.MockMvc;
46 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
47 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
48 import org.springframework.util.ResourceUtils;
49 import org.springframework.web.multipart.MultipartFile;
51 class NetconfControllerTest {
53 private MockMvc mockMvc;
56 private NetconfConfigurationService netconfService;
59 private NetconfModelLoaderService netconfModelLoaderService;
62 private NetconfController controller;
64 private static final String SAMPLE_CONFIGURATION = "<config xmlns=\"http://onap.org/pnf-simulator\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><itemValue1>11</itemValue1><itemValue2>22</itemValue2></config>";
69 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
73 void testShouldDigestMultipartFile() throws Exception {
75 Files.readAllBytes(ResourceUtils.getFile("classpath:updatedConfig.xml").toPath());
76 MockMultipartFile file = new MockMultipartFile("editConfigXml", bytes);
79 .perform(MockMvcRequestBuilders.multipart("/netconf/edit-config").file(file))
80 .andExpect(status().isAccepted());
82 verify(netconfService).editCurrentConfiguration(any(MultipartFile.class));
86 void testShouldThrowExceptionWhenEditConfigFileWithIncorrectNameProvided() throws Exception {
87 MockMultipartFile file = new MockMultipartFile("wrongName", new byte[0]);
90 .perform(MockMvcRequestBuilders.multipart("/netconf/edit-config").file(file))
91 .andExpect(status().isBadRequest());
93 verify(netconfService, never()).editCurrentConfiguration(any(MultipartFile.class));
97 void testShouldReturnCurrentConfiguration() throws Exception {
98 when(netconfService.getCurrentConfiguration()).thenReturn(SAMPLE_CONFIGURATION);
100 String contentAsString =
102 .perform(get("/netconf/get"))
103 .andExpect(status().isOk())
106 .getContentAsString();
108 verify(netconfService).getCurrentConfiguration();
109 assertThat(contentAsString).isEqualTo(SAMPLE_CONFIGURATION);
113 void testShouldReturnConfigurationForGivenPath() throws Exception {
114 when(netconfService.getCurrentConfiguration("sampleModel", "sampleContainer"))
115 .thenReturn(SAMPLE_CONFIGURATION);
117 String contentAsString =
119 .perform(get("/netconf/get/sampleModel/sampleContainer"))
120 .andExpect(status().isOk())
123 .getContentAsString();
125 verify(netconfService).getCurrentConfiguration("sampleModel", "sampleContainer");
126 assertThat(contentAsString).isEqualTo(SAMPLE_CONFIGURATION);
130 void testShouldRaiseBadRequestWhenConfigurationIsNotPresent() throws Exception {
131 when(netconfService.getCurrentConfiguration("sampleModel", "sampleContainer2"))
132 .thenThrow(new JNCException(JNCException.ELEMENT_MISSING, "/sampleModel:sampleContainer2"));
134 String contentAsString =
136 .perform(get("/netconf/get/sampleModel/sampleContainer2"))
137 .andExpect(status().isBadRequest())
140 .getContentAsString();
142 assertThat(contentAsString).isEqualTo("Element does not exists: /sampleModel:sampleContainer2");
146 void shouldThrowExceptionWhenNoConfigurationPresent() throws IOException, JNCException {
147 when(netconfService.getCurrentConfiguration()).thenThrow(JNCException.class);
149 assertThatThrownBy(() -> mockMvc.perform(get("/netconf/get")))
150 .hasRootCauseExactlyInstanceOf(JNCException.class);
154 void testShouldDeleteYangModel() throws Exception {
155 String responseOkString = "Alles klar";
156 String yangModelName = "someModel";
157 LoadModelResponse loadModelResponse = new LoadModelResponse(200, responseOkString);
158 String uri = String.format("/netconf/model/%s", yangModelName);
159 when(netconfModelLoaderService.deleteYangModel(yangModelName)).thenReturn(loadModelResponse);
161 String contentAsString =
163 .perform(delete(uri))
164 .andExpect(status().isOk())
167 .getContentAsString();
169 verify(netconfModelLoaderService).deleteYangModel(yangModelName);
170 assertThat(contentAsString).isEqualTo(responseOkString);