Split OpenAPI yaml file
[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.model.Anchor
27 import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException
28 import org.onap.cps.spi.model.SchemaSet
29 import org.spockframework.spring.SpringBean
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
32 import org.springframework.http.HttpStatus
33 import org.springframework.http.MediaType
34 import org.springframework.mock.web.MockMultipartFile
35 import org.springframework.test.web.servlet.MockMvc
36 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
37 import org.springframework.util.LinkedMultiValueMap
38 import org.springframework.util.MultiValueMap
39 import spock.lang.Specification
40
41 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
42 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
43
44 @WebMvcTest
45 class AdminRestControllerSpec extends Specification {
46
47     @SpringBean
48     CpsModuleService mockCpsModuleService = Mock()
49
50     @SpringBean
51     CpsAdminService mockCpsAdminService = Mock()
52
53     @SpringBean
54     ModelMapper modelMapper = Mock()
55
56     @Autowired
57     MockMvc mvc
58
59     def anchorsEndpoint = '/v1/dataspaces/my_dataspace/anchors'
60     def schemaSetsEndpoint = '/v1/dataspaces/test-dataspace/schema-sets'
61     def schemaSetEndpoint = schemaSetsEndpoint + '/my_schema_set'
62
63     def anchor = new Anchor(name: 'my_anchor')
64     def anchorList = [anchor]
65
66     def 'Create new dataspace'() {
67         when:
68             def response = performCreateDataspaceRequest("new-dataspace")
69         then: 'Service method is invoked with expected parameters'
70             1 * mockCpsAdminService.createDataspace("new-dataspace")
71         and: 'Dataspace is create successfully'
72             response.status == HttpStatus.CREATED.value()
73     }
74
75     def 'Create dataspace over existing with same name'() {
76         given:
77             def thrownException = new DataspaceAlreadyDefinedException("", new RuntimeException())
78             mockCpsAdminService.createDataspace("existing-dataspace") >> { throw thrownException }
79         when:
80             def response = performCreateDataspaceRequest("existing-dataspace")
81         then: 'Dataspace creation fails'
82             response.status == HttpStatus.BAD_REQUEST.value()
83     }
84
85     def 'Create schema set from yang file'() {
86         def yangResourceMapCapture
87         given:
88             def multipartFile = createMultipartFile("filename.yang", "content")
89         when:
90             def response = performCreateSchemaSetRequest(multipartFile)
91         then: 'Service method is invoked with expected parameters'
92             1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
93                     { args -> yangResourceMapCapture = args[2] }
94             yangResourceMapCapture['filename.yang'] == 'content'
95         and: 'Response code indicates success'
96             response.status == HttpStatus.CREATED.value()
97     }
98
99     def 'Create schema set from file with invalid filename extension'() {
100         given:
101             def multipartFile = createMultipartFile("filename.doc", "content")
102         when:
103             def response = performCreateSchemaSetRequest(multipartFile)
104         then: 'Create schema fails'
105             response.status == HttpStatus.BAD_REQUEST.value()
106     }
107
108     def performCreateDataspaceRequest(String dataspaceName) {
109         return mvc.perform(
110                 MockMvcRequestBuilders
111                         .post('/v1/dataspaces')
112                         .param('dataspace-name', dataspaceName)
113         ).andReturn().response
114     }
115
116     def createMultipartFile(filename, content) {
117         return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
118     }
119
120     def performCreateSchemaSetRequest(multipartFile) {
121         return mvc.perform(
122                 MockMvcRequestBuilders
123                         .multipart(schemaSetsEndpoint)
124                         .file(multipartFile)
125                         .param('schema-set-name', 'test-schema-set')
126         ).andReturn().response
127     }
128
129     def 'Get existing schema set'() {
130         given:
131             mockCpsModuleService.getSchemaSet('test-dataspace', 'my_schema_set') >>
132                     new SchemaSet(name: 'my_schema_set', dataspaceName: 'test-dataspace')
133         when: 'get schema set API is invoked'
134             def response = mvc.perform(get(schemaSetEndpoint)).andReturn().response
135         then: 'the correct schema set is returned'
136             response.status == HttpStatus.OK.value()
137             response.getContentAsString().contains('my_schema_set')
138     }
139
140     def 'Create Anchor'() {
141         given:
142             def requestParams = new LinkedMultiValueMap<>()
143             requestParams.add('schema-set-name', 'my_schema-set')
144             requestParams.add('anchor-name', 'my_anchor')
145         when: 'post is invoked'
146             def response = mvc.perform(post(anchorsEndpoint).contentType(MediaType.APPLICATION_JSON)
147                     .params(requestParams as MultiValueMap)).andReturn().response
148         then: 'Anchor is created successfully'
149             1 * mockCpsAdminService.createAnchor('my_dataspace', 'my_schema-set', 'my_anchor')
150             response.status == HttpStatus.CREATED.value()
151             response.getContentAsString().contains('my_anchor')
152     }
153
154     def 'Get existing anchor'() {
155         given:
156             mockCpsAdminService.getAnchors('my_dataspace') >> anchorList
157         when: 'get all anchors API is invoked'
158             def response = mvc.perform(get(anchorsEndpoint)).andReturn().response
159         then: 'the correct anchor is returned'
160             response.status == HttpStatus.OK.value()
161             response.getContentAsString().contains('my_anchor')
162     }
163 }