|
| 1 | +package com.softlayer.api; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.net.URLEncoder; |
| 6 | +import java.util.Collections; |
| 7 | + |
| 8 | +import org.junit.Ignore; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import com.softlayer.api.http.FakeHttpClientFactory; |
| 12 | +import com.softlayer.api.json.GsonJsonMarshallerFactoryTest; |
| 13 | +import com.softlayer.api.service.TestEntity; |
| 14 | + |
| 15 | +public class MaskTest { |
| 16 | + static { |
| 17 | + GsonJsonMarshallerFactoryTest.addTestEntityToGson(); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testWithMask() throws Exception { |
| 22 | + FakeHttpClientFactory http = new FakeHttpClientFactory(200, |
| 23 | + Collections.emptyMap(), "\"some response\""); |
| 24 | + RestApiClient client = new RestApiClient("http://example.com/") |
| 25 | + .withCredentials("user", "key"); |
| 26 | + client.setHttpClientFactory(http); |
| 27 | + |
| 28 | + TestEntity entity = new TestEntity(); |
| 29 | + entity.setFoo("blah"); |
| 30 | + TestEntity.Service service = TestEntity.service(client); |
| 31 | + service.withMask().foo().child().date(); |
| 32 | + service.withMask().child().baz(); |
| 33 | + |
| 34 | + assertEquals("some response", service.doSomethingStatic(123L, entity)); |
| 35 | + assertEquals("http://example.com/SoftLayer_TestEntity/doSomethingStatic.json" |
| 36 | + + "?objectMask=" + URLEncoder.encode(service.withMask().getMask(), "UTF-8"), http.fullUrl); |
| 37 | + assertTrue(http.invokeSyncCalled); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testSetObjectMask() throws Exception { |
| 42 | + FakeHttpClientFactory http = new FakeHttpClientFactory(200, |
| 43 | + Collections.emptyMap(), "\"some response\""); |
| 44 | + RestApiClient client = new RestApiClient("http://example.com/") |
| 45 | + .withCredentials("user", "key"); |
| 46 | + client.setHttpClientFactory(http); |
| 47 | + |
| 48 | + TestEntity entity = new TestEntity(); |
| 49 | + entity.setFoo("blah"); |
| 50 | + TestEntity.Service service = TestEntity.service(client); |
| 51 | + TestEntity.Mask mask = new TestEntity.Mask(); |
| 52 | + mask.foo().child().date(); |
| 53 | + mask.child().baz(); |
| 54 | + service.setMask(mask); |
| 55 | + |
| 56 | + assertEquals("some response", service.doSomethingStatic(123L, entity)); |
| 57 | + assertEquals("http://example.com/SoftLayer_TestEntity/doSomethingStatic.json" |
| 58 | + + "?objectMask=" + URLEncoder.encode(mask.getMask(), "UTF-8"), http.fullUrl); |
| 59 | + assertTrue(http.invokeSyncCalled); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testSetStringMask() { |
| 64 | + FakeHttpClientFactory http = new FakeHttpClientFactory(200, |
| 65 | + Collections.emptyMap(), "\"some response\""); |
| 66 | + RestApiClient client = new RestApiClient("http://example.com/") |
| 67 | + .withCredentials("user", "key"); |
| 68 | + client.setHttpClientFactory(http); |
| 69 | + |
| 70 | + TestEntity entity = new TestEntity(); |
| 71 | + entity.setFoo("blah"); |
| 72 | + TestEntity.Service service = TestEntity.service(client); |
| 73 | + service.setMask("yay-a-mask"); |
| 74 | + |
| 75 | + assertEquals("some response", service.doSomethingStatic(123L, entity)); |
| 76 | + assertEquals("http://example.com/SoftLayer_TestEntity/doSomethingStatic.json" |
| 77 | + + "?objectMask=yay-a-mask", http.fullUrl); |
| 78 | + assertTrue(http.invokeSyncCalled); |
| 79 | + } |
| 80 | + |
| 81 | + @Test(expected = IllegalArgumentException.class) |
| 82 | + public void testMaskMustNotBeNull() { |
| 83 | + RestApiClient client = new RestApiClient("http://example.com/"); |
| 84 | + TestEntity.Service service = TestEntity.service(client); |
| 85 | + service.setMask((Mask) null); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testMaskRemoval() { |
| 90 | + RestApiClient client = new RestApiClient("http://example.com/"); |
| 91 | + TestEntity.Service service = TestEntity.service(client); |
| 92 | + service.withMask().baz(); |
| 93 | + assertEquals("baz", service.withMask().toString()); |
| 94 | + service.clearMask(); |
| 95 | + assertEquals("", service.withMask().toString()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testRecursiveMaskAndLocal() { |
| 100 | + RestApiClient client = new RestApiClient("http://example.com/"); |
| 101 | + TestEntity.Service service = TestEntity.service(client); |
| 102 | + service.withMask().recursiveProperty().recursiveProperty().baz(); |
| 103 | + service.withMask().recursiveProperty().recursiveProperty().foo(); |
| 104 | + service.withMask().recursiveProperty().date(); |
| 105 | + assertEquals("recursiveProperty[date,recursiveProperty[foo,baz]]", |
| 106 | + service.withMask().toString()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testRecursiveMask() { |
| 111 | + RestApiClient client = new RestApiClient("http://example.com/"); |
| 112 | + TestEntity.Service service = TestEntity.service(client); |
| 113 | + service.withMask().recursiveProperty().baz(); |
| 114 | + service.withMask().recursiveProperty().foo(); |
| 115 | + service.withMask().recursiveProperty().date(); |
| 116 | + |
| 117 | + assertEquals("recursiveProperty[date,foo,baz]", |
| 118 | + service.withMask().toString()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testMultiLevelMask() { |
| 123 | + FakeHttpClientFactory http = new FakeHttpClientFactory(200, |
| 124 | + Collections.emptyMap(),""); |
| 125 | + RestApiClient client = new RestApiClient("http://example.com/"); |
| 126 | + client.setHttpClientFactory(http); |
| 127 | + |
| 128 | + TestEntity.Service service = TestEntity.service(client); |
| 129 | + service.withMask().recursiveProperty().baz(); |
| 130 | + service.withMask().recursiveProperty().foo(); |
| 131 | + |
| 132 | + service.withMask().moreChildren().recursiveProperty().baz(); |
| 133 | + service.withMask().moreChildren().date(); |
| 134 | + String result = service.getRecursiveProperty(); |
| 135 | + |
| 136 | + assertEquals("moreChildren[date,recursiveProperty.baz],recursiveProperty[foo,baz]", |
| 137 | + service.withMask().toString()); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testNoChangeMaskScope() { |
| 142 | + FakeHttpClientFactory http = new FakeHttpClientFactory(200, |
| 143 | + Collections.emptyMap(),""); |
| 144 | + RestApiClient client = new RestApiClient("http://example.com/"); |
| 145 | + client.setHttpClientFactory(http); |
| 146 | + |
| 147 | + TestEntity.Service service = TestEntity.service(client); |
| 148 | + service.withMask().testThing().id(); |
| 149 | + service.withMask().testThing().first(); |
| 150 | + |
| 151 | + TestEntity result = service.getObject(); |
| 152 | + assertEquals("testThing[id,first]", service.withMask().toString()); |
| 153 | + String expected = "http://example.com/SoftLayer_TestEntity.json?objectMask=mask%5BtestThing%5Bid%2Cfirst%5D%5D"; |
| 154 | + assertEquals(expected, http.fullUrl); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * This doesn't work due to the issues mentioned in https://github.com/softlayer/softlayer-java/issues/19 |
| 159 | + */ |
| 160 | + @Test |
| 161 | + @Ignore |
| 162 | + public void testChangeMaskScope() { |
| 163 | + RestApiClient client = new RestApiClient("http://example.com/"); |
| 164 | + |
| 165 | + TestEntity.Service service = TestEntity.service(client); |
| 166 | + service.withMask().recursiveProperty().baz(); |
| 167 | + service.withMask().recursiveProperty().foo(); |
| 168 | + |
| 169 | + String result = service.getRecursiveProperty(); |
| 170 | + assertEquals("baz,foo", service.withMask().toString()); |
| 171 | + } |
| 172 | +} |
| 173 | + |
0 commit comments