1 /* 2 * Copyright 2016 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.dns; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotEquals; 21 import static org.junit.Assert.assertNull; 22 23 import java.math.BigInteger; 24 import org.junit.Test; 25 26 public class ProjectInfoTest { 27 28 private static final String ID = "project-id-123"; 29 private static final BigInteger NUMBER = new BigInteger("123"); 30 private static final ProjectInfo.Quota QUOTA = new ProjectInfo.Quota(1, 2, 3, 4, 5, 6); 31 private static final ProjectInfo PROJECT_INFO = 32 ProjectInfo.newBuilder().setId(ID).setNumber(NUMBER).setQuota(QUOTA).build(); 33 34 @Test testBuilder()35 public void testBuilder() { 36 ProjectInfo withId = ProjectInfo.newBuilder().setId(ID).build(); 37 assertEquals(ID, withId.getId()); 38 assertNull(withId.getNumber()); 39 assertNull(withId.getQuota()); 40 ProjectInfo withNumber = ProjectInfo.newBuilder().setNumber(NUMBER).build(); 41 assertEquals(NUMBER, withNumber.getNumber()); 42 assertNull(withNumber.getQuota()); 43 assertNull(withNumber.getId()); 44 ProjectInfo withQuota = ProjectInfo.newBuilder().setQuota(QUOTA).build(); 45 assertEquals(QUOTA, withQuota.getQuota()); 46 assertNull(withQuota.getId()); 47 assertNull(withQuota.getNumber()); 48 assertEquals(QUOTA, PROJECT_INFO.getQuota()); 49 assertEquals(NUMBER, PROJECT_INFO.getNumber()); 50 assertEquals(ID, PROJECT_INFO.getId()); 51 } 52 53 @Test testQuotaConstructor()54 public void testQuotaConstructor() { 55 assertEquals(1, QUOTA.getZones()); 56 assertEquals(2, QUOTA.getResourceRecordsPerRrset()); 57 assertEquals(3, QUOTA.getRrsetAdditionsPerChange()); 58 assertEquals(4, QUOTA.getRrsetDeletionsPerChange()); 59 assertEquals(5, QUOTA.getRrsetsPerZone()); 60 assertEquals(6, QUOTA.getTotalRrdataSizePerChange()); 61 } 62 63 @Test testEqualsAndNotEqualsQuota()64 public void testEqualsAndNotEqualsQuota() { 65 ProjectInfo.Quota clone = new ProjectInfo.Quota(6, 5, 4, 3, 2, 1); 66 assertNotEquals(QUOTA, clone); 67 clone = ProjectInfo.Quota.fromPb(QUOTA.toPb()); 68 assertEquals(QUOTA, clone); 69 } 70 71 @Test testSameHashCodeOnEqualsQuota()72 public void testSameHashCodeOnEqualsQuota() { 73 ProjectInfo.Quota clone = ProjectInfo.Quota.fromPb(QUOTA.toPb()); 74 assertEquals(QUOTA, clone); 75 assertEquals(QUOTA.hashCode(), clone.hashCode()); 76 } 77 78 @Test testEqualsAndNotEquals()79 public void testEqualsAndNotEquals() { 80 ProjectInfo clone = ProjectInfo.newBuilder().build(); 81 assertNotEquals(PROJECT_INFO, clone); 82 clone = 83 ProjectInfo.newBuilder() 84 .setId(PROJECT_INFO.getId()) 85 .setNumber(PROJECT_INFO.getNumber()) 86 .build(); 87 assertNotEquals(PROJECT_INFO, clone); 88 clone = 89 ProjectInfo.newBuilder() 90 .setId(PROJECT_INFO.getId()) 91 .setQuota(PROJECT_INFO.getQuota()) 92 .build(); 93 assertNotEquals(PROJECT_INFO, clone); 94 clone = 95 ProjectInfo.newBuilder() 96 .setNumber(PROJECT_INFO.getNumber()) 97 .setQuota(PROJECT_INFO.getQuota()) 98 .build(); 99 assertNotEquals(PROJECT_INFO, clone); 100 clone = ProjectInfo.fromPb(PROJECT_INFO.toPb()); 101 assertEquals(PROJECT_INFO, clone); 102 } 103 104 @Test testSameHashCodeOnEquals()105 public void testSameHashCodeOnEquals() { 106 ProjectInfo clone = ProjectInfo.fromPb(PROJECT_INFO.toPb()); 107 assertEquals(PROJECT_INFO, clone); 108 assertEquals(PROJECT_INFO.hashCode(), clone.hashCode()); 109 } 110 111 @Test testToAndFromPb()112 public void testToAndFromPb() { 113 assertEquals(PROJECT_INFO, ProjectInfo.fromPb(PROJECT_INFO.toPb())); 114 ProjectInfo partial = ProjectInfo.newBuilder().setId(ID).build(); 115 assertEquals(partial, ProjectInfo.fromPb(partial.toPb())); 116 partial = ProjectInfo.newBuilder().setNumber(NUMBER).build(); 117 assertEquals(partial, ProjectInfo.fromPb(partial.toPb())); 118 partial = ProjectInfo.newBuilder().setQuota(QUOTA).build(); 119 assertEquals(partial, ProjectInfo.fromPb(partial.toPb())); 120 assertNotEquals(PROJECT_INFO, partial); 121 } 122 123 @Test testToAndFromPbQuota()124 public void testToAndFromPbQuota() { 125 assertEquals(QUOTA, ProjectInfo.Quota.fromPb(QUOTA.toPb())); 126 ProjectInfo.Quota wrong = new ProjectInfo.Quota(5, 6, 3, 6, 2, 1); 127 assertNotEquals(QUOTA, ProjectInfo.Quota.fromPb(wrong.toPb())); 128 } 129 } 130