1 /* 2 * Copyright 2017 Google LLC 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.notification; 18 19 import static org.junit.Assert.assertEquals; 20 21 import com.google.cloud.notification.NotificationInfo.PayloadFormat; 22 import com.google.common.collect.ImmutableList; 23 import com.google.common.collect.ImmutableMap; 24 import com.google.pubsub.v1.ProjectTopicName; 25 import java.util.List; 26 import java.util.Map; 27 import org.junit.Test; 28 29 public class NotificationInfoTest { 30 31 private static final String ETAG = "0xFF00"; 32 private static final String GENERATED_ID = "B/N:1"; 33 private static final String SELF_LINK = "http://storage/b/n"; 34 private static final List<String> EVENT_TYPES = 35 ImmutableList.of("OBJECT_FINALIZE", "OBJECT_METADATA_UPDATE"); 36 private static final String OBJECT_NAME_PREFIX = "index.html"; 37 private static final PayloadFormat PAYLOAD_FORMAT = PayloadFormat.JSON_API_V1; 38 private static final ProjectTopicName TOPIC = ProjectTopicName.of("myProject", "topic1"); 39 private static final Map<String, String> CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); 40 private static final NotificationInfo NOTIFICATION_INFO = 41 NotificationInfo.newBuilder(TOPIC) 42 .setEtag(ETAG) 43 .setCustomAttributes(CUSTOM_ATTRIBUTES) 44 .setSelfLink(SELF_LINK) 45 .setEventTypes(EVENT_TYPES) 46 .setObjectNamePrefix(OBJECT_NAME_PREFIX) 47 .setPayloadFormat(PAYLOAD_FORMAT) 48 .setGeneratedId(GENERATED_ID) 49 .build(); 50 51 @Test testToBuilder()52 public void testToBuilder() { 53 compareBuckets(NOTIFICATION_INFO, NOTIFICATION_INFO.toBuilder().build()); 54 NotificationInfo bucketInfo = NOTIFICATION_INFO.toBuilder().setGeneratedId("id").build(); 55 assertEquals("id", bucketInfo.getGeneratedId()); 56 bucketInfo = bucketInfo.toBuilder().setGeneratedId(GENERATED_ID).build(); 57 compareBuckets(NOTIFICATION_INFO, bucketInfo); 58 } 59 60 @Test testToBuilderIncomplete()61 public void testToBuilderIncomplete() { 62 NotificationInfo incompleteBucketInfo = 63 NotificationInfo.newBuilder(ProjectTopicName.of("myProject", "topic1")).build(); 64 compareBuckets(incompleteBucketInfo, incompleteBucketInfo.toBuilder().build()); 65 } 66 67 @Test testOf()68 public void testOf() { 69 NotificationInfo bucketInfo = NotificationInfo.of(ProjectTopicName.of("myProject", "topic1")); 70 assertEquals(ProjectTopicName.of("myProject", "topic1"), bucketInfo.getTopic()); 71 } 72 73 @Test testBuilder()74 public void testBuilder() { 75 assertEquals(ETAG, NOTIFICATION_INFO.getEtag()); 76 assertEquals(GENERATED_ID, NOTIFICATION_INFO.getGeneratedId()); 77 assertEquals(SELF_LINK, NOTIFICATION_INFO.getSelfLink()); 78 assertEquals(EVENT_TYPES, NOTIFICATION_INFO.getEventTypes()); 79 assertEquals(OBJECT_NAME_PREFIX, NOTIFICATION_INFO.getObjectNamePrefix()); 80 assertEquals(PAYLOAD_FORMAT, NOTIFICATION_INFO.getPayloadFormat()); 81 assertEquals(TOPIC, NOTIFICATION_INFO.getTopic()); 82 assertEquals(CUSTOM_ATTRIBUTES, NOTIFICATION_INFO.getCustomAttributes()); 83 } 84 85 @Test testToPbAndFromPb()86 public void testToPbAndFromPb() { 87 compareBuckets(NOTIFICATION_INFO, NotificationInfo.fromPb(NOTIFICATION_INFO.toPb())); 88 NotificationInfo bucketInfo = 89 NotificationInfo.of(ProjectTopicName.of("myProject", "topic1")) 90 .toBuilder() 91 .setPayloadFormat(PayloadFormat.NONE) 92 .build(); 93 compareBuckets(bucketInfo, NotificationInfo.fromPb(bucketInfo.toPb())); 94 } 95 compareBuckets(NotificationInfo expected, NotificationInfo value)96 private void compareBuckets(NotificationInfo expected, NotificationInfo value) { 97 assertEquals(expected, value); 98 assertEquals(expected.getGeneratedId(), value.getGeneratedId()); 99 assertEquals(expected.getCustomAttributes(), value.getCustomAttributes()); 100 assertEquals(expected.getEtag(), value.getEtag()); 101 assertEquals(expected.getSelfLink(), value.getSelfLink()); 102 assertEquals(expected.getEventTypes(), value.getEventTypes()); 103 assertEquals(expected.getObjectNamePrefix(), value.getObjectNamePrefix()); 104 assertEquals(expected.getPayloadFormat(), value.getPayloadFormat()); 105 assertEquals(expected.getTopic(), value.getTopic()); 106 } 107 } 108