ddc87bc4a1b4683a9dd2b3920ebb9813ccb40f78
[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 package org.onap.dcaegen2.services.sdk.services.hvves.client.producer.ct;
21
22 import io.netty.buffer.ByteBuf;
23 import io.vavr.collection.HashSet;
24 import io.vavr.control.Try;
25 import java.net.InetSocketAddress;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.time.Duration;
29 import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeys;
30 import org.onap.dcaegen2.services.sdk.security.ssl.Passwords;
31 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.HvVesProducer;
32 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.HvVesProducerFactory;
33 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options.ImmutableProducerOptions;
34 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options.ImmutableProducerOptions.Builder;
35 import org.onap.ves.VesEventOuterClass.VesEvent;
36 import reactor.core.publisher.Flux;
37
38 /**
39  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
40  */
41 public class SystemUnderTestWrapper {
42
43     private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(5);
44     private final DummyCollector collector = new DummyCollector();
45     private HvVesProducer cut;
46     private final Duration timeout;
47
48     public SystemUnderTestWrapper(Duration timeout) {
49         this.timeout = timeout;
50     }
51
52     public SystemUnderTestWrapper() {
53         this(DEFAULT_TIMEOUT);
54     }
55
56     public void startSecure() {
57         start(ImmutableProducerOptions.builder()
58                 .securityKeys(ImmutableSecurityKeys.builder()
59                         .keyStore(resource("/client.p12").get())
60                         .keyStorePassword(Passwords.fromResource("/client.pass").get())
61                         .trustStore(resource("/trust.p12").get())
62                         .trustStorePassword(Passwords.fromResource("/trust.pass").get())
63                         .build()));
64     }
65
66     public void start() {
67         start(createDefaultOptions());
68     }
69
70     public void start(ImmutableProducerOptions.Builder optionsBuilder) {
71         InetSocketAddress collectorAddress = collector.start();
72         cut = HvVesProducerFactory.create(
73                 optionsBuilder.collectorAddresses(HashSet.of(collectorAddress)).build());
74     }
75
76     public void stop() {
77         collector.stop();
78     }
79
80     public ByteBuf blockingSend(Flux<VesEvent> events) {
81         events.transform(cut::send).subscribe();
82         collector.blockUntilFirstClientIsHandled(timeout);
83         return collector.dataFromFirstClient();
84     }
85
86     private Builder createDefaultOptions() {
87         return ImmutableProducerOptions.builder();
88     }
89
90     private Try<Path> resource(String resource) {
91         return Try.of(() -> Paths.get(Passwords.class.getResource(resource).toURI()));
92     }
93
94 }