github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/resources/quota_resource_test.go (about)

     1  package resources
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/types"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/ginkgo/extensions/table"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("quota limits", func() {
    13  	Describe("AppLimit", func() {
    14  		DescribeTable("MarshalJSON",
    15  			func(appLimit AppLimit, expectedBytes []byte) {
    16  				bytes, err := json.Marshal(appLimit)
    17  				Expect(err).ToNot(HaveOccurred())
    18  				Expect(bytes).To(Equal(expectedBytes))
    19  			},
    20  			Entry("total memory", AppLimit{TotalMemory: &types.NullInt{IsSet: true, Value: 1}}, []byte(`{"total_memory_in_mb":1}`)),
    21  			Entry("total memory", AppLimit{TotalMemory: nil}, []byte(`{}`)),
    22  			Entry("total memory", AppLimit{TotalMemory: &types.NullInt{IsSet: false}}, []byte(`{"total_memory_in_mb":null}`)),
    23  			Entry("instance memory", AppLimit{InstanceMemory: &types.NullInt{IsSet: true, Value: 1}}, []byte(`{"per_process_memory_in_mb":1}`)),
    24  			Entry("instance memory", AppLimit{InstanceMemory: nil}, []byte(`{}`)),
    25  			Entry("instance memory", AppLimit{InstanceMemory: &types.NullInt{IsSet: false}}, []byte(`{"per_process_memory_in_mb":null}`)),
    26  			Entry("total app instances", AppLimit{TotalAppInstances: &types.NullInt{IsSet: true, Value: 1}}, []byte(`{"total_instances":1}`)),
    27  			Entry("total app instances", AppLimit{TotalAppInstances: nil}, []byte(`{}`)),
    28  			Entry("total app instances", AppLimit{TotalAppInstances: &types.NullInt{IsSet: false}}, []byte(`{"total_instances":null}`)),
    29  		)
    30  
    31  		DescribeTable("UnmarshalJSON",
    32  			func(givenBytes []byte, expectedStruct AppLimit) {
    33  				var actualStruct AppLimit
    34  				err := json.Unmarshal(givenBytes, &actualStruct)
    35  				Expect(err).ToNot(HaveOccurred())
    36  				Expect(actualStruct).To(Equal(expectedStruct))
    37  			},
    38  			Entry(
    39  				"no null values",
    40  				[]byte(`{"total_memory_in_mb":1,"per_process_memory_in_mb":2,"total_instances":3}`),
    41  				AppLimit{
    42  					TotalMemory:       &types.NullInt{IsSet: true, Value: 1},
    43  					InstanceMemory:    &types.NullInt{IsSet: true, Value: 2},
    44  					TotalAppInstances: &types.NullInt{IsSet: true, Value: 3},
    45  				}),
    46  			Entry(
    47  				"total memory is null",
    48  				[]byte(`{"total_memory_in_mb":null,"per_process_memory_in_mb":2,"total_instances":3}`),
    49  				AppLimit{
    50  					TotalMemory:       &types.NullInt{IsSet: false, Value: 0},
    51  					InstanceMemory:    &types.NullInt{IsSet: true, Value: 2},
    52  					TotalAppInstances: &types.NullInt{IsSet: true, Value: 3},
    53  				}),
    54  			Entry(
    55  				"per process memory is null",
    56  				[]byte(`{"total_memory_in_mb":1,"per_process_memory_in_mb":null,"total_instances":3}`),
    57  				AppLimit{
    58  					TotalMemory:       &types.NullInt{IsSet: true, Value: 1},
    59  					InstanceMemory:    &types.NullInt{IsSet: false, Value: 0},
    60  					TotalAppInstances: &types.NullInt{IsSet: true, Value: 3},
    61  				}),
    62  			Entry(
    63  				"total instances is null",
    64  				[]byte(`{"total_memory_in_mb":1,"per_process_memory_in_mb":2,"total_instances":null}`),
    65  				AppLimit{
    66  					TotalMemory:       &types.NullInt{IsSet: true, Value: 1},
    67  					InstanceMemory:    &types.NullInt{IsSet: true, Value: 2},
    68  					TotalAppInstances: &types.NullInt{IsSet: false, Value: 0},
    69  				}),
    70  		)
    71  	})
    72  
    73  	Describe("ServiceLimit", func() {
    74  		var (
    75  			trueValue  = true
    76  			falseValue = false
    77  		)
    78  
    79  		DescribeTable("MarshalJSON",
    80  			func(serviceLimit ServiceLimit, expectedBytes []byte) {
    81  				bytes, err := json.Marshal(serviceLimit)
    82  				Expect(err).ToNot(HaveOccurred())
    83  				Expect(bytes).To(Equal(expectedBytes))
    84  			},
    85  			Entry("total service instances", ServiceLimit{TotalServiceInstances: &types.NullInt{IsSet: true, Value: 1}}, []byte(`{"total_service_instances":1}`)),
    86  			Entry("total service instances", ServiceLimit{TotalServiceInstances: nil}, []byte(`{}`)),
    87  			Entry("total service instances", ServiceLimit{TotalServiceInstances: &types.NullInt{IsSet: false}}, []byte(`{"total_service_instances":null}`)),
    88  			Entry("paid service plans", ServiceLimit{PaidServicePlans: &trueValue}, []byte(`{"paid_services_allowed":true}`)),
    89  			Entry("paid service plans", ServiceLimit{PaidServicePlans: nil}, []byte(`{}`)),
    90  			Entry("paid service plans", ServiceLimit{PaidServicePlans: &falseValue}, []byte(`{"paid_services_allowed":false}`)),
    91  		)
    92  
    93  		DescribeTable("UnmarshalJSON",
    94  			func(givenBytes []byte, expectedStruct ServiceLimit) {
    95  				var actualStruct ServiceLimit
    96  				err := json.Unmarshal(givenBytes, &actualStruct)
    97  				Expect(err).ToNot(HaveOccurred())
    98  				Expect(actualStruct).To(Equal(expectedStruct))
    99  			},
   100  			Entry(
   101  				"no null values",
   102  				[]byte(`{"total_service_instances":1,"paid_services_allowed":true}`),
   103  				ServiceLimit{
   104  					TotalServiceInstances: &types.NullInt{IsSet: true, Value: 1},
   105  					PaidServicePlans:      &trueValue,
   106  				}),
   107  			Entry(
   108  				"total service instances is null and paid services allowed is false",
   109  				[]byte(`{"total_service_instances":null,"paid_services_allowed":false}`),
   110  				ServiceLimit{
   111  					TotalServiceInstances: &types.NullInt{IsSet: false, Value: 0},
   112  					PaidServicePlans:      &falseValue,
   113  				}),
   114  		)
   115  	})
   116  
   117  	Describe("RouteLimit", func() {
   118  		DescribeTable("MarshalJSON",
   119  			func(routeLimit RouteLimit, expectedBytes []byte) {
   120  				bytes, err := json.Marshal(routeLimit)
   121  				Expect(err).ToNot(HaveOccurred())
   122  				Expect(bytes).To(Equal(expectedBytes))
   123  			},
   124  			Entry("total routes", RouteLimit{TotalRoutes: &types.NullInt{IsSet: true, Value: 1}}, []byte(`{"total_routes":1}`)),
   125  			Entry("total routes", RouteLimit{TotalRoutes: nil}, []byte(`{}`)),
   126  			Entry("total routes", RouteLimit{TotalRoutes: &types.NullInt{IsSet: false}}, []byte(`{"total_routes":null}`)),
   127  			Entry("total reserved ports", RouteLimit{TotalReservedPorts: &types.NullInt{IsSet: true, Value: 1}}, []byte(`{"total_reserved_ports":1}`)),
   128  			Entry("total reserved ports", RouteLimit{TotalReservedPorts: nil}, []byte(`{}`)),
   129  			Entry("total reserved ports", RouteLimit{TotalReservedPorts: &types.NullInt{IsSet: false}}, []byte(`{"total_reserved_ports":null}`)),
   130  		)
   131  
   132  		DescribeTable("UnmarshalJSON",
   133  			func(givenBytes []byte, expectedStruct RouteLimit) {
   134  				var actualStruct RouteLimit
   135  				err := json.Unmarshal(givenBytes, &actualStruct)
   136  				Expect(err).ToNot(HaveOccurred())
   137  				Expect(actualStruct).To(Equal(expectedStruct))
   138  			},
   139  			Entry(
   140  				"no null values",
   141  				[]byte(`{"total_routes":1,"total_reserved_ports":2}`),
   142  				RouteLimit{
   143  					TotalRoutes:        &types.NullInt{IsSet: true, Value: 1},
   144  					TotalReservedPorts: &types.NullInt{IsSet: true, Value: 2},
   145  				}),
   146  			Entry(
   147  				"total routes is null",
   148  				[]byte(`{"total_routes":null,"total_reserved_ports":3}`),
   149  				RouteLimit{
   150  					TotalRoutes:        &types.NullInt{IsSet: false, Value: 0},
   151  					TotalReservedPorts: &types.NullInt{IsSet: true, Value: 3},
   152  				}),
   153  			Entry(
   154  				"total reserved ports is null",
   155  				[]byte(`{"total_routes":4,"total_reserved_ports":null}`),
   156  				RouteLimit{
   157  					TotalRoutes:        &types.NullInt{IsSet: true, Value: 4},
   158  					TotalReservedPorts: &types.NullInt{IsSet: false, Value: 0},
   159  				}),
   160  		)
   161  
   162  	})
   163  })