Skip to content

Commit c3d5354

Browse files
committed
Code cleanup. The ServiceTester classes don't really work the way the client expects, so they've been removed in favor of using the FakeHttpClientFactory in place where it is needed. Additionally the TestApiClient duplicates a lot of the same code as the RestApiClient and is not flexible for service types other than the TestEntity ServiceTester class.
1 parent 21494f7 commit c3d5354

File tree

5 files changed

+97
-348
lines changed

5 files changed

+97
-348
lines changed

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

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,27 @@
22

33
import static org.junit.Assert.*;
44

5-
6-
import java.io.ByteArrayOutputStream;
7-
import java.io.PrintStream;
8-
import java.lang.reflect.Proxy;
95
import java.net.URLEncoder;
106
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;
167

8+
import org.junit.Ignore;
179
import org.junit.Test;
1810

1911
import com.softlayer.api.http.FakeHttpClientFactory;
20-
import com.softlayer.api.http.HttpBasicAuthCredentials;
2112
import com.softlayer.api.json.GsonJsonMarshallerFactoryTest;
2213
import com.softlayer.api.service.TestEntity;
2314

24-
25-
2615
public class MaskTest {
2716
static {
2817
GsonJsonMarshallerFactoryTest.addTestEntityToGson();
2918
}
19+
3020
@Test
3121
public void testWithMask() throws Exception {
3222
FakeHttpClientFactory http = new FakeHttpClientFactory(200,
33-
Collections.emptyMap(), "\"some response\"");
23+
Collections.emptyMap(), "\"some response\"");
3424
RestApiClient client = new RestApiClient("http://example.com/")
35-
.withCredentials("user", "key");
25+
.withCredentials("user", "key");
3626
client.setHttpClientFactory(http);
3727

3828
TestEntity entity = new TestEntity();
@@ -43,16 +33,16 @@ public void testWithMask() throws Exception {
4333

4434
assertEquals("some response", service.doSomethingStatic(123L, entity));
4535
assertEquals("http://example.com/SoftLayer_TestEntity/doSomethingStatic.json"
46-
+ "?objectMask=" + URLEncoder.encode(service.withMask().getMask(), "UTF-8"), http.fullUrl);
36+
+ "?objectMask=" + URLEncoder.encode(service.withMask().getMask(), "UTF-8"), http.fullUrl);
4737
assertTrue(http.invokeSyncCalled);
4838
}
4939

5040
@Test
5141
public void testSetObjectMask() throws Exception {
5242
FakeHttpClientFactory http = new FakeHttpClientFactory(200,
53-
Collections.emptyMap(), "\"some response\"");
43+
Collections.emptyMap(), "\"some response\"");
5444
RestApiClient client = new RestApiClient("http://example.com/")
55-
.withCredentials("user", "key");
45+
.withCredentials("user", "key");
5646
client.setHttpClientFactory(http);
5747

5848
TestEntity entity = new TestEntity();
@@ -65,16 +55,16 @@ public void testSetObjectMask() throws Exception {
6555

6656
assertEquals("some response", service.doSomethingStatic(123L, entity));
6757
assertEquals("http://example.com/SoftLayer_TestEntity/doSomethingStatic.json"
68-
+ "?objectMask=" + URLEncoder.encode(mask.getMask(), "UTF-8"), http.fullUrl);
58+
+ "?objectMask=" + URLEncoder.encode(mask.getMask(), "UTF-8"), http.fullUrl);
6959
assertTrue(http.invokeSyncCalled);
7060
}
7161

7262
@Test
7363
public void testSetStringMask() {
7464
FakeHttpClientFactory http = new FakeHttpClientFactory(200,
75-
Collections.emptyMap(), "\"some response\"");
65+
Collections.emptyMap(), "\"some response\"");
7666
RestApiClient client = new RestApiClient("http://example.com/")
77-
.withCredentials("user", "key");
67+
.withCredentials("user", "key");
7868
client.setHttpClientFactory(http);
7969

8070
TestEntity entity = new TestEntity();
@@ -84,20 +74,20 @@ public void testSetStringMask() {
8474

8575
assertEquals("some response", service.doSomethingStatic(123L, entity));
8676
assertEquals("http://example.com/SoftLayer_TestEntity/doSomethingStatic.json"
87-
+ "?objectMask=yay-a-mask", http.fullUrl);
77+
+ "?objectMask=yay-a-mask", http.fullUrl);
8878
assertTrue(http.invokeSyncCalled);
8979
}
9080

9181
@Test(expected = IllegalArgumentException.class)
9282
public void testMaskMustNotBeNull() {
93-
RestApiClient client = new TestApiClient("http://example.com/");
83+
RestApiClient client = new RestApiClient("http://example.com/");
9484
TestEntity.Service service = TestEntity.service(client);
9585
service.setMask((Mask) null);
9686
}
9787

9888
@Test
9989
public void testMaskRemoval() {
100-
RestApiClient client = new TestApiClient("http://example.com/");
90+
RestApiClient client = new RestApiClient("http://example.com/");
10191
TestEntity.Service service = TestEntity.service(client);
10292
service.withMask().baz();
10393
assertEquals("baz", service.withMask().toString());
@@ -107,30 +97,34 @@ public void testMaskRemoval() {
10797

10898
@Test
10999
public void testRecursiveMaskAndLocal() {
110-
RestApiClient client = new TestApiClient("http://example.com/");
100+
RestApiClient client = new RestApiClient("http://example.com/");
111101
TestEntity.Service service = TestEntity.service(client);
112102
service.withMask().recursiveProperty().recursiveProperty().baz();
113103
service.withMask().recursiveProperty().recursiveProperty().foo();
114104
service.withMask().recursiveProperty().date();
115105
assertEquals("recursiveProperty[date,recursiveProperty[foo,baz]]",
116-
service.withMask().toString());
106+
service.withMask().toString());
117107
}
118108

119109
@Test
120110
public void testRecursiveMask() {
121-
RestApiClient client = new TestApiClient("http://example.com/");
111+
RestApiClient client = new RestApiClient("http://example.com/");
122112
TestEntity.Service service = TestEntity.service(client);
123113
service.withMask().recursiveProperty().baz();
124114
service.withMask().recursiveProperty().foo();
125115
service.withMask().recursiveProperty().date();
126116

127117
assertEquals("recursiveProperty[date,foo,baz]",
128-
service.withMask().toString());
118+
service.withMask().toString());
129119
}
130120

131121
@Test
132122
public void testMultiLevelMask() {
133-
RestApiClient client = new TestApiClient("http://example.com/");
123+
FakeHttpClientFactory http = new FakeHttpClientFactory(200,
124+
Collections.emptyMap(),"");
125+
RestApiClient client = new RestApiClient("http://example.com/");
126+
client.setHttpClientFactory(http);
127+
134128
TestEntity.Service service = TestEntity.service(client);
135129
service.withMask().recursiveProperty().baz();
136130
service.withMask().recursiveProperty().foo();
@@ -140,43 +134,40 @@ public void testMultiLevelMask() {
140134
String result = service.getRecursiveProperty();
141135

142136
assertEquals("moreChildren[date,recursiveProperty.baz],recursiveProperty[foo,baz]",
143-
service.withMask().toString());
137+
service.withMask().toString());
144138
}
145139

146140
@Test
147141
public void testNoChangeMaskScope() {
148-
149-
150-
TestApiClient client = new TestApiClient("http://example.com/");
151-
152-
// client.setLoggingEnabled(true);
142+
FakeHttpClientFactory http = new FakeHttpClientFactory(200,
143+
Collections.emptyMap(),"");
144+
RestApiClient client = new RestApiClient("http://example.com/");
145+
client.setHttpClientFactory(http);
153146

154147
TestEntity.Service service = TestEntity.service(client);
155148
service.withMask().testThing().id();
156149
service.withMask().testThing().first();
157150

158-
159151
TestEntity result = service.getObject();
160152
assertEquals("testThing[id,first]", service.withMask().toString());
161153
String expected = "http://example.com/SoftLayer_TestEntity.json?objectMask=mask%5BtestThing%5Bid%2Cfirst%5D%5D";
162-
assertEquals(expected, client.httpClientFactory.fullUrl);
163-
164-
154+
assertEquals(expected, http.fullUrl);
165155
}
166156

157+
/**
158+
* This doesn't work due to the issues mentioned in https://github.com/softlayer/softlayer-java/issues/19
159+
*/
167160
@Test
161+
@Ignore
168162
public void testChangeMaskScope() {
169-
// https://github.com/softlayer/softlayer-java/issues/19
170-
171-
TestApiClient client = new TestApiClient("http://example.com/");
172-
// client.setLoggingEnabled(true);
163+
RestApiClient client = new RestApiClient("http://example.com/");
173164

174165
TestEntity.Service service = TestEntity.service(client);
175166
service.withMask().recursiveProperty().baz();
176167
service.withMask().recursiveProperty().foo();
177168

178169
String result = service.getRecursiveProperty();
179-
// assertEquals("baz,foo", service.withMask());
170+
assertEquals("baz,foo", service.withMask().toString());
180171
}
181172
}
182173

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

Lines changed: 0 additions & 4 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;
@@ -400,8 +398,6 @@ public void onSuccess(String value) {
400398
assertTrue(successCalled.get());
401399
}
402400

403-
404-
405401
@Test
406402
public void testNormalObjectMethodsOnService() {
407403
RestApiClient client = new RestApiClient("http://example.com/");

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

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)