9cb0702ee09ef53bee46f183273bcf007eddbeee
[so.git] /
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.svnfm.simulator.controllers;
22
23 import static org.mockito.ArgumentMatchers.anyString;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.when;
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
27 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
28 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest;
38 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
39 import org.onap.svnfm.simulator.constants.Constant;
40 import org.onap.svnfm.simulator.controller.SvnfmController;
41 import org.onap.svnfm.simulator.repository.VnfmCacheRepository;
42 import org.onap.svnfm.simulator.services.SvnfmService;
43 import org.springframework.http.MediaType;
44 import org.springframework.test.web.servlet.MockMvc;
45 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class TestSvnfmController {
49
50     @InjectMocks
51     private SvnfmController svnfmController;
52
53     private MockMvc mockMvc;
54
55     @Mock
56     private SvnfmService svnfmService;
57
58     @Mock
59     private VnfmCacheRepository vnfmCacheRepository;
60
61     @Before
62     public void setup() {
63         MockitoAnnotations.initMocks(this);
64         this.mockMvc = MockMvcBuilders.standaloneSetup(svnfmController).build();
65     }
66
67     @Test
68     public void createVnfInstanceTest() throws Exception {
69         final CreateVnfRequest createVnfRequest = new CreateVnfRequest();
70
71         createVnfRequest.setVnfdId("123456798");
72         createVnfRequest.setVnfInstanceName("createVnfInstanceTest");
73         createVnfRequest.setVnfInstanceDescription("createVnfInstanceTest");
74
75         when(vnfmCacheRepository.createVnf(eq(createVnfRequest), anyString())).thenReturn(new InlineResponse201());
76
77         final String body = (new ObjectMapper()).valueToTree(createVnfRequest).toString();
78         this.mockMvc
79                 .perform(post(Constant.BASE_URL + "/vnf_instances").content(body)
80                         .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
81                 .andExpect(status().isCreated()).andExpect(content().contentType(MediaType.APPLICATION_JSON));
82     }
83 }
84
85