2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.svnfm.simulator.controllers;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.anyString;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.when;
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
30 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
31 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest;
41 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
42 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum;
43 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
44 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
45 import org.onap.so.svnfm.simulator.constants.Constant;
46 import org.onap.so.svnfm.simulator.controller.SvnfmController;
47 import org.onap.so.svnfm.simulator.repository.VnfmCacheRepository;
48 import org.onap.so.svnfm.simulator.services.SvnfmService;
49 import org.springframework.http.MediaType;
50 import org.springframework.test.web.servlet.MockMvc;
51 import org.springframework.test.web.servlet.MvcResult;
52 import org.springframework.test.web.servlet.ResultActions;
53 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
55 @RunWith(MockitoJUnitRunner.class)
56 public class TestSvnfmController {
58 private static final String VNF_INSTANCES_URL = "/vnf_instances/";
59 private static final String VNFD_ID = "vnfdId";
60 private static final String VNF_INSTANCE_NAME = "vnfInstanceName";
61 private static final String VNF_INSTANCE_DESCRIPTION = "vnfInstanceDescription";
62 private static final String VNF_INSTANCE_ID = "vnfInstanceId";
63 private static final String VNF_OPERATION_ID = "vnfOperationId";
64 private static final String VNF_INSTANTIATE_URL = "/instantiate";
65 private static final String VNF_TERMINATE_URL = "/terminate";
66 private static final String VNF_LCM_OP_OCC_ID = "vnfLcmOpOccId";
67 private static final String VNF_OPERATION_STATUS_URL = "/vnf_lcm_op_occs/";
68 private static final String SUBSCRIPTIONS_URL = "/subscriptions";
71 private SvnfmController svnfmController;
73 private MockMvc mockMvc;
76 private SvnfmService svnfmService;
79 private VnfmCacheRepository vnfmCacheRepository;
83 MockitoAnnotations.initMocks(this);
84 this.mockMvc = MockMvcBuilders.standaloneSetup(svnfmController).build();
88 public void createVnfInstanceTest() throws Exception {
89 final CreateVnfRequest createVnfRequest = new CreateVnfRequest();
91 createVnfRequest.setVnfdId(VNFD_ID);
92 createVnfRequest.setVnfInstanceName(VNF_INSTANCE_NAME);
93 createVnfRequest.setVnfInstanceDescription(VNF_INSTANCE_DESCRIPTION);
95 when(vnfmCacheRepository.createVnf(eq(createVnfRequest), anyString())).thenReturn(new InlineResponse201());
97 final String body = (new ObjectMapper()).valueToTree(createVnfRequest).toString();
99 .perform(post(Constant.BASE_URL + VNF_INSTANCES_URL).content(body)
100 .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
101 .andExpect(status().isCreated()).andExpect(content().contentType(MediaType.APPLICATION_JSON));
105 public void test_getVnf_usingValidVnfInstanceId_vnfInstanceRetrievedSuccessfully() throws Exception {
106 final InlineResponse201 inlineResponse201 = new InlineResponse201();
107 inlineResponse201.setId(VNF_INSTANCE_ID);
108 inlineResponse201.setVnfInstanceName(VNF_INSTANCE_NAME);
110 when(vnfmCacheRepository.getVnf(VNF_INSTANCE_ID)).thenReturn(inlineResponse201);
112 final ResultActions resultActions = this.mockMvc
113 .perform(get(Constant.BASE_URL + VNF_INSTANCES_URL + VNF_INSTANCE_ID)
114 .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
115 .andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
117 final MvcResult result = resultActions.andReturn();
118 final String content = result.getResponse().getContentAsString();
119 final InlineResponse201 response201 = new ObjectMapper().readValue(content, InlineResponse201.class);
120 assertThat(response201.getId()).isEqualTo(VNF_INSTANCE_ID);
121 assertThat(response201.getVnfInstanceName()).isEqualTo(VNF_INSTANCE_NAME);
125 public void test_instantiateVnf_usingValidVnfInstanceId_returnsHttpStatusAccepted() throws Exception {
126 when(svnfmService.instantiateVnf(VNF_INSTANCE_ID)).thenReturn(VNF_OPERATION_ID);
129 .perform(post(Constant.BASE_URL + VNF_INSTANCES_URL + VNF_INSTANCE_ID + VNF_INSTANTIATE_URL)
130 .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
131 .andExpect(status().isAccepted()).andExpect(content().contentType(MediaType.APPLICATION_JSON));
135 public void test_deleteVnf_usingValidVnfInstanceId_returnsHttpStatusNoContent() throws Exception {
137 .perform(delete(Constant.BASE_URL + VNF_INSTANCES_URL + VNF_INSTANCE_ID)
138 .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
139 .andExpect(status().isNoContent()).andExpect(content().contentType(MediaType.APPLICATION_JSON));
143 public void test_terminateVnf_usingValidVnfInstanceId_returnsHttpStatusIsAccepted() throws Exception {
144 when(svnfmService.terminateVnf(VNF_INSTANCE_ID)).thenReturn(VNF_OPERATION_ID);
147 .perform(post(Constant.BASE_URL + VNF_INSTANCES_URL + VNF_INSTANCE_ID + VNF_TERMINATE_URL)
148 .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
149 .andExpect(status().isAccepted()).andExpect(content().contentType(MediaType.APPLICATION_JSON));
153 public void test_getOperationStatus_usingValidOperationId_operationStatusRetrievedSuccessfully() throws Exception {
154 final InlineResponse200 inlineResponse200 = new InlineResponse200();
155 inlineResponse200.setId(VNF_LCM_OP_OCC_ID);
156 inlineResponse200.setOperation(OperationEnum.INSTANTIATE);
158 when(svnfmService.getOperationStatus(VNF_LCM_OP_OCC_ID)).thenReturn(inlineResponse200);
160 final ResultActions resultActions = this.mockMvc
161 .perform(get(Constant.BASE_URL + VNF_OPERATION_STATUS_URL + VNF_LCM_OP_OCC_ID)
162 .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
163 .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
165 final MvcResult result = resultActions.andReturn();
166 final String content = result.getResponse().getContentAsString();
167 final InlineResponse200 response200 = new ObjectMapper().readValue(content, InlineResponse200.class);
168 assertThat(response200.getId()).isEqualTo(VNF_LCM_OP_OCC_ID);
169 assertThat(response200.getOperation()).isEqualTo(OperationEnum.INSTANTIATE);
173 public void test_subscribeForNotifications_usingValidSubscriptionRequest_returnsHttpStatusCreated()
175 final LccnSubscriptionRequest lccnSubscriptionRequest = new LccnSubscriptionRequest();
176 final String body = (new ObjectMapper()).valueToTree(lccnSubscriptionRequest).toString();
179 .perform(post(Constant.BASE_URL + SUBSCRIPTIONS_URL).content(body)
180 .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
181 .andExpect(status().isCreated()).andExpect(content().contentType(MediaType.APPLICATION_JSON));