// Copyright © 2019 Banzai Cloud
//
// Licensed 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
//
//     http://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 workflow

import (
	"encoding/json"
	"reflect"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestCreateAzureInfrastructureWorkflowInputSerializable(t *testing.T) {
	t.Run("default CreateAzureInfrastructureWorkflowInput marshall-unmarshall", func(t *testing.T) {
		testJSONSerializationIdempotent(t, CreateAzureInfrastructureWorkflowInput{})
	})
}

func testJSONMarshallable(t *testing.T, value interface{}) []byte {
	data, err := json.Marshal(value)
	assert.NoError(t, err)
	return data
}

func testJSONUnmarshallable(t *testing.T, data []byte, output interface{}) {
	err := json.Unmarshal(data, output)
	assert.NoError(t, err)
}

func testJSONSerializable(t *testing.T, value interface{}) reflect.Value {
	data := testJSONMarshallable(t, value)
	output := reflect.New(reflect.TypeOf(value))
	testJSONUnmarshallable(t, data, output.Interface())
	return output
}

func testJSONSerializationIdempotent(t *testing.T, value interface{}) {
	output := testJSONSerializable(t, value)
	assert.Equal(t, value, output.Elem().Interface())
}
