github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/notification/client_test.go (about) 1 package notification 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "net/http/httptest" 9 "testing" 10 11 "github.com/gorilla/mux" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestClient_CreateEvent(t *testing.T) { 16 // given 17 server := fixHTTPServer(t) 18 defer server.Close() 19 20 client := NewClient(server.Client(), ClientConfig{URL: server.URL}) 21 22 // when 23 tenants := []NotificationTenant{ 24 { 25 InstanceID: "WEAJKG-INSTANCE-1", 26 StartDate: "2022-01-01T20:00:02Z", 27 }, 28 } 29 eventRequest := CreateEventRequest{ 30 OrchestrationID: "ASKHGK-SAKJHTJ-ALKJSHT-HUZIUOP", 31 EventType: KymaMaintenanceNumber, 32 Tenants: tenants, 33 } 34 err := client.CreateEvent(eventRequest) 35 36 // then 37 assert.NoError(t, err) 38 39 response, err := server.Client().Get(fmt.Sprintf("%s/get", server.URL)) 40 assert.NoError(t, err) 41 42 var conf CreateEventRequest 43 err = json.NewDecoder(response.Body).Decode(&conf) 44 assert.NoError(t, err) 45 46 assert.Equal(t, "ASKHGK-SAKJHTJ-ALKJSHT-HUZIUOP", conf.OrchestrationID) 47 assert.Equal(t, "1", conf.EventType) 48 assert.Equal(t, "WEAJKG-INSTANCE-1", conf.Tenants[0].InstanceID) 49 assert.Equal(t, "2022-01-01T20:00:02Z", conf.Tenants[0].StartDate) 50 } 51 52 func TestClient_UpdateEvent(t *testing.T) { 53 // given 54 server := fixHTTPServer(t) 55 defer server.Close() 56 57 client := NewClient(server.Client(), ClientConfig{URL: server.URL}) 58 59 // when 60 tenants := []NotificationTenant{ 61 { 62 InstanceID: "WEAJKG-INSTANCE-1", 63 StartDate: "2022-01-01T20:00:02Z", 64 State: UnderMaintenanceEventState, 65 }, 66 } 67 eventRequest := UpdateEventRequest{ 68 OrchestrationID: "ASKHGK-SAKJHTJ-ALKJSHT-HUZIUOP", 69 Tenants: tenants, 70 } 71 err := client.UpdateEvent(eventRequest) 72 73 // then 74 assert.NoError(t, err) 75 76 response, err := server.Client().Get(fmt.Sprintf("%s/get", server.URL)) 77 assert.NoError(t, err) 78 79 var conf UpdateEventRequest 80 err = json.NewDecoder(response.Body).Decode(&conf) 81 assert.NoError(t, err) 82 83 assert.Equal(t, "ASKHGK-SAKJHTJ-ALKJSHT-HUZIUOP", conf.OrchestrationID) 84 assert.Equal(t, "1", conf.Tenants[0].State) 85 assert.Equal(t, "2022-01-01T20:00:02Z", conf.Tenants[0].StartDate) 86 } 87 88 func TestClient_CancelEvent(t *testing.T) { 89 // given 90 server := fixHTTPServer(t) 91 defer server.Close() 92 93 client := NewClient(server.Client(), ClientConfig{URL: server.URL}) 94 95 // when 96 eventRequest := CancelEventRequest{ 97 OrchestrationID: "ASKHGK-SAKJHTJ-ALKJSHT-HUZIUOP", 98 } 99 err := client.CancelEvent(eventRequest) 100 101 // then 102 assert.NoError(t, err) 103 104 response, err := server.Client().Get(fmt.Sprintf("%s/get", server.URL)) 105 assert.NoError(t, err) 106 107 var conf CancelEventRequest 108 err = json.NewDecoder(response.Body).Decode(&conf) 109 assert.NoError(t, err) 110 111 assert.Equal(t, "ASKHGK-SAKJHTJ-ALKJSHT-HUZIUOP", conf.OrchestrationID) 112 } 113 114 type server struct { 115 t *testing.T 116 request []byte 117 } 118 119 func fixHTTPServer(t *testing.T) *httptest.Server { 120 s := server{t: t} 121 122 r := mux.NewRouter() 123 r.HandleFunc("/createMaintenanceEvent", s.authorized(s.createEvent)).Methods(http.MethodPost) 124 r.HandleFunc("/updateMaintenanceEvent", s.authorized(s.updateEvent)).Methods(http.MethodPatch) 125 r.HandleFunc("/cancelMaintenanceEvent", s.authorized(s.cancelEvent)).Methods(http.MethodPatch) 126 127 r.HandleFunc("/get", s.getConfiguration).Methods(http.MethodGet) 128 129 return httptest.NewServer(r) 130 } 131 132 func (s *server) authorized(pass func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { 133 return func(w http.ResponseWriter, r *http.Request) { 134 //assume that comunication between application already built up 135 pass(w, r) 136 } 137 } 138 139 func (s *server) createEvent(w http.ResponseWriter, r *http.Request) { 140 body, err := ioutil.ReadAll(r.Body) 141 if err != nil { 142 w.WriteHeader(http.StatusBadRequest) 143 s.t.Errorf("test server cannot read request body: %s", err) 144 return 145 } 146 s.request = body 147 w.WriteHeader(http.StatusOK) 148 } 149 150 func (s *server) updateEvent(w http.ResponseWriter, r *http.Request) { 151 body, err := ioutil.ReadAll(r.Body) 152 if err != nil { 153 w.WriteHeader(http.StatusBadRequest) 154 s.t.Errorf("test server cannot read request body: %s", err) 155 return 156 } 157 s.request = body 158 w.WriteHeader(http.StatusOK) 159 } 160 161 func (s *server) cancelEvent(w http.ResponseWriter, r *http.Request) { 162 body, err := ioutil.ReadAll(r.Body) 163 if err != nil { 164 w.WriteHeader(http.StatusBadRequest) 165 s.t.Errorf("test server cannot read request body: %s", err) 166 return 167 } 168 s.request = body 169 w.WriteHeader(http.StatusOK) 170 } 171 172 func (s *server) getConfiguration(w http.ResponseWriter, r *http.Request) { 173 _, err := w.Write(s.request) 174 if err != nil { 175 s.t.Errorf("test server cannot write response body: %s", err) 176 } 177 }