[OOM-K8S-CERT-EXTERNAL-PROVIDER] Add client for CertService API
[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_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_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_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
95 type httpClientMock struct {
96         DoFunc func(*http.Request) (*http.Response, error)
97 }
98
99 func (client httpClientMock) Do(req *http.Request) (*http.Response, error) {
100         return client.DoFunc(req)
101 }