2bfa754f0e1ecfcab9a9f94f352bda99a6f9fc9e
[so.git] / common / src / test / java / org / onap / so / client / cds / CDSProcessingClientTest.java
1 /*
2  * Copyright (C) 2019 Bell Canada.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.so.client.cds;
17
18
19 import static org.junit.Assert.*;
20 import static org.mockito.Mockito.*;
21
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;
44
45 @RunWith(JUnit4.class)
46 public class CDSProcessingClientTest {
47
48     @Rule
49     public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
50
51     @Mock
52     private CDSProcessingListener listener = spy(new TestCDSProcessingListener());
53
54     private CDSProcessingHandler handler;
55     private CDSProcessingClient client;
56
57
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<>();
62
63     @Before
64     public void setUp() throws Exception {
65         String serverName = InProcessServerBuilder.generateName();
66         grpcCleanup.register(InProcessServerBuilder.forName(serverName)
67             .fallbackHandlerRegistry(serviceRegistry).directExecutor().build().start());
68
69         handler = new CDSProcessingHandler(listener);
70
71         client =
72             new CDSProcessingClient(InProcessChannelBuilder.forName(serverName).directExecutor().build(), handler);
73
74         final BluePrintProcessingServiceImplBase routeChatImpl =
75             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
117     @Test
118     public void testSendMessageFail() throws Exception {
119
120         ExecutionServiceInput fakeRequest1 = ExecutionServiceInput.newBuilder().setActionIdentifiers(
121             ActionIdentifiers.newBuilder().setActionName("request1").build()).build();
122
123         CountDownLatch finishLatch = client.sendRequest(fakeRequest1);
124
125         responseObserverRef.get().onError(new Throwable("fail test"));
126         verify(listener).onError(any(Throwable.class));
127
128         assertTrue(finishLatch.await(1, TimeUnit.SECONDS));
129     }
130
131     @Test
132     public void testSendMessage() throws Exception {
133
134         ExecutionServiceInput fakeRequest1 = ExecutionServiceInput.newBuilder().setActionIdentifiers(
135             ActionIdentifiers.newBuilder().setActionName("request1").build()).build();
136
137         ExecutionServiceOutput fakeResponse1 = ExecutionServiceOutput.newBuilder().setActionIdentifiers(
138             ActionIdentifiers.newBuilder().setActionName("response1").build()).build();
139
140         ExecutionServiceOutput fakeResponse2 = ExecutionServiceOutput.newBuilder().setActionIdentifiers(
141             ActionIdentifiers.newBuilder().setActionName("response2").build()).build();
142
143         CountDownLatch finishLatch = client.sendRequest(fakeRequest1);
144
145         // request message sent and delivered for one time
146         assertTrue(allRequestsDelivered.await(1, TimeUnit.SECONDS));
147         assertEquals(Collections.singletonList("request1"), messagesDelivered);
148
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);
155
156         // let server complete.
157         responseObserverRef.get().onCompleted();
158
159         assertTrue(finishLatch.await(1, TimeUnit.SECONDS));
160     }
161
162 }