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 "strconv" 20 "testing" 21 "time" 22 23 "github.com/stretchr/testify/assert" 24) 25 26var testNewsFeedJSON = ` 27[ 28 { 29 "date": "1498396961", 30 "desc": "[2017\u5e7406\u670825\u65e5]\u30d0\u30c3\u30af\u30dc\u30fc\u30f3\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306e\u4e00\u90e8\u3067\u969c\u5bb3\u304c\u767a\u751f\u3057\u307e\u3057\u305f", 31 "event_end": "1498394400", 32 "event_start": "1498393200", 33 "title": "[\u969c\u5bb3] \u30d0\u30c3\u30af\u30dc\u30fc\u30f3\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306e\u4e00\u90e8", 34 "url": "http://support.sakura.ad.jp/mainte/mainteentry.php?id=22178" 35 }, 36 { 37 "date": "1498261173", 38 "desc": " [2017\u5e746\u670824\u65e5]\u3055\u304f\u3089\u306e\u30af\u30e9\u30a6\u30c9\u306e\u4e00\u90e8\u3067\u969c\u5bb3\u304c\u767a\u751f\u3057\u307e\u3057\u305f", 39 "event_end": "1498262400", 40 "event_start": "1498260900", 41 "title": "[\u969c\u5bb3] \u3055\u304f\u3089\u306e\u30af\u30e9\u30a6\u30c9 \u4e00\u90e8\u306e\u304a\u5ba2\u69d8", 42 "url": "http://support.sakura.ad.jp/mainte/mainteentry.php?id=22162" 43 } 44] 45` 46 47func TestMarshalNewsFeedJSON(t *testing.T) { 48 var feeds = []NewsFeed{} 49 err := json.Unmarshal([]byte(testNewsFeedJSON), &feeds) 50 assert.NoError(t, err) 51 assert.NotEmpty(t, feeds) 52 assert.Equal(t, 2, len(feeds)) 53 54 var rawData = []map[string]interface{}{} 55 err = json.Unmarshal([]byte(testNewsFeedJSON), &rawData) 56 assert.NoError(t, err) 57 58 feed := feeds[0] 59 rawFeed := rawData[0] 60 61 assert.EqualValues(t, rawFeed["date"], feed.StrDate) 62 assert.EqualValues(t, rawFeed["desc"], feed.Description) 63 assert.EqualValues(t, rawFeed["event_start"], feed.StrEventStart) 64 assert.EqualValues(t, rawFeed["event_end"], feed.StrEventEnd) 65 assert.EqualValues(t, rawFeed["title"], feed.Title) 66 assert.EqualValues(t, rawFeed["url"], feed.URL) 67 68 parsedDateSec, _ := strconv.ParseInt(feed.StrDate, 10, 64) 69 assert.EqualValues(t, time.Unix(parsedDateSec, 0), feed.Date()) 70 71} 72