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
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/testdata"
37 certificationUrl = "https://oom-cert-service:8443/v1/certificate/RA"
41 func Test_GetCertificates_shouldParseCertificateResponseCorrectly(t *testing.T) {
42 responseJson := `{"certificateChain": ["cert-0", "cert-1"], "trustedCertificates": ["trusted-cert-0", "trusted-cert-1"]}`
43 responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
44 client := CertServiceClientImpl{
45 certificationUrl: certificationUrl,
46 httpClient: getMockedClient(responseJsonReader, http.StatusOK),
48 response, _ := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
49 assert.ElementsMatch(t, []string{"cert-0", "cert-1"}, response.CertificateChain)
50 assert.ElementsMatch(t, []string{"trusted-cert-0", "trusted-cert-1"}, response.TrustedCertificates)
53 func Test_GetCertificates_shouldReturnError_whenResponseIsNotJson(t *testing.T) {
54 responseJson := `not a json`
55 responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
56 client := CertServiceClientImpl{
57 certificationUrl: certificationUrl,
58 httpClient: &httpClientMock{
59 DoFunc: func(req *http.Request) (response *http.Response, e error) {
60 mockedResponse := &http.Response{
61 Body: responseJsonReader,
63 return mockedResponse, nil
67 response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
69 assert.Nil(t, response)
73 func Test_GetCertificates_shouldReturnError_whenHttpClientReturnsError(t *testing.T) {
74 client := CertServiceClientImpl{
75 certificationUrl: certificationUrl,
76 httpClient: &httpClientMock{
77 DoFunc: func(req *http.Request) (response *http.Response, err error) {
78 return nil, fmt.Errorf("mock error")
82 response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
84 assert.Nil(t, response)
88 func Test_GetCertificates_shouldReturnError_whenResponseOtherThan200(t *testing.T) {
89 responseJson := `{"errorMessage": "CertService API error"}`
90 responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
91 client := CertServiceClientImpl{
92 certificationUrl: certificationUrl,
93 httpClient: getMockedClient(responseJsonReader, http.StatusNotFound),
95 response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
97 assert.Nil(t, response)
101 func Test_CheckHealth_shouldReturnNil_whenHttpClientReturnsStatusCode200(t *testing.T) {
102 client := CertServiceClientImpl{
103 certificationUrl: certificationUrl,
104 httpClient: &httpClientMock{
105 DoFunc: func(req *http.Request) (response *http.Response, e error) {
106 mockedResponse := &http.Response{
110 return mockedResponse, nil
115 err := client.CheckHealth()
120 func Test_CheckHealth_shouldReturnError_whenHttpClientReturnsStatusCode404(t *testing.T) {
121 client := CertServiceClientImpl{
122 certificationUrl: certificationUrl,
123 httpClient: &httpClientMock{
124 DoFunc: func(req *http.Request) (response *http.Response, e error) {
125 mockedResponse := &http.Response{
129 return mockedResponse, nil
134 err := client.CheckHealth()
139 func Test_CheckHealth_shouldReturnError_whenHttpClientReturnsError(t *testing.T) {
140 client := CertServiceClientImpl{
141 certificationUrl: certificationUrl,
142 httpClient: &httpClientMock{
143 DoFunc: func(req *http.Request) (response *http.Response, err error) {
144 return nil, fmt.Errorf("mock error")
148 err := client.CheckHealth()
153 func getMockedClient(responseJsonReader io.ReadCloser, responseCode int) *httpClientMock {
154 return &httpClientMock{
155 DoFunc: func(req *http.Request) (response *http.Response, e error) {
156 mockedResponse := &http.Response{
157 Body: responseJsonReader,
158 StatusCode: responseCode,
160 return mockedResponse, nil
165 type httpClientMock struct {
166 DoFunc func(*http.Request) (*http.Response, error)
169 func (client httpClientMock) Do(req *http.Request) (*http.Response, error) {
170 return client.DoFunc(req)