fbfeb5d5c4010d28e67a5b3db849cda1c9f00a84
[dcaegen2/services/sdk.git] /
1 /*
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
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 org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
25
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;
31
32 /**
33  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
34  */
35 class PasswordTest {
36
37     @Test
38     void use_shouldInvokeConsumerWithStoredPassword() {
39         // given
40         final String password = "hej ho";
41         final Password cut = new Password(password.toCharArray());
42
43         // when
44         String result = cut.useChecked(String::new).get();
45
46         // then
47         assertThat(result).isEqualTo(password);
48     }
49
50     @Test
51     void use_shouldClearPasswordAfterUse() {
52         // given
53         final char[] passwordChars = "hej ho".toCharArray();
54         final Password cut = new Password(passwordChars);
55
56         // when
57         useThePassword(cut);
58
59         // then
60         assertAllCharsAreNull(passwordChars);
61     }
62
63     @Test
64     void use_shouldFail_whenItWasAlreadyCalled() {
65         // given
66         final Password cut = new Password("ala ma kota".toCharArray());
67
68         // when & then
69         useThePassword(cut).get();
70
71         assertThatExceptionOfType(GeneralSecurityException.class).isThrownBy(() ->
72                 useThePassword(cut).get());
73     }
74
75     @Test
76     void use_shouldFail_whenItWasCleared() {
77         // given
78         final Password cut = new Password("ala ma kota".toCharArray());
79
80         // when & then
81         cut.clear();
82
83         assertThatExceptionOfType(GeneralSecurityException.class).isThrownBy(() ->
84                 useThePassword(cut).get());
85     }
86
87     @Test
88     void clear_shouldClearThePassword() {
89         // given
90         final char[] passwordChars = "hej ho".toCharArray();
91         final Password cut = new Password(passwordChars);
92
93         // when
94         cut.clear();
95
96         // then
97         assertAllCharsAreNull(passwordChars);
98     }
99
100     private Try<Object> useThePassword(Password cut) {
101         return cut.use((pass) -> Try.success(42));
102     }
103
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'")
107                 .isTrue();
108     }
109 }