Move the VNFM Simulator into CSIT
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / vnfm-simulator / vnfm-service / src / test / java / org / onap / so / svnfm / simulator / controllers / TestSvnfmController.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.svnfm.simulator.controllers;
22
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;
54
55 @RunWith(MockitoJUnitRunner.class)
56 public class TestSvnfmController {
57
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";
69
70     @InjectMocks
71     private SvnfmController svnfmController;
72
73     private MockMvc mockMvc;
74
75     @Mock
76     private SvnfmService svnfmService;
77
78     @Mock
79     private VnfmCacheRepository vnfmCacheRepository;
80
81     @Before
82     public void setup() {
83         MockitoAnnotations.initMocks(this);
84         this.mockMvc = MockMvcBuilders.standaloneSetup(svnfmController).build();
85     }
86
87     @Test
88     public void createVnfInstanceTest() throws Exception {
89         final CreateVnfRequest createVnfRequest = new CreateVnfRequest();
90
91         createVnfRequest.setVnfdId(VNFD_ID);
92         createVnfRequest.setVnfInstanceName(VNF_INSTANCE_NAME);
93         createVnfRequest.setVnfInstanceDescription(VNF_INSTANCE_DESCRIPTION);
94
95         when(vnfmCacheRepository.createVnf(eq(createVnfRequest), anyString())).thenReturn(new InlineResponse201());
96
97         final String body = (new ObjectMapper()).valueToTree(createVnfRequest).toString();
98         this.mockMvc
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));
102     }
103
104     @Test
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);
109
110         when(vnfmCacheRepository.getVnf(VNF_INSTANCE_ID)).thenReturn(inlineResponse201);
111
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));
116
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);
122     }
123
124     @Test
125     public void test_instantiateVnf_usingValidVnfInstanceId_returnsHttpStatusAccepted() throws Exception {
126         when(svnfmService.instantiateVnf(VNF_INSTANCE_ID)).thenReturn(VNF_OPERATION_ID);
127
128         this.mockMvc
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));
132     }
133
134     @Test
135     public void test_deleteVnf_usingValidVnfInstanceId_returnsHttpStatusNoContent() throws Exception {
136         this.mockMvc
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));
140     }
141
142     @Test
143     public void test_terminateVnf_usingValidVnfInstanceId_returnsHttpStatusIsAccepted() throws Exception {
144         when(svnfmService.terminateVnf(VNF_INSTANCE_ID)).thenReturn(VNF_OPERATION_ID);
145
146         this.mockMvc
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));
150     }
151
152     @Test
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);
157
158         when(svnfmService.getOperationStatus(VNF_LCM_OP_OCC_ID)).thenReturn(inlineResponse200);
159
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());
164
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);
170     }
171
172     @Test
173     public void test_subscribeForNotifications_usingValidSubscriptionRequest_returnsHttpStatusCreated()
174             throws Exception {
175         final LccnSubscriptionRequest lccnSubscriptionRequest = new LccnSubscriptionRequest();
176         final String body = (new ObjectMapper()).valueToTree(lccnSubscriptionRequest).toString();
177
178         this.mockMvc
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));
182     }
183 }