73fb627ea27cba625bcb2e2bdcca7421474d4770
[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;
22
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;
33
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;
50
51 class NetconfControllerTest {
52
53     private MockMvc mockMvc;
54
55     @Mock
56     private NetconfConfigurationService netconfService;
57
58     @Mock
59     private NetconfModelLoaderService netconfModelLoaderService;
60
61     @InjectMocks
62     private NetconfController controller;
63
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>";
65
66     @BeforeEach
67     void setUp() {
68         initMocks(this);
69         mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
70     }
71
72     @Test
73     void testShouldDigestMultipartFile() throws Exception {
74         byte[] bytes =
75             Files.readAllBytes(ResourceUtils.getFile("classpath:updatedConfig.xml").toPath());
76         MockMultipartFile file = new MockMultipartFile("editConfigXml", bytes);
77
78         mockMvc
79             .perform(MockMvcRequestBuilders.multipart("/netconf/edit-config").file(file))
80             .andExpect(status().isAccepted());
81
82         verify(netconfService).editCurrentConfiguration(any(MultipartFile.class));
83     }
84
85     @Test
86     void testShouldThrowExceptionWhenEditConfigFileWithIncorrectNameProvided() throws Exception {
87         MockMultipartFile file = new MockMultipartFile("wrongName", new byte[0]);
88
89         mockMvc
90             .perform(MockMvcRequestBuilders.multipart("/netconf/edit-config").file(file))
91             .andExpect(status().isBadRequest());
92
93         verify(netconfService, never()).editCurrentConfiguration(any(MultipartFile.class));
94     }
95
96     @Test
97     void testShouldReturnCurrentConfiguration() throws Exception {
98         when(netconfService.getCurrentConfiguration()).thenReturn(SAMPLE_CONFIGURATION);
99
100         String contentAsString =
101             mockMvc
102                 .perform(get("/netconf/get"))
103                 .andExpect(status().isOk())
104                 .andReturn()
105                 .getResponse()
106                 .getContentAsString();
107
108         verify(netconfService).getCurrentConfiguration();
109         assertThat(contentAsString).isEqualTo(SAMPLE_CONFIGURATION);
110     }
111
112     @Test
113     void testShouldReturnConfigurationForGivenPath() throws Exception {
114         when(netconfService.getCurrentConfiguration("sampleModel", "sampleContainer"))
115             .thenReturn(SAMPLE_CONFIGURATION);
116
117         String contentAsString =
118             mockMvc
119                 .perform(get("/netconf/get/sampleModel/sampleContainer"))
120                 .andExpect(status().isOk())
121                 .andReturn()
122                 .getResponse()
123                 .getContentAsString();
124
125         verify(netconfService).getCurrentConfiguration("sampleModel", "sampleContainer");
126         assertThat(contentAsString).isEqualTo(SAMPLE_CONFIGURATION);
127     }
128
129     @Test
130     void testShouldRaiseBadRequestWhenConfigurationIsNotPresent() throws Exception {
131         when(netconfService.getCurrentConfiguration("sampleModel", "sampleContainer2"))
132             .thenThrow(new JNCException(JNCException.ELEMENT_MISSING, "/sampleModel:sampleContainer2"));
133
134         String contentAsString =
135             mockMvc
136             .perform(get("/netconf/get/sampleModel/sampleContainer2"))
137             .andExpect(status().isBadRequest())
138             .andReturn()
139             .getResponse()
140             .getContentAsString();
141
142         assertThat(contentAsString).isEqualTo("Element does not exists: /sampleModel:sampleContainer2");
143     }
144
145     @Test
146     void shouldThrowExceptionWhenNoConfigurationPresent() throws IOException, JNCException {
147         when(netconfService.getCurrentConfiguration()).thenThrow(JNCException.class);
148
149         assertThatThrownBy(() -> mockMvc.perform(get("/netconf/get")))
150             .hasRootCauseExactlyInstanceOf(JNCException.class);
151     }
152
153     @Test
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);
160
161         String contentAsString =
162             mockMvc
163                 .perform(delete(uri))
164                 .andExpect(status().isOk())
165                 .andReturn()
166                 .getResponse()
167                 .getContentAsString();
168
169         verify(netconfModelLoaderService).deleteYangModel(yangModelName);
170         assertThat(contentAsString).isEqualTo(responseOkString);
171     }
172 }