9f91afb71292e0fbddc27e9f87bcae31d082058d
[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.Fail.fail;
25
26 import io.vavr.control.Try;
27 import java.io.File;
28 import java.net.URISyntaxException;
29 import java.nio.file.NoSuchFileException;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34
35 /**
36  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
37  * @since January 2019
38  */
39 class PasswordsTest {
40
41     @Test
42     void fromFile() {
43         // given
44         final File file = new File("./src/test/resources/password.txt");
45
46         // when
47         final Try<Password> result = Passwords.fromFile(file);
48
49         // then
50         assertSuccessful(result);
51         assertThat(extractPassword(result)).isEqualTo("ja baczewski\n2nd line");
52     }
53
54     @Test
55     void fromPath() throws URISyntaxException {
56         // given
57         final Path path = Paths.get(PasswordsTest.class.getResource("/password.txt").toURI());
58
59         // when
60         final Try<Password> result = Passwords.fromPath(path);
61
62         // then
63         assertSuccessful(result);
64         assertThat(extractPassword(result)).isEqualTo("ja baczewski\n2nd line");
65     }
66
67     @Test
68     void fromPath_shouldFail_whenNotFound() {
69         // given
70         final Path path = Paths.get("/", UUID.randomUUID().toString());
71
72         // when
73         final Try<Password> result = Passwords.fromPath(path);
74
75         // then
76         assertThat(result.isFailure()).describedAs("Try.failure?").isTrue();
77         assertThat(result.getCause()).isInstanceOf(NoSuchFileException.class);
78     }
79
80     @Test
81     void fromResource() {
82         // given
83         final String resource = "/password.txt";
84
85         // when
86         final Try<Password> result = Passwords.fromResource(resource);
87
88         // then
89         assertSuccessful(result);
90         assertThat(extractPassword(result)).isEqualTo("ja baczewski\n2nd line");
91     }
92
93     private void assertSuccessful(Try<Password> result) {
94         assertThat(result.isSuccess()).describedAs("Try.success?").isTrue();
95     }
96
97     private String extractPassword(Try<Password> result) {
98         return result.flatMap(pass -> pass.useChecked(String::new)).get();
99     }
100 }