Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Unit Tests #7

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@

package io.dapr.spring.cloud.stream.binder;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import com.google.protobuf.Empty;

import io.dapr.serializer.DaprObjectSerializer;
import io.dapr.serializer.DefaultObjectSerializer;
import io.dapr.spring.cloud.stream.binder.messaging.DaprMessageConverter;
import io.dapr.v1.DaprAppCallbackProtos;
import io.dapr.v1.DaprAppCallbackProtos.TopicSubscription;
import io.grpc.stub.StreamObserver;
import com.google.protobuf.Empty;

public class DaprGrpcServiceTest {

private String pubsubName;
private String topic;
private DaprObjectSerializer objectSerializer;
Expand All @@ -50,14 +54,54 @@ public void beforeEach() {
producer = new DaprMessageProducer(daprGrpcServic, daprMessageConverter, pubsubName, topic);
}

/**
* This test is to show if the subscription information can be sent successfully
* via GRPC.
*/
@Test
public void testListTopicSubscriptionst_default() {
Empty request = Empty.newBuilder().build();
StreamObserver<DaprAppCallbackProtos.ListTopicSubscriptionsResponse> responseObserver = mock(
StreamObserver.class);
ArgumentCaptor<DaprAppCallbackProtos.ListTopicSubscriptionsResponse> captor = ArgumentCaptor
.forClass(DaprAppCallbackProtos.ListTopicSubscriptionsResponse.class);

daprGrpcServic.listTopicSubscriptions(request, responseObserver);

verify(responseObserver, times(1)).onNext(captor.capture());
DaprAppCallbackProtos.ListTopicSubscriptionsResponse response = captor.getValue();
List<TopicSubscription> list = response.getSubscriptionsList();

Assert.assertEquals(list.size(), 1);
Assert.assertEquals(list.get(0).getPubsubName(), "pubsubName");
Assert.assertEquals(list.get(0).getTopic(), "topic");
verify(responseObserver, times(1)).onCompleted();
}

@Test
public void testListTopicSubscriptionst_exception() {
Empty request = Empty.newBuilder().build();
StreamObserver<DaprAppCallbackProtos.ListTopicSubscriptionsResponse> responseObserver = mock(
StreamObserver.class);

doThrow(new RuntimeException()).when(responseObserver)
.onNext(any());

daprGrpcServic.listTopicSubscriptions(request, responseObserver);

verify(responseObserver, times(1)).onError(any());
verify(responseObserver, times(1)).onCompleted();
}

/**
* The purpose of this test is to show if the response when GRPC event is called
* is reasonable.
* It can indicate subscribing message from Dapr sidecar is successful.
*/
@Test
public void testOnTopicEvent() {
DaprAppCallbackProtos.TopicEventRequest request = DaprAppCallbackProtos.TopicEventRequest.newBuilder().build();
public void testOnTopicEvent_default() {
DaprAppCallbackProtos.TopicEventRequest request = DaprAppCallbackProtos.TopicEventRequest.newBuilder()
.build();
StreamObserver<DaprAppCallbackProtos.TopicEventResponse> responseObserver = mock(StreamObserver.class);
ArgumentCaptor<DaprAppCallbackProtos.TopicEventResponse> captor = ArgumentCaptor
.forClass(DaprAppCallbackProtos.TopicEventResponse.class);
Expand All @@ -71,27 +115,17 @@ public void testOnTopicEvent() {
verify(responseObserver, times(1)).onCompleted();
}

/**
* This test is to show if the subscription information can be sent successfully
* via GRPC.
*/
@Test
public void testListTopicSubscriptionst() {
Empty request = Empty.newBuilder().build();
StreamObserver<DaprAppCallbackProtos.ListTopicSubscriptionsResponse> responseObserver = mock(
StreamObserver.class);
ArgumentCaptor<DaprAppCallbackProtos.ListTopicSubscriptionsResponse> captor = ArgumentCaptor
.forClass(DaprAppCallbackProtos.ListTopicSubscriptionsResponse.class);

daprGrpcServic.listTopicSubscriptions(request, responseObserver);
public void testOnTopicEvent_exception() {
DaprAppCallbackProtos.TopicEventRequest request = DaprAppCallbackProtos.TopicEventRequest.newBuilder()
.build();
StreamObserver<DaprAppCallbackProtos.TopicEventResponse> responseObserver = mock(StreamObserver.class);
doThrow(new RuntimeException()).when(responseObserver)
.onNext(any());

verify(responseObserver, times(1)).onNext(captor.capture());
DaprAppCallbackProtos.ListTopicSubscriptionsResponse response = captor.getValue();
List<TopicSubscription> list = response.getSubscriptionsList();
daprGrpcServic.onTopicEvent(request, responseObserver);

Assert.assertEquals(list.size(), 1);
Assert.assertEquals(list.get(0).getPubsubName(), "pubsubName");
Assert.assertEquals(list.get(0).getTopic(), "topic");
verify(responseObserver, times(1)).onCompleted();
verify(responseObserver, times(1)).onError(any());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

package io.dapr.spring.cloud.stream.binder;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -32,6 +34,7 @@
import io.dapr.serializer.DaprObjectSerializer;
import io.dapr.serializer.DefaultObjectSerializer;
import io.dapr.spring.cloud.stream.binder.messaging.DaprMessageConverter;
import reactor.core.publisher.Mono;

public class DaprMessageHandlerTest {

Expand All @@ -55,15 +58,9 @@ public void testPublish() {
DaprMessageHandler daprMessageHandler = new DaprMessageHandler(converter, topic, pubsubName,
daprClient);

try {
daprMessageHandler.handleMessage(message);
} catch (MessageHandlingException e) {
// without initialization and sidecar running the method block() will throw an
// error.
Assert.assertTrue(true);
} catch (Exception e) {
Assert.assertTrue(false);
}
when(daprClient.publishEvent(any())).thenReturn(Mono.empty());
daprMessageHandler.handleMessage(message);

verify(daprClient, times(1)).publishEvent(isA(PublishEventRequest.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

import org.junit.Assert;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -69,4 +74,19 @@ public void testToMessage() {
Assert.assertArrayEquals((byte[]) result.getPayload(), "fake payload".getBytes());
}

@Test
public void testToMessage_Exception() throws IOException {
String data = "{\"headers\":{\"contentType\":\"application/json\",\"key\":\"value\"},\"payload\":\"ZmFrZSBwYXlsb2Fk\"}";
TopicEventRequest request = TopicEventRequest.newBuilder().setData(ByteString.copyFrom(data.getBytes()))
.build();
DaprObjectSerializer objectSerializer = mock(DefaultObjectSerializer.class);
DaprMessageConverter converter = new DaprMessageConverter(objectSerializer);

when(objectSerializer.deserialize(any(),any())).thenThrow(new RuntimeException() );
Message<?> result = converter.toMessage(request);

//when deserialization fails, all data will be regarded as payload.
Assert.assertArrayEquals((byte[]) result.getPayload(), data.getBytes());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2022 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.dapr.spring.cloud.stream.binder.messaging;

import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;

import org.junit.jupiter.api.Test;

public class DaprMessageTest {

@Test
public void testDaprMessage() {
byte[] payload = "payload".getBytes();
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("key", "value");
DaprMessage daprMessage = new DaprMessage(payload, headers);

Assert.assertEquals(daprMessage.getHeaders(), headers);
Assert.assertEquals(daprMessage.getPayload(), payload);

byte[] fakePayload = "fake-payload".getBytes();
daprMessage.setPayload(fakePayload);
Map<String, Object> fakeHeaders = new HashMap<String, Object>();
fakeHeaders.put("fake-key", "fake-value");
daprMessage.setHeaders(fakeHeaders);

Assert.assertEquals(daprMessage.getHeaders(), fakeHeaders);
Assert.assertEquals(daprMessage.getPayload(), fakePayload);
}

}