github.com/rohankumardubey/aresdb@v0.0.2-0.20190517170215-e54e3ca06b9c/subscriber/common/message/json_decoder_test.go (about)

     1  //  Copyright (c) 2017-2018 Uber Technologies, Inc.
     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 message
    16  
    17  import (
    18  	"time"
    19  
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  var _ = Describe("json decoder tests", func() {
    25  	It("json decoder must pass", func() {
    26  		jd := &JSONDecoder{}
    27  		msg := &StringMessage{
    28  			msg: `{"project": "ares-subscriber"}`,
    29  		}
    30  		m, err := jd.DecodeMsg(msg)
    31  		Ω(err).Should(BeNil())
    32  		Ω(m).ShouldNot(BeNil())
    33  		Ω(m.DecodedMessage[MsgPrefix].(map[string]interface{})["project"]).Should(Equal("ares-subscriber"))
    34  
    35  		msg = &StringMessage{
    36  			msg: `{"ts":1.468449680235607e+09}`,
    37  		}
    38  		m, err = jd.DecodeMsg(msg)
    39  		Ω(err).Should(BeNil())
    40  		Ω(m).ShouldNot(BeNil())
    41  		Ω(m.MsgMetaDataTS).Should(Equal(time.Unix(0, int64(1468449680000)*int64(time.Millisecond))))
    42  
    43  		msg = &StringMessage{
    44  			msg: `{"_updated":"2016-07-12T22:5478009+00:00"}`,
    45  		}
    46  		m, err = jd.DecodeMsg(msg)
    47  		Ω(err).Should(BeNil())
    48  		Ω(m).ShouldNot(BeNil())
    49  	})
    50  
    51  	It("json decoder will fail", func() {
    52  		jd := &JSONDecoder{}
    53  		msg := &StringMessage{
    54  			msg: "project",
    55  		}
    56  		m, err := jd.DecodeMsg(msg)
    57  		Ω(err).ShouldNot(BeNil())
    58  		Ω(m).Should(BeNil())
    59  	})
    60  })