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_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_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_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)
95 type httpClientMock struct {
96 DoFunc func(*http.Request) (*http.Response, error)
99 func (client httpClientMock) Do(req *http.Request) (*http.Response, error) {
100 return client.DoFunc(req)