79c78ed50c55117a2f55df8daa2d7e0ee5a5583c
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / cmpv2controller / cmpv2_issuer_controller_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 cmpv2controller
22
23 import (
24         "testing"
25
26         "github.com/go-logr/logr"
27         "github.com/stretchr/testify/assert"
28         "github.com/stretchr/testify/mock"
29
30         "onap.org/oom-certservice/k8s-external-provider/src/cmpv2api"
31 )
32
33 func Test_shouldBeValidCMPv2IssuerSpec_whenAllFieldsAreSet(t *testing.T) {
34         spec := getValidCMPv2IssuerSpec()
35
36         err := validateCMPv2IssuerSpec(spec, &MockLogger{})
37         assert.Nil(t, err)
38 }
39
40 func Test_shouldBeInvalidCMPv2IssuerSpec_whenSpecIsEmpty(t *testing.T) {
41         spec := cmpv2api.CMPv2IssuerSpec{}
42         err := validateCMPv2IssuerSpec(spec, nil)
43         assert.NotNil(t, err)
44 }
45
46 func Test_shouldBeInvalidCMPv2IssuerSpec_whenNotAllFieldsAreSet(t *testing.T) {
47         setEmptyFieldFunctions := map[string]func(spec *cmpv2api.CMPv2IssuerSpec){
48                 "emptyUrl":            func(spec *cmpv2api.CMPv2IssuerSpec) { spec.URL = "" },
49                 "empryCaName":         func(spec *cmpv2api.CMPv2IssuerSpec) { spec.CaName = "" },
50                 "emptySecretName":     func(spec *cmpv2api.CMPv2IssuerSpec) { spec.CertSecretRef.Name = "" },
51                 "emptySecretKeyRef":   func(spec *cmpv2api.CMPv2IssuerSpec) { spec.CertSecretRef.KeyRef = "" },
52                 "emptySecretCertRef":  func(spec *cmpv2api.CMPv2IssuerSpec) { spec.CertSecretRef.CertRef = "" },
53                 "emptySecretCaertRef": func(spec *cmpv2api.CMPv2IssuerSpec) { spec.CertSecretRef.CacertRef = "" },
54         }
55
56         for caseName, setEmptyFieldFunction := range setEmptyFieldFunctions {
57                 t.Run(caseName, func(t *testing.T) {
58                         test_shouldBeInvalidCMPv2IssuerSpec_whenFunctionApplied(t, setEmptyFieldFunction)
59                 })
60         }
61 }
62
63 func test_shouldBeInvalidCMPv2IssuerSpec_whenFunctionApplied(t *testing.T, transformSpec func(spec *cmpv2api.CMPv2IssuerSpec)) {
64         spec := getValidCMPv2IssuerSpec()
65         transformSpec(&spec)
66         err := validateCMPv2IssuerSpec(spec, nil)
67         assert.NotNil(t, err)
68 }
69
70 func getValidCMPv2IssuerSpec() cmpv2api.CMPv2IssuerSpec {
71         issuerSpec := cmpv2api.CMPv2IssuerSpec{
72                 URL:    "https://oom-cert-service:8443/v1/certificate/",
73                 CaName: "RA",
74                 CertSecretRef: cmpv2api.SecretKeySelector{
75                         Name:      "issuer-cert-secret",
76                         KeyRef:    "cmpv2Issuer-key.pem",
77                         CertRef:   "cmpv2Issuer-cert.pem",
78                         CacertRef: "cacert.pem",
79                 },
80         }
81         return issuerSpec
82 }
83
84 type MockLogger struct {
85         mock.Mock
86 }
87
88 func (m *MockLogger) Info(msg string, keysAndValues ...interface{})             {}
89 func (m *MockLogger) Error(err error, msg string, keysAndValues ...interface{}) {}
90 func (m *MockLogger) Enabled() bool                                             { return false }
91 func (m *MockLogger) V(level int) logr.Logger                                   { return m }
92 func (m *MockLogger) WithValues(keysAndValues ...interface{}) logr.Logger       { return m }
93 func (m *MockLogger) WithName(name string) logr.Logger                          { return m }