Skip to content

Commit 1ab674f

Browse files
Merge pull request #57 from softlayer/FernandoOjeda-fo_mask_resultLimit
Unit tests for ObjectMask
2 parents 28ca195 + c3d5354 commit 1ab674f

File tree

5 files changed

+395
-118
lines changed

5 files changed

+395
-118
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
/gen/build.log
1010
/examples/target
1111
settings.xml
12+
.DS_Store
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+

src/test/java/com/softlayer/api/RestApiClientTest.java

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
import java.io.ByteArrayOutputStream;
66
import java.io.PrintStream;
77
import java.lang.reflect.Proxy;
8-
import java.net.URLEncoder;
98
import java.util.Collections;
109
import java.util.GregorianCalendar;
11-
import java.util.List;
1210
import java.util.concurrent.Callable;
1311
import java.util.concurrent.ExecutionException;
1412
import java.util.concurrent.atomic.AtomicBoolean;
@@ -313,68 +311,7 @@ public void testDifferentMethodName() throws Exception {
313311
assertEquals(RestApiClient.HEADERS, http.headers);
314312
assertTrue(http.invokeSyncCalled);
315313
}
316-
317-
@Test
318-
public void testWithMask() throws Exception {
319-
FakeHttpClientFactory http = new FakeHttpClientFactory(200,
320-
Collections.emptyMap(), "\"some response\"");
321-
RestApiClient client = new RestApiClient("http://example.com/")
322-
.withCredentials("user", "key");
323-
client.setHttpClientFactory(http);
324314

325-
TestEntity entity = new TestEntity();
326-
entity.setFoo("blah");
327-
TestEntity.Service service = TestEntity.service(client);
328-
service.withMask().foo().child().date();
329-
service.withMask().child().baz();
330-
331-
assertEquals("some response", service.doSomethingStatic(123L, entity));
332-
assertEquals("http://example.com/SoftLayer_TestEntity/doSomethingStatic.json"
333-
+ "?objectMask=" + URLEncoder.encode(service.withMask().getMask(), "UTF-8"), http.fullUrl);
334-
assertTrue(http.invokeSyncCalled);
335-
}
336-
337-
@Test
338-
public void testSetObjectMask() throws Exception {
339-
FakeHttpClientFactory http = new FakeHttpClientFactory(200,
340-
Collections.emptyMap(), "\"some response\"");
341-
RestApiClient client = new RestApiClient("http://example.com/")
342-
.withCredentials("user", "key");
343-
client.setHttpClientFactory(http);
344-
345-
TestEntity entity = new TestEntity();
346-
entity.setFoo("blah");
347-
TestEntity.Service service = TestEntity.service(client);
348-
TestEntity.Mask mask = new TestEntity.Mask();
349-
mask.foo().child().date();
350-
mask.child().baz();
351-
service.setMask(mask);
352-
353-
assertEquals("some response", service.doSomethingStatic(123L, entity));
354-
assertEquals("http://example.com/SoftLayer_TestEntity/doSomethingStatic.json"
355-
+ "?objectMask=" + URLEncoder.encode(mask.getMask(), "UTF-8"), http.fullUrl);
356-
assertTrue(http.invokeSyncCalled);
357-
}
358-
359-
@Test
360-
public void testSetStringMask() throws Exception {
361-
FakeHttpClientFactory http = new FakeHttpClientFactory(200,
362-
Collections.emptyMap(), "\"some response\"");
363-
RestApiClient client = new RestApiClient("http://example.com/")
364-
.withCredentials("user", "key");
365-
client.setHttpClientFactory(http);
366-
367-
TestEntity entity = new TestEntity();
368-
entity.setFoo("blah");
369-
TestEntity.Service service = TestEntity.service(client);
370-
service.setMask("yay-a-mask");
371-
372-
assertEquals("some response", service.doSomethingStatic(123L, entity));
373-
assertEquals("http://example.com/SoftLayer_TestEntity/doSomethingStatic.json"
374-
+ "?objectMask=yay-a-mask", http.fullUrl);
375-
assertTrue(http.invokeSyncCalled);
376-
}
377-
378315
@Test
379316
public void testWithResultLimit() throws Exception {
380317
FakeHttpClientFactory http = new FakeHttpClientFactory(200,
@@ -461,23 +398,6 @@ public void onSuccess(String value) {
461398
assertTrue(successCalled.get());
462399
}
463400

464-
@Test(expected = IllegalArgumentException.class)
465-
public void testMaskMustNotBeNull() {
466-
RestApiClient client = new RestApiClient("http://example.com/");
467-
TestEntity.Service service = TestEntity.service(client);
468-
service.setMask((Mask) null);
469-
}
470-
471-
@Test
472-
public void testMaskRemoval() {
473-
RestApiClient client = new RestApiClient("http://example.com/");
474-
TestEntity.Service service = TestEntity.service(client);
475-
service.withMask().baz();
476-
assertEquals("baz", service.withMask().toString());
477-
service.clearMask();
478-
assertEquals("", service.withMask().toString());
479-
}
480-
481401
@Test
482402
public void testNormalObjectMethodsOnService() {
483403
RestApiClient client = new RestApiClient("http://example.com/");

0 commit comments

Comments
 (0)