f8f9d8797762f14a918a3c6d91d46e1e1a3c88d4
[ealt-edge.git] / mep / mepagent / test / util_test.go
1 /*
2  *  Copyright 2020 Huawei Technologies Co., Ltd.
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 test
18
19 import (
20         "encoding/json"
21         "pkg/service"
22         "net/http"
23         "net/http/httptest"
24         "testing"
25 )
26
27 func TestGetConfSuccess(t *testing.T) {
28         _, err := service.GetConf("../../conf/app_instance_info.yaml")
29         if err != nil {
30                 t.Error("Read conf file failed")
31         }
32 }
33
34 func TestGetConfFail(t *testing.T) {
35         _, err := service.GetConf("../conf/app_instance_info.yaml")
36         if err == nil {
37                 t.Error("Read conf file failed")
38         }
39 }
40
41 func TestRegisterToMepSuccess(t *testing.T) {
42         httpResponse := "response body"
43         var httpResponseBytes, err1 = json.Marshal(httpResponse)
44         if err1 != nil {
45                 t.Error("Marshal http Response Error")
46         }
47
48         ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
49                 w.WriteHeader(http.StatusCreated)
50                 _, err2 := w.Write(httpResponseBytes)
51
52                 if err2 != nil {
53                         t.Error("Write Response Error")
54                 }
55                 if r.Method != "POST" {
56                         t.Error("UnExcepted Method")
57                 }
58         }))
59
60         defer ts.Close()
61         api := ts.URL
62
63         _, err := service.RegisterToMep("param", api)
64         if err != nil {
65                 t.Error("error")
66         }
67 }
68
69 func TestRegisterToMepFail1(t *testing.T) {
70         httpResponse := "response body"
71         var httpResponseBytes, err1 = json.Marshal(httpResponse)
72         if err1 != nil {
73                 t.Error("Marshal http Response Error")
74         }
75
76         ts2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
77                 w.WriteHeader(http.StatusBadRequest)
78                 _, err2 := w.Write(httpResponseBytes)
79
80                 if err2 != nil {
81                         t.Error("Write Response Error")
82                 }
83                 if r.Method != "POST" {
84                         t.Error("UnExcepted Method")
85                 }
86         }))
87
88         defer ts2.Close()
89         api := ts2.URL
90
91         _, err := service.RegisterToMep("param", api)
92         if err == nil {
93                 t.Error("error")
94         }
95 }
96