2 * Copyright (C) 2019 Bell Canada.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.so.client.cds;
19 import static org.junit.Assert.*;
20 import static org.mockito.Mockito.*;
22 import io.grpc.inprocess.InProcessChannelBuilder;
23 import io.grpc.inprocess.InProcessServerBuilder;
24 import io.grpc.stub.StreamObserver;
25 import io.grpc.testing.GrpcCleanupRule;
26 import io.grpc.util.MutableHandlerRegistry;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.concurrent.CountDownLatch;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.atomic.AtomicReference;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.junit.runners.JUnit4;
39 import org.mockito.Mock;
40 import org.onap.ccsdk.apps.controllerblueprints.common.api.ActionIdentifiers;
41 import org.onap.ccsdk.apps.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase;
42 import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceInput;
43 import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput;
45 @RunWith(JUnit4.class)
46 public class CDSProcessingClientTest {
49 public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
52 private CDSProcessingListener listener = spy(new TestCDSProcessingListener());
54 private CDSProcessingHandler handler;
55 private CDSProcessingClient client;
58 private final MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry();
59 private final List<String> messagesDelivered = new ArrayList<>();
60 private final CountDownLatch allRequestsDelivered = new CountDownLatch(1);
61 private final AtomicReference<StreamObserver<ExecutionServiceOutput>> responseObserverRef = new AtomicReference<>();
64 public void setUp() throws Exception {
65 String serverName = InProcessServerBuilder.generateName();
66 grpcCleanup.register(InProcessServerBuilder.forName(serverName)
67 .fallbackHandlerRegistry(serviceRegistry).directExecutor().build().start());
69 handler = new CDSProcessingHandler(listener);
72 new CDSProcessingClient(InProcessChannelBuilder.forName(serverName).directExecutor().build(), handler);
74 final BluePrintProcessingServiceImplBase routeChatImpl =
75 new BluePrintProcessingServiceImplBase() {
77 public StreamObserver<ExecutionServiceInput> process(
78 StreamObserver<ExecutionServiceOutput> responseObserver) {
80 responseObserverRef.set(responseObserver);
82 StreamObserver<ExecutionServiceInput> requestObserver = new StreamObserver<ExecutionServiceInput>() {
84 public void onNext(ExecutionServiceInput message) {
85 messagesDelivered.add(message.getActionIdentifiers().getActionName());
89 public void onError(Throwable t) {
94 public void onCompleted() {
95 allRequestsDelivered.countDown();
99 return requestObserver;
103 serviceRegistry.addService(routeChatImpl);
107 public void tearDown() {
112 public void testClientCst() {
113 new CDSProcessingClient(listener);
118 public void testSendMessageFail() throws Exception {
120 ExecutionServiceInput fakeRequest1 = ExecutionServiceInput.newBuilder().setActionIdentifiers(
121 ActionIdentifiers.newBuilder().setActionName("request1").build()).build();
123 CountDownLatch finishLatch = client.sendRequest(fakeRequest1);
125 responseObserverRef.get().onError(new Throwable("fail test"));
126 verify(listener).onError(any(Throwable.class));
128 assertTrue(finishLatch.await(1, TimeUnit.SECONDS));
132 public void testSendMessage() throws Exception {
134 ExecutionServiceInput fakeRequest1 = ExecutionServiceInput.newBuilder().setActionIdentifiers(
135 ActionIdentifiers.newBuilder().setActionName("request1").build()).build();
137 ExecutionServiceOutput fakeResponse1 = ExecutionServiceOutput.newBuilder().setActionIdentifiers(
138 ActionIdentifiers.newBuilder().setActionName("response1").build()).build();
140 ExecutionServiceOutput fakeResponse2 = ExecutionServiceOutput.newBuilder().setActionIdentifiers(
141 ActionIdentifiers.newBuilder().setActionName("response2").build()).build();
143 CountDownLatch finishLatch = client.sendRequest(fakeRequest1);
145 // request message sent and delivered for one time
146 assertTrue(allRequestsDelivered.await(1, TimeUnit.SECONDS));
147 assertEquals(Collections.singletonList("request1"), messagesDelivered);
149 // Let the server send out two simple response messages
150 // and verify that the client receives them.
151 responseObserverRef.get().onNext(fakeResponse1);
152 verify(listener).onMessage(fakeResponse1);
153 responseObserverRef.get().onNext(fakeResponse2);
154 verify(listener).onMessage(fakeResponse2);
156 // let server complete.
157 responseObserverRef.get().onCompleted();
159 assertTrue(finishLatch.await(1, TimeUnit.SECONDS));