[OOM-K8S-CERT-EXTERNAL-PROVIDER] Add health check of CMPv2 provisioner (cert-service...
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / certserviceclient / cert_service_client_test.go
1 /*
2  * ============LICENSE_START=======================================================
3  * oom-certservice-k8s-external-provider
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package certserviceclient
22
23 import (
24         "bytes"
25         "fmt"
26         "io/ioutil"
27         "net/http"
28         "testing"
29
30         "github.com/stretchr/testify/assert"
31
32         "onap.org/oom-certservice/k8s-external-provider/src/testdata"
33 )
34
35 const (
36         certificationUrl = "https://oom-cert-service:8443/v1/certificate/RA"
37 )
38
39
40 func Test_GetCertificates_shouldParseCertificateResponseCorrectly(t *testing.T) {
41         responseJson := `{"certificateChain": ["cert-0", "cert-1"], "trustedCertificates": ["trusted-cert-0", "trusted-cert-1"]}`
42         responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
43         client := CertServiceClientImpl{
44                 certificationUrl: certificationUrl,
45                 httpClient:       &httpClientMock{
46                         DoFunc: func(req *http.Request) (response *http.Response, e error) {
47                                 mockedResponse := &http.Response{
48                                         Body: responseJsonReader,
49                                 }
50                                 return mockedResponse, nil
51                         },
52                 },
53         }
54         response, _ := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
55         assert.ElementsMatch(t, []string{"cert-0", "cert-1"}, response.CertificateChain)
56         assert.ElementsMatch(t, []string{"trusted-cert-0", "trusted-cert-1"}, response.TrustedCertificates)
57 }
58
59 func Test_GetCertificates_shouldReturnError_whenResponseIsNotJson(t *testing.T) {
60         responseJson := `not a json`
61         responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
62         client := CertServiceClientImpl{
63                 certificationUrl: certificationUrl,
64                 httpClient:       &httpClientMock{
65                         DoFunc: func(req *http.Request) (response *http.Response, e error) {
66                                 mockedResponse := &http.Response{
67                                         Body: responseJsonReader,
68                                 }
69                                 return mockedResponse, nil
70                         },
71                 },
72         }
73         response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
74
75         assert.Nil(t, response)
76         assert.Error(t, err)
77 }
78
79 func Test_GetCertificates_shouldReturnError_whenHttpClientReturnsError(t *testing.T) {
80         client := CertServiceClientImpl{
81                 certificationUrl: certificationUrl,
82                 httpClient:       &httpClientMock{
83                         DoFunc: func(req *http.Request) (response *http.Response, err error) {
84                                 return nil, fmt.Errorf("mock error")
85                         },
86                 },
87         }
88         response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
89
90         assert.Nil(t, response)
91         assert.Error(t, err)
92 }
93
94 func Test_CheckHealth_shouldReturnNil_whenHttpClientReturnsStatusCode200(t *testing.T) {
95         client := CertServiceClientImpl{
96                 certificationUrl: certificationUrl,
97                 httpClient:       &httpClientMock{
98                         DoFunc: func(req *http.Request) (response *http.Response, e error) {
99                                 mockedResponse := &http.Response{
100                                         Body: nil,
101                                         StatusCode: 200,
102                                 }
103                                 return mockedResponse, nil
104                         },
105                 },
106         }
107
108         err := client.CheckHealth()
109
110         assert.Nil(t, err)
111 }
112
113 func Test_CheckHealth_shouldReturnError_whenHttpClientReturnsStatusCode404(t *testing.T) {
114         client := CertServiceClientImpl{
115                 certificationUrl: certificationUrl,
116                 httpClient:       &httpClientMock{
117                         DoFunc: func(req *http.Request) (response *http.Response, e error) {
118                                 mockedResponse := &http.Response{
119                                         Body: nil,
120                                         StatusCode: 404,
121                                 }
122                                 return mockedResponse, nil
123                         },
124                 },
125         }
126
127         err := client.CheckHealth()
128
129         assert.Error(t, err)
130 }
131
132 func Test_CheckHealth_shouldReturnError_whenHttpClientReturnsError(t *testing.T) {
133         client := CertServiceClientImpl{
134                 certificationUrl: certificationUrl,
135                 httpClient:       &httpClientMock{
136                         DoFunc: func(req *http.Request) (response *http.Response, err error) {
137                                 return nil, fmt.Errorf("mock error")
138                         },
139                 },
140         }
141         err := client.CheckHealth()
142
143         assert.Error(t, err)
144 }
145
146 type httpClientMock struct {
147         DoFunc func(*http.Request) (*http.Response, error)
148 }
149
150 func (client httpClientMock) Do(req *http.Request) (*http.Response, error) {
151         return client.DoFunc(req)
152 }