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