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