// Code generated by azure-service-operator-codegen. DO NOT EDIT.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package arm

import (
	"encoding/json"
	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"github.com/kr/pretty"
	"github.com/kylelemons/godebug/diff"
	"github.com/leanovate/gopter"
	"github.com/leanovate/gopter/gen"
	"github.com/leanovate/gopter/prop"
	"os"
	"reflect"
	"testing"
)

func Test_TopicProperties_WhenSerializedToJson_DeserializesAsEqual(t *testing.T) {
	t.Parallel()
	parameters := gopter.DefaultTestParameters()
	parameters.MinSuccessfulTests = 100
	parameters.MaxSize = 3
	properties := gopter.NewProperties(parameters)
	properties.Property(
		"Round trip of TopicProperties via JSON returns original",
		prop.ForAll(RunJSONSerializationTestForTopicProperties, TopicPropertiesGenerator()))
	properties.TestingRun(t, gopter.NewFormatedReporter(true, 240, os.Stdout))
}

// RunJSONSerializationTestForTopicProperties runs a test to see if a specific instance of TopicProperties round trips to JSON and back losslessly
func RunJSONSerializationTestForTopicProperties(subject TopicProperties) string {
	// Serialize to JSON
	bin, err := json.Marshal(subject)
	if err != nil {
		return err.Error()
	}

	// Deserialize back into memory
	var actual TopicProperties
	err = json.Unmarshal(bin, &actual)
	if err != nil {
		return err.Error()
	}

	// Check for outcome
	match := cmp.Equal(subject, actual, cmpopts.EquateEmpty())
	if !match {
		actualFmt := pretty.Sprint(actual)
		subjectFmt := pretty.Sprint(subject)
		result := diff.Diff(subjectFmt, actualFmt)
		return result
	}

	return ""
}

// Generator of TopicProperties instances for property testing - lazily instantiated by TopicPropertiesGenerator()
var topicPropertiesGenerator gopter.Gen

// TopicPropertiesGenerator returns a generator of TopicProperties instances for property testing.
// We first initialize topicPropertiesGenerator with a simplified generator based on the
// fields with primitive types then replacing it with a more complex one that also handles complex fields
// to ensure any cycles in the object graph properly terminate.
func TopicPropertiesGenerator() gopter.Gen {
	if topicPropertiesGenerator != nil {
		return topicPropertiesGenerator
	}

	generators := make(map[string]gopter.Gen)
	AddIndependentPropertyGeneratorsForTopicProperties(generators)
	topicPropertiesGenerator = gen.Struct(reflect.TypeOf(TopicProperties{}), generators)

	// The above call to gen.Struct() captures the map, so create a new one
	generators = make(map[string]gopter.Gen)
	AddIndependentPropertyGeneratorsForTopicProperties(generators)
	AddRelatedPropertyGeneratorsForTopicProperties(generators)
	topicPropertiesGenerator = gen.Struct(reflect.TypeOf(TopicProperties{}), generators)

	return topicPropertiesGenerator
}

// AddIndependentPropertyGeneratorsForTopicProperties is a factory method for creating gopter generators
func AddIndependentPropertyGeneratorsForTopicProperties(gens map[string]gopter.Gen) {
	gens["InputSchema"] = gen.PtrOf(gen.OneConstOf(TopicProperties_InputSchema_CloudEventSchemaV1_0, TopicProperties_InputSchema_CustomEventSchema, TopicProperties_InputSchema_EventGridSchema))
	gens["PublicNetworkAccess"] = gen.PtrOf(gen.OneConstOf(TopicProperties_PublicNetworkAccess_Disabled, TopicProperties_PublicNetworkAccess_Enabled))
}

// AddRelatedPropertyGeneratorsForTopicProperties is a factory method for creating gopter generators
func AddRelatedPropertyGeneratorsForTopicProperties(gens map[string]gopter.Gen) {
	gens["InboundIpRules"] = gen.SliceOf(InboundIpRuleGenerator())
	gens["InputSchemaMapping"] = gen.PtrOf(InputSchemaMappingGenerator())
}

func Test_Topic_Spec_WhenSerializedToJson_DeserializesAsEqual(t *testing.T) {
	t.Parallel()
	parameters := gopter.DefaultTestParameters()
	parameters.MinSuccessfulTests = 80
	parameters.MaxSize = 3
	properties := gopter.NewProperties(parameters)
	properties.Property(
		"Round trip of Topic_Spec via JSON returns original",
		prop.ForAll(RunJSONSerializationTestForTopic_Spec, Topic_SpecGenerator()))
	properties.TestingRun(t, gopter.NewFormatedReporter(true, 240, os.Stdout))
}

// RunJSONSerializationTestForTopic_Spec runs a test to see if a specific instance of Topic_Spec round trips to JSON and back losslessly
func RunJSONSerializationTestForTopic_Spec(subject Topic_Spec) string {
	// Serialize to JSON
	bin, err := json.Marshal(subject)
	if err != nil {
		return err.Error()
	}

	// Deserialize back into memory
	var actual Topic_Spec
	err = json.Unmarshal(bin, &actual)
	if err != nil {
		return err.Error()
	}

	// Check for outcome
	match := cmp.Equal(subject, actual, cmpopts.EquateEmpty())
	if !match {
		actualFmt := pretty.Sprint(actual)
		subjectFmt := pretty.Sprint(subject)
		result := diff.Diff(subjectFmt, actualFmt)
		return result
	}

	return ""
}

// Generator of Topic_Spec instances for property testing - lazily instantiated by Topic_SpecGenerator()
var topic_SpecGenerator gopter.Gen

// Topic_SpecGenerator returns a generator of Topic_Spec instances for property testing.
// We first initialize topic_SpecGenerator with a simplified generator based on the
// fields with primitive types then replacing it with a more complex one that also handles complex fields
// to ensure any cycles in the object graph properly terminate.
func Topic_SpecGenerator() gopter.Gen {
	if topic_SpecGenerator != nil {
		return topic_SpecGenerator
	}

	generators := make(map[string]gopter.Gen)
	AddIndependentPropertyGeneratorsForTopic_Spec(generators)
	topic_SpecGenerator = gen.Struct(reflect.TypeOf(Topic_Spec{}), generators)

	// The above call to gen.Struct() captures the map, so create a new one
	generators = make(map[string]gopter.Gen)
	AddIndependentPropertyGeneratorsForTopic_Spec(generators)
	AddRelatedPropertyGeneratorsForTopic_Spec(generators)
	topic_SpecGenerator = gen.Struct(reflect.TypeOf(Topic_Spec{}), generators)

	return topic_SpecGenerator
}

// AddIndependentPropertyGeneratorsForTopic_Spec is a factory method for creating gopter generators
func AddIndependentPropertyGeneratorsForTopic_Spec(gens map[string]gopter.Gen) {
	gens["Location"] = gen.PtrOf(gen.AlphaString())
	gens["Name"] = gen.AlphaString()
	gens["Tags"] = gen.MapOf(
		gen.AlphaString(),
		gen.AlphaString())
}

// AddRelatedPropertyGeneratorsForTopic_Spec is a factory method for creating gopter generators
func AddRelatedPropertyGeneratorsForTopic_Spec(gens map[string]gopter.Gen) {
	gens["Properties"] = gen.PtrOf(TopicPropertiesGenerator())
}
