matrei commented on code in PR #15478:
URL: https://github.com/apache/grails-core/pull/15478#discussion_r2890665060


##########
grails-test-examples/views-functional-tests/src/integration-test/groovy/functional/tests/ProductSpec.groovy:
##########
@@ -16,209 +16,161 @@
  *  specific language governing permissions and limitations
  *  under the License.
  */
-
 package functional.tests
 
-import com.fasterxml.jackson.databind.ObjectMapper
+import spock.lang.Specification
+
 import grails.testing.mixin.integration.Integration
-import grails.testing.spock.RunOnce
-import grails.web.http.HttpHeaders
-import io.micronaut.http.HttpRequest
-import io.micronaut.http.HttpResponse
-import io.micronaut.http.HttpStatus
-import org.junit.jupiter.api.BeforeEach
-import spock.lang.Shared
-
-@Integration(applicationClass = Application)
-class ProductSpec extends HttpClientSpec {
-
-    @Shared
-    ObjectMapper objectMapper
-
-    def setup() {
-        objectMapper = new ObjectMapper()
-    }
+import org.apache.grails.testing.httpclient.HttpClientSupport
 
-    @RunOnce
-    @BeforeEach
-    void init() {
-        super.init()
-    }
+@Integration
+class ProductSpec extends Specification implements HttpClientSupport {
 
     void testEmptyProducts() {
         when:
-        HttpRequest request = HttpRequest.GET('/products')
-        HttpResponse<String> resp = client.toBlocking().exchange(request, 
String)
-        Map body = objectMapper.readValue(resp.body(), Map)
-
-        then:
-        resp.status == HttpStatus.OK
-        resp.headers.getFirst(HttpHeaders.CONTENT_TYPE).isPresent()
-        resp.headers.getFirst(HttpHeaders.CONTENT_TYPE).get() == 
'application/hal+json;charset=UTF-8'
-
-        and: 'The values returned are there'
-        body.count == 0
-        body.max == 10
-        body.offset == 0
-        body.sort == null
-        body.order == null
+        def response = http('/products')
+
+        then: 'The values returned are there'
+        response.expectJsonContains(200, 'Content-Type': 
'application/hal+json;charset=UTF-8', [
+                count: 0,
+                max: 10,
+                offset: 0,
+                order: null,
+                sort: null
+        ])
 
         and: 'the hal _links attribute is present'
-        body._links.size() == 1
-        body._links.self.href.startsWith("${baseUrl}/product")
+        def json = response.json()
+        json._links.size() == 1
+        json._links.self.href.startsWith("$httpClientRootUri/product")
 
         and: 'there are no products yet'
-        body._embedded.products.size() == 0
+        json._embedded.products.size() == 0
     }
 
     void testSingleProduct() {
-        given:
-        HttpRequest request = HttpRequest.POST('/products', [
+        when:
+        def createResponse = httpPost('/products', [
                 name: 'Product 1',
                 description: 'product 1 description',
                 price: 123.45
         ])
 
-        when:
-        HttpResponse<String> createResp = 
client.toBlocking().exchange(request, String)
-        Map createBody = objectMapper.readValue(createResp.body(), Map)
-
         then:
-        createResp.status == HttpStatus.CREATED
+        createResponse.expectStatus(201)
+        def createBody = createResponse.json()
 
         when: 'We get the products'
-        request = HttpRequest.GET('/products')
-        HttpResponse<String> resp = client.toBlocking().exchange(request, 
String)
-        Map body = objectMapper.readValue(resp.body(), Map)
-
-        then:
-        resp.status == HttpStatus.OK
-        resp.headers.getFirst(HttpHeaders.CONTENT_TYPE).isPresent()
-        resp.headers.getFirst(HttpHeaders.CONTENT_TYPE).get() == 
'application/hal+json;charset=UTF-8'
-
-        and: 'The values returned are there'
-        body.count == 1
-        body.max == 10
-        body.offset == 0
-        body.sort == null
-        body.order == null
+        def response = http('/products')
+
+        then: 'The values returned are there'
+        response.expectJsonContains(200, 'Content-Type': 
'application/hal+json;charset=UTF-8', [
+                count: 1,
+                max: 10,
+                offset: 0,
+                sort: null,
+                order: null
+        ])
 
         and: 'the hal _links attribute is present'
-        body._links.size() == 1
-        body._links.self.href.startsWith("${baseUrl}/product")
+        def json = response.json()
+        json._links.size() == 1
+        json._links.self.href.startsWith("$httpClientRootUri/product")
 
         and: 'the product is present'
-        body._embedded.products.size() == 1
-        body._embedded.products.first().name == 'Product 1'
+        json._embedded.products.size() == 1
+        json._embedded.products.first().name == 'Product 1'
 
         cleanup:
-        resp = 
client.toBlocking().exchange(HttpRequest.DELETE("/products/${createBody.id}"))
-        assert resp.status() == HttpStatus.OK
+        httpDelete("/products/${createBody.id}")
     }
 
     void 'test a page worth of products'() {
         given:
         def productsIds = []
         15.times { productNumber ->
-            ProductVM product = new ProductVM(
-                    name: "Product $productNumber",
-                    description: "product ${productNumber} description",
-                    price: productNumber + (productNumber / 100)
-            )
-            HttpResponse<String> createResp = client.toBlocking()
-                    .exchange(HttpRequest.POST('/products', product), String)
-            Map createBody = objectMapper.readValue(createResp.body(), Map)
-            assert createResp.status == HttpStatus.CREATED
-            productsIds << createBody.id
+            def product = [
+                name: "Product $productNumber",
+                description: "product ${productNumber} description",
+                price: productNumber + (productNumber / 100)
+            ]
+            def createResponse = httpPost('/products', product)
+            assert createResponse.statusCode() == 201
+            productsIds << createResponse.json().id
         }
 
         when: 'We get the products'
-        HttpRequest request = HttpRequest.GET('/products')
-        HttpResponse<String> resp = client.toBlocking().exchange(request, 
String)
-        Map body = objectMapper.readValue(resp.body(), Map)
+        def response = http('/products')
 
         then:
-        resp.status == HttpStatus.OK
-        resp.headers.getFirst(HttpHeaders.CONTENT_TYPE).isPresent()
-        resp.headers.getFirst(HttpHeaders.CONTENT_TYPE).get() == 
'application/hal+json;charset=UTF-8'
+        response.expectHeaders(200, 'Content-Type': 
'application/hal+json;charset=UTF-8')
 

Review Comment:
   Not correct, it uses the named param feature in Groovy.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to