<commit_before>package grails.plugin.gson.binding

import grails.persistence.Entity
import grails.test.mixin.Mock
import org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletRequest
import spock.lang.Specification
import spock.util.mop.ConfineMetaClassChanges

import javax.servlet.http.HttpServletRequest

import com.google.gson.*
import grails.plugin.gson.GsonFactory

@ConfineMetaClassChanges(HttpServletRequest)
@Mock(Album)
class RequestBodySpec extends Specification {

	Gson gson

	void setup() {
		gson = new GsonFactory(grailsApplication).createGson()

        GrailsMockHttpServletRequest.metaClass.getJSON = { ->
            new JsonParser().parse(delegate.reader)
        }

		for (domainClass in grailsApplication.domainClasses) {
			domainClass.clazz.metaClass.constructor = { JsonElement json ->
				gson.fromJson(json, delegate)
			}
		}
    }

    void 'can get JSON data from request'() {
        given:
        def request = new GrailsMockHttpServletRequest()
        request.content = '{"artist":"Metric","title":"Synthetica"}'.bytes

        expect:
        JsonElement.isAssignableFrom(request.JSON.getClass())
        request.JSON.artist.getAsString() == 'Metric'
        request.JSON.title.getAsString() == 'Synthetica'
    }

    void 'can bind request json direct to domain class'() {
        given:
        def request = new GrailsMockHttpServletRequest()
        request.content = '{"artist":"Metric","title":"Synthetica"}'.bytes

        when:
        def album = new Album(request.JSON)
<commit_msg>a failing test to get working when I'm less drunk<commit_after>package grails.plugin.gson.binding

import grails.persistence.Entity
import grails.test.mixin.Mock
import org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletRequest
import spock.lang.Specification
import spock.util.mop.ConfineMetaClassChanges

import javax.servlet.http.HttpServletRequest

import com.google.gson.*
import grails.plugin.gson.GsonFactory

@ConfineMetaClassChanges(HttpServletRequest)
@Mock(Album)
class RequestBodySpec extends Specification {

	Gson gson

	void setup() {
		gson = new GsonFactory(grailsApplication).createGson()

        GrailsMockHttpServletRequest.metaClass.getJSON = { ->
            new JsonParser().parse(delegate.reader)
        }

		for (domainClass in grailsApplication.domainClasses) {
			domainClass.clazz.metaClass.constructor = { JsonElement json ->
				gson.fromJson(json, delegate)
			}
		}
    }

    void 'can get JSON data from request'() {
        given:
        def request = new GrailsMockHttpServletRequest()
        request.content = '{"artist":"Metric","title":"Synthetica"}'.bytes

        expect:
        JsonElement.isAssignableFrom(request.JSON.getClass())
        request.JSON.artist.getAsString() == 'Metric'
        request.JSON.title.getAsString() == 'Synthetica'
    }

    void 'can bind request json direct to new domain class'() {
        given:
        def request = new GrailsMockHttpServletRequest()
        request.content = '{"artist":"Metric","title":"Synthetica"}'.bytes

        when:
        def album = new Album(request.JSON)

        then:
        album.artist == 'Metric'
        album.title == 'Synthetica'
    }

    void 'can bind request json direct to existing domain class'() {
        given:
        def request = new GrailsMockHttpServletRequest()
        request.content = '{"artist":"Metric","title":"Synthetica"}'.bytes

		and:
		def album = new Album()

        when:
        album.properties = request.JSON

{'fraction_non_alphanumeric': 0.1075085324232082, 'fraction_numerical': 0.0, 'mean_word_length': 3.401752190237797, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7587868', 'n_tokens_mistral': 1049, 'n_tokens_neox': 1008, 'n_words': 208}