1// Copyright 2016-2020 The Libsacloud 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
15package sacloud
16
17import (
18	"encoding/json"
19	"fmt"
20	"testing"
21
22	"github.com/stretchr/testify/assert"
23)
24
25const testSIMJSON = `
26    {
27      "Index": 0,
28      "ID": 123456789012,
29      "Name": "example-sim",
30      "Description": "example-desc",
31      "Settings": null,
32      "SettingsHash": null,
33      "Status": {
34        "ICCID": "898104xxxxxxxxxxxxx"
35		%s
36      },
37      "ServiceClass": "cloud/sim/1",
38      "Availability": "available",
39      "CreatedAt": "2018-01-12T15:33:53+09:00",
40      "ModifiedAt": "2018-01-12T15:33:53+09:00",
41      "Provider": {
42        "ID": 8000001,
43        "Class": "sim",
44        "Name": "sakura-sim",
45        "ServiceClass": "cloud/sim"
46      },
47      "Icon": null,
48      "Tags": []
49    }
50`
51
52const testSIMStatusBody = `
53  "sim": {
54    "iccid": "898104xxxxxxxxxxxxx",
55    "imsi": [
56      "xxxxxxxxxxxxxxx"
57    ],
58    "session_status": "DOWN",
59    "imei_lock": false,
60    "registered": true,
61    "activated": true,
62    "resource_id": "xxxxxxxxxxxx",
63    "registered_date": "2018-04-04T07:46:11+00:00",
64    "activated_date": "2018-04-04T07:48:48+00:00",
65    "deactivated_date": "2018-04-04T07:46:02+00:00",
66    "traffic_bytes_of_current_month": {
67      "uplink_bytes": "169734354",
68      "downlink_bytes": "509606410"
69    },
70    "connected_imei": "xxxxxxxxxxxxxxx"
71  },
72  "is_ok": true
73`
74
75const testSIMStatusNoTrafficBody = `
76{
77  "sim": {
78    "iccid": "898104xxxxxxxxxxxxx",
79    "imsi": [
80      "xxxxxxxxxxxxxxx"
81    ],
82    "session_status": "DOWN",
83    "imei_lock": false,
84    "registered": true,
85    "activated": true,
86    "resource_id": "xxxxxxxxxxxx",
87    "registered_date": "2018-04-04T07:46:11+00:00",
88    "activated_date": "2018-04-04T07:48:48+00:00",
89    "deactivated_date": "2018-04-04T07:46:02+00:00",
90    "traffic_bytes_of_current_month": [],
91    "connected_imei": "xxxxxxxxxxxxxxx"
92  },
93  "is_ok": true
94}
95`
96
97const testSIMLogJSON = `
98{
99    "date": "2018-04-03T08:18:07+00:00",
100    "session_status": "Created",
101    "resource_id": "xxxxxxxxxxxx",
102    "imei": "xxxxxxxxxxxxxxx",
103    "imsi": "xxxxxxxxxxxxxxx"
104}
105`
106
107const testSIMNetworkOperatorConfigsJSON = `
108{
109  "network_operator_config": [
110    {
111      "name": "SoftBank",
112      "allow": true
113    },
114    {
115      "name": "NTT DOCOMO",
116      "allow": true
117    },
118    {
119      "name": "KDDI",
120      "allow": true
121    }
122  ]
123}
124`
125
126func TestMarshalSIMJSON(t *testing.T) {
127
128	t.Run("SIM no status", func(t *testing.T) {
129		var sim SIM
130		err := json.Unmarshal([]byte(fmt.Sprintf(testSIMJSON, "")), &sim)
131		assert.NoError(t, err)
132		assert.NotEmpty(t, sim)
133		assert.NotEmpty(t, sim.ID)
134		assert.NotEmpty(t, sim.Name)
135		assert.NotEmpty(t, sim.Description)
136		assert.NotEmpty(t, sim.ServiceClass)
137		assert.NotEmpty(t, sim.Availability)
138		assert.NotEmpty(t, sim.CreatedAt)
139		assert.NotEmpty(t, sim.ModifiedAt)
140		assert.NotEmpty(t, sim.Provider)
141		assert.Empty(t, sim.Status.SIMInfo)
142	})
143
144	t.Run("SIM includes status", func(t *testing.T) {
145		var sim SIM
146		err := json.Unmarshal([]byte(fmt.Sprintf(testSIMJSON, ","+testSIMStatusBody)), &sim)
147		assert.NoError(t, err)
148		assert.NotEmpty(t, sim)
149		assert.NotEmpty(t, sim.Status.SIMInfo)
150		info := sim.Status.SIMInfo
151
152		assert.NotEmpty(t, info.ICCID)
153		assert.NotEmpty(t, info.IMSI)
154		assert.NotEmpty(t, info.SessionStatus)
155		assert.False(t, info.IMEILock)
156		assert.True(t, info.Registered)
157		assert.True(t, info.Activated)
158		assert.NotEmpty(t, info.ResourceID)
159		assert.NotEmpty(t, info.RegisteredDate)
160		assert.NotEmpty(t, info.ActivatedDate)
161		assert.NotEmpty(t, info.DeactivatedDate)
162		assert.NotEmpty(t, info.TrafficBytesOfCurrentMonth)
163		assert.NotEmpty(t, info.ConnectedIMEI)
164	})
165
166	t.Run("SIM status no traffic bytes", func(t *testing.T) {
167		var simInfo SIMInfo
168		err := json.Unmarshal([]byte(testSIMStatusNoTrafficBody), &simInfo)
169		assert.NoError(t, err)
170		assert.Empty(t, simInfo.TrafficBytesOfCurrentMonth)
171	})
172
173	t.Run("SIM logs", func(t *testing.T) {
174		var logs SIMLog
175		err := json.Unmarshal([]byte(testSIMLogJSON), &logs)
176		assert.NoError(t, err)
177		assert.NotEmpty(t, logs)
178		assert.NotEmpty(t, logs.Date)
179		assert.NotEmpty(t, logs.SessionStatus)
180		assert.NotEmpty(t, logs.ResourceID)
181		assert.NotEmpty(t, logs.IMEI)
182		assert.NotEmpty(t, logs.IMSI)
183	})
184
185	t.Run("SIM NetworkOperatorConfig", func(t *testing.T) {
186		var configs SIMNetworkOperatorConfigs
187		err := json.Unmarshal([]byte(testSIMNetworkOperatorConfigsJSON), &configs)
188
189		assert.NoError(t, err)
190		assert.NotEmpty(t, configs)
191		assert.Len(t, configs.NetworkOperatorConfigs, 3)
192
193	})
194}
195
196func TestCreateNewSIM(t *testing.T) {
197
198	sim := CreateNewSIM("name", "iccID", "passcode")
199	assert.NotNil(t, sim)
200	assert.Equal(t, "name", sim.Name)
201	assert.Equal(t, "iccID", sim.Status.ICCID)
202	assert.Equal(t, "passcode", sim.Remark.PassCode)
203
204}
205