Update CDS and grpc dependencies
[so.git] / common / src / test / java / org / onap / so / client / cds / CDSProcessingClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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
21 package org.onap.so.client.cds;
22
23 import static org.junit.Assert.*;
24 import static org.mockito.Mockito.*;
25 import io.grpc.inprocess.InProcessChannelBuilder;
26 import io.grpc.inprocess.InProcessServerBuilder;
27 import io.grpc.stub.StreamObserver;
28 import io.grpc.testing.GrpcCleanupRule;
29 import io.grpc.util.MutableHandlerRegistry;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.concurrent.CountDownLatch;
34 import java.util.concurrent.TimeUnit;
35 import java.util.concurrent.atomic.AtomicReference;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.junit.runners.JUnit4;
42 import org.mockito.Mock;
43 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers;
44 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BlueprintProcessingServiceGrpc.BlueprintProcessingServiceImplBase;
45 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
46 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
47
48 @RunWith(JUnit4.class)
49 public class CDSProcessingClientTest {
50
51     @Rule
52     public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
53
54     @Mock
55     private CDSProcessingListener listener = spy(new TestCDSProcessingListener());
56
57     private CDSProcessingHandler handler;
58     private CDSProcessingClient client;
59
60     private final MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry();
61     private final List<String> messagesDelivered = new ArrayList<>();
62     private final CountDownLatch allRequestsDelivered = new CountDownLatch(1);
63     private final AtomicReference<StreamObserver<ExecutionServiceOutput>> responseObserverRef = new AtomicReference<>();
64
65     @Before
66     public void setUp() throws Exception {
67         String serverName = InProcessServerBuilder.generateName();
68         grpcCleanup.register(InProcessServerBuilder.forName(serverName).fallbackHandlerRegistry(serviceRegistry)
69                 .directExecutor().build().start());
70
71         handler = new CDSProcessingHandler(listener);
72
73         client = new CDSProcessingClient(InProcessChannelBuilder.forName(serverName).directExecutor().build(), handler);
74
75         final BlueprintProcessingServiceImplBase routeChatImpl = new BlueprintProcessingServiceImplBase() {
76             @Override
77             public StreamObserver<ExecutionServiceInput> process(
78                     StreamObserver<ExecutionServiceOutput> responseObserver) {
79
80                 responseObserverRef.set(responseObserver);
81
82                 StreamObserver<ExecutionServiceInput> requestObserver = new StreamObserver<ExecutionServiceInput>() {
83                     @Override
84                     public void onNext(ExecutionServiceInput message) {
85                         messagesDelivered.add(message.getActionIdentifiers().getActionName());
86                     }
87
88                     @Override
89                     public void onError(Throwable t) {
90
91                     }
92
93                     @Override
94                     public void onCompleted() {
95                         allRequestsDelivered.countDown();
96                     }
97                 };
98
99                 return requestObserver;
100             }
101         };
102
103         serviceRegistry.addService(routeChatImpl);
104     }
105
106     @After
107     public void tearDown() {
108         client.close();
109     }
110
111     @Test
112     public void testClientCst() {
113         new CDSProcessingClient(listener);
114     }
115
116     @Test
117     public void testSendMessageFail() throws Exception {
118
119         ExecutionServiceInput fakeRequest1 = ExecutionServiceInput.newBuilder()
120                 .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("request1").build()).build();
121
122         CountDownLatch finishLatch = client.sendRequest(fakeRequest1);
123
124         responseObserverRef.get().onError(new Throwable("fail test"));
125         verify(listener).onError(any(Throwable.class));
126
127         assertTrue(finishLatch.await(1, TimeUnit.SECONDS));
128     }
129
130     @Test
131     public void testSendMessage() throws Exception {
132
133         ExecutionServiceInput fakeRequest1 = ExecutionServiceInput.newBuilder()
134                 .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("request1").build()).build();
135
136         ExecutionServiceOutput fakeResponse1 = ExecutionServiceOutput.newBuilder()
137                 .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("response1").build()).build();
138
139         ExecutionServiceOutput fakeResponse2 = ExecutionServiceOutput.newBuilder()
140                 .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("response2").build()).build();
141
142         CountDownLatch finishLatch = client.sendRequest(fakeRequest1);
143
144         // request message sent and delivered for one time
145         assertTrue(allRequestsDelivered.await(1, TimeUnit.SECONDS));
146         assertEquals(Collections.singletonList("request1"), messagesDelivered);
147
148         // Let the server send out two simple response messages
149         // and verify that the client receives them.
150         responseObserverRef.get().onNext(fakeResponse1);
151         verify(listener).onMessage(fakeResponse1);
152         responseObserverRef.get().onNext(fakeResponse2);
153         verify(listener).onMessage(fakeResponse2);
154
155         // let server complete.
156         responseObserverRef.get().onCompleted();
157
158         assertTrue(finishLatch.await(1, TimeUnit.SECONDS));
159     }
160
161 }