Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@
</module>
<module name="NonEmptyAtclauseDescription" />
<module name="InvalidJavadocPosition" />
<module name="JavadocTagContinuationIndentation" />
<module name="SummaryJavadoc">
<property name="forbiddenSummaryFragments"
value="^@return the *|^This method returns |^A [{]@code [a-zA-Z0-9]+[}]( is a )" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package com.tvd12.ezyhttp.core.codec;

import com.tvd12.ezyfox.collect.Lists;
import com.tvd12.ezyfox.collect.Sets;
import com.tvd12.ezyfox.io.EzyDataConverter;
import com.tvd12.ezyfox.io.EzyDates;
import com.tvd12.ezyfox.io.EzyStrings;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.tvd12.ezyfox.collect.Lists;
import com.tvd12.ezyfox.collect.Sets;
import com.tvd12.ezyfox.io.EzyDataConverter;
import com.tvd12.ezyfox.io.EzyDates;
import com.tvd12.ezyfox.io.EzyStrings;
import java.util.*;

public class DefaultStringDeserializer implements StringDeserializer {

Expand Down Expand Up @@ -49,7 +41,10 @@ public <T> T deserialize(
}

@SuppressWarnings({"unchecked"})
public <T> T deserialize(String value, Class<T> outType) throws IOException {
public <T> T deserialize(
String value,
Class<T> outType
) throws IOException {
StringMapper mapper = mappers.get(outType);
if (mapper == null) {
if (value == null) {
Expand All @@ -70,6 +65,29 @@ public <T> T deserialize(String value, Class<T> outType) throws IOException {
}
}

@SuppressWarnings({"unchecked"})
public <T> T deserializeOrNull(
String value,
Class<T> outType
) {
StringMapper mapper = mappers.get(outType);
if (mapper == null) {
if (value == null) {
return null;
}
if (outType.isEnum()) {
return stringToEnum(value, outType);
}
} else {
try {
return (T) mapper.apply(value);
} catch (Exception e) {
return null;
}
}
return null;
}

@SuppressWarnings({"unchecked", "rawtypes"})
private <T> T stringToEnum(String value, Class<T> outType) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class HttpBadRequestException extends HttpRequestException {
public HttpBadRequestException(Object data) {
super(StatusCodes.BAD_REQUEST, data);
}

public HttpBadRequestException(Object data, Throwable cause) {
super(StatusCodes.BAD_REQUEST, data, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class HttpConflictException extends HttpRequestException {
public HttpConflictException(Object data) {
super(StatusCodes.CONFLICT, data);
}

public HttpConflictException(Object data, Throwable cause) {
super(StatusCodes.CONFLICT, data, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class HttpForbiddenException extends HttpRequestException {
public HttpForbiddenException(Object data) {
super(StatusCodes.FORBIDDEN, data);
}

public HttpForbiddenException(Object data, Throwable cause) {
super(StatusCodes.FORBIDDEN, data, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class HttpInternalServerErrorException extends HttpRequestException {
public HttpInternalServerErrorException(Object data) {
super(StatusCodes.INTERNAL_SERVER_ERROR, data);
}

public HttpInternalServerErrorException(Object data, Throwable cause) {
super(StatusCodes.INTERNAL_SERVER_ERROR, data, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class HttpMethodNotAllowedException extends HttpRequestException {
public HttpMethodNotAllowedException(Object data) {
super(StatusCodes.METHOD_NOT_ALLOWED, data);
}

public HttpMethodNotAllowedException(Object data, Throwable cause) {
super(StatusCodes.METHOD_NOT_ALLOWED, data, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class HttpNotAcceptableException extends HttpRequestException {
public HttpNotAcceptableException(Object data) {
super(StatusCodes.NOT_ACCEPTABLE, data);
}

public HttpNotAcceptableException(Object data, Throwable cause) {
super(StatusCodes.NOT_ACCEPTABLE, data, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class HttpNotFoundException extends HttpRequestException {
public HttpNotFoundException(Object data) {
super(StatusCodes.NOT_FOUND, data);
}

public HttpNotFoundException(Object data, Throwable cause) {
super(StatusCodes.NOT_FOUND, data, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class HttpPaymentRequiredException extends HttpRequestException {
public HttpPaymentRequiredException(Object data) {
super(StatusCodes.PAYMENT_REQUIRED, data);
}

public HttpPaymentRequiredException(Object data, Throwable cause) {
super(StatusCodes.PAYMENT_REQUIRED, data, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@ public class HttpRequestException extends RuntimeException {
protected final int code;
protected final Object data;

public HttpRequestException(int code, Object data) {
public HttpRequestException(
int code,
Object data
) {
super("code: " + code + ", data: " + data);
this.code = code;
this.data = data;
}

public HttpRequestException(
int code,
Object data,
Throwable cause
) {
super("code: " + code + ", data: " + data, cause);
this.code = code;
this.data = data;
}

@SuppressWarnings("unchecked")
public <T> T getData() {
return (T) data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class HttpRequestTimeoutException extends HttpRequestException {
public HttpRequestTimeoutException(Object data) {
super(StatusCodes.REQUEST_TIMEOUT, data);
}

public HttpRequestTimeoutException(Object data, Throwable cause) {
super(StatusCodes.REQUEST_TIMEOUT, data, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class HttpTooManyRequestsException extends HttpRequestException {
public HttpTooManyRequestsException(Object data) {
super(StatusCodes.TOO_MANY_REQUESTS, data);
}

public HttpTooManyRequestsException(Object data, Throwable cause) {
super(StatusCodes.TOO_MANY_REQUESTS, data, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class HttpUnauthorizedException extends HttpRequestException {
public HttpUnauthorizedException(Object data) {
super(StatusCodes.UNAUTHORIZED, data);
}

public HttpUnauthorizedException(Object data, Throwable cause) {
super(StatusCodes.UNAUTHORIZED, data, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class HttpUnsupportedMediaTypeException extends HttpRequestException {
public HttpUnsupportedMediaTypeException(Object data) {
super(StatusCodes.UNSUPPORTED_MEDIA_TYPE, data);
}

public HttpUnsupportedMediaTypeException(Object data, Throwable cause) {
super(StatusCodes.UNSUPPORTED_MEDIA_TYPE, data, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,35 @@ public void deserializeEnumTest() throws Exception {
.deserialize("hello", MyEnum.class), MyEnum.HELLO);
}

@Test
public void deserializeOrNullTest() {
// given
// when
// then
Asserts.assertEquals(
SingletonStringDeserializer.getInstance()
.deserializeOrNull("1", int.class),
1
);
Asserts.assertNull(
SingletonStringDeserializer.getInstance()
.deserializeOrNull("1", void.class)
);
Asserts.assertNull(
SingletonStringDeserializer.getInstance()
.deserializeOrNull(null, void.class)
);
Asserts.assertNull(
SingletonStringDeserializer.getInstance()
.deserializeOrNull("hello", Long.class)
);
Asserts.assertEquals(
SingletonStringDeserializer.getInstance()
.deserializeOrNull("WORLD", MyEnum.class),
MyEnum.WORLD
);
}

public enum MyEnum {
HELLO, WORLD
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.tvd12.ezyhttp.core.test.exception;

import org.testng.annotations.Test;

import com.tvd12.ezyhttp.core.constant.StatusCodes;
import com.tvd12.ezyhttp.core.exception.HttpBadRequestException;
import com.tvd12.test.assertion.Asserts;
import org.testng.annotations.Test;

public class HttpBadRequestExceptionTest {

Expand All @@ -21,4 +20,23 @@ public void test() {
Asserts.assertEquals(code, sut.getCode());
Asserts.assertEquals(data, sut.getData());
}

@Test
public void withCauseTest() {
// given
int code = StatusCodes.BAD_REQUEST;
String data = "error";
Exception cause = new Exception("test");

// when
HttpBadRequestException sut = new HttpBadRequestException(
data,
cause
);

// then
Asserts.assertEquals(code, sut.getCode());
Asserts.assertEquals(data, sut.getData());
Asserts.assertEquals(sut.getCause(), cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.tvd12.ezyhttp.core.test.exception;

import org.testng.annotations.Test;

import com.tvd12.ezyhttp.core.constant.StatusCodes;
import com.tvd12.ezyhttp.core.exception.HttpConflictException;
import com.tvd12.test.assertion.Asserts;
import org.testng.annotations.Test;

public class HttpConflictExceptionTest {

Expand All @@ -21,4 +20,23 @@ public void test() {
Asserts.assertEquals(code, sut.getCode());
Asserts.assertEquals(data, sut.getData());
}

@Test
public void withCauseTest() {
// given
int code = StatusCodes.CONFLICT;
String data = "error";
Exception cause = new Exception("test");

// when
HttpConflictException sut = new HttpConflictException(
data,
cause
);

// then
Asserts.assertEquals(code, sut.getCode());
Asserts.assertEquals(data, sut.getData());
Asserts.assertEquals(sut.getCause(), cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.tvd12.ezyhttp.core.test.exception;

import org.testng.annotations.Test;

import com.tvd12.ezyhttp.core.constant.StatusCodes;
import com.tvd12.ezyhttp.core.exception.HttpForbiddenException;
import com.tvd12.test.assertion.Asserts;
import org.testng.annotations.Test;

public class HttpForbiddenExceptionTest {

Expand All @@ -21,4 +20,23 @@ public void test() {
Asserts.assertEquals(code, sut.getCode());
Asserts.assertEquals(data, sut.getData());
}

@Test
public void withCauseTest() {
// given
int code = StatusCodes.FORBIDDEN;
String data = "error";
Exception cause = new Exception("test");

// when
HttpForbiddenException sut = new HttpForbiddenException(
data,
cause
);

// then
Asserts.assertEquals(code, sut.getCode());
Asserts.assertEquals(data, sut.getData());
Asserts.assertEquals(sut.getCause(), cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.tvd12.ezyhttp.core.test.exception;

import org.testng.annotations.Test;

import com.tvd12.ezyhttp.core.constant.StatusCodes;
import com.tvd12.ezyhttp.core.exception.HttpInternalServerErrorException;
import com.tvd12.test.assertion.Asserts;
import org.testng.annotations.Test;

public class HttpInternalServerErrorExceptionTest {

Expand All @@ -21,4 +20,23 @@ public void test() {
Asserts.assertEquals(code, sut.getCode());
Asserts.assertEquals(data, sut.getData());
}

@Test
public void withCauseTest() {
// given
int code = StatusCodes.INTERNAL_SERVER_ERROR;
String data = "error";
Exception cause = new Exception("test");

// when
HttpInternalServerErrorException sut = new HttpInternalServerErrorException(
data,
cause
);

// then
Asserts.assertEquals(code, sut.getCode());
Asserts.assertEquals(data, sut.getData());
Asserts.assertEquals(sut.getCause(), cause);
}
}
Loading
Loading