2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 Bell Canada.
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.so.client.cds;
24 import static org.junit.Assert.*;
25 import static org.mockito.Mockito.*;
26 import io.grpc.inprocess.InProcessChannelBuilder;
27 import io.grpc.inprocess.InProcessServerBuilder;
28 import io.grpc.stub.StreamObserver;
29 import io.grpc.testing.GrpcCleanupRule;
30 import io.grpc.util.MutableHandlerRegistry;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.TimeUnit;
36 import java.util.concurrent.atomic.AtomicReference;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.junit.runners.JUnit4;
43 import org.mockito.Mock;
44 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers;
45 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase;
46 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
47 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
49 @RunWith(JUnit4.class)
50 public class CDSProcessingClientTest {
53 public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
56 private CDSProcessingListener listener = spy(new TestCDSProcessingListener());
58 private CDSProcessingHandler handler;
59 private CDSProcessingClient client;
62 private final MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry();
63 private final List<String> messagesDelivered = new ArrayList<>();
64 private final CountDownLatch allRequestsDelivered = new CountDownLatch(1);
65 private final AtomicReference<StreamObserver<ExecutionServiceOutput>> responseObserverRef = new AtomicReference<>();
68 public void setUp() throws Exception {
69 String serverName = InProcessServerBuilder.generateName();
70 grpcCleanup.register(InProcessServerBuilder.forName(serverName).fallbackHandlerRegistry(serviceRegistry)
71 .directExecutor().build().start());
73 handler = new CDSProcessingHandler(listener);
75 client = new CDSProcessingClient(InProcessChannelBuilder.forName(serverName).directExecutor().build(), handler);
77 final BluePrintProcessingServiceImplBase routeChatImpl = new BluePrintProcessingServiceImplBase() {
79 public StreamObserver<ExecutionServiceInput> process(
80 StreamObserver<ExecutionServiceOutput> responseObserver) {
82 responseObserverRef.set(responseObserver);
84 StreamObserver<ExecutionServiceInput> requestObserver = new StreamObserver<ExecutionServiceInput>() {
86 public void onNext(ExecutionServiceInput message) {
87 messagesDelivered.add(message.getActionIdentifiers().getActionName());
91 public void onError(Throwable t) {
96 public void onCompleted() {
97 allRequestsDelivered.countDown();
101 return requestObserver;
105 serviceRegistry.addService(routeChatImpl);
109 public void tearDown() {
114 public void testClientCst() {
115 new CDSProcessingClient(listener);
120 public void testSendMessageFail() throws Exception {
122 ExecutionServiceInput fakeRequest1 = ExecutionServiceInput.newBuilder()
123 .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("request1").build()).build();
125 CountDownLatch finishLatch = client.sendRequest(fakeRequest1);
127 responseObserverRef.get().onError(new Throwable("fail test"));
128 verify(listener).onError(any(Throwable.class));
130 assertTrue(finishLatch.await(1, TimeUnit.SECONDS));
134 public void testSendMessage() throws Exception {
136 ExecutionServiceInput fakeRequest1 = ExecutionServiceInput.newBuilder()
137 .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("request1").build()).build();
139 ExecutionServiceOutput fakeResponse1 = ExecutionServiceOutput.newBuilder()
140 .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("response1").build()).build();
142 ExecutionServiceOutput fakeResponse2 = ExecutionServiceOutput.newBuilder()
143 .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("response2").build()).build();
145 CountDownLatch finishLatch = client.sendRequest(fakeRequest1);
147 // request message sent and delivered for one time
148 assertTrue(allRequestsDelivered.await(1, TimeUnit.SECONDS));
149 assertEquals(Collections.singletonList("request1"), messagesDelivered);
151 // Let the server send out two simple response messages
152 // and verify that the client receives them.
153 responseObserverRef.get().onNext(fakeResponse1);
154 verify(listener).onMessage(fakeResponse1);
155 responseObserverRef.get().onNext(fakeResponse2);
156 verify(listener).onMessage(fakeResponse2);
158 // let server complete.
159 responseObserverRef.get().onCompleted();
161 assertTrue(finishLatch.await(1, TimeUnit.SECONDS));