Enable log level management via actuator, /cps/api path to use for REST controllers...
[cps.git] / cps-rest / src / test / groovy / org / onap / cps / rest / controller / AdminRestControllerSpec.groovy
1 /*
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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.rest.controller
22
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
41
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
47
48 @WebMvcTest
49 class AdminRestControllerSpec extends Specification {
50
51     @SpringBean
52     CpsModuleService mockCpsModuleService = Mock()
53
54     @SpringBean
55     CpsAdminService mockCpsAdminService = Mock()
56
57     @SpringBean
58     ModelMapper modelMapper = Mock()
59
60     @Autowired
61     MockMvc mvc
62
63     @Value('${rest.api.base-path}')
64     def basePath
65
66     def anchorsEndpoint = '/v1/dataspaces/my_dataspace/anchors'
67     def schemaSetsEndpoint = '/v1/dataspaces/test-dataspace/schema-sets'
68     def schemaSetEndpoint = schemaSetsEndpoint + '/my_schema_set'
69
70     def anchor = new Anchor(name: 'my_anchor')
71     def anchorList = [anchor]
72
73     def 'Create new dataspace'() {
74         when:
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()
80     }
81
82     def 'Create dataspace over existing with same name'() {
83         given:
84             def thrownException = new DataspaceAlreadyDefinedException("", new RuntimeException())
85             mockCpsAdminService.createDataspace("existing-dataspace") >> { throw thrownException }
86         when:
87             def response = performCreateDataspaceRequest("existing-dataspace")
88         then: 'Dataspace creation fails'
89             response.status == HttpStatus.BAD_REQUEST.value()
90     }
91
92     def 'Create schema set from yang file'() {
93         def yangResourceMapCapture
94         given:
95             def multipartFile = createMultipartFile("filename.yang", "content")
96         when:
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()
104     }
105
106     def 'Create schema set from file with invalid filename extension'() {
107         given:
108             def multipartFile = createMultipartFile("filename.doc", "content")
109         when:
110             def response = performCreateSchemaSetRequest(multipartFile)
111         then: 'Create schema fails'
112             response.status == HttpStatus.BAD_REQUEST.value()
113     }
114
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()
122     }
123
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()
133     }
134
135     def performCreateDataspaceRequest(String dataspaceName) {
136         return mvc.perform(
137                 post("$basePath/v1/dataspaces").param('dataspace-name', dataspaceName)
138         ).andReturn().response
139     }
140
141     def createMultipartFile(filename, content) {
142         return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
143     }
144
145     def performCreateSchemaSetRequest(multipartFile) {
146         return mvc.perform(
147                 multipart("$basePath$schemaSetsEndpoint")
148                         .file(multipartFile)
149                         .param('schema-set-name', 'test-schema-set')
150         ).andReturn().response
151     }
152
153     def performDeleteRequest(String deleteEndpoint) {
154         return mvc.perform(delete("$basePath$deleteEndpoint")).andReturn().response
155     }
156
157     def 'Get existing schema set'() {
158         given:
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')
166     }
167
168     def 'Create Anchor'() {
169         given:
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')
180     }
181
182     def 'Get existing anchor'() {
183         given:
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')
190     }
191 }