Skip to content

Commit 84cf905

Browse files
committed
added test cases
1 parent ce39525 commit 84cf905

File tree

10 files changed

+347
-87
lines changed

10 files changed

+347
-87
lines changed

src/main/java/org/codebrewery/AbstractRESTModel.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.ning.http.client.AsyncCompletionHandler;
44
import com.ning.http.client.AsyncHttpClient;
5-
import com.ning.http.client.ListenableFuture;
65
import com.ning.http.client.Response;
76
import org.json.simple.JSONObject;
87
import org.json.simple.parser.JSONParser;
@@ -26,6 +25,8 @@
2625
*/
2726
public abstract class AbstractRESTModel implements CrudModelInterface {
2827

28+
private RestModelConfiguration config = new RestModelConfiguration();
29+
2930
/**
3031
* This method returns the base url for a resource.
3132
*
@@ -56,32 +57,39 @@ public abstract class AbstractRESTModel implements CrudModelInterface {
5657
public abstract AbstractRESTModel parse(JSONObject response) throws IOException;
5758

5859

59-
private String generateFullHttpUrl() {
60+
protected String generateCollectionUrl() {
6061

61-
return "http://localhost:8081"+resourceUrl();
62+
return config.getRequestBaseUrl() + "/" + resourceUrl();
6263

6364
}
6465

65-
private JSONObject convertResponseToJSONObject(Response response) throws IOException, ParseException {
66+
protected String generatedInstanceUrl() {
6667

67-
return (JSONObject)new JSONParser().parse(response.getResponseBody());
68+
return generateCollectionUrl() + "/" + identifierValue();
6869

6970
}
7071

71-
public void fetch(final ActionCompletedInterface actions) throws ExecutionException, InterruptedException {
72+
public void setConfiguration( RestModelConfiguration config ) {
7273

74+
this.config = config;
7375

74-
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
75-
String urlToUse = generateFullHttpUrl() + identifierValue();
76-
asyncHttpClient.prepareGet(urlToUse).execute(new AsyncCompletionHandler() {
76+
}
77+
protected JSONObject convertResponseToJSONObject(Response response) throws IOException, ParseException {
78+
79+
return (JSONObject)new JSONParser().parse(response.getResponseBody());
80+
81+
}
82+
83+
protected AsyncCompletionHandler getAsyncCompletionHandler(ActionCompletedInterface actions) {
84+
85+
return new AsyncCompletionHandler(){
7786

7887
@Override
7988
public Object onCompleted(Response response) throws Exception {
8089

8190

8291
actions.onDone(parse(convertResponseToJSONObject(response)));
8392

84-
// is it possible to not return void?
8593
return null;
8694
}
8795

@@ -91,7 +99,18 @@ public void onThrowable(Throwable t) {
9199
actions.onError(t);
92100

93101
}
94-
});
102+
};
103+
104+
}
105+
106+
107+
public void fetch(final ActionCompletedInterface actions) throws ExecutionException, InterruptedException {
108+
109+
110+
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
111+
String urlToUse = generatedInstanceUrl();
112+
asyncHttpClient.prepareGet(urlToUse).execute(getAsyncCompletionHandler(actions));
113+
95114

96115
}
97116

src/main/java/org/codebrewery/App.java

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

src/main/java/org/codebrewery/ClientConfigurer.java

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

src/main/java/org/codebrewery/ClientConfigurerInterface.java

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.codebrewery;
2+
3+
/**
4+
* Created by jepp3 on 2015-09-06.
5+
*/
6+
public class RestModelConfiguration {
7+
8+
9+
public String getRequestBaseUrl(){
10+
11+
return getBaseUrl() + ":" + getPort() + "/" + getApiLocation();
12+
13+
}
14+
15+
public String getBaseUrl() {
16+
17+
return "http://localhost";
18+
19+
}
20+
21+
public String getPort() {
22+
23+
return "8081";
24+
25+
}
26+
27+
public String getApiLocation() {
28+
29+
return "api";
30+
31+
}
32+
}

src/test/java/org/codebrewery/DogModel.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ public class DogModel extends AbstractRESTModel {
1515

1616
public final int age;
1717

18-
public DogModel() {
19-
20-
name = "pluto";
21-
age = 5;
22-
}
2318

2419
public DogModel(String name, int age) {
2520
this.name = name;
@@ -46,6 +41,7 @@ public final AbstractRESTModel parse(JSONObject responseJson) {
4641
return new DogModel(name,123);
4742

4843
}
44+
4945
// dedicated method
5046
public String bark() {
5147
return "VOFF says" + this.name;
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package org.codebrewery;
2+
3+
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
4+
import com.ning.http.client.Response;
5+
import com.ning.http.client.cookie.Cookie;
6+
import com.ning.http.client.uri.Uri;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.nio.ByteBuffer;
11+
import java.util.List;
12+
13+
/**
14+
* Created by jepp3 on 2015-09-06.
15+
*/
16+
public class MockResponse implements Response {
17+
18+
@Override
19+
public int getStatusCode() {
20+
return 0;
21+
}
22+
23+
@Override
24+
public String getStatusText() {
25+
return null;
26+
}
27+
28+
@Override
29+
public byte[] getResponseBodyAsBytes() throws IOException {
30+
return new byte[0];
31+
}
32+
33+
@Override
34+
public ByteBuffer getResponseBodyAsByteBuffer() throws IOException {
35+
return null;
36+
}
37+
38+
@Override
39+
public InputStream getResponseBodyAsStream() throws IOException {
40+
return null;
41+
}
42+
43+
@Override
44+
public String getResponseBodyExcerpt(int i, String s) throws IOException {
45+
return null;
46+
}
47+
48+
@Override
49+
public String getResponseBody(String s) throws IOException {
50+
return null;
51+
}
52+
53+
@Override
54+
public String getResponseBodyExcerpt(int i) throws IOException {
55+
return null;
56+
}
57+
58+
@Override
59+
public String getResponseBody() throws IOException {
60+
return "{\"name\":\"pluto\"}";
61+
}
62+
63+
@Override
64+
public Uri getUri() {
65+
return null;
66+
}
67+
68+
@Override
69+
public String getContentType() {
70+
return null;
71+
}
72+
73+
@Override
74+
public String getHeader(String s) {
75+
return null;
76+
}
77+
78+
@Override
79+
public List<String> getHeaders(String s) {
80+
return null;
81+
}
82+
83+
@Override
84+
public FluentCaseInsensitiveStringsMap getHeaders() {
85+
return null;
86+
}
87+
88+
@Override
89+
public boolean isRedirected() {
90+
return false;
91+
}
92+
93+
@Override
94+
public String toString() {
95+
return null;
96+
}
97+
98+
@Override
99+
public List<Cookie> getCookies() {
100+
return null;
101+
}
102+
103+
@Override
104+
public boolean hasResponseStatus() {
105+
return false;
106+
}
107+
108+
@Override
109+
public boolean hasResponseHeaders() {
110+
return false;
111+
}
112+
113+
@Override
114+
public boolean hasResponseBody() {
115+
return false;
116+
}
117+
118+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.codebrewery;
2+
3+
import com.ning.http.client.AsyncHttpClient;
4+
import org.json.simple.JSONObject;
5+
6+
import java.io.IOException;
7+
8+
/**
9+
* Created by jepp3 on 2015-09-06.
10+
*/
11+
public class MockRestModel extends AbstractRESTModel{
12+
@Override
13+
String resourceUrl() {
14+
return "dogs";
15+
}
16+
17+
@Override
18+
String identifierValue() {
19+
20+
return "identifier";
21+
22+
}
23+
24+
@Override
25+
public AbstractRESTModel parse(JSONObject response) throws IOException {
26+
27+
return new MockRestModel();
28+
}
29+
30+
31+
}

0 commit comments

Comments
 (0)