Skip to content

Commit 7f11e8d

Browse files
authored
PR:BAEL-6037 (#17170)
* Update pom.xml * Java Enums with all HTTP status codes I added a new module. * Update pom.xml * Update EnumWithHttpStatusCodesLiveTest.java * Update EnumWithHttpStatusCodesUnitTest.java * Update pom.xml * Update pom.xml * Update pom.xml * Update pom.xml * Update pom.xml * Update pom.xml * Update pom.xml * Update pom.xml * Update EnumWithHttpStatusCodesUnitTest.java * Update EnumWithHttpStatusCodesUnitTest.java * Update pom.xml
1 parent 709e235 commit 7f11e8d

File tree

5 files changed

+171
-2
lines changed

5 files changed

+171
-2
lines changed

libraries-http-3/pom.xml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>libraries-http-3</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<properties>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<okhttp.version>4.12.0</okhttp.version>
20+
<apache.httpclient.version>5.3.1</apache.httpclient.version>
21+
<spring.web.version>6.1.5</spring.web.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>com.squareup.okhttp3</groupId>
27+
<artifactId>okhttp</artifactId>
28+
<version>${okhttp.version}</version>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.apache.httpcomponents.client5</groupId>
33+
<artifactId>httpclient5</artifactId>
34+
<version>${apache.httpclient.version}</version>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework</groupId>
39+
<artifactId>spring-web</artifactId>
40+
<version>${spring.web.version}</version>
41+
</dependency>
42+
</dependencies>
43+
44+
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.enumwithallhttpstatuscodes;
2+
3+
public enum HttpStatus {
4+
CONTINUE(100, "Continue"),
5+
SWITCHING_PROTOCOLS(101, "Switching Protocols"),
6+
OK(200, "OK"),
7+
CREATED(201, "Created"),
8+
ACCEPTED(202, "Accepted"),
9+
MULTIPLE_CHOICES(300, "Multiple Choices"),
10+
MOVED_PERMANENTLY(301, "Moved Permanently"),
11+
FOUND(302, "Found"),
12+
BAD_REQUEST(400, "Bad Request"),
13+
UNAUTHORIZED(401, "Unauthorized"),
14+
FORBIDDEN(403, "Forbidden"),
15+
NOT_FOUND(404, "Not Found"),
16+
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
17+
NOT_IMPLEMENTED(501, "Not Implemented"),
18+
BAD_GATEWAY(502, "Bad Gateway"),
19+
UNKNOWN(-1, "Unknown Status");
20+
21+
private final int code;
22+
private final String description;
23+
24+
HttpStatus(int code, String description) {
25+
this.code = code;
26+
this.description = description;
27+
}
28+
29+
public static HttpStatus getStatusFromCode(int code) {
30+
for (HttpStatus status : HttpStatus.values()) {
31+
if (status.getCode() == code) {
32+
return status;
33+
}
34+
}
35+
return UNKNOWN;
36+
}
37+
38+
public int getCode() {
39+
return code;
40+
}
41+
42+
public String getDescription() {
43+
return description;
44+
}
45+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.enumwithallhttpstatuscodes;
2+
3+
import okhttp3.OkHttpClient;
4+
import okhttp3.Request;
5+
import okhttp3.Response;
6+
import org.apache.hc.client5.http.classic.methods.HttpGet;
7+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
8+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
9+
import org.apache.hc.client5.http.impl.classic.HttpClients;
10+
11+
import org.junit.jupiter.api.Test;
12+
import org.springframework.http.ResponseEntity;
13+
import org.springframework.web.client.RestTemplate;
14+
15+
import java.io.IOException;
16+
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
19+
public class EnumWithHttpStatusCodesLiveTest {
20+
21+
@Test
22+
public void givenHttpRequest_whenUsingApacheHttpComponents_thenCorrectStatusDescription() throws IOException {
23+
CloseableHttpClient httpClient = HttpClients.createDefault();
24+
HttpGet request = new HttpGet("http://example.com");
25+
try (CloseableHttpResponse response = httpClient.execute(request)) {
26+
String reasonPhrase = response.getReasonPhrase();
27+
assertEquals("OK", reasonPhrase);
28+
}
29+
}
30+
31+
@Test
32+
public void givenHttpRequest_whenUsingSpringRestTemplate_thenCorrectStatusDescription() {
33+
RestTemplate restTemplate = new RestTemplate();
34+
ResponseEntity<String> response = restTemplate.getForEntity("http://example.com", String.class);
35+
int statusCode = response.getStatusCode().value();
36+
String statusDescription = HttpStatus.getStatusFromCode(statusCode).getDescription();
37+
assertEquals("OK", statusDescription);
38+
}
39+
40+
@Test
41+
public void givenHttpRequest_whenUsingOkHttp_thenCorrectStatusDescription() throws IOException {
42+
OkHttpClient client = new OkHttpClient();
43+
Request request = new Request.Builder()
44+
.url("http://example.com")
45+
.build();
46+
try (Response response = client.newCall(request).execute()) {
47+
int statusCode = response.code();
48+
String statusDescription = HttpStatus.getStatusFromCode(statusCode).getDescription();
49+
assertEquals("OK", statusDescription);
50+
}
51+
}
52+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.enumwithallhttpstatuscodes;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class EnumWithHttpStatusCodesUnitTest {
8+
9+
@Test
10+
public void givenStatusCode_whenUsingBasicApproach_thenGetCorrectCode() {
11+
assertEquals(100, HttpStatus.CONTINUE.getCode());
12+
assertEquals(200, HttpStatus.OK.getCode());
13+
assertEquals(300, HttpStatus.MULTIPLE_CHOICES.getCode());
14+
assertEquals(400, HttpStatus.BAD_REQUEST.getCode());
15+
assertEquals(500, HttpStatus.INTERNAL_SERVER_ERROR.getCode());
16+
}
17+
18+
@Test
19+
public void givenStatusCode_whenUsingBasicApproach_thenGetCorrectDescription() {
20+
assertEquals("Continue", HttpStatus.CONTINUE.getDescription());
21+
assertEquals("OK", HttpStatus.OK.getDescription());
22+
assertEquals("Multiple Choices", HttpStatus.MULTIPLE_CHOICES.getDescription());
23+
assertEquals("Bad Request", HttpStatus.BAD_REQUEST.getDescription());
24+
assertEquals("Internal Server Error", HttpStatus.INTERNAL_SERVER_ERROR.getDescription());
25+
}
26+
}

pom.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,9 @@
656656
<module>libraries-data-io</module>
657657
<module>libraries-data</module>
658658
<module>libraries-files</module>
659-
<module>libraries-http-2</module>
660659
<module>libraries-http</module>
660+
<module>libraries-http-2</module>
661+
<module>libraries-http-3</module>
661662
<module>libraries-io</module>
662663
<module>libraries-llms</module>
663664
<module>libraries-primitive</module>
@@ -911,8 +912,9 @@
911912
<module>libraries-data-io</module>
912913
<module>libraries-data</module>
913914
<module>libraries-files</module>
915+
<module>libraries-http</module>
914916
<module>libraries-http-2</module>
915-
<module>libraries-http</module>
917+
<module>libraries-http-3</module>
916918
<module>libraries-io</module>
917919
<module>libraries-llms</module>
918920
<module>libraries-primitive</module>

0 commit comments

Comments
 (0)