package main

import (
	"bytes"
	"encoding/json"
	"testing"

	"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/handler"
	"github.com/stretchr/testify/require"
)

func TestDefaultNamespace(t *testing.T) {
	xmlBodyWithNamespace := `
 <CompleteMultipartUpload xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
   <Part>
      <ETag>string</ETag>
      <PartNumber>1</PartNumber>
   </Part>
</CompleteMultipartUpload>
`
	xmlBodyWithInvalidNamespace := `
 <CompleteMultipartUpload xmlns="http://bla.bla.bla/">
   <Part>
      <ETag>string</ETag>
      <PartNumber>1</PartNumber>
   </Part>
</CompleteMultipartUpload>
`
	xmlBody := `
 <CompleteMultipartUpload>
   <Part>
      <ETag>string</ETag>
      <PartNumber>1</PartNumber>
   </Part>
</CompleteMultipartUpload>
`

	for _, tc := range []struct {
		settings *appSettings
		input    string
		err      bool
	}{
		{
			settings: &appSettings{
				defaultXMLNS: false,
			},
			input: xmlBodyWithNamespace,
			err:   false,
		},
		{
			settings: &appSettings{
				defaultXMLNS: false,
			},
			input: xmlBody,
			err:   true,
		},
		{
			settings: &appSettings{
				defaultXMLNS: false,
			},
			input: xmlBodyWithInvalidNamespace,
			err:   true,
		},
		{
			settings: &appSettings{
				defaultXMLNS: true,
			},
			input: xmlBodyWithNamespace,
			err:   false,
		},
		{
			settings: &appSettings{
				defaultXMLNS: true,
			},
			input: xmlBody,
			err:   false,
		},
		{
			settings: &appSettings{
				defaultXMLNS: true,
			},
			input: xmlBodyWithInvalidNamespace,
			err:   true,
		},
	} {
		t.Run("", func(t *testing.T) {
			model := new(handler.CompleteMultipartUpload)
			err := tc.settings.NewXMLDecoder(bytes.NewBufferString(tc.input)).Decode(model)
			if tc.err {
				require.Error(t, err)
			} else {
				require.NoError(t, err)
			}
		})
	}
}

func TestNamespacesMarshaling(t *testing.T) {
	dataJSON := `
{
  "namespaces": {
    "kapusta": {
      "location_constraints": {
        "default": "REP 3",
        "load-1-1": "REP 1 CBF 1 SELECT 1 FROM *"
      },
      "copies_numbers": {
        "default": [
          0
        ],
        "load-1-1": [
          1
        ]
      }
    },
    "root": {
      "location_constraints": {
        "default": "REP 3",
        "test": "{\"replicas\":[{\"count\":1,\"selector\":\"\"}],\"containerBackupFactor\":1,\"selectors\":[{\"name\":\"\",\"count\":1,\"clause\":\"CLAUSE_UNSPECIFIED\",\"attribute\":\"\",\"filter\":\"Color\"}],\"filters\":[{\"name\":\"Color\",\"key\":\"Color\",\"op\":\"EQ\",\"value\":\"Red\",\"filters\":[]}],\"unique\":false}"
      },
      "copies_numbers": {
        "default": [
          0
        ],
        "load-1-1": [
          1
        ]
      }
    }
  }
}
`

	var nsConfig NamespacesConfig
	err := json.Unmarshal([]byte(dataJSON), &nsConfig)
	require.NoError(t, err)

	data, err := json.Marshal(nsConfig)
	require.NoError(t, err)

	var nsConfig2 NamespacesConfig
	err = json.Unmarshal(data, &nsConfig2)
	require.NoError(t, err)

	require.Equal(t, nsConfig, nsConfig2)
}