WebMVC groovy test cases for AdminRestController 76/116476/7
authorputhuparambil.aditya <aditya.puthuparambil@bell.ca>
Wed, 6 Jan 2021 11:28:08 +0000 (11:28 +0000)
committerputhuparambil.aditya <aditya.puthuparambil@bell.ca>
Wed, 6 Jan 2021 11:41:16 +0000 (11:41 +0000)
Issue-ID: CPS-129
Signed-off-by: puthuparambil.aditya <aditya.puthuparambil@bell.ca>
Change-Id: Id4bdfb7b6602244ae5abac646a9ec9f68f63cad2

cps-rest/src/test/groovy/org/onap/cps/rest/controller/AdminRestControllerSpec.groovy

index f0d5b3f..ed87e3c 100644 (file)
@@ -1,6 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Pantheon.tech
+ *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -22,15 +23,23 @@ package org.onap.cps.rest.controller
 import org.modelmapper.ModelMapper
 import org.onap.cps.api.CpsAdminService
 import org.onap.cps.api.CpsModuleService
+import org.onap.cps.spi.model.Anchor
 import org.spockframework.spring.SpringBean
 import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
 import org.springframework.http.HttpStatus
+import org.springframework.http.MediaType
 import org.springframework.mock.web.MockMultipartFile
 import org.springframework.test.web.servlet.MockMvc
 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
+import org.springframework.util.LinkedMultiValueMap
+import org.springframework.util.MultiValueMap
 import spock.lang.Specification
 
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
+
 @WebMvcTest
 class AdminRestControllerSpec extends Specification {
 
@@ -41,11 +50,16 @@ class AdminRestControllerSpec extends Specification {
     CpsAdminService mockCpsAdminService = Mock()
 
     @SpringBean
-    ModelMapper modelMapper = Mock();
+    ModelMapper modelMapper = Mock()
 
     @Autowired
     MockMvc mvc
 
+    def anchorsEndpoint = '/v1/dataspaces/my_dataspace/anchors'
+
+    def anchor = new Anchor(name: 'my_anchor')
+    def anchorList = [anchor]
+
     def 'Create schema set from yang file'() {
         def yangResourceMapCapture
         given:
@@ -82,4 +96,27 @@ class AdminRestControllerSpec extends Specification {
         ).andReturn().response
     }
 
+    def 'when createAnchor API is called, the response status is 201. '() {
+        given:
+            def requestParams = new LinkedMultiValueMap<>()
+            requestParams.add('schema-set-name', 'my_schema-set')
+            requestParams.add('anchor-name', 'my_anchor')
+        when: 'post is invoked'
+            def response = mvc.perform(post(anchorsEndpoint).contentType(MediaType.APPLICATION_JSON)
+                    .params(requestParams as MultiValueMap)).andReturn().response
+        then: 'Status is 201 and the response is the name of the created anchor -> my_anchor'
+            1 * mockCpsAdminService.createAnchor('my_dataspace', 'my_schema-set', 'my_anchor')
+            assert response.status == HttpStatus.CREATED.value()
+            assert response.getContentAsString().contains('my_anchor')
+    }
+
+    def 'when get all anchors for a dataspace API is called, the response status is 200 '() {
+        given:
+            mockCpsAdminService.getAnchors('my_dataspace') >> anchorList
+        when: 'get all anchors API is invoked'
+            def response = mvc.perform(get(anchorsEndpoint)).andReturn().response
+        then: 'Status is 200 and the response is Collection of Anchors containing anchor name -> my_anchor'
+            assert response.status == HttpStatus.OK.value()
+            assert response.getContentAsString().contains('my_anchor')
+    }
 }