Merge "Adding basic logging to CDS Simulator"
[policy/models.git] / models-interactions / model-simulators / src / test / java / org / onap / policy / simulators / CdsSimulatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.simulators;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import com.google.protobuf.util.JsonFormat;
28 import io.grpc.ManagedChannel;
29 import io.grpc.internal.DnsNameResolverProvider;
30 import io.grpc.internal.PickFirstLoadBalancerProvider;
31 import io.grpc.netty.NettyChannelBuilder;
32 import io.grpc.stub.StreamObserver;
33 import java.io.IOException;
34 import java.nio.charset.StandardCharsets;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.CountDownLatch;
37 import java.util.concurrent.TimeUnit;
38 import org.apache.commons.io.IOUtils;
39 import org.json.simple.parser.ParseException;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc;
44 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceStub;
45 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
46 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput.Builder;
47 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.common.utils.network.NetworkUtil;
51 import org.onap.policy.common.utils.resources.ResourceUtils;
52
53 public class CdsSimulatorTest {
54     private static final StandardCoder coder = new StandardCoder();
55
56     private CdsSimulator sim;
57
58     /**
59      * Starts the simulator, allocating a unique port for each test so we don't have to
60      * wait for the prior server to shut down.
61      */
62     @Before
63     public void setUp() throws Exception {
64         int port = NetworkUtil.allocPort();
65         sim = new CdsSimulator(Util.LOCALHOST, port);
66         sim.start();
67     }
68
69     @After
70     public void tearDown() {
71         sim.stop();
72     }
73
74     @Test
75     public void test() throws Exception {
76         String reqstr = IOUtils.toString(getClass().getResource("cds/cds.request.json"), StandardCharsets.UTF_8);
77         Builder builder = ExecutionServiceInput.newBuilder();
78         JsonFormat.parser().ignoringUnknownFields().merge(reqstr, builder);
79         ExecutionServiceInput request = builder.build();
80         ManagedChannel channel = NettyChannelBuilder.forAddress(Util.LOCALHOST, sim.getPort())
81             .nameResolverFactory(new DnsNameResolverProvider())
82             .loadBalancerFactory(new PickFirstLoadBalancerProvider()).usePlaintext().build();
83
84         try {
85             final CompletableFuture<ExecutionServiceOutput> future = new CompletableFuture<>();
86             final CountDownLatch completed = new CountDownLatch(1);
87
88             BluePrintProcessingServiceStub asyncStub = BluePrintProcessingServiceGrpc.newStub(channel);
89
90             StreamObserver<ExecutionServiceOutput> responseObserver = new StreamObserver<ExecutionServiceOutput>() {
91                 @Override
92                 public void onNext(ExecutionServiceOutput output) {
93                     future.complete(output);
94                 }
95
96                 @Override
97                 public void onError(Throwable throwable) {
98                     future.completeExceptionally(throwable);
99                 }
100
101                 @Override
102                 public void onCompleted() {
103                     completed.countDown();
104                 }
105             };
106
107             StreamObserver<ExecutionServiceInput> requestObserver = asyncStub.process(responseObserver);
108             try {
109                 // publish the message
110                 requestObserver.onNext(request);
111
112                 // indicate that the request is done
113                 requestObserver.onCompleted();
114
115             } catch (RuntimeException e) {
116                 requestObserver.onError(e);
117             }
118
119             // wait for it to complete
120             assertTrue(completed.await(5, TimeUnit.SECONDS));
121
122             ExecutionServiceOutput result = future.get();
123             assertEquals(200, result.getStatus().getCode());
124
125         } finally {
126             channel.shutdown();
127         }
128     }
129
130     @Test
131     public void testGetResponseString() throws IOException, CoderException, ParseException {
132         CdsSimulator cdsSimulator = new CdsSimulator(Util.LOCALHOST, sim.getPort());
133         String reqstr = ResourceUtils.getResourceAsString(
134             "org/onap/policy/simulators/cds/cds.request.json");
135         String responseqstr = ResourceUtils.getResourceAsString(
136             "org/onap/policy/simulators/cds/pm_control-create-subscription.json");
137         ExecutionServiceInput request = coder.decode(reqstr, ExecutionServiceInput.class);
138         assertEquals(responseqstr, cdsSimulator.getResponseString(request, 0));
139     }
140 }