github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/labeldef/converter_test.go (about)

     1  package labeldef_test
     2  
     3  import (
     4  	"database/sql"
     5  	"encoding/json"
     6  	"testing"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/internal/domain/labeldef"
     9  	"github.com/kyma-incubator/compass/components/director/internal/model"
    10  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  const testTenant = "e9ed370d-4056-45ee-8257-49a64f99771b"
    16  
    17  func TestFromGraphQL(t *testing.T) {
    18  	t.Run("Correct schema", func(t *testing.T) {
    19  		// GIVEN
    20  		sut := labeldef.NewConverter()
    21  		schema := graphql.JSONSchema(`{"schema":"schema"}`)
    22  		expectedSchema := map[string]interface{}{
    23  			"schema": interface{}("schema"),
    24  		}
    25  
    26  		// WHEN
    27  		actual, err := sut.FromGraphQL(graphql.LabelDefinitionInput{
    28  			Key:    "some-key",
    29  			Schema: &schema,
    30  		}, testTenant)
    31  		// THEN
    32  		require.NoError(t, err)
    33  		assert.Empty(t, actual.ID)
    34  		assert.Equal(t, testTenant, actual.Tenant)
    35  		require.NotNil(t, actual.Schema)
    36  		assert.Equal(t, expectedSchema, *actual.Schema)
    37  		assert.Equal(t, "some-key", actual.Key)
    38  	})
    39  
    40  	t.Run("Error - invalid schema", func(t *testing.T) {
    41  		// GIVEN
    42  		sut := labeldef.NewConverter()
    43  		invalidSchema := graphql.JSONSchema(`"schema":`)
    44  		// WHEN
    45  		_, err := sut.FromGraphQL(graphql.LabelDefinitionInput{
    46  			Key:    "some-key",
    47  			Schema: &invalidSchema,
    48  		}, testTenant)
    49  		// THEN
    50  		require.Error(t, err)
    51  	})
    52  }
    53  
    54  func TestToGraphQL(t *testing.T) {
    55  	t.Run("Success", func(t *testing.T) {
    56  		// GIVEN
    57  		sut := labeldef.NewConverter()
    58  		// WHEN
    59  		expectedSchema := graphql.JSONSchema(`{"schema":"schema"}`)
    60  		anySchema := map[string]interface{}{
    61  			"schema": interface{}("schema"),
    62  		}
    63  		var schema interface{} = anySchema
    64  		actual, err := sut.ToGraphQL(model.LabelDefinition{
    65  			Key:    "some-key",
    66  			Schema: &schema,
    67  		})
    68  		// THEN
    69  		require.NoError(t, err)
    70  		assert.Equal(t, "some-key", actual.Key)
    71  		require.NotNil(t, actual.Schema)
    72  		assert.Equal(t, expectedSchema, *actual.Schema)
    73  	})
    74  }
    75  
    76  func TestToEntity(t *testing.T) {
    77  	// GIVEN
    78  	var schema interface{} = ExampleSchema{
    79  		ID:    "id",
    80  		Title: "title",
    81  	}
    82  
    83  	sut := labeldef.NewConverter()
    84  	// WHEN
    85  	actual, err := sut.ToEntity(model.LabelDefinition{
    86  		Key:     "some-key",
    87  		Tenant:  "tenant",
    88  		ID:      "id",
    89  		Schema:  &schema,
    90  		Version: 0,
    91  	})
    92  	// THEN
    93  	require.NoError(t, err)
    94  	assert.Equal(t, "some-key", actual.Key)
    95  	assert.Equal(t, "tenant", actual.TenantID)
    96  	assert.Equal(t, "id", actual.ID)
    97  	assert.True(t, actual.SchemaJSON.Valid)
    98  	assert.Equal(t, `{"$id":"id","title":"title"}`, actual.SchemaJSON.String)
    99  	assert.Equal(t, 0, actual.Version)
   100  }
   101  
   102  func TestToEntityWhenNoSchema(t *testing.T) {
   103  	// GIVEN
   104  	sut := labeldef.NewConverter()
   105  	// WHEN
   106  	actual, err := sut.ToEntity(model.LabelDefinition{
   107  		Key:     "key",
   108  		Tenant:  "tenant",
   109  		ID:      "id",
   110  		Version: 0,
   111  	})
   112  	// THENr
   113  	require.NoError(t, err)
   114  	assert.Empty(t, actual.SchemaJSON)
   115  	assert.Equal(t, "key", actual.Key)
   116  	assert.Equal(t, "tenant", actual.TenantID)
   117  	assert.Equal(t, "id", actual.ID)
   118  	assert.Equal(t, 0, actual.Version)
   119  }
   120  
   121  func TestFromEntityWhenNoSchema(t *testing.T) {
   122  	// GIVEN
   123  	in := labeldef.Entity{
   124  		ID:       "id",
   125  		Key:      "key",
   126  		TenantID: "tenant",
   127  		Version:  0,
   128  	}
   129  	sut := labeldef.NewConverter()
   130  	// WHEN
   131  	actual, err := sut.FromEntity(in)
   132  	// THEN
   133  	require.NoError(t, err)
   134  	assert.Equal(t, "id", actual.ID)
   135  	assert.Equal(t, "key", actual.Key)
   136  	assert.Equal(t, "tenant", actual.Tenant)
   137  	assert.Equal(t, 0, actual.Version)
   138  	assert.Nil(t, actual.Schema)
   139  }
   140  
   141  func TestFromEntityWhenSchemaProvided(t *testing.T) {
   142  	// GIVEN
   143  	in := labeldef.Entity{
   144  		ID:       "id",
   145  		Key:      "key",
   146  		TenantID: "tenant",
   147  		SchemaJSON: sql.NullString{
   148  			Valid:  true,
   149  			String: `{"$id":"xxx","title":"title"}`,
   150  		},
   151  		Version: 0,
   152  	}
   153  	sut := labeldef.NewConverter()
   154  	// WHEN
   155  	actual, err := sut.FromEntity(in)
   156  	// THEN
   157  	require.NoError(t, err)
   158  	assert.Equal(t, "id", actual.ID)
   159  	assert.Equal(t, "key", actual.Key)
   160  	assert.Equal(t, "tenant", actual.Tenant)
   161  	assert.Equal(t, 0, actual.Version)
   162  	assert.NotNil(t, actual.Schema)
   163  	// converting to specific type
   164  	b, err := json.Marshal(actual.Schema)
   165  	require.NoError(t, err)
   166  	var exSchema ExampleSchema
   167  	err = json.Unmarshal(b, &exSchema)
   168  	require.NoError(t, err)
   169  	assert.Equal(t, ExampleSchema{ID: "xxx", Title: "title"}, exSchema)
   170  }
   171  
   172  type ExampleSchema struct {
   173  	ID    string `json:"$id"`
   174  	Title string `json:"title"`
   175  }