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
30 "github.com/stretchr/testify/assert"
32 "onap.org/oom-certservice/k8s-external-provider/src/testdata"
36 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: &httpClientMock{
46 DoFunc: func(req *http.Request) (response *http.Response, e error) {
47 mockedResponse := &http.Response{
48 Body: responseJsonReader,
50 return mockedResponse, nil
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)
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,
69 return mockedResponse, nil
73 response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
75 assert.Nil(t, response)
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")
88 response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
90 assert.Nil(t, response)
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{
103 return mockedResponse, nil
108 err := client.CheckHealth()
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{
122 return mockedResponse, nil
127 err := client.CheckHealth()
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")
141 err := client.CheckHealth()
146 type httpClientMock struct {
147 DoFunc func(*http.Request) (*http.Response, error)
150 func (client httpClientMock) Do(req *http.Request) (*http.Response, error) {
151 return client.DoFunc(req)