github.com/sacloud/iaas-api-go@v1.12.0/naked/database_test.go (about)

     1  // Copyright 2022-2023 The sacloud/iaas-api-go Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package naked
    16  
    17  import (
    18  	"encoding/json"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestDatabaseSettingSourceNetworks_UnmarshalJSON(t *testing.T) {
    25  	cases := []struct {
    26  		name     string
    27  		in       string
    28  		expect   DatabaseSettingSourceNetworks
    29  		hasError bool
    30  	}{
    31  		{
    32  			name:   "empty string",
    33  			in:     `""`,
    34  			expect: nil,
    35  		},
    36  		{
    37  			name:   "allow all",
    38  			in:     `["0.0.0.0/0"]`,
    39  			expect: nil,
    40  		},
    41  		{
    42  			name:   "allow a CIDR block",
    43  			in:     `["192.168.0.0/24"]`,
    44  			expect: DatabaseSettingSourceNetworks{"192.168.0.0/24"},
    45  		},
    46  		{
    47  			name:   "allow CIDR blocks",
    48  			in:     `["192.168.0.0/24","192.168.1.0/24"]`,
    49  			expect: DatabaseSettingSourceNetworks{"192.168.0.0/24", "192.168.1.0/24"},
    50  		},
    51  		{
    52  			name:     "invalid values",
    53  			in:       `"dummy"`,
    54  			expect:   nil,
    55  			hasError: true,
    56  		},
    57  	}
    58  
    59  	for _, tc := range cases {
    60  		var sn DatabaseSettingSourceNetworks
    61  		err := json.Unmarshal([]byte(tc.in), &sn)
    62  		require.Equal(t, tc.expect, sn, tc.name)
    63  		require.Equal(t, tc.hasError, err != nil, tc.name)
    64  	}
    65  }
    66  
    67  func TestDatabaseSettingSourceNetworks_MarshalJSON(t *testing.T) {
    68  	cases := []struct {
    69  		name     string
    70  		in       DatabaseSettingSourceNetworks
    71  		expect   string
    72  		hasError bool
    73  	}{
    74  		{
    75  			name:     "nil",
    76  			in:       nil,
    77  			expect:   `["0.0.0.0/0"]`,
    78  			hasError: false,
    79  		},
    80  		{
    81  			name:     "empty list",
    82  			in:       DatabaseSettingSourceNetworks{},
    83  			expect:   `["0.0.0.0/0"]`,
    84  			hasError: false,
    85  		},
    86  		{
    87  			name:     "allow a CIDR block",
    88  			in:       DatabaseSettingSourceNetworks{"192.168.0.0/24"},
    89  			expect:   `["192.168.0.0/24"]`,
    90  			hasError: false,
    91  		},
    92  		{
    93  			name:     "allow CIDR blocks",
    94  			in:       DatabaseSettingSourceNetworks{"192.168.0.0/24", "192.168.1.0/24"},
    95  			expect:   `["192.168.0.0/24","192.168.1.0/24"]`,
    96  			hasError: false,
    97  		},
    98  	}
    99  
   100  	for _, tc := range cases {
   101  		data, err := json.Marshal(tc.in)
   102  		require.Equal(t, tc.expect, string(data), tc.name)
   103  		require.Equal(t, tc.hasError, err != nil, tc.name)
   104  	}
   105  }