8409ea784ff8be4ad98a70f13c1ce1056fc893f0
[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         "github.com/go-logr/logr"
25         "github.com/stretchr/testify/assert"
26         "github.com/stretchr/testify/mock"
27         "onap.org/oom-certservice/k8s-external-provider/src/cmpv2api"
28         "testing"
29 )
30
31 func Test_shouldBeInvalidCMPv2IssuerSpec_whenSpecIsEmpty(t *testing.T) {
32         spec := cmpv2api.CMPv2IssuerSpec{}
33         err := validateCMPv2IssuerSpec(spec, nil)
34         assert.NotNil(t, err)
35 }
36
37 func Test_shouldBeInvalidCMPv2IssuerSpec_whenNotAllFieldsAreSet(t *testing.T) {
38         spec := cmpv2api.CMPv2IssuerSpec{}
39         spec.URL = "https://localhost"
40         spec.KeyRef = cmpv2api.SecretKeySelector{}
41         spec.KeyRef.Name = "secret-key"
42
43         err := validateCMPv2IssuerSpec(spec, &MockLogger{})
44         assert.NotNil(t, err)
45 }
46
47 func Test_shouldBeValidCMPv2IssuerSpec_whenAllFieldsAreSet(t *testing.T) {
48         spec := cmpv2api.CMPv2IssuerSpec{}
49         spec.URL = "https://localhost"
50         spec.KeyRef = cmpv2api.SecretKeySelector{}
51         spec.KeyRef.Name = "secret-key"
52         spec.KeyRef.Key = "the-key"
53
54         err := validateCMPv2IssuerSpec(spec, &MockLogger{})
55         assert.Nil(t, err)
56 }
57
58 type MockLogger struct {
59         mock.Mock
60 }
61 func (m *MockLogger) Info(msg string, keysAndValues ...interface{}) {}
62 func (m *MockLogger) Error(err error, msg string, keysAndValues ...interface{}) {}
63 func (m *MockLogger) Enabled() bool { return false }
64 func (m *MockLogger) V(level int) logr.Logger { return m }
65 func (m *MockLogger) WithValues(keysAndValues ...interface{}) logr.Logger { return m }
66 func (m *MockLogger) WithName(name string) logr.Logger { return m }