Activate JUnits in project
[dcaegen2/services/sdk.git] / security / crypt-password / src / test / java / org / onap / dcaegen2 / services / sdk / security / EncodePasswordTest.java
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2021 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 package org.onap.dcaegen2.services.sdk.security;
21
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24
25 import java.io.IOException;
26
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.doNothing;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.never;
32 import static org.mockito.Mockito.spy;
33 import static org.mockito.Mockito.verify;
34
35 class EncodePasswordTest {
36
37     public static final String PASSWORD_CANNOT_BE_EMPTY = "Password cannot be empty";
38     public static final String PASSWORD_TO_ENCODE = "testPass";
39     public static final String EMPTY_PASSWORD = "";
40     private final CryptPassword cryptPassword = mock(CryptPassword.class);
41     private final CharsFromStreamReader charsFromStreamReader = mock(CharsFromStreamReader.class);
42     private final EncodePassword encodePassword = spy(new EncodePassword(cryptPassword, charsFromStreamReader));
43
44     @BeforeEach
45     void setUp() {
46         doNothing().when(encodePassword).printErrorAndExit(any(), any());
47     }
48
49     @Test
50     void shouldExitWithErrorCodeWhenPasswordIsNotAvailableInArgs() throws IOException {
51         // when
52         encodePassword.run(new String[]{});
53
54         // then
55         verify(charsFromStreamReader).readPasswordFromStdIn();
56         verify(encodePassword).printErrorAndExit(
57                 eq(ExitCode.INVALID_PASSWORD),
58                 eq(PASSWORD_CANNOT_BE_EMPTY)
59         );
60     }
61
62     @Test
63     void shouldExitWithErrorCodeWhenPasswordIsEmpty() {
64         // when
65         encodePassword.run(new String[]{EMPTY_PASSWORD});
66
67         // then
68         verify(encodePassword).printErrorAndExit(
69                 eq(ExitCode.INVALID_PASSWORD),
70                 eq(PASSWORD_CANNOT_BE_EMPTY)
71         );
72     }
73
74     @Test
75     void shouldEncodePasswordWhenPasswordIsAvailableInArgs() throws IOException {
76         // when
77         encodePassword.run(new String[]{PASSWORD_TO_ENCODE});
78
79         // then
80         verify(charsFromStreamReader, never()).readPasswordFromStdIn();
81         verify(cryptPassword).encode(eq(PASSWORD_TO_ENCODE));
82         verify(encodePassword, never()).printErrorAndExit(
83                 eq(ExitCode.INVALID_PASSWORD),
84                 eq(PASSWORD_CANNOT_BE_EMPTY)
85         );
86     }
87 }