|
1 | 1 | package upcloud |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/xml" |
| 4 | + "encoding/json" |
5 | 5 | "testing" |
6 | 6 |
|
7 | 7 | "github.com/stretchr/testify/assert" |
8 | 8 | ) |
9 | 9 |
|
10 | 10 | // TestUnmarshalPlans tests that Plans and Plan objects unmarshal correctly |
11 | 11 | func TestUnmarshalPlans(t *testing.T) { |
12 | | - originalXML := `<?xml version="1.0" encoding="utf-8"?> |
13 | | -<plans> |
14 | | - <plan> |
15 | | - <core_number>1</core_number> |
16 | | - <memory_amount>1024</memory_amount> |
17 | | - <name>1xCPU-1GB</name> |
18 | | - <public_traffic_out>2048</public_traffic_out> |
19 | | - <storage_size>30</storage_size> |
20 | | - <storage_tier>maxiops</storage_tier> |
21 | | - </plan> |
22 | | - <plan> |
23 | | - <core_number>2</core_number> |
24 | | - <memory_amount>2048</memory_amount> |
25 | | - <name>2xCPU-2GB</name> |
26 | | - <public_traffic_out>3072</public_traffic_out> |
27 | | - <storage_size>50</storage_size> |
28 | | - <storage_tier>maxiops</storage_tier> |
29 | | - </plan> |
30 | | - <plan> |
31 | | - <core_number>4</core_number> |
32 | | - <memory_amount>4096</memory_amount> |
33 | | - <name>4xCPU-4GB</name> |
34 | | - <public_traffic_out>4096</public_traffic_out> |
35 | | - <storage_size>100</storage_size> |
36 | | - <storage_tier>maxiops</storage_tier> |
37 | | - </plan> |
38 | | - <plan> |
39 | | - <core_number>6</core_number> |
40 | | - <memory_amount>8192</memory_amount> |
41 | | - <name>6xCPU-8GB</name> |
42 | | - <public_traffic_out>8192</public_traffic_out> |
43 | | - <storage_size>200</storage_size> |
44 | | - <storage_tier>maxiops</storage_tier> |
45 | | - </plan> |
46 | | -</plans>` |
| 12 | + originalJSON := ` |
| 13 | + { |
| 14 | + "plans" : { |
| 15 | + "plan" : [ |
| 16 | + { |
| 17 | + "core_number" : 1, |
| 18 | + "memory_amount" : 2048, |
| 19 | + "name" : "1xCPU-2GB", |
| 20 | + "public_traffic_out" : 2048, |
| 21 | + "storage_size" : 50, |
| 22 | + "storage_tier" : "maxiops" |
| 23 | + }, |
| 24 | + { |
| 25 | + "core_number" : 2, |
| 26 | + "memory_amount" : 4096, |
| 27 | + "name" : "2xCPU-4GB", |
| 28 | + "public_traffic_out" : 4096, |
| 29 | + "storage_size" : 80, |
| 30 | + "storage_tier" : "maxiops" |
| 31 | + } |
| 32 | + ] |
| 33 | + } |
| 34 | + } |
| 35 | + ` |
47 | 36 |
|
48 | 37 | plans := Plans{} |
49 | | - err := xml.Unmarshal([]byte(originalXML), &plans) |
| 38 | + err := json.Unmarshal([]byte(originalJSON), &plans) |
50 | 39 | assert.Nil(t, err) |
51 | | - assert.Len(t, plans.Plans, 4) |
| 40 | + assert.Len(t, plans.Plans, 2) |
52 | 41 |
|
53 | | - plan := plans.Plans[0] |
54 | | - assert.Equal(t, 1, plan.CoreNumber) |
55 | | - assert.Equal(t, 1024, plan.MemoryAmount) |
56 | | - assert.Equal(t, "1xCPU-1GB", plan.Name) |
57 | | - assert.Equal(t, 2048, plan.PublicTrafficOut) |
58 | | - assert.Equal(t, 30, plan.StorageSize) |
59 | | - assert.Equal(t, StorageTierMaxIOPS, plan.StorageTier) |
| 42 | + testData := []Plan{ |
| 43 | + { |
| 44 | + CoreNumber: 1, |
| 45 | + MemoryAmount: 2048, |
| 46 | + Name: "1xCPU-2GB", |
| 47 | + PublicTrafficOut: 2048, |
| 48 | + StorageSize: 50, |
| 49 | + StorageTier: "maxiops", |
| 50 | + }, |
| 51 | + { |
| 52 | + CoreNumber: 2, |
| 53 | + MemoryAmount: 4096, |
| 54 | + Name: "2xCPU-4GB", |
| 55 | + PublicTrafficOut: 4096, |
| 56 | + StorageSize: 80, |
| 57 | + StorageTier: "maxiops", |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for i, p := range testData { |
| 62 | + plan := plans.Plans[i] |
| 63 | + assert.Equal(t, p.CoreNumber, plan.CoreNumber) |
| 64 | + assert.Equal(t, p.MemoryAmount, plan.MemoryAmount) |
| 65 | + assert.Equal(t, p.Name, plan.Name) |
| 66 | + assert.Equal(t, p.PublicTrafficOut, plan.PublicTrafficOut) |
| 67 | + assert.Equal(t, p.StorageSize, plan.StorageSize) |
| 68 | + assert.Equal(t, p.StorageTier, plan.StorageTier) |
| 69 | + } |
60 | 70 | } |
0 commit comments