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.beans.factory.annotation.Value
33 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
34 import org.springframework.http.HttpStatus
35 import org.springframework.http.MediaType
36 import org.springframework.mock.web.MockMultipartFile
37 import org.springframework.test.web.servlet.MockMvc
38 import org.springframework.util.LinkedMultiValueMap
39 import org.springframework.util.MultiValueMap
40 import spock.lang.Specification
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 @Value('${rest.api.base-path}')
66 def anchorsEndpoint = '/v1/dataspaces/my_dataspace/anchors'
67 def schemaSetsEndpoint = '/v1/dataspaces/test-dataspace/schema-sets'
68 def schemaSetEndpoint = schemaSetsEndpoint + '/my_schema_set'
70 def anchor = new Anchor(name: 'my_anchor')
71 def anchorList = [anchor]
73 def 'Create new dataspace'() {
75 def response = performCreateDataspaceRequest("new-dataspace")
76 then: 'Service method is invoked with expected parameters'
77 1 * mockCpsAdminService.createDataspace("new-dataspace")
78 and: 'Dataspace is create successfully'
79 response.status == HttpStatus.CREATED.value()
82 def 'Create dataspace over existing with same name'() {
84 def thrownException = new DataspaceAlreadyDefinedException("", new RuntimeException())
85 mockCpsAdminService.createDataspace("existing-dataspace") >> { throw thrownException }
87 def response = performCreateDataspaceRequest("existing-dataspace")
88 then: 'Dataspace creation fails'
89 response.status == HttpStatus.BAD_REQUEST.value()
92 def 'Create schema set from yang file'() {
93 def yangResourceMapCapture
95 def multipartFile = createMultipartFile("filename.yang", "content")
97 def response = performCreateSchemaSetRequest(multipartFile)
98 then: 'Service method is invoked with expected parameters'
99 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
100 { args -> yangResourceMapCapture = args[2] }
101 yangResourceMapCapture['filename.yang'] == 'content'
102 and: 'Response code indicates success'
103 response.status == HttpStatus.CREATED.value()
106 def 'Create schema set from file with invalid filename extension'() {
108 def multipartFile = createMultipartFile("filename.doc", "content")
110 def response = performCreateSchemaSetRequest(multipartFile)
111 then: 'Create schema fails'
112 response.status == HttpStatus.BAD_REQUEST.value()
115 def 'Delete schema set.'() {
116 when: 'delete schema set endpoint is invoked'
117 def response = performDeleteRequest(schemaSetEndpoint)
118 then: 'associated service method is invoked with expected parameters'
119 1 * mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED)
120 and: 'response code indicates success'
121 response.status == HttpStatus.NO_CONTENT.value()
124 def 'Delete schema set which is in use.'() {
125 given: 'the service method throws an exception indicating the schema set is in use'
126 def thrownException = new SchemaSetInUseException('test-dataspace', 'my_schema_set')
127 mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED) >>
128 { throw thrownException }
129 when: 'delete schema set endpoint is invoked'
130 def response = performDeleteRequest(schemaSetEndpoint)
131 then: 'schema set deletion fails with conflict response code'
132 response.status == HttpStatus.CONFLICT.value()
135 def performCreateDataspaceRequest(String dataspaceName) {
137 post("$basePath/v1/dataspaces").param('dataspace-name', dataspaceName)
138 ).andReturn().response
141 def createMultipartFile(filename, content) {
142 return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
145 def performCreateSchemaSetRequest(multipartFile) {
147 multipart("$basePath$schemaSetsEndpoint")
149 .param('schema-set-name', 'test-schema-set')
150 ).andReturn().response
153 def performDeleteRequest(String deleteEndpoint) {
154 return mvc.perform(delete("$basePath$deleteEndpoint")).andReturn().response
157 def 'Get existing schema set'() {
159 mockCpsModuleService.getSchemaSet('test-dataspace', 'my_schema_set') >>
160 new SchemaSet(name: 'my_schema_set', dataspaceName: 'test-dataspace')
161 when: 'get schema set API is invoked'
162 def response = mvc.perform(get("$basePath$schemaSetEndpoint")).andReturn().response
163 then: 'the correct schema set is returned'
164 response.status == HttpStatus.OK.value()
165 response.getContentAsString().contains('my_schema_set')
168 def 'Create Anchor'() {
170 def requestParams = new LinkedMultiValueMap<>()
171 requestParams.add('schema-set-name', 'my_schema-set')
172 requestParams.add('anchor-name', 'my_anchor')
173 when: 'post is invoked'
174 def response = mvc.perform(post("$basePath$anchorsEndpoint").contentType(MediaType.APPLICATION_JSON)
175 .params(requestParams as MultiValueMap)).andReturn().response
176 then: 'Anchor is created successfully'
177 1 * mockCpsAdminService.createAnchor('my_dataspace', 'my_schema-set', 'my_anchor')
178 response.status == HttpStatus.CREATED.value()
179 response.getContentAsString().contains('my_anchor')
182 def 'Get existing anchor'() {
184 mockCpsAdminService.getAnchors('my_dataspace') >> anchorList
185 when: 'get all anchors API is invoked'
186 def response = mvc.perform(get("$basePath$anchorsEndpoint")).andReturn().response
187 then: 'the correct anchor is returned'
188 response.status == HttpStatus.OK.value()
189 response.getContentAsString().contains('my_anchor')