|
| 1 | +package com.softlayer.api; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | + |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.PrintStream; |
| 8 | +import java.lang.reflect.Proxy; |
| 9 | +import java.net.URLEncoder; |
| 10 | +import java.util.Collections; |
| 11 | +import java.util.GregorianCalendar; |
| 12 | +import java.util.List; |
| 13 | +import java.util.concurrent.Callable; |
| 14 | +import java.util.concurrent.ExecutionException; |
| 15 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 16 | + |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +import com.softlayer.api.http.FakeHttpClientFactory; |
| 20 | +import com.softlayer.api.http.HttpBasicAuthCredentials; |
| 21 | +import com.softlayer.api.json.GsonJsonMarshallerFactoryTest; |
| 22 | +import com.softlayer.api.service.TestEntity; |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +public class MaskTest { |
| 27 | + static { |
| 28 | + GsonJsonMarshallerFactoryTest.addTestEntityToGson(); |
| 29 | + } |
| 30 | + @Test |
| 31 | + public void testWithMask() throws Exception { |
| 32 | + FakeHttpClientFactory http = new FakeHttpClientFactory(200, |
| 33 | + Collections.emptyMap(), "\"some response\""); |
| 34 | + RestApiClient client = new RestApiClient("http://example.com/") |
| 35 | + .withCredentials("user", "key"); |
| 36 | + client.setHttpClientFactory(http); |
| 37 | + |
| 38 | + TestEntity entity = new TestEntity(); |
| 39 | + entity.setFoo("blah"); |
| 40 | + TestEntity.Service service = TestEntity.service(client); |
| 41 | + service.withMask().foo().child().date(); |
| 42 | + service.withMask().child().baz(); |
| 43 | + |
| 44 | + assertEquals("some response", service.doSomethingStatic(123L, entity)); |
| 45 | + assertEquals("http://example.com/SoftLayer_TestEntity/doSomethingStatic.json" |
| 46 | + + "?objectMask=" + URLEncoder.encode(service.withMask().getMask(), "UTF-8"), http.fullUrl); |
| 47 | + assertTrue(http.invokeSyncCalled); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testSetObjectMask() throws Exception { |
| 52 | + FakeHttpClientFactory http = new FakeHttpClientFactory(200, |
| 53 | + Collections.emptyMap(), "\"some response\""); |
| 54 | + RestApiClient client = new RestApiClient("http://example.com/") |
| 55 | + .withCredentials("user", "key"); |
| 56 | + client.setHttpClientFactory(http); |
| 57 | + |
| 58 | + TestEntity entity = new TestEntity(); |
| 59 | + entity.setFoo("blah"); |
| 60 | + TestEntity.Service service = TestEntity.service(client); |
| 61 | + TestEntity.Mask mask = new TestEntity.Mask(); |
| 62 | + mask.foo().child().date(); |
| 63 | + mask.child().baz(); |
| 64 | + service.setMask(mask); |
| 65 | + |
| 66 | + assertEquals("some response", service.doSomethingStatic(123L, entity)); |
| 67 | + assertEquals("http://example.com/SoftLayer_TestEntity/doSomethingStatic.json" |
| 68 | + + "?objectMask=" + URLEncoder.encode(mask.getMask(), "UTF-8"), http.fullUrl); |
| 69 | + assertTrue(http.invokeSyncCalled); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testSetStringMask() throws Exception { |
| 74 | + FakeHttpClientFactory http = new FakeHttpClientFactory(200, |
| 75 | + Collections.emptyMap(), "\"some response\""); |
| 76 | + RestApiClient client = new RestApiClient("http://example.com/") |
| 77 | + .withCredentials("user", "key"); |
| 78 | + client.setHttpClientFactory(http); |
| 79 | + |
| 80 | + TestEntity entity = new TestEntity(); |
| 81 | + entity.setFoo("blah"); |
| 82 | + TestEntity.Service service = TestEntity.service(client); |
| 83 | + service.setMask("yay-a-mask"); |
| 84 | + |
| 85 | + assertEquals("some response", service.doSomethingStatic(123L, entity)); |
| 86 | + assertEquals("http://example.com/SoftLayer_TestEntity/doSomethingStatic.json" |
| 87 | + + "?objectMask=yay-a-mask", http.fullUrl); |
| 88 | + assertTrue(http.invokeSyncCalled); |
| 89 | + } |
| 90 | + |
| 91 | + @Test(expected = IllegalArgumentException.class) |
| 92 | + public void testMaskMustNotBeNull() { |
| 93 | + RestApiClient client = new RestApiClient("http://example.com/"); |
| 94 | + TestEntity.Service service = TestEntity.service(client); |
| 95 | + service.setMask((Mask) null); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testMaskRemoval() { |
| 100 | + RestApiClient client = new RestApiClient("http://example.com/"); |
| 101 | + TestEntity.Service service = TestEntity.service(client); |
| 102 | + service.withMask().baz(); |
| 103 | + assertEquals("baz", service.withMask().toString()); |
| 104 | + service.clearMask(); |
| 105 | + assertEquals("", service.withMask().toString()); |
| 106 | + } |
| 107 | +} |
| 108 | + |
0 commit comments