2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Pantheon.tech
4 * Modifications Copyright (C) 2020, 2021 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.CpsAdminService
32 import org.onap.cps.api.CpsDataService
33 import org.onap.cps.api.CpsModuleService
34 import org.onap.cps.api.CpsQueryService
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.Unroll
52 class AdminRestControllerSpec extends RestControllerSpecification {
55 CpsModuleService mockCpsModuleService = Mock()
58 CpsAdminService mockCpsAdminService = Mock()
61 CpsDataService mockCpsDataService = Mock()
64 CpsQueryService mockCpsQueryService = Mock()
67 ModelMapper modelMapper = Mock()
72 @Value('${rest.api.cps-base-path}')
75 def dataspaceName = 'my_dataspace'
76 def anchor = new Anchor(name: 'my_anchor')
77 def anchorList = [anchor]
78 def anchorName = 'my_anchor'
79 def schemaSetName = 'my_schema_set'
81 def 'Create new dataspace.'() {
83 def createDataspaceEndpoint = "$basePath/v1/dataspaces";
84 when: 'post is invoked'
87 post(createDataspaceEndpoint)
88 .header("Authorization", getAuthorizationHeader())
89 .param('dataspace-name', dataspaceName))
91 then: 'service method is invoked with expected parameters'
92 1 * mockCpsAdminService.createDataspace(dataspaceName)
93 and: 'dataspace is create successfully'
94 response.status == HttpStatus.CREATED.value()
97 def 'Create dataspace over existing with same name.'() {
99 def createDataspaceEndpoint = "$basePath/v1/dataspaces";
100 and: 'the service method throws an exception indicating the dataspace is already defined'
101 def thrownException = new DataspaceAlreadyDefinedException("", new RuntimeException())
102 mockCpsAdminService.createDataspace(dataspaceName) >> { throw thrownException }
103 when: 'post is invoked'
106 post(createDataspaceEndpoint)
107 .header("Authorization", getAuthorizationHeader())
108 .param('dataspace-name', dataspaceName))
109 .andReturn().response
110 then: 'dataspace creation fails'
111 response.status == HttpStatus.BAD_REQUEST.value()
114 def 'Create schema set from yang file.'() {
115 def yangResourceMapCapture
116 given: 'single yang file'
117 def multipartFile = createMultipartFile("filename.yang", "content")
119 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
120 when: 'file uploaded with schema set create request'
123 multipart(schemaSetEndpoint)
125 .header("Authorization", getAuthorizationHeader())
126 .param('schema-set-name', schemaSetName))
127 .andReturn().response
128 then: 'associated service method is invoked with expected parameters'
129 1 * mockCpsModuleService.createSchemaSet(dataspaceName, schemaSetName, _) >>
130 { args -> yangResourceMapCapture = args[2] }
131 yangResourceMapCapture['filename.yang'] == 'content'
132 and: 'response code indicates success'
133 response.status == HttpStatus.CREATED.value()
136 def 'Create schema set from zip archive.'() {
137 def yangResourceMapCapture
138 given: 'zip archive with multiple .yang files inside'
139 def multipartFile = createZipMultipartFileFromResource("/yang-files-set.zip")
141 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
142 when: 'file uploaded with schema set create request'
145 multipart(schemaSetEndpoint)
147 .header("Authorization", getAuthorizationHeader())
148 .param('schema-set-name', schemaSetName))
149 .andReturn().response
150 then: 'associated service method is invoked with expected parameters'
151 1 * mockCpsModuleService.createSchemaSet(dataspaceName, schemaSetName, _) >>
152 { args -> yangResourceMapCapture = args[2] }
153 yangResourceMapCapture['assembly.yang'] == "fake assembly content 1\n"
154 yangResourceMapCapture['component.yang'] == "fake component content 1\n"
155 and: 'response code indicates success'
156 response.status == HttpStatus.CREATED.value()
160 def 'Create schema set from zip archive having #caseDescriptor.'() {
162 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
163 when: 'zip archive having #caseDescriptor is uploaded with create schema set request'
166 multipart(schemaSetEndpoint)
168 .header("Authorization", getAuthorizationHeader())
169 .param('schema-set-name', schemaSetName))
170 .andReturn().response
171 then: 'create schema set rejected'
172 response.status == HttpStatus.BAD_REQUEST.value()
173 where: 'following cases are tested'
174 caseDescriptor | multipartFile
175 'no .yang files inside' | createZipMultipartFileFromResource("/no-yang-files.zip")
176 'multiple .yang files with same name' | createZipMultipartFileFromResource("/yang-files-multiple-sets.zip")
179 def 'Create schema set from file with unsupported filename extension.'() {
180 given: 'file with unsupported filename extension (.doc)'
181 def multipartFile = createMultipartFile("filename.doc", "content")
183 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
184 when: 'file uploaded with schema set create request'
187 multipart(schemaSetEndpoint)
189 .header("Authorization", getAuthorizationHeader())
190 .param('schema-set-name', schemaSetName))
191 .andReturn().response
192 then: 'create schema set rejected'
193 response.status == HttpStatus.BAD_REQUEST.value()
197 def 'Create schema set from #fileType file with IOException occurrence on processing.'() {
199 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
200 when: 'file uploaded with schema set create request'
201 def multipartFile = createMultipartFileForIOException(fileType)
204 multipart(schemaSetEndpoint)
206 .header("Authorization", getAuthorizationHeader())
207 .param('schema-set-name', schemaSetName))
208 .andReturn().response
209 then: 'the error response returned indicating internal server error occurrence'
210 response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
211 where: 'following file types are used'
212 fileType << ['YANG', 'ZIP']
215 def 'Delete schema set.'() {
217 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
218 when: 'delete schema set endpoint is invoked'
220 mvc.perform(delete(schemaSetEndpoint).header("Authorization", getAuthorizationHeader()))
221 .andReturn().response
222 then: 'associated service method is invoked with expected parameters'
223 1 * mockCpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED)
224 and: 'response code indicates success'
225 response.status == HttpStatus.NO_CONTENT.value()
228 def 'Delete schema set which is in use.'() {
229 given: 'service method throws an exception indicating the schema set is in use'
230 def thrownException = new SchemaSetInUseException(dataspaceName, schemaSetName)
231 mockCpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED) >>
232 { throw thrownException }
234 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
235 when: 'delete schema set endpoint is invoked'
237 mvc.perform(delete(schemaSetEndpoint).header("Authorization", getAuthorizationHeader()))
238 .andReturn().response
239 then: 'schema set deletion fails with conflict response code'
240 response.status == HttpStatus.CONFLICT.value()
243 def 'Get existing schema set.'() {
244 given: 'service method returns a new schema set'
245 mockCpsModuleService.getSchemaSet(dataspaceName, schemaSetName) >>
246 new SchemaSet(name: schemaSetName, dataspaceName: dataspaceName)
248 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
249 when: 'get schema set API is invoked'
251 mvc.perform(get(schemaSetEndpoint).header("Authorization", getAuthorizationHeader()))
252 .andReturn().response
253 then: 'the correct schema set is returned'
254 response.status == HttpStatus.OK.value()
255 response.getContentAsString().contains(schemaSetName)
258 def 'Create Anchor.'() {
259 given: 'request parameters'
260 def requestParams = new LinkedMultiValueMap<>()
261 requestParams.add('schema-set-name', schemaSetName)
262 requestParams.add('anchor-name', anchorName)
264 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors"
265 when: 'post is invoked'
268 post(anchorEndpoint).contentType(MediaType.APPLICATION_JSON)
269 .header("Authorization", getAuthorizationHeader())
270 .params(requestParams as MultiValueMap))
271 .andReturn().response
272 then: 'anchor is created successfully'
273 1 * mockCpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName)
274 response.status == HttpStatus.CREATED.value()
275 response.getContentAsString().contains(anchorName)
278 def 'Get existing anchor.'() {
279 given: 'service method returns a list of anchors'
280 mockCpsAdminService.getAnchors(dataspaceName) >> anchorList
282 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors"
283 when: 'get all anchors API is invoked'
285 mvc.perform(get(anchorEndpoint).header("Authorization", getAuthorizationHeader()))
286 .andReturn().response
287 then: 'the correct anchor is returned'
288 response.status == HttpStatus.OK.value()
289 response.getContentAsString().contains(anchorName)
292 def 'Get existing anchor by dataspace and anchor name.'() {
293 given: 'service method returns an anchor'
294 mockCpsAdminService.getAnchor(dataspaceName,anchorName) >> new Anchor(name: anchorName, dataspaceName: dataspaceName, schemaSetName:schemaSetName)
296 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName"
297 when: 'get anchor API is invoked'
299 mvc.perform(get(anchorEndpoint).header("Authorization", getAuthorizationHeader()))
300 .andReturn().response
301 def responseContent = response.getContentAsString()
302 then: 'the correct anchor is returned'
303 response.status == HttpStatus.OK.value()
304 responseContent.contains(anchorName)
305 responseContent.contains(dataspaceName)
306 responseContent.contains(schemaSetName)
309 def createMultipartFile(filename, content) {
310 return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
313 def createZipMultipartFileFromResource(resourcePath) {
314 return new MockMultipartFile("file", "test.zip", "application/zip",
315 getClass().getResource(resourcePath).getBytes())
318 def createMultipartFileForIOException(extension) {
319 def multipartFile = Mock(MockMultipartFile)
320 multipartFile.getOriginalFilename() >> "TEST." + extension
321 multipartFile.getBytes() >> { throw new IOException() }
322 multipartFile.getInputStream() >> { throw new IOException() }