2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Pantheon.tech
4 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5 * Copyright (C) 2021 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.rest.controller
24 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
25 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
30 import org.modelmapper.ModelMapper
31 import org.onap.cps.api.CpsQueryService
32 import org.onap.cps.api.CpsAdminService
33 import org.onap.cps.api.CpsDataService
34 import org.onap.cps.api.CpsModuleService
35 import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException
36 import org.onap.cps.spi.exceptions.SchemaSetInUseException
37 import org.onap.cps.spi.model.Anchor
38 import org.onap.cps.spi.model.SchemaSet
39 import org.spockframework.spring.SpringBean
40 import org.springframework.beans.factory.annotation.Autowired
41 import org.springframework.beans.factory.annotation.Value
42 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
43 import org.springframework.http.HttpStatus
44 import org.springframework.http.MediaType
45 import org.springframework.mock.web.MockMultipartFile
46 import org.springframework.test.web.servlet.MockMvc
47 import org.springframework.util.LinkedMultiValueMap
48 import org.springframework.util.MultiValueMap
49 import spock.lang.Specification
50 import spock.lang.Unroll
53 class AdminRestControllerSpec extends Specification {
56 CpsModuleService mockCpsModuleService = Mock()
59 CpsAdminService mockCpsAdminService = Mock()
62 CpsDataService mockCpsDataService = Mock()
65 CpsQueryService mockCpsQueryService = Mock()
68 ModelMapper modelMapper = Mock()
73 @Value('${rest.api.cps-base-path}')
76 def dataspaceName = 'my_dataspace'
77 def anchor = new Anchor(name: 'my_anchor')
78 def anchorList = [anchor]
79 def anchorName = 'my_anchor'
80 def schemaSetName = 'my_schema_set'
82 def 'Create new dataspace.'() {
84 def createDataspaceEndpoint = "$basePath/v1/dataspaces";
85 when: 'post is invoked'
86 def response = mvc.perform(
87 post(createDataspaceEndpoint).param('dataspace-name', dataspaceName)).andReturn().response
88 then: 'service method is invoked with expected parameters'
89 1 * mockCpsAdminService.createDataspace(dataspaceName)
90 and: 'dataspace is create successfully'
91 response.status == HttpStatus.CREATED.value()
94 def 'Create dataspace over existing with same name.'() {
96 def createDataspaceEndpoint = "$basePath/v1/dataspaces";
97 and: 'the service method throws an exception indicating the dataspace is already defined'
98 def thrownException = new DataspaceAlreadyDefinedException("", new RuntimeException())
99 mockCpsAdminService.createDataspace(dataspaceName) >> { throw thrownException }
100 when: 'post is invoked'
101 def response = mvc.perform(post(createDataspaceEndpoint).param('dataspace-name', dataspaceName)).andReturn().response
102 then: 'dataspace creation fails'
103 response.status == HttpStatus.BAD_REQUEST.value()
106 def 'Create schema set from yang file.'() {
107 def yangResourceMapCapture
108 given: 'single yang file'
109 def multipartFile = createMultipartFile("filename.yang", "content")
111 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
112 when: 'file uploaded with schema set create request'
113 def response = mvc.perform(multipart(schemaSetEndpoint)
114 .file(multipartFile).param('schema-set-name', schemaSetName)).andReturn().response
115 then: 'associated service method is invoked with expected parameters'
116 1 * mockCpsModuleService.createSchemaSet(dataspaceName, schemaSetName, _) >>
117 { args -> yangResourceMapCapture = args[2] }
118 yangResourceMapCapture['filename.yang'] == 'content'
119 and: 'response code indicates success'
120 response.status == HttpStatus.CREATED.value()
123 def 'Create schema set from zip archive.'() {
124 def yangResourceMapCapture
125 given: 'zip archive with multiple .yang files inside'
126 def multipartFile = createZipMultipartFileFromResource("/yang-files-set.zip")
128 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
129 when: 'file uploaded with schema set create request'
130 def response = mvc.perform(multipart(schemaSetEndpoint)
131 .file(multipartFile).param('schema-set-name', schemaSetName)).andReturn().response
132 then: 'associated service method is invoked with expected parameters'
133 1 * mockCpsModuleService.createSchemaSet(dataspaceName, schemaSetName, _) >>
134 { args -> yangResourceMapCapture = args[2] }
135 yangResourceMapCapture['assembly.yang'] == "fake assembly content 1\n"
136 yangResourceMapCapture['component.yang'] == "fake component content 1\n"
137 and: 'response code indicates success'
138 response.status == HttpStatus.CREATED.value()
142 def 'Create schema set from zip archive having #caseDescriptor.'() {
144 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
145 when: 'zip archive having #caseDescriptor is uploaded with create schema set request'
146 def response = mvc.perform(multipart(schemaSetEndpoint)
147 .file(multipartFile).param('schema-set-name', schemaSetName)).andReturn().response
148 then: 'create schema set rejected'
149 response.status == HttpStatus.BAD_REQUEST.value()
150 where: 'following cases are tested'
151 caseDescriptor | multipartFile
152 'no .yang files inside' | createZipMultipartFileFromResource("/no-yang-files.zip")
153 'multiple .yang files with same name' | createZipMultipartFileFromResource("/yang-files-multiple-sets.zip")
156 def 'Create schema set from file with unsupported filename extension.'() {
157 given: 'file with unsupported filename extension (.doc)'
158 def multipartFile = createMultipartFile("filename.doc", "content")
160 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
161 when: 'file uploaded with schema set create request'
162 def response = mvc.perform(multipart(schemaSetEndpoint)
163 .file(multipartFile).param('schema-set-name', schemaSetName)).andReturn().response
164 then: 'create schema set rejected'
165 response.status == HttpStatus.BAD_REQUEST.value()
169 def 'Create schema set from #fileType file with IOException occurrence on processing.'() {
171 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
172 when: 'file uploaded with schema set create request'
173 def multipartFile = createMultipartFileForIOException(fileType)
174 def response = mvc.perform(multipart(schemaSetEndpoint)
175 .file(multipartFile).param('schema-set-name', schemaSetName)).andReturn().response
176 then: 'the error response returned indicating internal server error occurrence'
177 response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
178 where: 'following file types are used'
179 fileType << ['YANG', 'ZIP']
182 def 'Delete schema set.'() {
184 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
185 when: 'delete schema set endpoint is invoked'
186 def response = mvc.perform(delete(schemaSetEndpoint)).andReturn().response
187 then: 'associated service method is invoked with expected parameters'
188 1 * mockCpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED)
189 and: 'response code indicates success'
190 response.status == HttpStatus.NO_CONTENT.value()
193 def 'Delete schema set which is in use.'() {
194 given: 'service method throws an exception indicating the schema set is in use'
195 def thrownException = new SchemaSetInUseException(dataspaceName, schemaSetName)
196 mockCpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED) >>
197 { throw thrownException }
199 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
200 when: 'delete schema set endpoint is invoked'
201 def response = mvc.perform(delete(schemaSetEndpoint)).andReturn().response
202 then: 'schema set deletion fails with conflict response code'
203 response.status == HttpStatus.CONFLICT.value()
206 def 'Get existing schema set.'() {
207 given: 'service method returns a new schema set'
208 mockCpsModuleService.getSchemaSet(dataspaceName, schemaSetName) >>
209 new SchemaSet(name: schemaSetName, dataspaceName: dataspaceName)
211 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
212 when: 'get schema set API is invoked'
213 def response = mvc.perform(get(schemaSetEndpoint)).andReturn().response
214 then: 'the correct schema set is returned'
215 response.status == HttpStatus.OK.value()
216 response.getContentAsString().contains(schemaSetName)
219 def 'Create Anchor.'() {
220 given: 'request parameters'
221 def requestParams = new LinkedMultiValueMap<>()
222 requestParams.add('schema-set-name', schemaSetName)
223 requestParams.add('anchor-name', anchorName)
225 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors"
226 when: 'post is invoked'
227 def response = mvc.perform(post(anchorEndpoint).contentType(MediaType.APPLICATION_JSON)
228 .params(requestParams as MultiValueMap)).andReturn().response
229 then: 'anchor is created successfully'
230 1 * mockCpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName)
231 response.status == HttpStatus.CREATED.value()
232 response.getContentAsString().contains(anchorName)
235 def 'Get existing anchor.'() {
236 given: 'service method returns a list of anchors'
237 mockCpsAdminService.getAnchors(dataspaceName) >> anchorList
239 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors"
240 when: 'get all anchors API is invoked'
241 def response = mvc.perform(get(anchorEndpoint)).andReturn().response
242 then: 'the correct anchor is returned'
243 response.status == HttpStatus.OK.value()
244 response.getContentAsString().contains(anchorName)
247 def 'Get existing anchor by dataspace and anchor name.'() {
248 given: 'service method returns an anchor'
249 mockCpsAdminService.getAnchor(dataspaceName,anchorName) >> new Anchor(name: anchorName, dataspaceName: dataspaceName, schemaSetName:schemaSetName)
251 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName"
252 when: 'get anchor API is invoked'
253 def response = mvc.perform(get(anchorEndpoint))
254 .andReturn().response
255 def responseContent = response.getContentAsString()
256 then: 'the correct anchor is returned'
257 response.status == HttpStatus.OK.value()
258 responseContent.contains(anchorName)
259 responseContent.contains(dataspaceName)
260 responseContent.contains(schemaSetName)
263 def createMultipartFile(filename, content) {
264 return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
267 def createZipMultipartFileFromResource(resourcePath) {
268 return new MockMultipartFile("file", "test.zip", "application/zip",
269 getClass().getResource(resourcePath).getBytes())
272 def createMultipartFileForIOException(extension) {
273 def multipartFile = Mock(MockMultipartFile)
274 multipartFile.getOriginalFilename() >> "TEST." + extension
275 multipartFile.getBytes() >> { throw new IOException() }
276 multipartFile.getInputStream() >> { throw new IOException() }