WebMVC groovy test cases for AdminRestController
[cps.git] / cps-rest / src / test / groovy / org / onap / cps / rest / controller / AdminRestControllerSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Pantheon.tech
4  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.rest.controller
22
23 import org.modelmapper.ModelMapper
24 import org.onap.cps.api.CpsAdminService
25 import org.onap.cps.api.CpsModuleService
26 import org.onap.cps.spi.model.Anchor
27 import org.spockframework.spring.SpringBean
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
30 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
31 import org.springframework.http.HttpStatus
32 import org.springframework.http.MediaType
33 import org.springframework.mock.web.MockMultipartFile
34 import org.springframework.test.web.servlet.MockMvc
35 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
36 import org.springframework.util.LinkedMultiValueMap
37 import org.springframework.util.MultiValueMap
38 import spock.lang.Specification
39
40 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
41 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
42
43 @WebMvcTest
44 class AdminRestControllerSpec extends Specification {
45
46     @SpringBean
47     CpsModuleService mockCpsModuleService = Mock()
48
49     @SpringBean
50     CpsAdminService mockCpsAdminService = Mock()
51
52     @SpringBean
53     ModelMapper modelMapper = Mock()
54
55     @Autowired
56     MockMvc mvc
57
58     def anchorsEndpoint = '/v1/dataspaces/my_dataspace/anchors'
59
60     def anchor = new Anchor(name: 'my_anchor')
61     def anchorList = [anchor]
62
63     def 'Create schema set from yang file'() {
64         def yangResourceMapCapture
65         given:
66             def multipartFile = createMultipartFile("filename.yang", "content")
67         when:
68             def response = performCreateSchemaSetRequest(multipartFile)
69         then: 'Service method is invoked with expected parameters'
70             1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
71                     { args -> yangResourceMapCapture = args[2] }
72             yangResourceMapCapture['filename.yang'] == 'content'
73         and: 'Response code indicates success'
74             response.status == HttpStatus.CREATED.value()
75     }
76
77     def 'Create schema set from file with invalid filename extension'() {
78         given:
79             def multipartFile = createMultipartFile("filename.doc", "content")
80         when:
81             def response = performCreateSchemaSetRequest(multipartFile)
82         then:
83             response.status == HttpStatus.BAD_REQUEST.value()
84     }
85
86     def createMultipartFile(filename, content) {
87         return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
88     }
89
90     def performCreateSchemaSetRequest(multipartFile) {
91         return mvc.perform(
92                 MockMvcRequestBuilders
93                         .multipart('/v1/dataspaces/test-dataspace/schema-sets')
94                         .file(multipartFile)
95                         .param('schemaSetName', 'test-schema-set')
96         ).andReturn().response
97     }
98
99     def 'when createAnchor API is called, the response status is 201. '() {
100         given:
101             def requestParams = new LinkedMultiValueMap<>()
102             requestParams.add('schema-set-name', 'my_schema-set')
103             requestParams.add('anchor-name', 'my_anchor')
104         when: 'post is invoked'
105             def response = mvc.perform(post(anchorsEndpoint).contentType(MediaType.APPLICATION_JSON)
106                     .params(requestParams as MultiValueMap)).andReturn().response
107         then: 'Status is 201 and the response is the name of the created anchor -> my_anchor'
108             1 * mockCpsAdminService.createAnchor('my_dataspace', 'my_schema-set', 'my_anchor')
109             assert response.status == HttpStatus.CREATED.value()
110             assert response.getContentAsString().contains('my_anchor')
111     }
112
113     def 'when get all anchors for a dataspace API is called, the response status is 200 '() {
114         given:
115             mockCpsAdminService.getAnchors('my_dataspace') >> anchorList
116         when: 'get all anchors API is invoked'
117             def response = mvc.perform(get(anchorsEndpoint)).andReturn().response
118         then: 'Status is 200 and the response is Collection of Anchors containing anchor name -> my_anchor'
119             assert response.status == HttpStatus.OK.value()
120             assert response.getContentAsString().contains('my_anchor')
121     }
122 }