Merge "Adding Listsecret capability"
[aaf/sms.git] / sms-service / src / sms / handler / handler_test.go
1 /*
2  * Copyright 2018 Intel Corporation, Inc
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
17 package handler
18
19 import (
20         "encoding/json"
21         "net/http"
22         "net/http/httptest"
23         "reflect"
24         smsbackend "sms/backend"
25         "strings"
26         "testing"
27 )
28
29 var h handler
30
31 // Here we are using the anonymous variable feature of golang to
32 // override methods form an interface
33 type TestBackend struct {
34         smsbackend.SecretBackend
35 }
36
37 func (b *TestBackend) Init() error {
38         return nil
39 }
40
41 func (b *TestBackend) GetStatus() (bool, error) {
42         return true, nil
43 }
44
45 func (b *TestBackend) GetSecretDomain(name string) (smsbackend.SecretDomain, error) {
46         return smsbackend.SecretDomain{}, nil
47 }
48
49 func (b *TestBackend) GetSecret(dom string, sec string) (smsbackend.Secret, error) {
50         return smsbackend.Secret{}, nil
51 }
52
53 func (b *TestBackend) ListSecret(dom string) ([]string, error) {
54         return nil, nil
55 }
56
57 func (b *TestBackend) CreateSecretDomain(name string) (smsbackend.SecretDomain, error) {
58         return smsbackend.SecretDomain{}, nil
59 }
60
61 func (b *TestBackend) CreateSecret(dom string, sec smsbackend.Secret) error {
62         return nil
63 }
64
65 func (b *TestBackend) DeleteSecretDomain(name string) error {
66         return nil
67 }
68
69 func (b *TestBackend) DeleteSecret(dom string, name string) error {
70         return nil
71 }
72
73 func init() {
74         testBackend := &TestBackend{}
75         h = handler{secretBackend: testBackend}
76 }
77
78 func TestCreateRouter(t *testing.T) {
79         router := CreateRouter(h.secretBackend)
80         if router == nil {
81                 t.Fatal("CreateRouter: Got error when none expected")
82         }
83 }
84
85 func TestStatusHandler(t *testing.T) {
86         req, err := http.NewRequest("GET", "/v1/sms/status", nil)
87         if err != nil {
88                 t.Fatal(err)
89         }
90
91         rr := httptest.NewRecorder()
92         hr := http.HandlerFunc(h.statusHandler)
93
94         hr.ServeHTTP(rr, req)
95
96         ret := rr.Code
97         if ret != http.StatusOK {
98                 t.Errorf("statusHandler returned wrong status code: %v vs %v",
99                         ret, http.StatusOK)
100         }
101
102         expected := backendStatus{}
103         got := backendStatus{}
104         expectedStr := strings.NewReader(`{"sealstatus":true}`)
105         json.NewDecoder(expectedStr).Decode(&expected)
106         json.NewDecoder(rr.Body).Decode(&got)
107
108         if reflect.DeepEqual(expected, got) == false {
109                 t.Errorf("statusHandler returned unexpected body: got %v vs %v",
110                         rr.Body.String(), expectedStr)
111         }
112 }