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.CpsModuleService
32 import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException
33 import org.onap.cps.spi.exceptions.SchemaSetInUseException
34 import org.onap.cps.spi.model.Anchor
35 import org.onap.cps.spi.model.SchemaSet
36 import org.spockframework.spring.SpringBean
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.beans.factory.annotation.Value
39 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
40 import org.springframework.http.HttpStatus
41 import org.springframework.http.MediaType
42 import org.springframework.mock.web.MockMultipartFile
43 import org.springframework.test.web.servlet.MockMvc
44 import org.springframework.util.LinkedMultiValueMap
45 import org.springframework.util.MultiValueMap
46 import spock.lang.Specification
47 import spock.lang.Unroll
50 class AdminRestControllerSpec extends Specification {
53 CpsModuleService mockCpsModuleService = Mock()
56 CpsAdminService mockCpsAdminService = Mock()
59 ModelMapper modelMapper = Mock()
64 @Value('${rest.api.base-path}')
67 def anchorsEndpoint = '/v1/dataspaces/my_dataspace/anchors'
68 def anchorEndpoint = '/v1/dataspaces/my_dataspace/anchors/my_anchorname'
69 def schemaSetsEndpoint = '/v1/dataspaces/test-dataspace/schema-sets'
70 def schemaSetEndpoint = schemaSetsEndpoint + '/my_schema_set'
72 def anchor = new Anchor(name: 'my_anchor')
73 def anchorList = [anchor]
75 def 'Create new dataspace'() {
77 def response = performCreateDataspaceRequest("new-dataspace")
78 then: 'Service method is invoked with expected parameters'
79 1 * mockCpsAdminService.createDataspace("new-dataspace")
80 and: 'Dataspace is create successfully'
81 response.status == HttpStatus.CREATED.value()
84 def 'Create dataspace over existing with same name'() {
86 def thrownException = new DataspaceAlreadyDefinedException("", new RuntimeException())
87 mockCpsAdminService.createDataspace("existing-dataspace") >> { throw thrownException }
89 def response = performCreateDataspaceRequest("existing-dataspace")
90 then: 'Dataspace creation fails'
91 response.status == HttpStatus.BAD_REQUEST.value()
94 def 'Create schema set from yang file.'() {
95 def yangResourceMapCapture
96 given: 'single yang file'
97 def multipartFile = createMultipartFile("filename.yang", "content")
98 when: 'file uploaded with schema set create request'
99 def response = performCreateSchemaSetRequest(multipartFile)
100 then: 'associated service method is invoked with expected parameters'
101 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
102 { args -> yangResourceMapCapture = args[2] }
103 yangResourceMapCapture['filename.yang'] == 'content'
104 and: 'response code indicates success'
105 response.status == HttpStatus.CREATED.value()
108 def 'Create schema set from zip archive.'() {
109 def yangResourceMapCapture
110 given: 'zip archive with multiple .yang files inside'
111 def multipartFile = createZipMultipartFileFromResource("/yang-files-set.zip")
112 when: 'file uploaded with schema set create request'
113 def response = performCreateSchemaSetRequest(multipartFile)
114 then: 'associated service method is invoked with expected parameters'
115 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
116 { args -> yangResourceMapCapture = args[2] }
117 yangResourceMapCapture['assembly.yang'] == "fake assembly content 1\n"
118 yangResourceMapCapture['component.yang'] == "fake component content 1\n"
119 and: 'response code indicates success'
120 response.status == HttpStatus.CREATED.value()
124 def 'Create schema set from zip archive having #caseDescriptor.'() {
125 when: 'zip archive having #caseDescriptor is uploaded with create schema set request'
126 def response = performCreateSchemaSetRequest(multipartFile)
127 then: 'create schema set rejected'
128 response.status == HttpStatus.BAD_REQUEST.value()
129 where: 'following cases are tested'
130 caseDescriptor | multipartFile
131 'no .yang files inside' | createZipMultipartFileFromResource("/no-yang-files.zip")
132 'multiple .yang files with same name' | createZipMultipartFileFromResource("/yang-files-multiple-sets.zip")
135 def 'Create schema set from file with unsupported filename extension.'() {
136 given: 'file with unsupported filename extension (.doc)'
137 def multipartFile = createMultipartFile("filename.doc", "content")
138 when: 'file uploaded with schema set create request'
139 def response = performCreateSchemaSetRequest(multipartFile)
140 then: 'create schema set rejected'
141 response.status == HttpStatus.BAD_REQUEST.value()
145 def 'Create schema set from #fileType file with IOException occurrence on processing.'() {
146 when: 'file uploaded with schema set create request'
147 def response = performCreateSchemaSetRequest(createMultipartFileForIOException(fileType))
148 then: 'the error response returned indicating internal server error occurrence'
149 response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
150 where: 'following file types are used'
151 fileType << ['YANG', 'ZIP']
154 def 'Delete schema set.'() {
155 when: 'delete schema set endpoint is invoked'
156 def response = performDeleteRequest(schemaSetEndpoint)
157 then: 'associated service method is invoked with expected parameters'
158 1 * mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED)
159 and: 'response code indicates success'
160 response.status == HttpStatus.NO_CONTENT.value()
163 def 'Delete schema set which is in use.'() {
164 given: 'the service method throws an exception indicating the schema set is in use'
165 def thrownException = new SchemaSetInUseException('test-dataspace', 'my_schema_set')
166 mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED) >>
167 { throw thrownException }
168 when: 'delete schema set endpoint is invoked'
169 def response = performDeleteRequest(schemaSetEndpoint)
170 then: 'schema set deletion fails with conflict response code'
171 response.status == HttpStatus.CONFLICT.value()
174 def performCreateDataspaceRequest(String dataspaceName) {
176 post("$basePath/v1/dataspaces").param('dataspace-name', dataspaceName)
177 ).andReturn().response
180 def createMultipartFile(filename, content) {
181 return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
184 def createZipMultipartFileFromResource(resourcePath) {
185 return new MockMultipartFile("file", "test.zip", "application/zip",
186 getClass().getResource(resourcePath).getBytes())
189 def createMultipartFileForIOException(extension) {
190 def multipartFile = Mock(MockMultipartFile)
191 multipartFile.getOriginalFilename() >> "TEST." + extension
192 multipartFile.getBytes() >> { throw new IOException() }
193 multipartFile.getInputStream() >> { throw new IOException() }
197 def performCreateSchemaSetRequest(multipartFile) {
199 multipart("$basePath$schemaSetsEndpoint")
201 .param('schema-set-name', 'test-schema-set')
202 ).andReturn().response
205 def performDeleteRequest(String deleteEndpoint) {
206 return mvc.perform(delete("$basePath$deleteEndpoint")).andReturn().response
209 def 'Get existing schema set'() {
211 mockCpsModuleService.getSchemaSet('test-dataspace', 'my_schema_set') >>
212 new SchemaSet(name: 'my_schema_set', dataspaceName: 'test-dataspace')
213 when: 'get schema set API is invoked'
214 def response = mvc.perform(get("$basePath$schemaSetEndpoint")).andReturn().response
215 then: 'the correct schema set is returned'
216 response.status == HttpStatus.OK.value()
217 response.getContentAsString().contains('my_schema_set')
220 def 'Create Anchor'() {
222 def requestParams = new LinkedMultiValueMap<>()
223 requestParams.add('schema-set-name', 'my_schema-set')
224 requestParams.add('anchor-name', 'my_anchor')
225 when: 'post is invoked'
226 def response = mvc.perform(post("$basePath$anchorsEndpoint").contentType(MediaType.APPLICATION_JSON)
227 .params(requestParams as MultiValueMap)).andReturn().response
228 then: 'Anchor is created successfully'
229 1 * mockCpsAdminService.createAnchor('my_dataspace', 'my_schema-set', 'my_anchor')
230 response.status == HttpStatus.CREATED.value()
231 response.getContentAsString().contains('my_anchor')
234 def 'Get existing anchor'() {
236 mockCpsAdminService.getAnchors('my_dataspace') >> anchorList
237 when: 'get all anchors API is invoked'
238 def response = mvc.perform(get("$basePath$anchorsEndpoint")).andReturn().response
239 then: 'the correct anchor is returned'
240 response.status == HttpStatus.OK.value()
241 response.getContentAsString().contains('my_anchor')
244 def 'Get existing anchor by dataspace and anchor name.'() {
246 mockCpsAdminService.getAnchor('my_dataspace','my_anchorname') >> new Anchor(name: 'my_anchorname', dataspaceName: 'my_dataspace', schemaSetName: 'my_schemaSetName')
247 when: 'get anchor API is invoked'
248 def response = mvc.perform(get("$basePath$anchorEndpoint")).andReturn().response
249 def responseContent = response.getContentAsString()
250 then: 'the correct anchor is returned'
251 response.status == HttpStatus.OK.value()
252 responseContent.contains('my_anchorname')
253 responseContent.contains('my_dataspace')
254 responseContent.contains('my_schemaSetName')