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"
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: getMockedClient(responseJsonReader, http.StatusOK),
47 response, _ := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
48 assert.ElementsMatch(t, []string{"cert-0", "cert-1"}, response.CertificateChain)
49 assert.ElementsMatch(t, []string{"trusted-cert-0", "trusted-cert-1"}, response.TrustedCertificates)
52 func Test_GetCertificates_shouldReturnError_whenResponseIsNotJson(t *testing.T) {
53 responseJson := `not a json`
54 responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
55 client := CertServiceClientImpl{
56 certificationUrl: certificationUrl,
57 httpClient: &httpClientMock{
58 DoFunc: func(req *http.Request) (response *http.Response, e error) {
59 mockedResponse := &http.Response{
60 Body: responseJsonReader,
62 return mockedResponse, nil
66 response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
68 assert.Nil(t, response)
72 func Test_GetCertificates_shouldReturnError_whenHttpClientReturnsError(t *testing.T) {
73 client := CertServiceClientImpl{
74 certificationUrl: certificationUrl,
75 httpClient: &httpClientMock{
76 DoFunc: func(req *http.Request) (response *http.Response, err error) {
77 return nil, fmt.Errorf("mock error")
81 response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
83 assert.Nil(t, response)
87 func Test_GetCertificates_shouldReturnError_whenResponseOtherThan200(t *testing.T) {
88 responseJson := `{"errorMessage": "CertService API error"}`
89 responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
90 client := CertServiceClientImpl{
91 certificationUrl: certificationUrl,
92 httpClient: getMockedClient(responseJsonReader, http.StatusNotFound),
94 response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
96 assert.Nil(t, response)
100 func Test_CheckHealth_shouldReturnNil_whenHttpClientReturnsStatusCode200(t *testing.T) {
101 client := CertServiceClientImpl{
102 certificationUrl: certificationUrl,
103 httpClient: &httpClientMock{
104 DoFunc: func(req *http.Request) (response *http.Response, e error) {
105 mockedResponse := &http.Response{
109 return mockedResponse, nil
114 err := client.CheckHealth()
119 func Test_CheckHealth_shouldReturnError_whenHttpClientReturnsStatusCode404(t *testing.T) {
120 client := CertServiceClientImpl{
121 certificationUrl: certificationUrl,
122 httpClient: &httpClientMock{
123 DoFunc: func(req *http.Request) (response *http.Response, e error) {
124 mockedResponse := &http.Response{
128 return mockedResponse, nil
133 err := client.CheckHealth()
138 func Test_CheckHealth_shouldReturnError_whenHttpClientReturnsError(t *testing.T) {
139 client := CertServiceClientImpl{
140 certificationUrl: certificationUrl,
141 httpClient: &httpClientMock{
142 DoFunc: func(req *http.Request) (response *http.Response, err error) {
143 return nil, fmt.Errorf("mock error")
147 err := client.CheckHealth()
152 func getMockedClient(responseJsonReader io.ReadCloser, responseCode int) *httpClientMock {
153 return &httpClientMock{
154 DoFunc: func(req *http.Request) (response *http.Response, e error) {
155 mockedResponse := &http.Response{
156 Body: responseJsonReader,
157 StatusCode: responseCode,
159 return mockedResponse, nil
164 type httpClientMock struct {
165 DoFunc func(*http.Request) (*http.Response, error)
168 func (client httpClientMock) Do(req *http.Request) (*http.Response, error) {
169 return client.DoFunc(req)