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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.rest.controller
23 import org.modelmapper.ModelMapper
24 import org.onap.cps.api.CpsAdminService
25 import org.onap.cps.api.CpsDataService
26 import org.onap.cps.api.CpsModuleService
27 import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException
28 import org.onap.cps.spi.exceptions.SchemaSetInUseException
29 import org.onap.cps.spi.model.Anchor
30 import org.onap.cps.spi.model.SchemaSet
31 import org.spockframework.spring.SpringBean
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.beans.factory.annotation.Value
34 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
35 import org.springframework.http.HttpStatus
36 import org.springframework.http.MediaType
37 import org.springframework.mock.web.MockMultipartFile
38 import org.springframework.test.web.servlet.MockMvc
39 import org.springframework.util.LinkedMultiValueMap
40 import org.springframework.util.MultiValueMap
41 import spock.lang.Specification
42 import spock.lang.Unroll
44 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
45 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
46 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
47 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart
48 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
51 class AdminRestControllerSpec extends Specification {
54 CpsModuleService mockCpsModuleService = Mock()
57 CpsAdminService mockCpsAdminService = Mock()
60 CpsDataService mockCpsDataService = Mock()
63 ModelMapper modelMapper = Mock()
68 @Value('${rest.api.base-path}')
71 def anchorsEndpoint = '/v1/dataspaces/my_dataspace/anchors'
72 def schemaSetsEndpoint = '/v1/dataspaces/test-dataspace/schema-sets'
73 def schemaSetEndpoint = schemaSetsEndpoint + '/my_schema_set'
75 def anchor = new Anchor(name: 'my_anchor')
76 def anchorList = [anchor]
78 def 'Create new dataspace'() {
80 def response = performCreateDataspaceRequest("new-dataspace")
81 then: 'Service method is invoked with expected parameters'
82 1 * mockCpsAdminService.createDataspace("new-dataspace")
83 and: 'Dataspace is create successfully'
84 response.status == HttpStatus.CREATED.value()
87 def 'Create dataspace over existing with same name'() {
89 def thrownException = new DataspaceAlreadyDefinedException("", new RuntimeException())
90 mockCpsAdminService.createDataspace("existing-dataspace") >> { throw thrownException }
92 def response = performCreateDataspaceRequest("existing-dataspace")
93 then: 'Dataspace creation fails'
94 response.status == HttpStatus.BAD_REQUEST.value()
97 def 'Create schema set from yang file.'() {
98 def yangResourceMapCapture
99 given: 'single yang file'
100 def multipartFile = createMultipartFile("filename.yang", "content")
101 when: 'file uploaded with schema set create request'
102 def response = performCreateSchemaSetRequest(multipartFile)
103 then: 'associated service method is invoked with expected parameters'
104 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
105 { args -> yangResourceMapCapture = args[2] }
106 yangResourceMapCapture['filename.yang'] == 'content'
107 and: 'response code indicates success'
108 response.status == HttpStatus.CREATED.value()
111 def 'Create schema set from zip archive.'() {
112 def yangResourceMapCapture
113 given: 'zip archive with multiple .yang files inside'
114 def multipartFile = createZipMultipartFileFromResource("/yang-files-set.zip")
115 when: 'file uploaded with schema set create request'
116 def response = performCreateSchemaSetRequest(multipartFile)
117 then: 'associated service method is invoked with expected parameters'
118 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
119 { args -> yangResourceMapCapture = args[2] }
120 yangResourceMapCapture['assembly.yang'] == "fake assembly content 1\n"
121 yangResourceMapCapture['component.yang'] == "fake component content 1\n"
122 and: 'response code indicates success'
123 response.status == HttpStatus.CREATED.value()
127 def 'Create schema set from zip archive having #caseDescriptor.'() {
128 when: 'zip archive having #caseDescriptor is uploaded with create schema set request'
129 def response = performCreateSchemaSetRequest(multipartFile)
130 then: 'create schema set rejected'
131 response.status == HttpStatus.BAD_REQUEST.value()
132 where: 'following cases are tested'
133 caseDescriptor | multipartFile
134 'no .yang files inside' | createZipMultipartFileFromResource("/no-yang-files.zip")
135 'multiple .yang files with same name' | createZipMultipartFileFromResource("/yang-files-multiple-sets.zip")
138 def 'Create schema set from file with unsupported filename extension.'() {
139 given: 'file with unsupported filename extension (.doc)'
140 def multipartFile = createMultipartFile("filename.doc", "content")
141 when: 'file uploaded with schema set create request'
142 def response = performCreateSchemaSetRequest(multipartFile)
143 then: 'create schema set rejected'
144 response.status == HttpStatus.BAD_REQUEST.value()
148 def 'Create schema set from #fileType file with IOException occurrence on processing.'() {
149 when: 'file uploaded with schema set create request'
150 def response = performCreateSchemaSetRequest(createMultipartFileForIOException(fileType))
151 then: 'the error response returned indicating internal server error occurrence'
152 response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
153 where: 'following file types are used'
154 fileType << ['YANG', 'ZIP']
157 def 'Delete schema set.'() {
158 when: 'delete schema set endpoint is invoked'
159 def response = performDeleteRequest(schemaSetEndpoint)
160 then: 'associated service method is invoked with expected parameters'
161 1 * mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED)
162 and: 'response code indicates success'
163 response.status == HttpStatus.NO_CONTENT.value()
166 def 'Delete schema set which is in use.'() {
167 given: 'the service method throws an exception indicating the schema set is in use'
168 def thrownException = new SchemaSetInUseException('test-dataspace', 'my_schema_set')
169 mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED) >>
170 { throw thrownException }
171 when: 'delete schema set endpoint is invoked'
172 def response = performDeleteRequest(schemaSetEndpoint)
173 then: 'schema set deletion fails with conflict response code'
174 response.status == HttpStatus.CONFLICT.value()
177 def performCreateDataspaceRequest(String dataspaceName) {
179 post("$basePath/v1/dataspaces").param('dataspace-name', dataspaceName)
180 ).andReturn().response
183 def createMultipartFile(filename, content) {
184 return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
187 def createZipMultipartFileFromResource(resourcePath) {
188 return new MockMultipartFile("file", "test.zip", "application/zip",
189 getClass().getResource(resourcePath).getBytes())
192 def createMultipartFileForIOException(extension) {
193 def multipartFile = Mock(MockMultipartFile)
194 multipartFile.getOriginalFilename() >> "TEST." + extension
195 multipartFile.getBytes() >> { throw new IOException() }
196 multipartFile.getInputStream() >> { throw new IOException() }
200 def performCreateSchemaSetRequest(multipartFile) {
202 multipart("$basePath$schemaSetsEndpoint")
204 .param('schema-set-name', 'test-schema-set')
205 ).andReturn().response
208 def performDeleteRequest(String deleteEndpoint) {
209 return mvc.perform(delete("$basePath$deleteEndpoint")).andReturn().response
212 def 'Get existing schema set'() {
214 mockCpsModuleService.getSchemaSet('test-dataspace', 'my_schema_set') >>
215 new SchemaSet(name: 'my_schema_set', dataspaceName: 'test-dataspace')
216 when: 'get schema set API is invoked'
217 def response = mvc.perform(get("$basePath$schemaSetEndpoint")).andReturn().response
218 then: 'the correct schema set is returned'
219 response.status == HttpStatus.OK.value()
220 response.getContentAsString().contains('my_schema_set')
223 def 'Create Anchor'() {
225 def requestParams = new LinkedMultiValueMap<>()
226 requestParams.add('schema-set-name', 'my_schema-set')
227 requestParams.add('anchor-name', 'my_anchor')
228 when: 'post is invoked'
229 def response = mvc.perform(post("$basePath$anchorsEndpoint").contentType(MediaType.APPLICATION_JSON)
230 .params(requestParams as MultiValueMap)).andReturn().response
231 then: 'Anchor is created successfully'
232 1 * mockCpsAdminService.createAnchor('my_dataspace', 'my_schema-set', 'my_anchor')
233 response.status == HttpStatus.CREATED.value()
234 response.getContentAsString().contains('my_anchor')
237 def 'Get existing anchor'() {
239 mockCpsAdminService.getAnchors('my_dataspace') >> anchorList
240 when: 'get all anchors API is invoked'
241 def response = mvc.perform(get("$basePath$anchorsEndpoint")).andReturn().response
242 then: 'the correct anchor is returned'
243 response.status == HttpStatus.OK.value()
244 response.getContentAsString().contains('my_anchor')