|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 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 | + |
| 17 | +package com.google.cloud.pubsub; |
| 18 | + |
| 19 | +import static org.easymock.EasyMock.createMock; |
| 20 | +import static org.easymock.EasyMock.createStrictMock; |
| 21 | +import static org.easymock.EasyMock.expect; |
| 22 | +import static org.easymock.EasyMock.replay; |
| 23 | +import static org.easymock.EasyMock.verify; |
| 24 | +import static org.junit.Assert.assertArrayEquals; |
| 25 | +import static org.junit.Assert.assertEquals; |
| 26 | +import static org.junit.Assert.assertNull; |
| 27 | +import static org.junit.Assert.assertSame; |
| 28 | + |
| 29 | +import com.google.api.client.util.Charsets; |
| 30 | +import com.google.cloud.ByteArray; |
| 31 | +import com.google.common.collect.ImmutableMap; |
| 32 | +import com.google.common.util.concurrent.Futures; |
| 33 | + |
| 34 | +import org.easymock.EasyMock; |
| 35 | +import org.junit.After; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +import java.nio.charset.StandardCharsets; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.concurrent.ExecutionException; |
| 41 | +import java.util.concurrent.TimeUnit; |
| 42 | + |
| 43 | +public class ReceivedMessageTest { |
| 44 | + |
| 45 | + private static final String SUBSCRIPTION = "subscription"; |
| 46 | + private static final String ACK_ID = "ackId"; |
| 47 | + private static final String MESSAGE_ID = "messageId"; |
| 48 | + private static final String PAYLOAD_STRING = "payload"; |
| 49 | + private static final ByteArray PAYLOAD = |
| 50 | + ByteArray.copyFrom("payload".getBytes(StandardCharsets.UTF_8)); |
| 51 | + private static final Map<String, String> ATTRIBUTES = |
| 52 | + ImmutableMap.of("key1", "value1", "key2", "value2"); |
| 53 | + private static final Long PUBLISH_TIME = 42L; |
| 54 | + private static final Message MESSAGE = Message.builder(PAYLOAD) |
| 55 | + .id(MESSAGE_ID) |
| 56 | + .attributes(ATTRIBUTES) |
| 57 | + .publishTime(PUBLISH_TIME) |
| 58 | + .build(); |
| 59 | + private static final com.google.pubsub.v1.ReceivedMessage RECEIVED_MESSAGE_PB = |
| 60 | + com.google.pubsub.v1.ReceivedMessage.newBuilder() |
| 61 | + .setMessage(MESSAGE.toPb()) |
| 62 | + .setAckId(ACK_ID) |
| 63 | + .build(); |
| 64 | + |
| 65 | + private final PubSub serviceMockReturnsOptions = createStrictMock(PubSub.class); |
| 66 | + private final PubSubOptions mockOptions = createMock(PubSubOptions.class); |
| 67 | + private PubSub pubsub; |
| 68 | + private ReceivedMessage expectedMessage; |
| 69 | + private ReceivedMessage message; |
| 70 | + |
| 71 | + private void initializeExpectedMessage(int optionsCalls) { |
| 72 | + expect(serviceMockReturnsOptions.options()).andReturn(mockOptions).times(optionsCalls); |
| 73 | + replay(serviceMockReturnsOptions); |
| 74 | + pubsub = createStrictMock(PubSub.class); |
| 75 | + expectedMessage = |
| 76 | + ReceivedMessage.fromPb(serviceMockReturnsOptions, SUBSCRIPTION, RECEIVED_MESSAGE_PB); |
| 77 | + } |
| 78 | + |
| 79 | + private void initializeMessage() { |
| 80 | + message = ReceivedMessage.fromPb(pubsub, SUBSCRIPTION, RECEIVED_MESSAGE_PB); |
| 81 | + } |
| 82 | + |
| 83 | + @After |
| 84 | + public void tearDown() throws Exception { |
| 85 | + verify(pubsub, serviceMockReturnsOptions); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testBuilder() { |
| 90 | + initializeExpectedMessage(3); |
| 91 | + replay(pubsub); |
| 92 | + Map<String, String> attributes = ImmutableMap.of("newKey1", "newVal1"); |
| 93 | + ReceivedMessage builtMessage = expectedMessage.toBuilder() |
| 94 | + .payload("newPayload") |
| 95 | + .id("newMessageId") |
| 96 | + .attributes(attributes) |
| 97 | + .publishTime(PUBLISH_TIME + 1) |
| 98 | + .build(); |
| 99 | + assertSame(serviceMockReturnsOptions, builtMessage.pubsub()); |
| 100 | + assertEquals(SUBSCRIPTION, builtMessage.subscription()); |
| 101 | + assertEquals(ACK_ID, builtMessage.ackId()); |
| 102 | + assertEquals("newMessageId", builtMessage.id()); |
| 103 | + assertArrayEquals("newPayload".getBytes(Charsets.UTF_8), builtMessage.payload().toByteArray()); |
| 104 | + assertEquals("newPayload", builtMessage.payloadAsString()); |
| 105 | + assertEquals(attributes, builtMessage.attributes()); |
| 106 | + assertEquals(PUBLISH_TIME + 1, (long) builtMessage.publishTime()); |
| 107 | + builtMessage = builtMessage.toBuilder() |
| 108 | + .payload(PAYLOAD) |
| 109 | + .id(MESSAGE_ID) |
| 110 | + .clearAttributes() |
| 111 | + .addAttribute("key1", "value1") |
| 112 | + .addAttribute("key2", "value2") |
| 113 | + .publishTime(PUBLISH_TIME) |
| 114 | + .build(); |
| 115 | + assertSame(serviceMockReturnsOptions, builtMessage.pubsub()); |
| 116 | + assertEquals(MESSAGE_ID, builtMessage.id()); |
| 117 | + assertEquals(PAYLOAD, builtMessage.payload()); |
| 118 | + assertEquals(PAYLOAD_STRING, builtMessage.payloadAsString()); |
| 119 | + assertEquals(ATTRIBUTES, builtMessage.attributes()); |
| 120 | + assertEquals(PUBLISH_TIME, builtMessage.publishTime()); |
| 121 | + compareReceivedMessage(expectedMessage, builtMessage); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testToBuilder() { |
| 126 | + initializeExpectedMessage(2); |
| 127 | + replay(pubsub); |
| 128 | + compareReceivedMessage(expectedMessage, expectedMessage.toBuilder().build()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testAck() { |
| 133 | + initializeExpectedMessage(1); |
| 134 | + expect(pubsub.options()).andReturn(mockOptions); |
| 135 | + pubsub.ack(SUBSCRIPTION, ACK_ID); |
| 136 | + EasyMock.expectLastCall(); |
| 137 | + replay(pubsub); |
| 138 | + initializeMessage(); |
| 139 | + message.ack(); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testAckAsync() throws ExecutionException, InterruptedException { |
| 144 | + initializeExpectedMessage(1); |
| 145 | + expect(pubsub.options()).andReturn(mockOptions); |
| 146 | + expect(pubsub.ackAsync(SUBSCRIPTION, ACK_ID)).andReturn(Futures.<Void>immediateFuture(null)); |
| 147 | + EasyMock.expectLastCall(); |
| 148 | + replay(pubsub); |
| 149 | + initializeMessage(); |
| 150 | + assertNull(message.ackAsync().get()); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testModifyAckDeadline() { |
| 155 | + initializeExpectedMessage(1); |
| 156 | + expect(pubsub.options()).andReturn(mockOptions); |
| 157 | + pubsub.modifyAckDeadline(SUBSCRIPTION, 10, TimeUnit.SECONDS, ACK_ID); |
| 158 | + EasyMock.expectLastCall(); |
| 159 | + replay(pubsub); |
| 160 | + initializeMessage(); |
| 161 | + message.modifyAckDeadline(10, TimeUnit.SECONDS); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void testModifyAckDeadlineAsync() throws ExecutionException, InterruptedException { |
| 166 | + initializeExpectedMessage(1); |
| 167 | + expect(pubsub.options()).andReturn(mockOptions); |
| 168 | + expect(pubsub.modifyAckDeadlineAsync(SUBSCRIPTION, 10, TimeUnit.SECONDS, ACK_ID)) |
| 169 | + .andReturn(Futures.<Void>immediateFuture(null)); |
| 170 | + EasyMock.expectLastCall(); |
| 171 | + replay(pubsub); |
| 172 | + initializeMessage(); |
| 173 | + assertNull(message.modifyAckDeadlineAsync(10, TimeUnit.SECONDS).get()); |
| 174 | + } |
| 175 | + |
| 176 | + private void compareReceivedMessage(ReceivedMessage expected, ReceivedMessage value) { |
| 177 | + assertEquals(expected, value); |
| 178 | + assertEquals(expected.id(), value.id()); |
| 179 | + assertEquals(expected.payload(), value.payload()); |
| 180 | + assertEquals(expected.payloadAsString(), value.payloadAsString()); |
| 181 | + assertEquals(expected.attributes(), value.attributes()); |
| 182 | + assertEquals(expected.publishTime(), value.publishTime()); |
| 183 | + assertEquals(expected.ackId(), value.ackId()); |
| 184 | + assertEquals(expected.subscription(), value.subscription()); |
| 185 | + assertEquals(expected.hashCode(), value.hashCode()); |
| 186 | + } |
| 187 | +} |
0 commit comments