aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/healthcheck
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/healthcheck')
-rw-r--r--pkg/healthcheck/healthcheck.go3
-rw-r--r--pkg/healthcheck/healthcheck_test.go109
2 files changed, 107 insertions, 5 deletions
diff --git a/pkg/healthcheck/healthcheck.go b/pkg/healthcheck/healthcheck.go
index 4c8a13b..9a2a1a4 100644
--- a/pkg/healthcheck/healthcheck.go
+++ b/pkg/healthcheck/healthcheck.go
@@ -1,6 +1,6 @@
// -
// ========================LICENSE_START=================================
-// Copyright (C) 2024: Deutsche Telecom
+// Copyright (C) 2024: Deutsche Telekom
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
+// SPDX-License-Identifier: Apache-2.0
// ========================LICENSE_END===================================
// Package healthcheck provides functionalities for handling health check requests.
diff --git a/pkg/healthcheck/healthcheck_test.go b/pkg/healthcheck/healthcheck_test.go
index 3e1876f..c8c4d15 100644
--- a/pkg/healthcheck/healthcheck_test.go
+++ b/pkg/healthcheck/healthcheck_test.go
@@ -1,3 +1,24 @@
+// -
+// ========================LICENSE_START=================================
+// Copyright (C) 2024: Deutsche Telekom
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// SPDX-License-Identifier: Apache-2.0
+// ========================LICENSE_END===================================
+
+// Package healthcheck provides functionalities for handling health check requests.
+// This package includes a function to handle HTTP requests for health checks
+// and respond with the health status of the service.
package healthcheck
import (
@@ -29,7 +50,7 @@ func TestHealthCheckHandler_Success(t *testing.T) {
assert.Equal(t, pdpattributes.PdpName, *response.Name)
assert.Equal(t, "self", *response.Url)
assert.True(t, *response.Healthy)
- assert.Equal(t,int32(200), *response.Code)
+ assert.Equal(t, int32(200), *response.Code)
assert.Equal(t, "alive", *response.Message)
}
@@ -72,14 +93,94 @@ func TestHealthCheckHandler_Failure(t *testing.T) {
}
+func TestHealthCheckHandler_ValidUUID(t *testing.T) {
+ // Prepare a request with a valid UUID in the header
+ req := httptest.NewRequest(http.MethodGet, "/healthcheck", nil)
+ validUUID := "123e4567-e89b-12d3-a456-426614174000"
+ req.Header.Set("X-ONAP-RequestID", validUUID)
+ w := httptest.NewRecorder()
+
+ // Call the HealthCheckHandler
+ HealthCheckHandler(w, req)
+
+ // Check if the status code is OK (200)
+ assert.Equal(t, http.StatusOK, w.Code)
+
+ // Check the response headers
+ assert.Equal(t, validUUID, w.Header().Get("X-ONAP-RequestID"))
+
+ // Check the response body
+ var response oapicodegen.HealthCheckReport
+ err := json.NewDecoder(w.Body).Decode(&response)
+ assert.NoError(t, err)
+ assert.Equal(t, pdpattributes.PdpName, *response.Name)
+ assert.Equal(t, "self", *response.Url)
+ assert.True(t, *response.Healthy)
+ assert.Equal(t, int32(200), *response.Code)
+ assert.Equal(t, "alive", *response.Message)
+}
+
+func TestHealthCheckHandler_InvalidUUID(t *testing.T) {
+ // Prepare a request with an invalid UUID in the header
+ req := httptest.NewRequest(http.MethodGet, "/healthcheck", nil)
+ req.Header.Set("X-ONAP-RequestID", "invalid-uuid")
+ w := httptest.NewRecorder()
+
+ // Call the HealthCheckHandler
+ HealthCheckHandler(w, req)
+
+ // Check if the status code is OK (200)
+ assert.Equal(t, http.StatusOK, w.Code)
+
+ // Check the fallback request ID
+ assert.Equal(t, "000000000000", w.Header().Get("X-ONAP-RequestID"))
+}
+
+func TestHealthCheckHandler_MissingUUID(t *testing.T) {
+ // Prepare a request with no UUID header
+ req := httptest.NewRequest(http.MethodGet, "/healthcheck", nil)
+ w := httptest.NewRecorder()
+
+ // Call the HealthCheckHandler
+ HealthCheckHandler(w, req)
+
+ // Check if the status code is OK (200)
+ assert.Equal(t, http.StatusOK, w.Code)
+
+ // Check the fallback request ID
+ assert.Equal(t, "000000000000", w.Header().Get("X-ONAP-RequestID"))
+}
+
+func TestHealthCheckHandler_EmptyResponseBody(t *testing.T) {
+ // Simulate a case where the handler fails to set the response body
+ EmptyResponseHandler := func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusOK)
+ }
+
+ // Prepare a request to the health check endpoint
+ req := httptest.NewRequest(http.MethodGet, "/healthcheck", nil)
+ w := httptest.NewRecorder()
+
+ // Call the modified handler
+ EmptyResponseHandler(w, req)
+
+ // Check if the status code is OK (200)
+ assert.Equal(t, http.StatusOK, w.Code)
+
+ // Try decoding the empty body
+ var response oapicodegen.HealthCheckReport
+ err := json.NewDecoder(w.Body).Decode(&response)
+ assert.Error(t, err)
+}
+
func strPtr(s string) *string {
- return &s
+ return &s
}
func boolPtr(b bool) *bool {
- return &b
+ return &b
}
func int32Ptr(i int32) *int32 {
- return &i
+ return &i
}