2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.model;
24 import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import org.apache.http.HttpEntity;
34 import org.apache.http.HttpResponse;
35 import org.apache.http.StatusLine;
36 import org.apache.http.client.HttpClient;
37 import org.apache.http.client.methods.HttpDelete;
38 import org.apache.http.client.methods.HttpPost;
39 import org.apache.http.client.methods.HttpRequestBase;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42 import org.mockito.ArgumentCaptor;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.springframework.web.multipart.MultipartFile;
47 class NetconfModelLoaderServiceTest {
50 private HttpClient httpClient;
52 private NetconfModelLoaderService modelLoaderService;
56 MockitoAnnotations.initMocks(this);
57 modelLoaderService = new NetconfModelLoaderService(httpClient);
62 void shouldSendMultipartToServer() throws IOException {
64 String loadModelAddress = modelLoaderService.getBackendAddress();
65 makeMockClientReturnStatusOk(httpClient, HttpPost.class);
66 ArgumentCaptor<HttpPost> postArgumentCaptor = ArgumentCaptor.forClass(HttpPost.class);
67 MultipartFile yangMmodel = mock(MultipartFile.class);
68 MultipartFile initialConfig = mock(MultipartFile.class);
69 String moduleName = "moduleName";
70 when(yangMmodel.getInputStream()).thenReturn(getEmptyImputStream());
71 when(initialConfig.getInputStream()).thenReturn(getEmptyImputStream());
74 LoadModelResponse response = modelLoaderService.loadYangModel(yangMmodel, initialConfig, moduleName);
77 verify(httpClient).execute(postArgumentCaptor.capture());
78 HttpPost sentPost = postArgumentCaptor.getValue();
79 assertThat(response.getStatusCode()).isEqualTo(200);
80 assertThat(response.getMessage()).isEqualTo("");
81 assertThat(sentPost.getURI().toString()).isEqualTo(loadModelAddress);
82 assertThat(sentPost.getEntity().getContentType().getElements()[0].getName()).isEqualTo("multipart/form-data");
86 void shouldSendDeleteRequestToServer() throws IOException {
88 String yangModelName = "sampleModel";
89 String deleteModelAddress = modelLoaderService.getDeleteAddress(yangModelName);
90 makeMockClientReturnStatusOk(httpClient, HttpDelete.class);
91 ArgumentCaptor<HttpDelete> deleteArgumentCaptor = ArgumentCaptor.forClass(HttpDelete.class);
94 LoadModelResponse response = modelLoaderService.deleteYangModel(yangModelName);
97 verify(httpClient).execute(deleteArgumentCaptor.capture());
98 HttpDelete sendDelete = deleteArgumentCaptor.getValue();
99 assertThat(response.getStatusCode()).isEqualTo(200);
100 assertThat(response.getMessage()).isEqualTo("");
101 assertThat(sendDelete.getURI().toString()).isEqualTo(deleteModelAddress);
104 private void makeMockClientReturnStatusOk(HttpClient client,
105 Class<? extends HttpRequestBase> httpMethodClass) throws IOException {
106 HttpResponse httpResponse = mock(HttpResponse.class);
107 StatusLine mockStatus = mock(StatusLine.class);
108 HttpEntity mockEntity = mock(HttpEntity.class);
110 when(client.execute(any(httpMethodClass))).thenReturn(httpResponse);
111 when(httpResponse.getStatusLine()).thenReturn(mockStatus);
112 when(mockStatus.getStatusCode()).thenReturn(200);
113 when(httpResponse.getEntity()).thenReturn(mockEntity);
114 when(mockEntity.getContent()).thenReturn(getEmptyImputStream());
117 private InputStream getEmptyImputStream() {
118 return new ByteArrayInputStream("".getBytes());