11package remoteconfig
22
33import (
4+ "bytes"
45 "errors"
56 "fmt"
67 "net/http"
78 "net/http/httptest"
8- "testing"
9-
10- "bytes"
11-
129 "regexp"
10+ "strings"
11+ "testing"
1312
1413 "github.com/stretchr/testify/assert"
1514 "github.com/stretchr/testify/suite"
@@ -21,7 +20,6 @@ const (
2120 VALID_REMOTE_CONFIG_SQS_QUEUE_NAME string = "testQueue"
2221 VALID_REMOTE_CONFIG_DYNAMODB_CLIENT_REGION AWSRegion = AWS_REGION_US_EAST_1
2322 VALID_REMOTE_CONFIG_DYNAMODB_TABLE_NAME string = "testTable"
24- VALID_REMOTE_CONFIG_NO_ENDPOINT string = ""
2523 VALID_REMOTE_CONFIG_STORAGE_CONFIG_PROVIDER StorageProvider = STORAGE_PROVIDER_AWS
2624 VALID_REMOTE_CONFIG_STORAGE_CONFIG_LOCATION StorageLocation = (StorageLocation )(AWS_REGION_US_WEST_2 )
2725)
@@ -34,7 +32,13 @@ func TestRemoteConfigSuite(t *testing.T) {
3432 suite .Run (t , new (RemoteConfigSuite ))
3533}
3634
35+ type EmbeddedConfig struct {
36+ EmbeddedStr * string `json:"embedded_string,omitempty"`
37+ EmbeddedInt * int64 `json:"embedded_int,omitempty"`
38+ }
39+
3740type SampleConfig struct {
41+ EmbeddedConfig
3842 SQSQueueOptional * SQSQueueConfig `json:"sqs_queue_optional,omitempty" remoteconfig:"optional"`
3943 SQSClientOptional * SQSClientConfig `json:"sqs_client_optional,omitempty" remoteconfig:"optional"`
4044 DynamoDBTableOptional * DynamoDBTableConfig `json:"dynamodb_table_optional,omitempty" remoteconfig:"optional"`
@@ -54,11 +58,48 @@ type SampleConfig struct {
5458 MapStrStr map [string ]* string `json:"map_str_str,omitempty"`
5559}
5660
57- func (s * RemoteConfigSuite ) SetupSuite () {
58- }
59-
60- func (s * RemoteConfigSuite ) SetupTest () {
61- }
61+ var validConfigJSON = `
62+ {
63+ "embedded_string": "abc",
64+ "embedded_int": 123,
65+ "sqs_client" : {
66+ "region" : "us-east-1",
67+ "endpoint" : "http://localhost:3000/sqs"
68+ },
69+ "sqs_queue" : {
70+ "region" : "us-east-1",
71+ "aws_account_id" : "345833302425",
72+ "queue_name" : "testQueue"
73+ },
74+ "dynamodb_client" : {
75+ "region" : "us-east-1",
76+ "endpoint" : "http://localhost:8000/dynamodb"
77+ },
78+ "dynamodb_table" : {
79+ "table_name" : "testTable"
80+ },
81+ "str" : "testStr",
82+ "storage_config" : {
83+ "provider" : "aws",
84+ "location" : "us-west-2"
85+ },
86+ "storage_config_slice" : [{
87+ "provider" : "aws",
88+ "location" : "us-west-2"
89+ },
90+ {
91+ "provider" : "aws",
92+ "location" : "us-east-1"
93+ }],
94+ "storage_config_map": {
95+ "one": {
96+ "provider": "aws",
97+ "location": "us-west-2"
98+ }
99+ },
100+ "str_slice": [ "hello" ],
101+ "map_str_str": { "key": "value" }
102+ }`
62103
63104func (s * RemoteConfigSuite ) TestValidateConfigWithReflection () {
64105 c := s .buildValidSampleConfig ()
@@ -519,49 +560,8 @@ func (s *RemoteConfigSuite) TestValidateConfigWithReflectionErrorStorageConfigSl
519560}
520561
521562func (s * RemoteConfigSuite ) TestLoadConfigFromURL_Gold () {
522- configJSON := `
523- {
524- "sqs_client" : {
525- "region" : "us-east-1",
526- "endpoint" : "http://localhost:3000/sqs"
527- },
528- "sqs_queue" : {
529- "region" : "us-east-1",
530- "aws_account_id" : "345833302425",
531- "queue_name" : "testQueue"
532- },
533- "dynamodb_client" : {
534- "region" : "us-east-1",
535- "endpoint" : "http://localhost:8000/dynamodb"
536- },
537- "dynamodb_table" : {
538- "table_name" : "testTable"
539- },
540- "str" : "testStr",
541- "storage_config" : {
542- "provider" : "aws",
543- "location" : "us-west-2"
544- },
545- "storage_config_slice" : [{
546- "provider" : "aws",
547- "location" : "us-west-2"
548- },
549- {
550- "provider" : "aws",
551- "location" : "us-east-1"
552- }],
553- "storage_config_map": {
554- "one": {
555- "provider": "aws",
556- "location": "us-west-2"
557- }
558- },
559- "str_slice": [ "hello" ],
560- "map_str_str": { "key": "value" }
561- }`
562-
563563 ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
564- fmt .Fprintln (w , configJSON )
564+ fmt .Fprintln (w , validConfigJSON )
565565 }))
566566 defer ts .Close ()
567567
@@ -591,52 +591,32 @@ func (s *RemoteConfigSuite) TestLoadConfigFromURLError() {
591591}
592592
593593func (s * RemoteConfigSuite ) TestReadJSONValidate () {
594- configJSON := `
595- {
596- "sqs_client" : {
597- "region" : "us-east-1",
598- "endpoint" : "http://localhost:3000/sqs"
599- },
600- "sqs_queue" : {
601- "region" : "us-east-1",
602- "aws_account_id" : "345833302425",
603- "queue_name" : "testQueue"
604- },
605- "dynamodb_client" : {
606- "region" : "us-east-1",
607- "endpoint" : "http://localhost:8000/dynamodb"
608- },
609- "dynamodb_table" : {
610- "table_name" : "testTable"
611- },
612- "str" : "testStr",
613- "storage_config" : {
614- "provider" : "aws",
615- "location" : "us-west-2"
616- },
617- "storage_config_slice" : [{
618- "provider" : "aws",
619- "location" : "us-west-2"
620- },
621- {
622- "provider" : "aws",
623- "location" : "us-east-1"
624- }],
625- "storage_config_map": {
626- "one": {
627- "provider": "aws",
628- "location": "us-west-2"
629- }
630- },
631- "str_slice": [ "hello" ],
632- "map_str_str": { "key": "value" }
633- }`
594+ cfgBuffer := bytes .NewBufferString (validConfigJSON )
634595
635- cfgBuffer := bytes .NewBufferString (configJSON )
596+ c := & SampleConfig {}
597+ err := ReadJSONValidate (cfgBuffer , c )
598+ assert .Nil (s .T (), err )
599+ }
600+
601+ func (s * RemoteConfigSuite ) TestReadJSONParseEmbeddedStruct () {
602+ cfgBuffer := bytes .NewBufferString (validConfigJSON )
636603
637604 c := & SampleConfig {}
638605 err := ReadJSONValidate (cfgBuffer , c )
639606 assert .Nil (s .T (), err )
607+ assert .NotNil (s .T (), c .EmbeddedStr )
608+ assert .NotNil (s .T (), c .EmbeddedInt )
609+ assert .EqualValues (s .T (), "abc" , * c .EmbeddedStr )
610+ assert .EqualValues (s .T (), 123 , * c .EmbeddedInt )
611+ }
612+
613+ func (s * RemoteConfigSuite ) TestReadJSONValidateEmbeddedStruct () {
614+ invalidConfigJSON := strings .Replace (validConfigJSON , `"embedded_int": 123` , `"embedded_int": "123"` , 1 )
615+ cfgBuffer := bytes .NewBufferString (invalidConfigJSON )
616+
617+ c := & SampleConfig {}
618+ err := ReadJSONValidate (cfgBuffer , c )
619+ assert .EqualError (s .T (), err , "Failed to decode JSON, with error, json: cannot unmarshal string into Go value of type int64" )
640620}
641621
642622func (s * RemoteConfigSuite ) TestReadJSONValidateInvalidJSON () {
0 commit comments