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.Fail.fail;
26 import io.vavr.control.Try;
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;
36 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
44 final File file = new File("./src/test/resources/password.txt");
47 final Try<Password> result = Passwords.fromFile(file);
50 assertSuccessful(result);
51 assertThat(extractPassword(result)).isEqualTo("ja baczewski\n2nd line");
55 void fromPath() throws URISyntaxException {
57 final Path path = Paths.get(PasswordsTest.class.getResource("/password.txt").toURI());
60 final Try<Password> result = Passwords.fromPath(path);
63 assertSuccessful(result);
64 assertThat(extractPassword(result)).isEqualTo("ja baczewski\n2nd line");
68 void fromPath_shouldFail_whenNotFound() {
70 final Path path = Paths.get("/", UUID.randomUUID().toString());
73 final Try<Password> result = Passwords.fromPath(path);
76 assertThat(result.isFailure()).describedAs("Try.failure?").isTrue();
77 assertThat(result.getCause()).isInstanceOf(NoSuchFileException.class);
83 final String resource = "/password.txt";
86 final Try<Password> result = Passwords.fromResource(resource);
89 assertSuccessful(result);
90 assertThat(extractPassword(result)).isEqualTo("ja baczewski\n2nd line");
93 private void assertSuccessful(Try<Password> result) {
94 assertThat(result.isSuccess()).describedAs("Try.success?").isTrue();
97 private String extractPassword(Try<Password> result) {
98 return result.flatMap(pass -> pass.useChecked(String::new)).get();