-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add javadoc and tests for functional ReceivedMessage class #1038
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
gcloud-java-pubsub/src/test/java/com/google/cloud/pubsub/ReceivedMessageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* 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 com.google.cloud.pubsub; | ||
|
||
import static org.easymock.EasyMock.createMock; | ||
import static org.easymock.EasyMock.createStrictMock; | ||
import static org.easymock.EasyMock.expect; | ||
import static org.easymock.EasyMock.replay; | ||
import static org.easymock.EasyMock.verify; | ||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertSame; | ||
|
||
import com.google.api.client.util.Charsets; | ||
import com.google.cloud.ByteArray; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.util.concurrent.Futures; | ||
|
||
import org.easymock.EasyMock; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ReceivedMessageTest { | ||
|
||
private static final String SUBSCRIPTION = "subscription"; | ||
private static final String ACK_ID = "ackId"; | ||
private static final String MESSAGE_ID = "messageId"; | ||
private static final String PAYLOAD_STRING = "payload"; | ||
private static final ByteArray PAYLOAD = | ||
ByteArray.copyFrom("payload".getBytes(StandardCharsets.UTF_8)); | ||
private static final Map<String, String> ATTRIBUTES = | ||
ImmutableMap.of("key1", "value1", "key2", "value2"); | ||
private static final Long PUBLISH_TIME = 42L; | ||
private static final Message MESSAGE = Message.builder(PAYLOAD) | ||
.id(MESSAGE_ID) | ||
.attributes(ATTRIBUTES) | ||
.publishTime(PUBLISH_TIME) | ||
.build(); | ||
private static final com.google.pubsub.v1.ReceivedMessage RECEIVED_MESSAGE_PB = | ||
com.google.pubsub.v1.ReceivedMessage.newBuilder() | ||
.setMessage(MESSAGE.toPb()) | ||
.setAckId(ACK_ID) | ||
.build(); | ||
|
||
private final PubSub serviceMockReturnsOptions = createStrictMock(PubSub.class); | ||
private final PubSubOptions mockOptions = createMock(PubSubOptions.class); | ||
private PubSub pubsub; | ||
private ReceivedMessage expectedMessage; | ||
private ReceivedMessage message; | ||
|
||
private void initializeExpectedMessage(int optionsCalls) { | ||
expect(serviceMockReturnsOptions.options()).andReturn(mockOptions).times(optionsCalls); | ||
replay(serviceMockReturnsOptions); | ||
pubsub = createStrictMock(PubSub.class); | ||
expectedMessage = | ||
ReceivedMessage.fromPb(serviceMockReturnsOptions, SUBSCRIPTION, RECEIVED_MESSAGE_PB); | ||
} | ||
|
||
private void initializeMessage() { | ||
message = ReceivedMessage.fromPb(pubsub, SUBSCRIPTION, RECEIVED_MESSAGE_PB); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
verify(pubsub, serviceMockReturnsOptions); | ||
} | ||
|
||
@Test | ||
public void testBuilder() { | ||
initializeExpectedMessage(3); | ||
replay(pubsub); | ||
Map<String, String> attributes = ImmutableMap.of("newKey1", "newVal1"); | ||
ReceivedMessage builtMessage = expectedMessage.toBuilder() | ||
.payload("newPayload") | ||
.id("newMessageId") | ||
.attributes(attributes) | ||
.publishTime(PUBLISH_TIME + 1) | ||
.build(); | ||
assertSame(serviceMockReturnsOptions, builtMessage.pubsub()); | ||
assertEquals(SUBSCRIPTION, builtMessage.subscription()); | ||
assertEquals(ACK_ID, builtMessage.ackId()); | ||
assertEquals("newMessageId", builtMessage.id()); | ||
assertArrayEquals("newPayload".getBytes(Charsets.UTF_8), builtMessage.payload().toByteArray()); | ||
assertEquals("newPayload", builtMessage.payloadAsString()); | ||
assertEquals(attributes, builtMessage.attributes()); | ||
assertEquals(PUBLISH_TIME + 1, (long) builtMessage.publishTime()); | ||
builtMessage = builtMessage.toBuilder() | ||
.payload(PAYLOAD) | ||
.id(MESSAGE_ID) | ||
.clearAttributes() | ||
.addAttribute("key1", "value1") | ||
.addAttribute("key2", "value2") | ||
.publishTime(PUBLISH_TIME) | ||
.build(); | ||
assertSame(serviceMockReturnsOptions, builtMessage.pubsub()); | ||
assertEquals(MESSAGE_ID, builtMessage.id()); | ||
assertEquals(PAYLOAD, builtMessage.payload()); | ||
assertEquals(PAYLOAD_STRING, builtMessage.payloadAsString()); | ||
assertEquals(ATTRIBUTES, builtMessage.attributes()); | ||
assertEquals(PUBLISH_TIME, builtMessage.publishTime()); | ||
compareReceivedMessage(expectedMessage, builtMessage); | ||
} | ||
|
||
@Test | ||
public void testToBuilder() { | ||
initializeExpectedMessage(2); | ||
replay(pubsub); | ||
compareReceivedMessage(expectedMessage, expectedMessage.toBuilder().build()); | ||
} | ||
|
||
@Test | ||
public void testAck() { | ||
initializeExpectedMessage(1); | ||
expect(pubsub.options()).andReturn(mockOptions); | ||
pubsub.ack(SUBSCRIPTION, ACK_ID); | ||
EasyMock.expectLastCall(); | ||
replay(pubsub); | ||
initializeMessage(); | ||
message.ack(); | ||
} | ||
|
||
@Test | ||
public void testAckAsync() throws ExecutionException, InterruptedException { | ||
initializeExpectedMessage(1); | ||
expect(pubsub.options()).andReturn(mockOptions); | ||
expect(pubsub.ackAsync(SUBSCRIPTION, ACK_ID)).andReturn(Futures.<Void>immediateFuture(null)); | ||
EasyMock.expectLastCall(); | ||
replay(pubsub); | ||
initializeMessage(); | ||
assertNull(message.ackAsync().get()); | ||
} | ||
|
||
@Test | ||
public void testModifyAckDeadline() { | ||
initializeExpectedMessage(1); | ||
expect(pubsub.options()).andReturn(mockOptions); | ||
pubsub.modifyAckDeadline(SUBSCRIPTION, 10, TimeUnit.SECONDS, ACK_ID); | ||
EasyMock.expectLastCall(); | ||
replay(pubsub); | ||
initializeMessage(); | ||
message.modifyAckDeadline(10, TimeUnit.SECONDS); | ||
} | ||
|
||
@Test | ||
public void testModifyAckDeadlineAsync() throws ExecutionException, InterruptedException { | ||
initializeExpectedMessage(1); | ||
expect(pubsub.options()).andReturn(mockOptions); | ||
expect(pubsub.modifyAckDeadlineAsync(SUBSCRIPTION, 10, TimeUnit.SECONDS, ACK_ID)) | ||
.andReturn(Futures.<Void>immediateFuture(null)); | ||
EasyMock.expectLastCall(); | ||
replay(pubsub); | ||
initializeMessage(); | ||
assertNull(message.modifyAckDeadlineAsync(10, TimeUnit.SECONDS).get()); | ||
} | ||
|
||
private void compareReceivedMessage(ReceivedMessage expected, ReceivedMessage value) { | ||
assertEquals(expected, value); | ||
assertEquals(expected.id(), value.id()); | ||
assertEquals(expected.payload(), value.payload()); | ||
assertEquals(expected.payloadAsString(), value.payloadAsString()); | ||
assertEquals(expected.attributes(), value.attributes()); | ||
assertEquals(expected.publishTime(), value.publishTime()); | ||
assertEquals(expected.ackId(), value.ackId()); | ||
assertEquals(expected.subscription(), value.subscription()); | ||
assertEquals(expected.hashCode(), value.hashCode()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.