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
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=====================================
20 package org.onap.dcaegen2.services.sdk.security;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
25 import java.io.IOException;
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;
35 class EncodePasswordTest {
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));
46 doNothing().when(encodePassword).printErrorAndExit(any(), any());
50 void shouldExitWithErrorCodeWhenPasswordIsNotAvailableInArgs() throws IOException {
52 encodePassword.run(new String[]{});
55 verify(charsFromStreamReader).readPasswordFromStdIn();
56 verify(encodePassword).printErrorAndExit(
57 eq(ExitCode.INVALID_PASSWORD),
58 eq(PASSWORD_CANNOT_BE_EMPTY)
63 void shouldExitWithErrorCodeWhenPasswordIsEmpty() {
65 encodePassword.run(new String[]{EMPTY_PASSWORD});
68 verify(encodePassword).printErrorAndExit(
69 eq(ExitCode.INVALID_PASSWORD),
70 eq(PASSWORD_CANNOT_BE_EMPTY)
75 void shouldEncodePasswordWhenPasswordIsAvailableInArgs() throws IOException {
77 encodePassword.run(new String[]{PASSWORD_TO_ENCODE});
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)