Distributing Blueprint to DCAE Dashboard Issue-ID: DCAEGEN2-2385>
[dcaegen2/platform.git] / mod2 / catalog-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / SpecificationControllerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  org.onap.dcae
4  *  ================================================================================
5  *  Copyright (c) 2020 AT&T Intellectual Property. 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
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.platform.mod.web;
22
23 import org.onap.dcaegen2.platform.mod.model.specification.DeploymentType;
24 import org.onap.dcaegen2.platform.mod.model.restapi.SpecificationRequest;
25 import org.onap.dcaegen2.platform.mod.model.specification.Specification;
26 import org.onap.dcaegen2.platform.mod.web.controller.SpecificationController;
27 import org.onap.dcaegen2.platform.mod.web.service.specification.SpecificationService;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
33 import org.springframework.boot.test.mock.mockito.MockBean;
34 import org.springframework.http.MediaType;
35 import org.springframework.test.context.junit.jupiter.SpringExtension;
36 import org.springframework.test.web.servlet.MockMvc;
37
38 import static org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother.asJsonString;
39 import static org.onap.dcaegen2.platform.mod.objectmothers.MsInstanceObjectMother.MS_INSTANCE_ID;
40 import static org.onap.dcaegen2.platform.mod.objectmothers.SpecificationObjectMother.getMockSpecification;
41 import static org.onap.dcaegen2.platform.mod.objectmothers.SpecificationObjectMother.getSpecificationRequest;
42 import static org.hamcrest.Matchers.notNullValue;
43 import static org.mockito.Mockito.*;
44 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
45 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
46 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
47
48 @ExtendWith(SpringExtension.class)
49 @WebMvcTest(SpecificationController.class)
50 public class SpecificationControllerTest {
51
52     @Autowired
53     MockMvc mockMvc;
54
55     @MockBean
56     private SpecificationService mockSpecificationService;
57
58     @BeforeEach
59     void setup() {
60     }
61
62     @Test
63     void test_addSpecification_returnsSpecification() throws Exception {
64         //arrange
65         SpecificationRequest specificationRequest = getSpecificationRequest();
66         Specification specification = getMockSpecification(DeploymentType.DOCKER);
67
68         when(mockSpecificationService.createSpecification(MS_INSTANCE_ID, specificationRequest)).thenReturn(specification);
69
70         //act/assert
71         mockMvc.perform(post("/api/specification/" + MS_INSTANCE_ID)
72                 .contentType(MediaType.APPLICATION_JSON)
73                 .content(asJsonString(specificationRequest)).accept(MediaType.APPLICATION_JSON))
74                 .andExpect(jsonPath("$.id", notNullValue()))
75                 .andExpect(status().isCreated());
76         verify(mockSpecificationService, times(1)).createSpecification(MS_INSTANCE_ID, specificationRequest);
77     }
78
79 }