Skip to content

Commit c96a6d2

Browse files
authored
Merge pull request #23 from sashirestela/22-adding-unit-tests-for-recent-changes
Adding unit tests for recent changes
2 parents 388d68a + 7300b76 commit c96a6d2

File tree

7 files changed

+90
-15
lines changed

7 files changed

+90
-15
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,22 +137,25 @@ var cleverClient = CleverClient.builder()
137137
| POST | Method | POST endpoint's url | optional |
138138
| PUT | Method | PUT endpoint's url | optional |
139139
| DELETE | Method | DELETE endpoint's url | optional |
140+
| Header | Method | Header's name and value | mandatory |
140141
| Multipart | Method | (None) | none |
141142
| Path | Parameter | Path parameter name in url | mandatory |
142143
| Query | Parameter | Query parameter name in url | mandatory |
144+
| Query | Parameter | (None for Pojos) | none |
143145
| Body | Parameter | (None) | none |
144146

145147
* ```Resource``` could be used to separate the repeated part of the endpoints' url in an interface.
146148
* ```GET, POST, PUT, DELETE``` are used to mark the typical http methods (endpoints).
149+
* ```Header``` Used to include more headers (pairs of name and value) at method level.
147150
* ```Multipart``` is used to mark an endpoint with a multipart/form-data request. This is required when you need to upload files.
148151
* ```Path``` is used to replace the path parameter name in url with the matched method parameter's value.
149-
* ```Query``` is used to add a query parameter to the url in the way: [?]queryValue=parameterValue[&...].
152+
* ```Query``` is used to add a query parameter to the url in the way: [?]queryValue=parameterValue[&...] for scalar parameters. Also it can be used for Pojos using its properties and values.
150153
* ```Body``` is used to mark a method parameter as the endpoint's payload request, so the request will be application/json at least the endpoint is annotated with Multipart.
151154
* Check the above [Description's example](#💡-description) or the [Test](https://github.com/sashirestela/cleverclient/tree/main/src/test/java/io/github/sashirestela/cleverclient) folder to see more of these interface annotations in action.
152155

153156
### Supported Response Types
154157

155-
The reponse types are determined from the method responses. We don't need any annotation for that. We have five response types: [Stream](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/Stream.html) of objects, [List](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html) of objects, Generic of object, Single object, [Binary](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/InputStream.html) object, and Plain Text, and all of them can be asynchronous or synchronous. For async responses you have to use the Java class [CompletableFuture](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/CompletableFuture.html).
158+
The reponse types are determined from the method responses. We don't need any annotation for that. We have six response types: [Stream](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/Stream.html) of objects, [List](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html) of objects, Generic of object, Single object, [Binary](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/InputStream.html) object, and Plain Text, and all of them can be asynchronous or synchronous. For async responses you have to use the Java class [CompletableFuture](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/CompletableFuture.html).
156159

157160
| Method Response | Sync/Async | Response Type |
158161
|-------------------------------------|------------|-----------------------------------------|

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.sashirestela</groupId>
88
<artifactId>cleverclient</artifactId>
9-
<version>0.9.1</version>
9+
<version>0.10.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>cleverclient</name>

src/main/java/io/github/sashirestela/cleverclient/http/HttpProcessor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public HttpProcessor(HttpClient httpClient, String urlBase, List<String> headers
3737

3838
/**
3939
* Creates a generic dynamic proxy with a new {@link HttpInvocationHandler
40-
* HttpInvocationHandler}
41-
* object which will resolve the requests.
40+
* HttpInvocationHandler} object which will resolve the requests.
4241
*
4342
* @param <T> A generic interface.
4443
* @param interfaceClass Service of a generic interface

src/main/java/io/github/sashirestela/cleverclient/util/JsonUtil.java

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

33
import java.io.IOException;
44
import java.util.ArrayList;
5-
import java.util.HashMap;
65
import java.util.List;
76
import java.util.Map;
87

@@ -26,15 +25,6 @@ public class JsonUtil {
2625
private JsonUtil() {
2726
}
2827

29-
public static String objectsToJson(List<?> objects) {
30-
Map<String, Object> jsonProperties = new HashMap<>();
31-
for (Object obj : objects) {
32-
jsonProperties.putAll(objectMapperStrict.convertValue(obj, jsonPropertiesTypeRef));
33-
}
34-
35-
return objectToJson(jsonProperties);
36-
}
37-
3828
public static <T> Map<String, Object> objectToMap(T object) {
3929
try {
4030
return objectMapperStrict.convertValue(object, jsonPropertiesTypeRef);

src/test/java/io/github/sashirestela/cleverclient/http/ITest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import io.github.sashirestela.cleverclient.annotation.Body;
1010
import io.github.sashirestela.cleverclient.annotation.GET;
11+
import io.github.sashirestela.cleverclient.annotation.Header;
1112
import io.github.sashirestela.cleverclient.annotation.Multipart;
1213
import io.github.sashirestela.cleverclient.annotation.POST;
1314
import io.github.sashirestela.cleverclient.annotation.Path;
@@ -58,6 +59,7 @@ interface AsyncService {
5859
CompletableFuture<Generic<Demo>> getGenericDemo(@Path("genericDemoId") Integer genericDemoId);
5960

6061
@GET
62+
@Header(name = "MyHeader", value = "MyValue")
6163
CompletableFuture<List<Demo>> getDemos();
6264

6365
@POST
@@ -88,6 +90,7 @@ interface SyncService {
8890
Generic<Demo> getGenericDemo(@Path("genericDemoId") Integer genericDemoId);
8991

9092
@GET
93+
@Header(name = "MyHeader", value = "MyValue")
9194
List<Demo> getDemos();
9295

9396
@POST

src/test/java/io/github/sashirestela/cleverclient/http/URLBuilderTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import java.util.Map;
1010

1111
import io.github.sashirestela.cleverclient.metadata.MethodSignature;
12+
import lombok.AllArgsConstructor;
13+
import lombok.Data;
14+
1215
import org.junit.jupiter.api.Test;
1316

1417
import io.github.sashirestela.cleverclient.metadata.Metadata;
@@ -103,4 +106,41 @@ void shouldReturnReplacedUrlWithQueryParamsWhenMethodContainsQueryParams() {
103106
var expectedUrl = "/api/domain/entities?sortedBy=name&rowsPerPage=20";
104107
assertEquals(expectedUrl, actualUrl);
105108
}
109+
110+
@Test
111+
void shouldReturnReplacedUrlWithQueryParamsWhenMethodContainsQueryParamsForPojos() {
112+
var methodName = "testMethod";
113+
var url = "/api/domain/entities";
114+
var paramsList = List.of(
115+
Metadata.Parameter.builder()
116+
.index(0)
117+
.annotationValue("")
118+
.build(),
119+
Metadata.Parameter.builder()
120+
.index(1)
121+
.annotationValue("sortedBy")
122+
.build());
123+
var paramsMap = Map.of(
124+
"Path", new ArrayList<Metadata.Parameter>(),
125+
"Query", paramsList);
126+
var methodMetadata = Metadata.Method.builder()
127+
.name(methodName)
128+
.url(url)
129+
.parametersByType(paramsMap)
130+
.build();
131+
var mapMethods = Map.of(methodSign, methodMetadata);
132+
133+
when(metadata.getMethods()).thenReturn(mapMethods);
134+
135+
var actualUrl = urlBuilder.build(methodSign, new Object[] { new Pagination(10, 3), "fullname" });
136+
var expectedUrl = "/api/domain/entities?size=10&page=3&sortedBy=fullname";
137+
assertEquals(expectedUrl, actualUrl);
138+
}
139+
140+
@Data
141+
@AllArgsConstructor
142+
static class Pagination {
143+
private Integer size;
144+
private Integer page;
145+
}
106146
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.github.sashirestela.cleverclient.util;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.time.Instant;
6+
import java.time.ZoneId;
7+
import java.time.ZonedDateTime;
8+
9+
import org.junit.jupiter.api.Test;
10+
11+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
12+
13+
import lombok.AllArgsConstructor;
14+
import lombok.Getter;
15+
import lombok.NoArgsConstructor;
16+
17+
public class UnixTimestampDeserializerTest {
18+
19+
@Test
20+
void testDeserialize() {
21+
var jsonData = "{\"id\":1,\"description\":\"sample data\",\"time\":1700671053}";
22+
var expectedTime = ZonedDateTime.ofInstant(Instant.ofEpochSecond(1700671053), ZoneId.systemDefault());
23+
var actualObject = JsonUtil.jsonToObject(jsonData, SampleClass.class);
24+
assertEquals(expectedTime, actualObject.getTime());
25+
}
26+
27+
@AllArgsConstructor
28+
@NoArgsConstructor
29+
@Getter
30+
static class SampleClass {
31+
32+
private Integer id;
33+
34+
private String description;
35+
36+
@JsonDeserialize(using = UnixTimestampDeserializer.class)
37+
private ZonedDateTime time;
38+
39+
}
40+
}

0 commit comments

Comments
 (0)