1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00
Mainflux.mainflux/readers/api/endpoint_test.go
Dušan Borovčanin dfa766e013 MF-407 - Values of zero are being omitted (#434)
* Fix empty protobuf values

Update Normalizer service and .proto files. Reader and Writer services
needs to be updated due to message format change.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update HTTP adapter to use gogo protobuf

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update Reader services API

Update API or Reader services to match Message changes due to switching to gogo/proto.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update InfluxDB services

Update InfluxDB Reader and Writer services to match new Message format.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update MongoDB services

Update MongoDB Reader and Writer services to match new message format.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update Cassandra services

Update Cassandra Reader and Writer service to match new Message format.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Fix InfluxDB Reader test

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update Makefile and docs accordingly

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Fix possible data race in InfluxDB writer

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update InfluxDB Writer tests

Raise test coverage.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Fix InfluxDB reader

Fix wrong ValueSum readings. Upadete tests and raise coverage.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update MongoDB services tests

Raise test coverage for MongoDB Reader and Writer services.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update Readers API tests

Raise test coverage.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Raise test coverage

Update Cassandra Reader and Writer services tests.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Use gogo/protobuf in CoAP adapter

Add gogo/protobuf to Gopkg.toml and update dependencies.
Update Dockerfile to run `make proto`.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update Cassandra Reader tests

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Refactor code

Improve code style and comments to improve readability.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Rename Sum to SumValue

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Rename Values to Value

Since message contains only single value (or possibly no value at all),
`Values` name could be misleading. Rename simple double value from
`Value` to `FloatValue` accordingly.

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Fix InfluxDB Reader logging

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Replace exclusive if statements with switch-case

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>

* Update Cassandra services tests

Signed-off-by: Dusan Borovcanin <dusan.borovcanin@mainflux.com>
2018-11-05 19:18:51 +01:00

181 lines
4.6 KiB
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package api_test
import (
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/readers"
"github.com/mainflux/mainflux/readers/api"
"github.com/mainflux/mainflux/readers/mocks"
"github.com/stretchr/testify/assert"
)
const (
svcName = "test-service"
token = "1"
invalid = "invalid"
numOfMessages = 42
chanID uint64 = 1
valueFields = 6
)
func newService() readers.MessageRepository {
messages := []mainflux.Message{}
for i := 0; i < numOfMessages; i++ {
msg := mainflux.Message{
Channel: chanID,
Publisher: 1,
Protocol: "mqtt",
}
// Mix possible values as well as value sum.
count := i % valueFields
switch count {
case 0:
msg.Value = &mainflux.Message_FloatValue{5}
case 1:
msg.Value = &mainflux.Message_BoolValue{false}
case 2:
msg.Value = &mainflux.Message_StringValue{"value"}
case 3:
msg.Value = &mainflux.Message_DataValue{"base64data"}
case 4:
msg.ValueSum = nil
case 5:
msg.ValueSum = &mainflux.SumValue{Value: 45}
}
messages = append(messages, msg)
}
return mocks.NewMessageRepository(map[uint64][]mainflux.Message{
chanID: messages,
})
}
func newServer(repo readers.MessageRepository, tc mainflux.ThingsServiceClient) *httptest.Server {
mux := api.MakeHandler(repo, tc, svcName)
return httptest.NewServer(mux)
}
type testRequest struct {
client *http.Client
method string
url string
token string
}
func (tr testRequest) make() (*http.Response, error) {
req, err := http.NewRequest(tr.method, tr.url, nil)
if err != nil {
return nil, err
}
if tr.token != "" {
req.Header.Set("Authorization", tr.token)
}
return tr.client.Do(req)
}
func TestReadAll(t *testing.T) {
svc := newService()
tc := mocks.NewThingsService()
ts := newServer(svc, tc)
defer ts.Close()
id := strconv.FormatUint(chanID, 10)
cases := map[string]struct {
url string
token string
status int
}{
"read page with valid offset and limit": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=10", ts.URL, id),
token: token,
status: http.StatusOK,
},
"read page with negative offset": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=-1&limit=10", ts.URL, id),
token: token,
status: http.StatusBadRequest,
},
"read page with negative limit": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=-10", ts.URL, id),
token: token,
status: http.StatusBadRequest,
},
"read page with zero limit": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=0", ts.URL, id),
token: token,
status: http.StatusBadRequest,
},
"read page with non-integer offset": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=abc&limit=10", ts.URL, id),
token: token,
status: http.StatusBadRequest,
},
"read page with non-integer limit": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=abc", ts.URL, id),
token: token,
status: http.StatusBadRequest,
},
"read page with invalid channel id": {
url: fmt.Sprintf("%s/channels/abc/messages?offset=0&limit=10", ts.URL),
token: token,
status: http.StatusBadRequest,
},
"read page with invalid token": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=10", ts.URL, id),
token: invalid,
status: http.StatusForbidden,
},
"read page with multiple offset": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&offset=1&limit=10", ts.URL, id),
token: token,
status: http.StatusBadRequest,
},
"read page with multiple limit": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=20&limit=10", ts.URL, id),
token: token,
status: http.StatusBadRequest,
},
"read page with empty token": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0&limit=10", ts.URL, id),
token: "",
status: http.StatusForbidden,
},
"read page with default offset": {
url: fmt.Sprintf("%s/channels/%s/messages?limit=10", ts.URL, id),
token: token,
status: http.StatusOK,
},
"read page with default limit": {
url: fmt.Sprintf("%s/channels/%s/messages?offset=0", ts.URL, id),
token: token,
status: http.StatusOK,
},
}
for desc, tc := range cases {
req := testRequest{
client: ts.Client(),
method: http.MethodGet,
url: tc.url,
token: tc.token,
}
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected %d got %d", desc, tc.status, res.StatusCode))
}
}