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 static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
24 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
25 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
29 import org.modelmapper.ModelMapper
30 import org.onap.cps.api.CpsAdminService
31 import org.onap.cps.api.CpsDataService
32 import org.onap.cps.api.CpsModuleService
33 import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException
34 import org.onap.cps.spi.exceptions.SchemaSetInUseException
35 import org.onap.cps.spi.model.Anchor
36 import org.onap.cps.spi.model.SchemaSet
37 import org.spockframework.spring.SpringBean
38 import org.springframework.beans.factory.annotation.Autowired
39 import org.springframework.beans.factory.annotation.Value
40 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
41 import org.springframework.http.HttpStatus
42 import org.springframework.http.MediaType
43 import org.springframework.mock.web.MockMultipartFile
44 import org.springframework.test.web.servlet.MockMvc
45 import org.springframework.util.LinkedMultiValueMap
46 import org.springframework.util.MultiValueMap
47 import spock.lang.Specification
48 import spock.lang.Unroll
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 anchorEndpoint = '/v1/dataspaces/my_dataspace/anchors/my_anchorname'
73 def schemaSetsEndpoint = '/v1/dataspaces/test-dataspace/schema-sets'
74 def schemaSetEndpoint = schemaSetsEndpoint + '/my_schema_set'
76 def anchor = new Anchor(name: 'my_anchor')
77 def anchorList = [anchor]
79 def 'Create new dataspace'() {
81 def response = performCreateDataspaceRequest("new-dataspace")
82 then: 'Service method is invoked with expected parameters'
83 1 * mockCpsAdminService.createDataspace("new-dataspace")
84 and: 'Dataspace is create successfully'
85 response.status == HttpStatus.CREATED.value()
88 def 'Create dataspace over existing with same name'() {
90 def thrownException = new DataspaceAlreadyDefinedException("", new RuntimeException())
91 mockCpsAdminService.createDataspace("existing-dataspace") >> { throw thrownException }
93 def response = performCreateDataspaceRequest("existing-dataspace")
94 then: 'Dataspace creation fails'
95 response.status == HttpStatus.BAD_REQUEST.value()
98 def 'Create schema set from yang file.'() {
99 def yangResourceMapCapture
100 given: 'single yang file'
101 def multipartFile = createMultipartFile("filename.yang", "content")
102 when: 'file uploaded with schema set create request'
103 def response = performCreateSchemaSetRequest(multipartFile)
104 then: 'associated service method is invoked with expected parameters'
105 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
106 { args -> yangResourceMapCapture = args[2] }
107 yangResourceMapCapture['filename.yang'] == 'content'
108 and: 'response code indicates success'
109 response.status == HttpStatus.CREATED.value()
112 def 'Create schema set from zip archive.'() {
113 def yangResourceMapCapture
114 given: 'zip archive with multiple .yang files inside'
115 def multipartFile = createZipMultipartFileFromResource("/yang-files-set.zip")
116 when: 'file uploaded with schema set create request'
117 def response = performCreateSchemaSetRequest(multipartFile)
118 then: 'associated service method is invoked with expected parameters'
119 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
120 { args -> yangResourceMapCapture = args[2] }
121 yangResourceMapCapture['assembly.yang'] == "fake assembly content 1\n"
122 yangResourceMapCapture['component.yang'] == "fake component content 1\n"
123 and: 'response code indicates success'
124 response.status == HttpStatus.CREATED.value()
128 def 'Create schema set from zip archive having #caseDescriptor.'() {
129 when: 'zip archive having #caseDescriptor is uploaded with create schema set request'
130 def response = performCreateSchemaSetRequest(multipartFile)
131 then: 'create schema set rejected'
132 response.status == HttpStatus.BAD_REQUEST.value()
133 where: 'following cases are tested'
134 caseDescriptor | multipartFile
135 'no .yang files inside' | createZipMultipartFileFromResource("/no-yang-files.zip")
136 'multiple .yang files with same name' | createZipMultipartFileFromResource("/yang-files-multiple-sets.zip")
139 def 'Create schema set from file with unsupported filename extension.'() {
140 given: 'file with unsupported filename extension (.doc)'
141 def multipartFile = createMultipartFile("filename.doc", "content")
142 when: 'file uploaded with schema set create request'
143 def response = performCreateSchemaSetRequest(multipartFile)
144 then: 'create schema set rejected'
145 response.status == HttpStatus.BAD_REQUEST.value()
149 def 'Create schema set from #fileType file with IOException occurrence on processing.'() {
150 when: 'file uploaded with schema set create request'
151 def response = performCreateSchemaSetRequest(createMultipartFileForIOException(fileType))
152 then: 'the error response returned indicating internal server error occurrence'
153 response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
154 where: 'following file types are used'
155 fileType << ['YANG', 'ZIP']
158 def 'Delete schema set.'() {
159 when: 'delete schema set endpoint is invoked'
160 def response = performDeleteRequest(schemaSetEndpoint)
161 then: 'associated service method is invoked with expected parameters'
162 1 * mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED)
163 and: 'response code indicates success'
164 response.status == HttpStatus.NO_CONTENT.value()
167 def 'Delete schema set which is in use.'() {
168 given: 'the service method throws an exception indicating the schema set is in use'
169 def thrownException = new SchemaSetInUseException('test-dataspace', 'my_schema_set')
170 mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED) >>
171 { throw thrownException }
172 when: 'delete schema set endpoint is invoked'
173 def response = performDeleteRequest(schemaSetEndpoint)
174 then: 'schema set deletion fails with conflict response code'
175 response.status == HttpStatus.CONFLICT.value()
178 def performCreateDataspaceRequest(String dataspaceName) {
180 post("$basePath/v1/dataspaces").param('dataspace-name', dataspaceName)
181 ).andReturn().response
184 def createMultipartFile(filename, content) {
185 return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
188 def createZipMultipartFileFromResource(resourcePath) {
189 return new MockMultipartFile("file", "test.zip", "application/zip",
190 getClass().getResource(resourcePath).getBytes())
193 def createMultipartFileForIOException(extension) {
194 def multipartFile = Mock(MockMultipartFile)
195 multipartFile.getOriginalFilename() >> "TEST." + extension
196 multipartFile.getBytes() >> { throw new IOException() }
197 multipartFile.getInputStream() >> { throw new IOException() }
201 def performCreateSchemaSetRequest(multipartFile) {
203 multipart("$basePath$schemaSetsEndpoint")
205 .param('schema-set-name', 'test-schema-set')
206 ).andReturn().response
209 def performDeleteRequest(String deleteEndpoint) {
210 return mvc.perform(delete("$basePath$deleteEndpoint")).andReturn().response
213 def 'Get existing schema set'() {
215 mockCpsModuleService.getSchemaSet('test-dataspace', 'my_schema_set') >>
216 new SchemaSet(name: 'my_schema_set', dataspaceName: 'test-dataspace')
217 when: 'get schema set API is invoked'
218 def response = mvc.perform(get("$basePath$schemaSetEndpoint")).andReturn().response
219 then: 'the correct schema set is returned'
220 response.status == HttpStatus.OK.value()
221 response.getContentAsString().contains('my_schema_set')
224 def 'Create Anchor'() {
226 def requestParams = new LinkedMultiValueMap<>()
227 requestParams.add('schema-set-name', 'my_schema-set')
228 requestParams.add('anchor-name', 'my_anchor')
229 when: 'post is invoked'
230 def response = mvc.perform(post("$basePath$anchorsEndpoint").contentType(MediaType.APPLICATION_JSON)
231 .params(requestParams as MultiValueMap)).andReturn().response
232 then: 'Anchor is created successfully'
233 1 * mockCpsAdminService.createAnchor('my_dataspace', 'my_schema-set', 'my_anchor')
234 response.status == HttpStatus.CREATED.value()
235 response.getContentAsString().contains('my_anchor')
238 def 'Get existing anchor'() {
240 mockCpsAdminService.getAnchors('my_dataspace') >> anchorList
241 when: 'get all anchors API is invoked'
242 def response = mvc.perform(get("$basePath$anchorsEndpoint")).andReturn().response
243 then: 'the correct anchor is returned'
244 response.status == HttpStatus.OK.value()
245 response.getContentAsString().contains('my_anchor')
248 def 'Get existing anchor by dataspace and anchor name.'() {
250 mockCpsAdminService.getAnchor('my_dataspace','my_anchorname') >> new Anchor(name: 'my_anchorname', dataspaceName: 'my_dataspace', schemaSetName: 'my_schemaSetName')
251 when: 'get anchor API is invoked'
252 def response = mvc.perform(get("$basePath$anchorEndpoint")).andReturn().response
253 def responseContent = response.getContentAsString()
254 then: 'the correct anchor is returned'
255 response.status == HttpStatus.OK.value()
256 responseContent.contains('my_anchorname')
257 responseContent.contains('my_dataspace')
258 responseContent.contains('my_schemaSetName')