Skip to content

Commit bb92e07

Browse files
#52 some unit tests around objectMasks, groundwork for withMask() improvements
1 parent 18ff779 commit bb92e07

File tree

6 files changed

+214
-30
lines changed

6 files changed

+214
-30
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

src/main/java/com/softlayer/api/Mask.java

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.HashSet;
55
import java.util.Map;
66
import java.util.Set;
7-
import java.util.regex.Pattern;
87

98
/** Object mask parameter. See http://sldn.softlayer.com/article/Object-Masks */
109
public class Mask {
@@ -45,32 +44,7 @@ protected String getMask() {
4544

4645
@Override
4746
public String toString() {
48-
String subMask = new String();
49-
String objectMask = new String();
50-
String builtMask = toString(new StringBuilder()).toString();
51-
if(builtMask.contains("[")){
52-
String [] mask = builtMask.split(Pattern.quote("["));
53-
for (int count = 0; count < mask.length; count ++ ) {
54-
if (count != 0){
55-
subMask = new StringBuilder().append(subMask).append("[").append(mask[count]).toString();
56-
}
57-
}
58-
objectMask = subMask.substring(1, subMask.length());
59-
}
60-
else {
61-
if(builtMask.contains(".")){
62-
String [] mask = builtMask.split(Pattern.quote("."));
63-
for (int count = 0; count < mask.length; count ++ ) {
64-
if (count != 0){
65-
objectMask = new StringBuilder().append(objectMask).append(mask[count]).append(".").toString();
66-
}
67-
}
68-
}else {
69-
objectMask = builtMask + ".";
70-
}
71-
}
72-
String resultMask = objectMask.substring(0, objectMask.length()-1);
73-
return resultMask;
47+
return toString(new StringBuilder()).toString();
7448
}
7549

7650
/** Append this mask's string representation to the given builder and return it */

src/main/java/com/softlayer/api/RestApiClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ public Object logAndHandleResponse(HttpResponse response, String url,
304304
}
305305

306306
public Object invokeService(Method method, final Object[] args) throws Throwable {
307+
System.out.print("invokeService: " + method + "\n");
307308
ApiMethod methodInfo = method.getAnnotation(ApiMethod.class);
308309
// Must have ID if instance is required
309310
if (methodInfo.instanceRequired() && id == null) {
@@ -440,7 +441,7 @@ public boolean isDone() {
440441
@Override
441442
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
442443
boolean noParams = args == null || args.length == 0;
443-
444+
System.out.print("Invoking: " + method.getName() + "\n");
444445
if ("asAsync".equals(method.getName()) && noParams) {
445446
ServiceProxy<S> asyncProxy = new ServiceProxy<>(serviceClass, id);
446447
asyncProxy.mask = mask;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.softlayer.api;
2+
3+
import com.softlayer.api.annotation.ApiMethod;
4+
import com.softlayer.api.annotation.ApiService;
5+
import com.softlayer.api.http.HttpBasicAuthCredentials;
6+
import com.softlayer.api.http.HttpClient;
7+
import com.softlayer.api.http.HttpResponse;
8+
9+
import java.lang.reflect.InvocationHandler;
10+
import java.lang.reflect.Method;
11+
import java.lang.reflect.Proxy;
12+
13+
public class TestApiClient extends RestApiClient{
14+
15+
16+
private HttpBasicAuthCredentials credentials;
17+
public HttpBasicAuthCredentials getCredentials() {
18+
return credentials;
19+
}
20+
21+
public TestApiClient(String baseUrl) {
22+
super(baseUrl);
23+
}
24+
25+
public <S extends Service> S createService(Class<S> serviceClass, String id) {
26+
return (S) Proxy.newProxyInstance(getClass().getClassLoader(),
27+
new Class<?>[] { serviceClass }, new TestServiceProxy<>(serviceClass, id));
28+
}
29+
30+
class TestServiceProxy<S extends Service> extends ServiceProxy {
31+
32+
final Class<S> serviceClass;
33+
final String id;
34+
Mask mask;
35+
String maskString;
36+
ResultLimit resultLimit;
37+
Integer lastResponseTotalItemCount;
38+
39+
public TestServiceProxy(Class serviceClass, String id) {
40+
super(serviceClass, id);
41+
this.serviceClass = serviceClass;
42+
this.id = id;
43+
44+
}
45+
46+
public String invokeService(Method method, final Object[] args) throws Throwable {
47+
System.out.print("invokeTESTService: " + method + "\n");
48+
ApiMethod methodInfo = method.getAnnotation(ApiMethod.class);
49+
// Must have ID if instance is required
50+
if (methodInfo.instanceRequired() && id == null) {
51+
throw new IllegalStateException("ID is required to invoke " + method);
52+
}
53+
String methodName = methodInfo.value().isEmpty() ? method.getName() : methodInfo.value();
54+
final String httpMethod = getHttpMethodFromMethodName(methodName);
55+
String methodId = methodInfo.instanceRequired() ? this.id : null;
56+
final String url = getFullUrl(serviceClass.getAnnotation(ApiService.class).value(),
57+
methodName, methodId, resultLimit, mask == null ? maskString : mask.getMask());
58+
return url;
59+
}
60+
}
61+
}

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

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void testSetObjectMask() throws Exception {
7070
}
7171

7272
@Test
73-
public void testSetStringMask() throws Exception {
73+
public void testSetStringMask() {
7474
FakeHttpClientFactory http = new FakeHttpClientFactory(200,
7575
Collections.emptyMap(), "\"some response\"");
7676
RestApiClient client = new RestApiClient("http://example.com/")
@@ -104,5 +104,60 @@ public void testMaskRemoval() {
104104
service.clearMask();
105105
assertEquals("", service.withMask().toString());
106106
}
107+
108+
@Test
109+
public void testRecursiveMaskandLocal() {
110+
RestApiClient client = new RestApiClient("http://example.com/");
111+
TestEntity.Service service = TestEntity.service(client);
112+
service.withMask().recursiveProperty().recursiveProperty().baz();
113+
service.withMask().recursiveProperty().recursiveProperty().foo();
114+
service.withMask().recursiveProperty().date();
115+
assertEquals("recursiveProperty[date,recursiveProperty[foo,baz]]",
116+
service.withMask().toString());
117+
}
118+
119+
@Test
120+
public void testRecursiveMask() {
121+
RestApiClient client = new RestApiClient("http://example.com/");
122+
TestEntity.Service service = TestEntity.service(client);
123+
service.withMask().recursiveProperty().baz();
124+
service.withMask().recursiveProperty().foo();
125+
service.withMask().recursiveProperty().date();
126+
127+
assertEquals("recursiveProperty[date,foo,baz]",
128+
service.withMask().toString());
129+
}
130+
131+
@Test
132+
public void testMultiLevelMask() {
133+
RestApiClient client = new RestApiClient("http://example.com/");
134+
TestEntity.Service service = TestEntity.service(client);
135+
service.withMask().recursiveProperty().baz();
136+
service.withMask().recursiveProperty().foo();
137+
138+
service.withMask().moreChildren().recursiveProperty().baz();
139+
service.withMask().moreChildren().date();
140+
141+
assertEquals("moreChildren[date,recursiveProperty.baz],recursiveProperty[foo,baz]",
142+
service.withMask().toString());
143+
}
144+
145+
@Test
146+
public void testChangeMaskScope() {
147+
RestApiClient client = new TestApiClient("http://example.com/");
148+
client.setLoggingEnabled(true);
149+
System.out.print("Hello");
150+
TestEntity.Service service = TestEntity.service(client);
151+
service.withMask().recursiveProperty().baz();
152+
service.withMask().recursiveProperty().foo();
153+
154+
String result = service.getRecursiveProperty();
155+
System.out.print(result);
156+
// RestApiClient.ServiceProxy serviceProxy = client.createService(TestEntity, 1234);
157+
// serviceProxy.invoke(service, service.getRecursiveProperty(),null);
158+
// assertEquals("http://example.com/SomeService/1234/someMethod.json?objectMask=someMask%26%26",
159+
// client.ServiceProxy.
160+
// )
161+
}
107162
}
108163

src/test/java/com/softlayer/api/service/TestEntity.java

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import java.util.concurrent.Future;
77

88
import com.softlayer.api.ApiClient;
9+
import com.softlayer.api.Mask;
910
import com.softlayer.api.ResponseHandler;
11+
import com.softlayer.api.ResultLimit;
1012
import com.softlayer.api.annotation.ApiMethod;
1113
import com.softlayer.api.annotation.ApiProperty;
1214
import com.softlayer.api.annotation.ApiService;
@@ -113,6 +115,15 @@ public List<TestEntity> getMoreChildren() {
113115
return moreChildren;
114116
}
115117

118+
@ApiProperty
119+
protected List<TestEntity> recursiveProperty;
120+
121+
public List<TestEntity> getrecursiveProperty() {
122+
if (recursiveProperty == null) {
123+
recursiveProperty = new ArrayList<TestEntity>();
124+
}
125+
return recursiveProperty;
126+
}
116127
public Service asService(ApiClient client) {
117128
return service(client, id);
118129
}
@@ -141,6 +152,9 @@ public static interface Service extends com.softlayer.api.Service {
141152

142153
@ApiMethod(instanceRequired = true)
143154
public Void doSomethingNonStatic(GregorianCalendar param1);
155+
156+
@ApiMethod("getRecursiveProperty")
157+
public String getRecursiveProperty();
144158
}
145159

146160
public static interface ServiceAsync extends com.softlayer.api.ServiceAsync {
@@ -162,7 +176,7 @@ public static interface ServiceAsync extends com.softlayer.api.ServiceAsync {
162176
public static class Mask extends Entity.Mask {
163177

164178
public Mask foo() {
165-
withLocalProperty("bar");
179+
withLocalProperty("foo");
166180
return this;
167181
}
168182

@@ -183,5 +197,83 @@ public Mask child() {
183197
public Mask moreChildren() {
184198
return withSubMask("moreChildren", Mask.class);
185199
}
200+
201+
public Mask recursiveProperty() {
202+
return withSubMask("recursiveProperty", Mask.class);
203+
}
204+
}
205+
206+
public class TestService implements Service {
207+
208+
@Override
209+
public ServiceAsync asAsync() {
210+
return null;
211+
}
212+
213+
@Override
214+
public Mask withNewMask() {
215+
return null;
216+
}
217+
218+
@Override
219+
public Mask withMask() {
220+
return null;
221+
}
222+
223+
@Override
224+
public void setMask(com.softlayer.api.Mask mask) {
225+
226+
}
227+
228+
@Override
229+
public void setMask(String mask) {
230+
231+
}
232+
233+
@Override
234+
public void clearMask() {
235+
236+
}
237+
238+
@Override
239+
public void setMask(Mask mask) {
240+
241+
}
242+
243+
@Override
244+
public String doSomethingStatic(Long param1, TestEntity param2) {
245+
return null;
246+
}
247+
248+
@Override
249+
public List<TestEntity> fakeName() {
250+
return null;
251+
}
252+
253+
@Override
254+
public Void doSomethingNonStatic(GregorianCalendar param1) {
255+
return null;
256+
}
257+
258+
@Override
259+
public String getRecursiveProperty() {
260+
System.out.print("This is playing");
261+
return "Thats playing to win";
262+
}
263+
264+
@Override
265+
public ResultLimit getResultLimit() {
266+
return null;
267+
}
268+
269+
@Override
270+
public ResultLimit setResultLimit(ResultLimit limit) {
271+
return null;
272+
}
273+
274+
@Override
275+
public Integer getLastResponseTotalItemCount() {
276+
return null;
277+
}
186278
}
187279
}

0 commit comments

Comments
 (0)