2 * ============LICENSE_START=======================================================
3 * oom-certservice-k8s-external-provider
4 * ================================================================================
5 * Copyright (C) 2020-2021 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package certserviceclient
31 "github.com/stretchr/testify/assert"
33 "onap.org/oom-certservice/k8s-external-provider/src/model"
34 "onap.org/oom-certservice/k8s-external-provider/src/testdata"
38 certificationUrl = "https://oom-cert-service:8443/v1/certificate/RA"
39 certificateUpdateUrl = "https://oom-cert-service:8443/v1/certificate-update/RA"
42 func Test_GetCertificates_shouldParseCertificateResponseCorrectly(t *testing.T) {
43 responseJson := `{"certificateChain": ["cert-0", "cert-1"], "trustedCertificates": ["trusted-cert-0", "trusted-cert-1"]}`
44 responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
45 client := CertServiceClientImpl{
46 certificationUrl: certificationUrl,
47 httpClient: getMockedClient(responseJsonReader, http.StatusOK),
49 response, _ := client.GetCertificates(getTestSignCertificateModel())
50 assert.ElementsMatch(t, []string{"cert-0", "cert-1"}, response.CertificateChain)
51 assert.ElementsMatch(t, []string{"trusted-cert-0", "trusted-cert-1"}, response.TrustedCertificates)
54 func Test_GetCertificates_shouldReturnError_whenResponseIsNotJson(t *testing.T) {
55 responseJson := `not a json`
56 responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
57 client := CertServiceClientImpl{
58 certificationUrl: certificationUrl,
59 httpClient: &httpClientMock{
60 DoFunc: func(req *http.Request) (response *http.Response, e error) {
61 mockedResponse := &http.Response{
62 Body: responseJsonReader,
64 return mockedResponse, nil
68 response, err := client.GetCertificates(getTestSignCertificateModel())
70 assert.Nil(t, response)
74 func Test_GetCertificates_shouldReturnError_whenHttpClientReturnsError(t *testing.T) {
75 client := CertServiceClientImpl{
76 certificationUrl: certificationUrl,
77 httpClient: &httpClientMock{
78 DoFunc: func(req *http.Request) (response *http.Response, err error) {
79 return nil, fmt.Errorf("mock error")
83 response, err := client.GetCertificates(getTestSignCertificateModel())
85 assert.Nil(t, response)
89 func Test_GetCertificates_shouldReturnError_whenResponseOtherThan200(t *testing.T) {
90 responseJson := `{"errorMessage": "CertService API error"}`
91 responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
92 client := CertServiceClientImpl{
93 certificationUrl: certificationUrl,
94 httpClient: getMockedClient(responseJsonReader, http.StatusNotFound),
96 response, err := client.GetCertificates(getTestSignCertificateModel())
98 assert.Nil(t, response)
102 func Test_UpdateCertificates_shouldParseCertificateResponseCorrectly(t *testing.T) {
103 responseJson := `{"certificateChain": ["cert-0", "cert-1"], "trustedCertificates": ["trusted-cert-0", "trusted-cert-1"]}`
104 responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
105 client := CertServiceClientImpl{
106 updateUrl: certificateUpdateUrl,
107 httpClient: getMockedClient(responseJsonReader, http.StatusOK),
110 response, _ := client.UpdateCertificate(getTestSignCertificateModel())
111 assert.ElementsMatch(t, []string{"cert-0", "cert-1"}, response.CertificateChain)
112 assert.ElementsMatch(t, []string{"trusted-cert-0", "trusted-cert-1"}, response.TrustedCertificates)
115 func Test_UpdateCertificates_shouldReturnError_whenHttpClientReturnsError(t *testing.T) {
116 client := CertServiceClientImpl{
117 updateUrl: certificateUpdateUrl,
118 httpClient: &httpClientMock{
119 DoFunc: func(req *http.Request) (response *http.Response, err error) {
120 return nil, fmt.Errorf("mock error")
124 response, err := client.UpdateCertificate(getTestSignCertificateModel())
126 assert.Nil(t, response)
130 func Test_UpdateCertificates_shouldReturnError_whenResponseOtherThan200(t *testing.T) {
131 responseJson := `{"errorMessage": "CertService API error"}`
132 responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
133 client := CertServiceClientImpl{
134 updateUrl: updateEndpoint,
135 httpClient: getMockedClient(responseJsonReader, http.StatusNotFound),
137 response, err := client.UpdateCertificate(getTestSignCertificateModel())
139 assert.Nil(t, response)
143 func Test_CheckHealth_shouldReturnNil_whenHttpClientReturnsStatusCode200(t *testing.T) {
144 client := CertServiceClientImpl{
145 certificationUrl: certificationUrl,
146 httpClient: &httpClientMock{
147 DoFunc: func(req *http.Request) (response *http.Response, e error) {
148 mockedResponse := &http.Response{
152 return mockedResponse, nil
157 err := client.CheckHealth()
162 func Test_CheckHealth_shouldReturnError_whenHttpClientReturnsStatusCode404(t *testing.T) {
163 client := CertServiceClientImpl{
164 certificationUrl: certificationUrl,
165 httpClient: &httpClientMock{
166 DoFunc: func(req *http.Request) (response *http.Response, e error) {
167 mockedResponse := &http.Response{
171 return mockedResponse, nil
176 err := client.CheckHealth()
181 func Test_CheckHealth_shouldReturnError_whenHttpClientReturnsError(t *testing.T) {
182 client := CertServiceClientImpl{
183 certificationUrl: certificationUrl,
184 httpClient: &httpClientMock{
185 DoFunc: func(req *http.Request) (response *http.Response, err error) {
186 return nil, fmt.Errorf("mock error")
190 err := client.CheckHealth()
195 func getMockedClient(responseJsonReader io.ReadCloser, responseCode int) *httpClientMock {
196 return &httpClientMock{
197 DoFunc: func(req *http.Request) (response *http.Response, e error) {
198 mockedResponse := &http.Response{
199 Body: responseJsonReader,
200 StatusCode: responseCode,
202 return mockedResponse, nil
207 type httpClientMock struct {
208 DoFunc func(*http.Request) (*http.Response, error)
211 func (client httpClientMock) Do(req *http.Request) (*http.Response, error) {
212 return client.DoFunc(req)
215 func getTestSignCertificateModel() model.SignCertificateModel {
216 testSignCertificateModel := model.SignCertificateModel{
217 FilteredCsr: testdata.CsrBytes,
218 PrivateKeyBytes: testdata.PkBytes,
219 OldCertificateBytes: testdata.OldCertificateBytes,
220 OldPrivateKeyBytes: testdata.OldPrivateKeyBytes,
222 return testSignCertificateModel