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.model.Anchor
27 import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException
28 import org.spockframework.spring.SpringBean
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
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
41 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
42 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
45 class AdminRestControllerSpec extends Specification {
48 CpsModuleService mockCpsModuleService = Mock()
51 CpsAdminService mockCpsAdminService = Mock()
54 ModelMapper modelMapper = Mock()
59 def anchorsEndpoint = '/v1/dataspaces/my_dataspace/anchors'
61 def anchor = new Anchor(name: 'my_anchor')
62 def anchorList = [anchor]
64 def 'Create new dataspace'() {
66 def response = performCreateDataspaceRequest("new-dataspace")
67 then: 'Service method is invoked with expected parameters'
68 1 * mockCpsAdminService.createDataspace("new-dataspace")
70 response.status == HttpStatus.CREATED.value()
73 def 'Create dataspace over existing with same name'() {
75 def thrownException = new DataspaceAlreadyDefinedException("", new RuntimeException())
76 mockCpsAdminService.createDataspace("existing-dataspace") >> { throw thrownException }
78 def response = performCreateDataspaceRequest("existing-dataspace")
80 response.status == HttpStatus.BAD_REQUEST.value()
83 def 'Create schema set from yang file'() {
84 def yangResourceMapCapture
86 def multipartFile = createMultipartFile("filename.yang", "content")
88 def response = performCreateSchemaSetRequest(multipartFile)
89 then: 'Service method is invoked with expected parameters'
90 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
91 { args -> yangResourceMapCapture = args[2] }
92 yangResourceMapCapture['filename.yang'] == 'content'
93 and: 'Response code indicates success'
94 response.status == HttpStatus.CREATED.value()
97 def 'Create schema set from file with invalid filename extension'() {
99 def multipartFile = createMultipartFile("filename.doc", "content")
101 def response = performCreateSchemaSetRequest(multipartFile)
103 response.status == HttpStatus.BAD_REQUEST.value()
106 def performCreateDataspaceRequest(String dataspaceName) {
108 MockMvcRequestBuilders
109 .post('/v1/dataspaces')
110 .param('dataspace-name', dataspaceName)
111 ).andReturn().response
114 def createMultipartFile(filename, content) {
115 return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
118 def performCreateSchemaSetRequest(multipartFile) {
120 MockMvcRequestBuilders
121 .multipart('/v1/dataspaces/test-dataspace/schema-sets')
123 .param('schemaSetName', 'test-schema-set')
124 ).andReturn().response
127 def 'when createAnchor API is called, the response status is 201. '() {
129 def requestParams = new LinkedMultiValueMap<>()
130 requestParams.add('schema-set-name', 'my_schema-set')
131 requestParams.add('anchor-name', 'my_anchor')
132 when: 'post is invoked'
133 def response = mvc.perform(post(anchorsEndpoint).contentType(MediaType.APPLICATION_JSON)
134 .params(requestParams as MultiValueMap)).andReturn().response
135 then: 'Status is 201 and the response is the name of the created anchor -> my_anchor'
136 1 * mockCpsAdminService.createAnchor('my_dataspace', 'my_schema-set', 'my_anchor')
137 assert response.status == HttpStatus.CREATED.value()
138 assert response.getContentAsString().contains('my_anchor')
141 def 'when get all anchors for a dataspace API is called, the response status is 200 '() {
143 mockCpsAdminService.getAnchors('my_dataspace') >> anchorList
144 when: 'get all anchors API is invoked'
145 def response = mvc.perform(get(anchorsEndpoint)).andReturn().response
146 then: 'Status is 200 and the response is Collection of Anchors containing anchor name -> my_anchor'
147 assert response.status == HttpStatus.OK.value()
148 assert response.getContentAsString().contains('my_anchor')