Copilot commented on code in PR #16641:
URL: https://github.com/apache/grails-core/pull/16641#discussion_r4264435014


##########
grails-rest-transforms/src/test/groovy/org/grails/plugins/web/rest/render/atom/AtomRendererSpec.groovy:
##########
@@ -0,0 +1,241 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.plugins.web.rest.render.atom
+
+import grails.core.DefaultGrailsApplication
+import grails.core.GrailsApplication
+import grails.persistence.Entity
+import grails.rest.render.atom.AtomCollectionRenderer
+import grails.rest.render.atom.AtomRenderer
+import grails.spring.BeanBuilder
+import grails.util.GrailsWebMockUtil
+import grails.util.GrailsWebUtil
+import grails.web.CamelCaseUrlConverter
+import grails.web.mapping.LinkGenerator
+import org.grails.config.PropertySourcesConfig
+import org.grails.core.lifecycle.ShutdownOperations
+import 
org.grails.datastore.mapping.keyvalue.mapping.config.KeyValueMappingContext
+import org.grails.plugins.web.mime.MimeTypesConfiguration
+import org.grails.plugins.web.rest.render.ServletRenderContext
+import org.grails.plugins.web.rest.render.hal.Product
+import org.grails.plugins.web.rest.render.hal.SimpleProduct
+import org.grails.support.MockApplicationContext
+import org.grails.web.converters.configuration.ConvertersConfigurationHolder
+import 
org.grails.web.converters.configuration.ConvertersConfigurationInitializer
+import org.grails.web.mapping.DefaultLinkGenerator
+import org.grails.web.mapping.DefaultUrlMappingEvaluator
+import org.grails.web.mapping.DefaultUrlMappingsHolder
+import org.grails.web.mime.DefaultMimeUtility
+import org.grails.web.mime.HttpServletResponseExtension
+import org.grails.web.servlet.mvc.GrailsWebRequest
+import org.springframework.context.support.GenericApplicationContext
+import org.springframework.context.support.StaticMessageSource
+import org.springframework.mock.web.MockHttpServletRequest
+import org.springframework.mock.web.MockHttpServletResponse
+import org.springframework.mock.web.MockServletContext
+import org.springframework.web.context.WebApplicationContext
+import org.springframework.web.context.request.RequestContextHolder
+import org.springframework.web.context.support.GenericWebApplicationContext
+import org.springframework.web.util.WebUtils
+import spock.lang.Specification
+
+class AtomRendererSpec extends Specification {
+
+    void setup() {
+        ShutdownOperations.runOperations()
+        HttpServletResponseExtension.@mimeTypes = null
+        def initializer = new ConvertersConfigurationInitializer()
+        initializer.grailsApplication = new DefaultGrailsApplication()
+        initializer.initialize()
+    }
+
+    void cleanup() {
+        ConvertersConfigurationHolder.clear()
+        RequestContextHolder.resetRequestAttributes()
+        ShutdownOperations.runOperations()
+        HttpServletResponseExtension.@mimeTypes = null
+    }
+
+    void 'Test rendering a single domain object as an Atom feed'() {
+        given:
+        def renderer = configureRenderer(new AtomRenderer(AtomBook))
+        def webRequest = boundMimeTypeRequest()
+        def response = webRequest.response as MockHttpServletResponse
+        def book = new AtomBook(title: 'Grails & Groovy')
+        book.id = 1L
+        book.dateCreated = new Date(1383908400000L)
+        book.lastUpdated = new Date(1383994800000L)
+
+        when:
+        renderer.render(book, new ServletRenderContext(webRequest))
+
+        then:
+        response.contentType == 
GrailsWebUtil.getContentType(AtomRenderer.MIME_TYPE.name, 
GrailsWebUtil.DEFAULT_ENCODING)
+        response.contentAsString.startsWith('<?xml version="1.0" 
encoding="UTF-8"?>')
+        response.contentAsString.contains('<feed 
xmlns="http://www.w3.org/2005/Atom";>')
+        response.contentAsString.contains('<title>Grails &amp; Groovy</title>')
+        response.contentAsString.contains('<published>')
+        response.contentAsString.contains('<updated>')
+        response.contentAsString.contains('<id>tag:localhost,')
+        response.contentAsString.contains(':1</id>')
+        response.contentAsString.contains('<link rel="self" 
href="http://localhost/books/1"; hreflang="en" title="Books" 
type="application/atom+xml" />')
+        response.contentAsString.contains('<link rel="alternate" 
href="http://localhost/books/1"; hreflang="en" title="Books" />')
+        response.contentAsString.endsWith('</feed>')

Review Comment:
   Similar to the HAL spec, these assertions are tightly coupled to the exact 
serialized XML string (notably attribute ordering in `<link ... />`). To reduce 
flakiness, parse the XML and assert on element existence/values/attributes 
(e.g., verify there is a `feed`, two `link` elements with expected 
`rel`/`href`/`type`, etc.).



##########
grails-rest-transforms/src/test/groovy/org/grails/plugins/web/rest/render/hal/HalXmlRendererSpec.groovy:
##########
@@ -0,0 +1,236 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.plugins.web.rest.render.hal
+
+import grails.core.DefaultGrailsApplication
+import grails.core.GrailsApplication
+import grails.rest.render.hal.HalXmlCollectionRenderer
+import grails.rest.render.hal.HalXmlRenderer
+import grails.spring.BeanBuilder
+import grails.util.GrailsWebMockUtil
+import grails.util.GrailsWebUtil
+import grails.web.CamelCaseUrlConverter
+import grails.web.mapping.LinkGenerator
+import org.grails.config.PropertySourcesConfig
+import org.grails.core.lifecycle.ShutdownOperations
+import 
org.grails.datastore.mapping.keyvalue.mapping.config.KeyValueMappingContext
+import org.grails.plugins.web.mime.MimeTypesConfiguration
+import org.grails.plugins.web.rest.render.ServletRenderContext
+import org.grails.support.MockApplicationContext
+import org.grails.web.converters.configuration.ConvertersConfigurationHolder
+import 
org.grails.web.converters.configuration.ConvertersConfigurationInitializer
+import org.grails.web.mapping.DefaultLinkGenerator
+import org.grails.web.mapping.DefaultUrlMappingEvaluator
+import org.grails.web.mapping.DefaultUrlMappingsHolder
+import org.grails.web.mime.DefaultMimeUtility
+import org.grails.web.mime.HttpServletResponseExtension
+import org.grails.web.servlet.mvc.GrailsWebRequest
+import org.springframework.context.support.GenericApplicationContext
+import org.springframework.context.support.StaticMessageSource
+import org.springframework.mock.web.MockHttpServletRequest
+import org.springframework.mock.web.MockHttpServletResponse
+import org.springframework.mock.web.MockServletContext
+import org.springframework.web.context.WebApplicationContext
+import org.springframework.web.context.request.RequestContextHolder
+import org.springframework.web.context.support.GenericWebApplicationContext
+import org.springframework.web.util.WebUtils
+import spock.lang.Specification
+
+class HalXmlRendererSpec extends Specification {
+
+    void setup() {
+        ShutdownOperations.runOperations()
+        HttpServletResponseExtension.@mimeTypes = null
+        def initializer = new ConvertersConfigurationInitializer()
+        initializer.grailsApplication = new DefaultGrailsApplication()
+        initializer.initialize()
+    }
+
+    void cleanup() {
+        ConvertersConfigurationHolder.clear()
+        RequestContextHolder.resetRequestAttributes()
+        ShutdownOperations.runOperations()
+        HttpServletResponseExtension.@mimeTypes = null
+    }

Review Comment:
   The spec mutates a static/internal state 
(`HttpServletResponseExtension.@mimeTypes`) without restoring the previous 
value. This can create cross-test interference when tests are run in a shared 
JVM. Consider capturing the prior value in `setup()` and restoring it in 
`cleanup()`, so the spec is isolated and order-independent.



##########
grails-rest-transforms/src/test/groovy/org/grails/plugins/web/rest/render/atom/AtomRendererSpec.groovy:
##########
@@ -0,0 +1,241 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.plugins.web.rest.render.atom
+
+import grails.core.DefaultGrailsApplication
+import grails.core.GrailsApplication
+import grails.persistence.Entity
+import grails.rest.render.atom.AtomCollectionRenderer
+import grails.rest.render.atom.AtomRenderer
+import grails.spring.BeanBuilder
+import grails.util.GrailsWebMockUtil
+import grails.util.GrailsWebUtil
+import grails.web.CamelCaseUrlConverter
+import grails.web.mapping.LinkGenerator
+import org.grails.config.PropertySourcesConfig
+import org.grails.core.lifecycle.ShutdownOperations
+import 
org.grails.datastore.mapping.keyvalue.mapping.config.KeyValueMappingContext
+import org.grails.plugins.web.mime.MimeTypesConfiguration
+import org.grails.plugins.web.rest.render.ServletRenderContext
+import org.grails.plugins.web.rest.render.hal.Product
+import org.grails.plugins.web.rest.render.hal.SimpleProduct
+import org.grails.support.MockApplicationContext
+import org.grails.web.converters.configuration.ConvertersConfigurationHolder
+import 
org.grails.web.converters.configuration.ConvertersConfigurationInitializer
+import org.grails.web.mapping.DefaultLinkGenerator
+import org.grails.web.mapping.DefaultUrlMappingEvaluator
+import org.grails.web.mapping.DefaultUrlMappingsHolder
+import org.grails.web.mime.DefaultMimeUtility
+import org.grails.web.mime.HttpServletResponseExtension
+import org.grails.web.servlet.mvc.GrailsWebRequest
+import org.springframework.context.support.GenericApplicationContext
+import org.springframework.context.support.StaticMessageSource
+import org.springframework.mock.web.MockHttpServletRequest
+import org.springframework.mock.web.MockHttpServletResponse
+import org.springframework.mock.web.MockServletContext
+import org.springframework.web.context.WebApplicationContext
+import org.springframework.web.context.request.RequestContextHolder
+import org.springframework.web.context.support.GenericWebApplicationContext
+import org.springframework.web.util.WebUtils
+import spock.lang.Specification
+
+class AtomRendererSpec extends Specification {
+
+    void setup() {
+        ShutdownOperations.runOperations()
+        HttpServletResponseExtension.@mimeTypes = null
+        def initializer = new ConvertersConfigurationInitializer()
+        initializer.grailsApplication = new DefaultGrailsApplication()
+        initializer.initialize()
+    }
+
+    void cleanup() {
+        ConvertersConfigurationHolder.clear()
+        RequestContextHolder.resetRequestAttributes()
+        ShutdownOperations.runOperations()
+        HttpServletResponseExtension.@mimeTypes = null
+    }

Review Comment:
   Same isolation concern as in the HAL spec: the static/internal `@mimeTypes` 
field is overwritten without restoring the prior value. Restoring the original 
value in `cleanup()` will prevent leakage into other specs running in the same 
JVM/process.



##########
grails-rest-transforms/src/test/groovy/org/grails/plugins/web/rest/render/hal/HalXmlRendererSpec.groovy:
##########
@@ -0,0 +1,236 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.plugins.web.rest.render.hal
+
+import grails.core.DefaultGrailsApplication
+import grails.core.GrailsApplication
+import grails.rest.render.hal.HalXmlCollectionRenderer
+import grails.rest.render.hal.HalXmlRenderer
+import grails.spring.BeanBuilder
+import grails.util.GrailsWebMockUtil
+import grails.util.GrailsWebUtil
+import grails.web.CamelCaseUrlConverter
+import grails.web.mapping.LinkGenerator
+import org.grails.config.PropertySourcesConfig
+import org.grails.core.lifecycle.ShutdownOperations
+import 
org.grails.datastore.mapping.keyvalue.mapping.config.KeyValueMappingContext
+import org.grails.plugins.web.mime.MimeTypesConfiguration
+import org.grails.plugins.web.rest.render.ServletRenderContext
+import org.grails.support.MockApplicationContext
+import org.grails.web.converters.configuration.ConvertersConfigurationHolder
+import 
org.grails.web.converters.configuration.ConvertersConfigurationInitializer
+import org.grails.web.mapping.DefaultLinkGenerator
+import org.grails.web.mapping.DefaultUrlMappingEvaluator
+import org.grails.web.mapping.DefaultUrlMappingsHolder
+import org.grails.web.mime.DefaultMimeUtility
+import org.grails.web.mime.HttpServletResponseExtension
+import org.grails.web.servlet.mvc.GrailsWebRequest
+import org.springframework.context.support.GenericApplicationContext
+import org.springframework.context.support.StaticMessageSource
+import org.springframework.mock.web.MockHttpServletRequest
+import org.springframework.mock.web.MockHttpServletResponse
+import org.springframework.mock.web.MockServletContext
+import org.springframework.web.context.WebApplicationContext
+import org.springframework.web.context.request.RequestContextHolder
+import org.springframework.web.context.support.GenericWebApplicationContext
+import org.springframework.web.util.WebUtils
+import spock.lang.Specification
+
+class HalXmlRendererSpec extends Specification {
+
+    void setup() {
+        ShutdownOperations.runOperations()
+        HttpServletResponseExtension.@mimeTypes = null
+        def initializer = new ConvertersConfigurationInitializer()
+        initializer.grailsApplication = new DefaultGrailsApplication()
+        initializer.initialize()
+    }
+
+    void cleanup() {
+        ConvertersConfigurationHolder.clear()
+        RequestContextHolder.resetRequestAttributes()
+        ShutdownOperations.runOperations()
+        HttpServletResponseExtension.@mimeTypes = null
+    }
+
+    void 'Test rendering a domain object with an embedded resource as HAL 
XML'() {
+        given:
+        def renderer = configureRenderer(new HalXmlRenderer(Product))
+        def webRequest = boundMimeTypeRequest()
+        def response = webRequest.response as MockHttpServletResponse
+        def product = new Product(name: 'MacBook & accessories', 
numberInStock: 10,
+                category: new Category(name: 'Laptops'))
+
+        when:
+        renderer.render(product, new ServletRenderContext(webRequest))
+
+        then:
+        response.contentType == 
GrailsWebUtil.getContentType(HalXmlRenderer.MIME_TYPE.name, 
GrailsWebUtil.DEFAULT_ENCODING)
+        response.contentAsString.startsWith('<?xml version="1.0" 
encoding="UTF-8"?><resource href="http://localhost/products"; hreflang="en" 
title="Products">')
+        response.contentAsString.contains('<name>MacBook &amp; 
accessories</name>')
+        response.contentAsString.contains('<numberInStock>10</numberInStock>')
+        response.contentAsString.contains('<resource 
href="http://localhost/category/index"; 
hreflang="en"><name>Laptops</name></resource>')
+        response.contentAsString.endsWith('</resource></resource>')

Review Comment:
   These assertions rely on exact serialized XML fragments (including attribute 
order and formatting), which tends to make tests brittle across XML 
serializer/converter changes. Prefer parsing `response.contentAsString` as XML 
(e.g., with `XmlSlurper`/DOM) and asserting on nodes/attributes, so the tests 
validate structure/semantics instead of serialization details.



-- 
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