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.CpsModuleService
26 import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException
27 import org.onap.cps.spi.exceptions.SchemaSetInUseException
28 import org.onap.cps.spi.model.Anchor
29 import org.onap.cps.spi.model.SchemaSet
30 import org.spockframework.spring.SpringBean
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
33 import org.springframework.http.HttpStatus
34 import org.springframework.http.MediaType
35 import org.springframework.mock.web.MockMultipartFile
36 import org.springframework.test.web.servlet.MockMvc
37 import org.springframework.util.LinkedMultiValueMap
38 import org.springframework.util.MultiValueMap
39 import spock.lang.Specification
40 import spock.lang.Unroll
42 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
43 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
44 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
45 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart
46 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
49 class AdminRestControllerSpec extends Specification {
52 CpsModuleService mockCpsModuleService = Mock()
55 CpsAdminService mockCpsAdminService = Mock()
58 ModelMapper modelMapper = Mock()
63 def anchorsEndpoint = '/v1/dataspaces/my_dataspace/anchors'
64 def schemaSetsEndpoint = '/v1/dataspaces/test-dataspace/schema-sets'
65 def schemaSetEndpoint = schemaSetsEndpoint + '/my_schema_set'
67 def anchor = new Anchor(name: 'my_anchor')
68 def anchorList = [anchor]
70 def 'Create new dataspace'() {
72 def response = performCreateDataspaceRequest("new-dataspace")
73 then: 'Service method is invoked with expected parameters'
74 1 * mockCpsAdminService.createDataspace("new-dataspace")
75 and: 'Dataspace is create successfully'
76 response.status == HttpStatus.CREATED.value()
79 def 'Create dataspace over existing with same name'() {
81 def thrownException = new DataspaceAlreadyDefinedException("", new RuntimeException())
82 mockCpsAdminService.createDataspace("existing-dataspace") >> { throw thrownException }
84 def response = performCreateDataspaceRequest("existing-dataspace")
85 then: 'Dataspace creation fails'
86 response.status == HttpStatus.BAD_REQUEST.value()
89 def 'Create schema set from yang file.'() {
90 def yangResourceMapCapture
91 given: 'single yang file'
92 def multipartFile = createMultipartFile("filename.yang", "content")
93 when: 'file uploaded with schema set create request'
94 def response = performCreateSchemaSetRequest(multipartFile)
95 then: 'associated service method is invoked with expected parameters'
96 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
97 { args -> yangResourceMapCapture = args[2] }
98 yangResourceMapCapture['filename.yang'] == 'content'
99 and: 'response code indicates success'
100 response.status == HttpStatus.CREATED.value()
103 def 'Create schema set from zip archive.'() {
104 def yangResourceMapCapture
105 given: 'zip archive with multiple .yang files inside'
106 def multipartFile = createZipMultipartFileFromResource("/yang-files-set.zip")
107 when: 'file uploaded with schema set create request'
108 def response = performCreateSchemaSetRequest(multipartFile)
109 then: 'associated service method is invoked with expected parameters'
110 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
111 { args -> yangResourceMapCapture = args[2] }
112 yangResourceMapCapture['assembly.yang'] == "fake assembly content 1\n"
113 yangResourceMapCapture['component.yang'] == "fake component content 1\n"
114 and: 'response code indicates success'
115 response.status == HttpStatus.CREATED.value()
119 def 'Create schema set from zip archive having #caseDescriptor.'() {
120 when: 'zip archive having #caseDescriptor is uploaded with create schema set request'
121 def response = performCreateSchemaSetRequest(multipartFile)
122 then: 'create schema set rejected'
123 response.status == HttpStatus.BAD_REQUEST.value()
124 where: 'following cases are tested'
125 caseDescriptor | multipartFile
126 'no .yang files inside' | createZipMultipartFileFromResource("/no-yang-files.zip")
127 'multiple .yang files with same name' | createZipMultipartFileFromResource("/yang-files-multiple-sets.zip")
130 def 'Create schema set from file with unsupported filename extension.'() {
131 given: 'file with unsupported filename extension (.doc)'
132 def multipartFile = createMultipartFile("filename.doc", "content")
133 when: 'file uploaded with schema set create request'
134 def response = performCreateSchemaSetRequest(multipartFile)
135 then: 'create schema set rejected'
136 response.status == HttpStatus.BAD_REQUEST.value()
140 def 'Create schema set from #fileType file with IOException occurrence on processing.'() {
141 when: 'file uploaded with schema set create request'
142 def response = performCreateSchemaSetRequest(createMultipartFileForIOException(fileType))
143 then: 'the error response returned indicating internal server error occurrence'
144 response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
145 where: 'following file types are used'
146 fileType << ['YANG', 'ZIP']
149 def 'Delete schema set.'() {
150 when: 'delete schema set endpoint is invoked'
151 def response = performDeleteRequest(schemaSetEndpoint)
152 then: 'associated service method is invoked with expected parameters'
153 1 * mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED)
154 and: 'response code indicates success'
155 response.status == HttpStatus.NO_CONTENT.value()
158 def 'Delete schema set which is in use.'() {
159 given: 'the service method throws an exception indicating the schema set is in use'
160 def thrownException = new SchemaSetInUseException('test-dataspace', 'my_schema_set')
161 mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED) >>
162 { throw thrownException }
163 when: 'delete schema set endpoint is invoked'
164 def response = performDeleteRequest(schemaSetEndpoint)
165 then: 'schema set deletion fails with conflict response code'
166 response.status == HttpStatus.CONFLICT.value()
169 def performCreateDataspaceRequest(String dataspaceName) {
171 post('/v1/dataspaces').param('dataspace-name', dataspaceName)
172 ).andReturn().response
175 def createMultipartFile(filename, content) {
176 return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
179 def createZipMultipartFileFromResource(resourcePath) {
180 return new MockMultipartFile("file", "test.zip", "application/zip",
181 getClass().getResource(resourcePath).getBytes())
184 def createMultipartFileForIOException(extension) {
185 def multipartFile = Mock(MockMultipartFile)
186 multipartFile.getOriginalFilename() >> "TEST." + extension
187 multipartFile.getBytes() >> { throw new IOException() }
188 multipartFile.getInputStream() >> { throw new IOException() }
192 def performCreateSchemaSetRequest(multipartFile) {
194 multipart(schemaSetsEndpoint)
196 .param('schema-set-name', 'test-schema-set')
197 ).andReturn().response
200 def performDeleteRequest(String uri) {
201 return mvc.perform(delete(uri)).andReturn().response
204 def 'Get existing schema set'() {
206 mockCpsModuleService.getSchemaSet('test-dataspace', 'my_schema_set') >>
207 new SchemaSet(name: 'my_schema_set', dataspaceName: 'test-dataspace')
208 when: 'get schema set API is invoked'
209 def response = mvc.perform(get(schemaSetEndpoint)).andReturn().response
210 then: 'the correct schema set is returned'
211 response.status == HttpStatus.OK.value()
212 response.getContentAsString().contains('my_schema_set')
215 def 'Create Anchor'() {
217 def requestParams = new LinkedMultiValueMap<>()
218 requestParams.add('schema-set-name', 'my_schema-set')
219 requestParams.add('anchor-name', 'my_anchor')
220 when: 'post is invoked'
221 def response = mvc.perform(post(anchorsEndpoint).contentType(MediaType.APPLICATION_JSON)
222 .params(requestParams as MultiValueMap)).andReturn().response
223 then: 'Anchor is created successfully'
224 1 * mockCpsAdminService.createAnchor('my_dataspace', 'my_schema-set', 'my_anchor')
225 response.status == HttpStatus.CREATED.value()
226 response.getContentAsString().contains('my_anchor')
229 def 'Get existing anchor'() {
231 mockCpsAdminService.getAnchors('my_dataspace') >> anchorList
232 when: 'get all anchors API is invoked'
233 def response = mvc.perform(get(anchorsEndpoint)).andReturn().response
234 then: 'the correct anchor is returned'
235 response.status == HttpStatus.OK.value()
236 response.getContentAsString().contains('my_anchor')