Rest API expose methods POST/ GET / PUT , etc on the protocol HTTP : this make us simple our life: Rest API allows interoperability in a very simple way.
Before any step, is strong advised understand the basic principles of HTTP (I’m planning to write something about this later).
Situation
A rest endpoint exposes the list of users of a lambda application, you need to extract this information and find the user “Charles”. The endpoint is
https://reqres.in/api/users?page=2
If you open the link you will see this json structure.

Problem
Get the metadata of user “Charles”
Solution
We will solve this problem using TDD (Test Driven Development). If you want to write a clean and scalable code use TDD. Besides this, to understand / learn anything use TDD.
0. For this excercise we will use retrofit and appache.httpcomponents to handle the call GET
Here the build.gradle file
plugins {
id 'java'
}
group 'groupid'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.7'
compile group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.5.0'
compile group: 'com.squareup.retrofit2', name: 'converter-gson', version: '2.5.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
1. Here my first unit test
@Test
public void findCustomerByName() throws IOException {
ProxyCustomer customerProvider = new ProxyCustomer();
Datum customer = customerProvider.findCustomerByName("Charles");
Assert.assertTrue(customer!=null);
}
Assume that the interface with the endpoint will be the responsibility of ProxyCustomer, this object will implement a method findCustomerByName that receives a parameter (name in this case). The test validate that at the end of the call an object Datum different to null will be returned.
3. Let create the object ProxyCustomer with a simple method findCustomerByName .
public class ProxyCustomer {
public Datum findCustomerByName(String name)
{
//For the moment we return null
return null;
}
}
and a second object called Datum.
public class Datum{
}
4. Run the test findCustomerByName(). This test must fail.
5. To pass the test, we need deserialize the json response. Go to http://www.jsonschema2pojo.org/ and paste the json response of https://reqres.in/api/users?page=2

Specify the Package and the Class name.
6. Click in Zip. The output will be the objects that we need to deserialize the json response.
7. First refactoring.
import Entities.Customer;
import Entities.Datum;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ProxyCustomer {
public Datum findCustomerByName(String name) throws IOException {
String json = get("https://reqres.in/api/users?page=2");
Customer customer = new Gson().fromJson(json, Customer.class);
Datum datum = null;
for (Datum c: customer.getData()) {
if(c.getFirstName().toLowerCase().contains(name.toLowerCase()))
datum = c;
}
return datum;
}
private String get(String url) throws IOException {
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = client.execute(httpget);
String reply = readTextFromHttpResponse(response);
return reply;
}
private String readTextFromHttpResponse(HttpResponse httpResp) throws IllegalStateException, IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResp.getEntity().getContent()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
return sb.toString();
}
}
8. Let analyze some interesting facts in the code :
Deserialization
Customer customer = new Gson().fromJson(json, Customer.class);
Http GET
private String get(String url) throws IOException {
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = client.execute(httpget);
String reply = readTextFromHttpResponse(response);
return reply;
}
8. Run the test. The test must pass
![215053-artifactid [C__google_artifactid] - ..._src_test_java_CustomerTest.java [artifac](https://tequila-automation.com/wp-content/uploads/2019/02/215053-artifactid-c__google_artifactid-..._src_test_java_customertest.java-artifac.png)
