Create schema set REST API and service level
[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  *  ================================================================================
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.rest.controller
21
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
33
34 @WebMvcTest
35 class AdminRestControllerSpec extends Specification {
36
37     @SpringBean
38     CpsModuleService mockCpsModuleService = Mock()
39
40     @SpringBean
41     CpsAdminService mockCpsAdminService = Mock()
42
43     @SpringBean
44     ModelMapper modelMapper = Mock();
45
46     @Autowired
47     MockMvc mvc
48
49     def 'Create schema set from yang file'() {
50         def yangResourceMapCapture
51         given:
52             def multipartFile = createMultipartFile("filename.yang", "content")
53         when:
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()
61     }
62
63     def 'Create schema set from file with invalid filename extension'() {
64         given:
65             def multipartFile = createMultipartFile("filename.doc", "content")
66         when:
67             def response = performCreateSchemaSetRequest(multipartFile)
68         then:
69             response.status == HttpStatus.BAD_REQUEST.value()
70     }
71
72     def createMultipartFile(filename, content) {
73         return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
74     }
75
76     def performCreateSchemaSetRequest(multipartFile) {
77         return mvc.perform(
78                 MockMvcRequestBuilders
79                         .multipart('/v1/dataspaces/test-dataspace/schema-sets')
80                         .file(multipartFile)
81                         .param('schemaSetName', 'test-schema-set')
82         ).andReturn().response
83     }
84
85 }