2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Pantheon.tech
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.rest.controller
22 import org.modelmapper.ModelMapper
23 import org.onap.cps.api.CpsAdminService
24 import org.onap.cps.api.CpsModuleService
25 import org.spockframework.spring.SpringBean
26 import org.springframework.beans.factory.annotation.Autowired
27 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
28 import org.springframework.http.HttpStatus
29 import org.springframework.mock.web.MockMultipartFile
30 import org.springframework.test.web.servlet.MockMvc
31 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
32 import spock.lang.Specification
35 class AdminRestControllerSpec extends Specification {
38 CpsModuleService mockCpsModuleService = Mock()
41 CpsAdminService mockCpsAdminService = Mock()
44 ModelMapper modelMapper = Mock();
49 def 'Create schema set from yang file'() {
50 def yangResourceMapCapture
52 def multipartFile = createMultipartFile("filename.yang", "content")
54 def response = performCreateSchemaSetRequest(multipartFile)
55 then: 'Service method is invoked with expected parameters'
56 1 * mockCpsModuleService.createSchemaSet('test-dataspace', 'test-schema-set', _) >>
57 { args -> yangResourceMapCapture = args[2] }
58 yangResourceMapCapture['filename.yang'] == 'content'
59 and: 'Response code indicates success'
60 response.status == HttpStatus.CREATED.value()
63 def 'Create schema set from file with invalid filename extension'() {
65 def multipartFile = createMultipartFile("filename.doc", "content")
67 def response = performCreateSchemaSetRequest(multipartFile)
69 response.status == HttpStatus.BAD_REQUEST.value()
72 def createMultipartFile(filename, content) {
73 return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
76 def performCreateSchemaSetRequest(multipartFile) {
78 MockMvcRequestBuilders
79 .multipart('/v1/dataspaces/test-dataspace/schema-sets')
81 .param('schemaSetName', 'test-schema-set')
82 ).andReturn().response