github.com/extrame/fabric-ca@v2.0.0-alpha+incompatible/lib/streamer/jsonstreamer_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package streamer_test
    17  
    18  import (
    19  	"encoding/json"
    20  	"strings"
    21  	"testing"
    22  
    23  	. "github.com/hyperledger/fabric-ca/lib/streamer"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  type element struct {
    28  	Name string
    29  	Type string
    30  }
    31  
    32  func TestJSONStreamer(t *testing.T) {
    33  	identityCount := 0
    34  	cb := func(decoder *json.Decoder) error {
    35  		ele := &element{}
    36  		err := decoder.Decode(ele)
    37  		if err != nil {
    38  			return err
    39  		}
    40  		identityCount++
    41  		return nil
    42  	}
    43  
    44  	const jsonStream = `{"a": "aval", "b": {"foo": [{"foo1":"bar1"}]}, "result": {"identities": [{"name": "id1", "type": "type1"}, {"name": "id2"}]}, "errors": [], "c": "cval", "d": "dval", "e": 1.234}`
    45  	dec := json.NewDecoder(strings.NewReader(jsonStream))
    46  	_, err := StreamJSONArray(dec, "result.identities", cb)
    47  	if assert.NoError(t, err, "Failed to correctly stream JSON") {
    48  		assert.True(t, identityCount == 2, "Identity function not called correct number of times")
    49  	}
    50  
    51  	const jsonStreamErr = `{"a": "aval", "b": {"foo": [{"foo1":"bar1"}]}, "result": "", "errors": [{"code":20,"message":"Authorization failure"}], "c": "cval", "d": "dval", "e": 1.234}`
    52  	dec = json.NewDecoder(strings.NewReader(jsonStreamErr))
    53  	_, err = StreamJSONArray(dec, "result.identities", cb)
    54  	if assert.Error(t, err, "Should have returned the error in the JSON stream") {
    55  		assert.Contains(t, err.Error(), "Authorization failure")
    56  	}
    57  
    58  	const jsonStreamBaderr = `{"a": "aval", "b": {"foo": [{"foo1":"bar1"}]}, "result": {"identities": [{"name": "id1", "type": "type1"}, {"name": "id2"}]}, "errors": {"code":20,"message":"Authorization failure"}, "c": "cval", "d": "dval", "e": 1.234}`
    59  	dec = json.NewDecoder(strings.NewReader(jsonStreamBaderr))
    60  	_, err = StreamJSONArray(dec, "result.identities", cb)
    61  	assert.Error(t, err, "Should have failed, errors is not array type")
    62  
    63  	const jsonStream3 = `["identities": [{"name": "id1", "type": "type1"}, {"name": "id2"}]`
    64  	dec = json.NewDecoder(strings.NewReader(jsonStream3))
    65  	_, err = StreamJSONArray(dec, "identities", cb)
    66  	assert.Error(t, err, "Should have failed, incorrect opening bracket")
    67  
    68  	const jsonStream4 = `{"identities": [[]}`
    69  	dec = json.NewDecoder(strings.NewReader(jsonStream4))
    70  	_, err = StreamJSONArray(dec, "identities", cb)
    71  	assert.Error(t, err, "Should have failed, incorrect number of square brackets")
    72  
    73  	const jsonStream5 = `{"a": "aval", "identities": [{]"name": "id1", "type": "type1"}, {"name": "id2"}], "c": "cval}`
    74  	dec = json.NewDecoder(strings.NewReader(jsonStream5))
    75  	_, err = StreamJSONArray(dec, "identities", cb)
    76  	assert.Error(t, err, "Should have failed, incorrect opening square bracket")
    77  
    78  	const jsonStream6 = `{"a": "aval", "identities": []{"name": "id1", "type": "type1"}, {"name": "id2"}], "c": "cval"}`
    79  	dec = json.NewDecoder(strings.NewReader(jsonStream6))
    80  	_, err = StreamJSONArray(dec, "identities", cb)
    81  	assert.Error(t, err, "Should have failed, incorrect format of 'identities'")
    82  
    83  	const jsonStream7 = `{"a"/ "aval", "identities": {"name": "id1", "type": "type1"}, {"name": "id2"}], "c": "cval"}`
    84  	dec = json.NewDecoder(strings.NewReader(jsonStream7))
    85  	_, err = StreamJSONArray(dec, "identities", cb)
    86  	assert.Error(t, err, "Should have failed, incorrect JSON syntax")
    87  
    88  	const jsonStream8 = `{"a": "aval", "identities":[{"name": "id1"}], "errors":[/]}`
    89  	dec = json.NewDecoder(strings.NewReader(jsonStream8))
    90  	_, err = StreamJSONArray(dec, "identities", cb)
    91  	assert.Error(t, err, "Should have failed, invalid JSON format")
    92  }