- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestAPITest.java
More file actions
Latest commit
90 lines (67 loc) · 3.35 KB
/
Copy pathRestAPITest.java
File metadata and controls
90 lines (67 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
importcom.fasterxml.jackson.databind.DeserializationFeature;
importcom.fasterxml.jackson.databind.ObjectMapper;
importokhttp3.*;
importorg.apache.commons.lang3.RandomStringUtils;
importorg.apache.commons.text.RandomStringGenerator;
importorg.apache.hc.client5.http.classic.methods.HttpGet;
importorg.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
importorg.apache.hc.client5.http.impl.classic.HttpClientBuilder;
importorg.apache.hc.core5.http.ContentType;
importorg.apache.hc.core5.http.HttpResponse;
importorg.apache.hc.core5.http.HttpStatus;
importorg.apache.hc.core5.http.ParseException;
importorg.apache.hc.core5.http.io.entity.EntityUtils;
importorg.junit.jupiter.api.Assertions;
importorg.junit.jupiter.api.DisplayName;
importorg.junit.jupiter.api.Test;
importjava.io.IOException;
publicclassRestAPITest {
@Test
publicvoidgivenUserDoesNotExists_whenUserInfoRetrieved_then404IsReceived() throwsIOException {
// Given
Stringname = RandomStringUtils.randomAlphabetic(10);
HttpGetrequest = newHttpGet("https://api.github.com/users/" + name);
// When
HttpResponsehttpResponse = HttpClientBuilder.create().build().execute(request);
// Then
Assertions.assertEquals(httpResponse.getCode(), HttpStatus.SC_NOT_FOUND);
}
@Test
publicvoidgivenRequestWithNoAcceptHeader_whenRequestIsExecuted_thenDefaultResponseContentTypeIsJson() throwsIOException {
// Given
StringjsonMimType = "application/json";
HttpGetrequest = newHttpGet("https://api.github.com/users/kkminseok");
// When
HttpResponseresponse = HttpClientBuilder.create().build().execute(request);
// Then
Assertions.assertEquals(response.getFirstHeader("Content-Type").getValue().split(";")[0], jsonMimType);
}
@Test
publicvoidgivenUserExists_whenUserInformationIsRetrieved_thenRetrievedResourceIsCorrect() throwsIOException, ParseException {
// Given
HttpGetrequest = newHttpGet("https://api.github.com/users/kkminseok");
// When
CloseableHttpResponseresponse = HttpClientBuilder.create().build().execute(request);
// Then
GitHubUsergitHubUser = retrieveResourceFromResponse(response, GitHubUser.class);
Assertions.assertEquals("kkminseok", gitHubUser.login());
}
@Test
@DisplayName("okhttp 테스트")
publicvoidokhttpTest() throwsIOException {
// Given
OkHttpClientclient = newOkHttpClient();
// When
RequestBodybody = RequestBody.Companion.create(null, MediaType.parse("application/json; charset=utf-8"));
Requestrequest = newRequest.Builder().url("http://tech.dev-oc.alpha-nhncloud.com:9003/cxsolution/api/v2/hub/setting/operation").put(body).build();
Callcall = client.newCall(request);
Responseresponse = call.execute();
System.out.println(response.body().string());
// Then
}
private <T> TretrieveResourceFromResponse(finalCloseableHttpResponseresponse, finalClass<T> clazz) throwsIOException, ParseException {
finalStringjsonFromResponse = EntityUtils.toString(response.getEntity());
finalObjectMappermapper = newObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
returnmapper.readValue(jsonFromResponse, clazz);
}
}