2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019 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=====================================
21 package org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
26 import io.vavr.collection.Array;
27 import io.vavr.control.Try;
28 import java.security.GeneralSecurityException;
29 import java.util.Arrays;
30 import org.junit.jupiter.api.Test;
33 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
38 void use_shouldInvokeConsumerWithStoredPassword() {
40 final String password = "hej ho";
41 final Password cut = new Password(password.toCharArray());
44 String result = cut.useChecked(String::new).get();
47 assertThat(result).isEqualTo(password);
51 void use_shouldClearPasswordAfterUse() {
53 final char[] passwordChars = "hej ho".toCharArray();
54 final Password cut = new Password(passwordChars);
60 assertAllCharsAreNull(passwordChars);
64 void use_shouldFail_whenItWasAlreadyCalled() {
66 final Password cut = new Password("ala ma kota".toCharArray());
69 useThePassword(cut).get();
71 assertThatExceptionOfType(GeneralSecurityException.class).isThrownBy(() ->
72 useThePassword(cut).get());
76 void use_shouldFail_whenItWasCleared() {
78 final Password cut = new Password("ala ma kota".toCharArray());
83 assertThatExceptionOfType(GeneralSecurityException.class).isThrownBy(() ->
84 useThePassword(cut).get());
88 void clear_shouldClearThePassword() {
90 final char[] passwordChars = "hej ho".toCharArray();
91 final Password cut = new Password(passwordChars);
97 assertAllCharsAreNull(passwordChars);
100 private Try<Object> useThePassword(Password cut) {
101 return cut.use((pass) -> Try.success(42));
104 private void assertAllCharsAreNull(char[] passwordChars) {
105 assertThat(Array.ofAll(passwordChars).forAll(ch -> ch == '\0'))
106 .describedAs("all characters in " + Arrays.toString(passwordChars) + " should be == '\\0'")