Improve testing stability
[sdc.git] / common-be / src / test / java / org / openecomp / sdc / be / csar / security / model / CertificateInfoImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.be.csar.security.model;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.Mockito.doNothing;
27 import static org.mockito.Mockito.doThrow;
28 import static org.mockito.Mockito.when;
29
30 import java.io.File;
31 import java.security.cert.CertificateExpiredException;
32 import java.security.cert.CertificateNotYetValidException;
33 import java.security.cert.X509Certificate;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38
39 class CertificateInfoImplTest {
40
41     @Mock
42     private X509Certificate certificate;
43
44     @BeforeEach
45     void setUp() {
46         MockitoAnnotations.openMocks(this);
47     }
48
49     @Test
50     void isValidTest() throws CertificateNotYetValidException, CertificateExpiredException {
51         when(certificate.getType()).thenReturn("X.509");
52         doNothing().when(certificate).checkValidity();
53         final CertificateInfoImpl certificateInfo = new CertificateInfoImpl(new File(""), certificate);
54         assertTrue(certificateInfo.isValid());
55         doThrow(CertificateExpiredException.class).when(certificate).checkValidity();
56         assertFalse(certificateInfo.isValid());
57     }
58
59     @Test
60     void unsupportedCertificateTypeTest() {
61         final String certificateType = "unknown";
62         when(certificate.getType()).thenReturn(certificateType);
63         final CertificateInfoImpl certificateInfo = new CertificateInfoImpl(new File(""), certificate);
64         final UnsupportedOperationException actualException =
65             assertThrows(UnsupportedOperationException.class, certificateInfo::isValid);
66         assertEquals(actualException.getMessage(),
67             String.format("Certificate type '%s' not supported", certificateType));
68     }
69 }