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.assertFalse; 20 import static org.junit.Assert.assertSame; 21 import static org.junit.Assert.assertTrue; 22 import static org.junit.Assert.fail; 23 24 import com.google.cloud.BatchResult; 25 import java.io.IOException; 26 import org.easymock.EasyMock; 27 import org.junit.Before; 28 import org.junit.Test; 29 30 public class DnsBatchResultTest { 31 32 private DnsBatchResult<Boolean> result; 33 34 @Before setUp()35 public void setUp() { 36 result = new DnsBatchResult<>(); 37 } 38 39 @Test testSuccess()40 public void testSuccess() { 41 assertFalse(result.completed()); 42 try { 43 result.get(); 44 fail("This was not completed yet."); 45 } catch (IllegalStateException ex) { 46 // expected 47 } 48 result.success(true); 49 assertTrue(result.get()); 50 } 51 52 @Test testError()53 public void testError() { 54 assertFalse(result.completed()); 55 try { 56 result.get(); 57 fail("This was not completed yet."); 58 } catch (IllegalStateException ex) { 59 // expected 60 } 61 DnsException ex = new DnsException(new IOException("some error"), true); 62 result.error(ex); 63 try { 64 result.get(); 65 fail("This is a failed operation and should have thrown a DnsException."); 66 } catch (DnsException real) { 67 assertSame(ex, real); 68 } 69 } 70 71 @Test testNotifyError()72 public void testNotifyError() { 73 DnsException ex = new DnsException(new IOException("some error"), false); 74 assertFalse(result.completed()); 75 BatchResult.Callback<Boolean, DnsException> callback = 76 EasyMock.createStrictMock(BatchResult.Callback.class); 77 callback.error(ex); 78 EasyMock.replay(callback); 79 result.notify(callback); 80 result.error(ex); 81 try { 82 result.notify(callback); 83 fail("The batch has been completed."); 84 } catch (IllegalStateException exception) { 85 // expected 86 } 87 EasyMock.verify(callback); 88 } 89 90 @Test testNotifySuccess()91 public void testNotifySuccess() { 92 assertFalse(result.completed()); 93 BatchResult.Callback<Boolean, DnsException> callback = 94 EasyMock.createStrictMock(BatchResult.Callback.class); 95 callback.success(true); 96 EasyMock.replay(callback); 97 result.notify(callback); 98 result.success(true); 99 try { 100 result.notify(callback); 101 fail("The batch has been completed."); 102 } catch (IllegalStateException exception) { 103 // expected 104 } 105 EasyMock.verify(callback); 106 } 107 } 108