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
41 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
42 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
43 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
44 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart
45 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
48 class AdminRestControllerSpec extends Specification {
51 CpsModuleService mockCpsModuleService = Mock()
54 CpsAdminService mockCpsAdminService = Mock()
57 ModelMapper modelMapper = Mock()
62 def anchorsEndpoint = '/v1/dataspaces/my_dataspace/anchors'
63 def schemaSetsEndpoint = '/v1/dataspaces/test-dataspace/schema-sets'
64 def schemaSetEndpoint = schemaSetsEndpoint + '/my_schema_set'
66 def anchor = new Anchor(name: 'my_anchor')
67 def anchorList = [anchor]
69 def 'Create new dataspace'() {
71 def response = performCreateDataspaceRequest("new-dataspace")
72 then: 'Service method is invoked with expected parameters'
73 1 * mockCpsAdminService.createDataspace("new-dataspace")
74 and: 'Dataspace is create successfully'
75 response.status == HttpStatus.CREATED.value()
78 def 'Create dataspace over existing with same name'() {
80 def thrownException = new DataspaceAlreadyDefinedException("", new RuntimeException())
81 mockCpsAdminService.createDataspace("existing-dataspace") >> { throw thrownException }
83 def response = performCreateDataspaceRequest("existing-dataspace")
84 then: 'Dataspace creation fails'
85 response.status == HttpStatus.BAD_REQUEST.value()
88 def 'Create schema set from yang file'() {
89 def yangResourceMapCapture
91 def multipartFile = createMultipartFile("filename.yang", "content")
93 def response = performCreateSchemaSetRequest(multipartFile)
94 then: 'Service method is invoked with expected parameters'
95 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
96 { args -> yangResourceMapCapture = args[2] }
97 yangResourceMapCapture['filename.yang'] == 'content'
98 and: 'Response code indicates success'
99 response.status == HttpStatus.CREATED.value()
102 def 'Create schema set from file with invalid filename extension'() {
104 def multipartFile = createMultipartFile("filename.doc", "content")
106 def response = performCreateSchemaSetRequest(multipartFile)
107 then: 'Create schema fails'
108 response.status == HttpStatus.BAD_REQUEST.value()
111 def 'Delete schema set.'() {
112 when: 'delete schema set endpoint is invoked'
113 def response = performDeleteRequest(schemaSetEndpoint)
114 then: 'associated service method is invoked with expected parameters'
115 1 * mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED)
116 and: 'response code indicates success'
117 response.status == HttpStatus.NO_CONTENT.value()
120 def 'Delete schema set which is in use.'() {
121 given: 'the service method throws an exception indicating the schema set is in use'
122 def thrownException = new SchemaSetInUseException('test-dataspace', 'my_schema_set')
123 mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED) >>
124 { throw thrownException }
125 when: 'delete schema set endpoint is invoked'
126 def response = performDeleteRequest(schemaSetEndpoint)
127 then: 'schema set deletion fails with conflict response code'
128 response.status == HttpStatus.CONFLICT.value()
131 def performCreateDataspaceRequest(String dataspaceName) {
133 post('/v1/dataspaces').param('dataspace-name', dataspaceName)
134 ).andReturn().response
137 def createMultipartFile(filename, content) {
138 return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
141 def performCreateSchemaSetRequest(multipartFile) {
143 multipart(schemaSetsEndpoint)
145 .param('schema-set-name', 'test-schema-set')
146 ).andReturn().response
149 def performDeleteRequest(String uri) {
150 return mvc.perform(delete(uri)).andReturn().response
153 def 'Get existing schema set'() {
155 mockCpsModuleService.getSchemaSet('test-dataspace', 'my_schema_set') >>
156 new SchemaSet(name: 'my_schema_set', dataspaceName: 'test-dataspace')
157 when: 'get schema set API is invoked'
158 def response = mvc.perform(get(schemaSetEndpoint)).andReturn().response
159 then: 'the correct schema set is returned'
160 response.status == HttpStatus.OK.value()
161 response.getContentAsString().contains('my_schema_set')
164 def 'Create Anchor'() {
166 def requestParams = new LinkedMultiValueMap<>()
167 requestParams.add('schema-set-name', 'my_schema-set')
168 requestParams.add('anchor-name', 'my_anchor')
169 when: 'post is invoked'
170 def response = mvc.perform(post(anchorsEndpoint).contentType(MediaType.APPLICATION_JSON)
171 .params(requestParams as MultiValueMap)).andReturn().response
172 then: 'Anchor is created successfully'
173 1 * mockCpsAdminService.createAnchor('my_dataspace', 'my_schema-set', 'my_anchor')
174 response.status == HttpStatus.CREATED.value()
175 response.getContentAsString().contains('my_anchor')
178 def 'Get existing anchor'() {
180 mockCpsAdminService.getAnchors('my_dataspace') >> anchorList
181 when: 'get all anchors API is invoked'
182 def response = mvc.perform(get(anchorsEndpoint)).andReturn().response
183 then: 'the correct anchor is returned'
184 response.status == HttpStatus.OK.value()
185 response.getContentAsString().contains('my_anchor')