Skip to content

Commit

Permalink
test: add httpResponseCode test case
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Apr 30, 2024
1 parent bad4e06 commit 5883131
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static Logger getLogger(String name) {
* @param clazz The class of the logger.
* @return A logger for the given class.
*/
public static Logger getLogger(Class clazz) {
public static Logger getLogger(Class<?> clazz) {
return getLogger(clazz.getName());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.brenoepics.at4j.util.rest;

import io.github.brenoepics.at4j.core.exceptions.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
Expand Down Expand Up @@ -231,18 +230,18 @@ public String getMeaning() {
* @param response The information about the response.
* @return The azure exception to throw for this kind of result code.
*/
public Optional<? extends AzureException> getAzureException(
Exception origin, String message, RestRequestInfo request, RestRequestResponseInfo response) {
public Optional<AzureException> getAzureException(
Exception origin, String message, RestRequestInfo request, RestRequestResponseInfo response) {
return Optional.ofNullable(azureExceptionInstantiation)
.map(instantiator -> instantiator.createInstance(origin, message, request, response));
.map(instantiation -> instantiation.createInstance(origin, message, request, response));
}

/**
* Gets the azure exception class to throw for this kind of result code.
*
* @return The azure exception class to throw for this kind of result code.
*/
public Optional<? extends Class<? extends AzureException>> getAzureExceptionClass() {
public Optional<Class<? extends AzureException>> getAzureExceptionClass() {
return Optional.ofNullable(azureExceptionClass);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package io.github.brenoepics.at4j.util.rest;

import static org.junit.jupiter.api.Assertions.*;

import io.github.brenoepics.at4j.core.exceptions.*;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class RestRequestHttpResponseCodeTest {

@Test
void fromCode() {
assertEquals(
RestRequestHttpResponseCode.OK, RestRequestHttpResponseCode.fromCode(200).orElse(null));
assertEquals(
RestRequestHttpResponseCode.CREATED,
RestRequestHttpResponseCode.fromCode(201).orElse(null));
}

@Test
void fromAzureExceptionClass() {
assertEquals(
RestRequestHttpResponseCode.BAD_REQUEST,
RestRequestHttpResponseCode.fromAzureExceptionClass(BadRequestException.class)
.orElse(null));
assertEquals(
RestRequestHttpResponseCode.UNAUTHORIZED,
RestRequestHttpResponseCode.fromAzureExceptionClass(UnauthorizedException.class)
.orElse(null));
}

@Test
void getCode() {
assertEquals(200, RestRequestHttpResponseCode.OK.getCode());
assertEquals(201, RestRequestHttpResponseCode.CREATED.getCode());
}

@Test
void getMeaning() {
assertEquals("The request completed successfully", RestRequestHttpResponseCode.OK.getMeaning());
assertEquals(
"The entity was created successfully", RestRequestHttpResponseCode.CREATED.getMeaning());
}

@Test
void getAzureBadRequestException() {
RestRequestInfo request = Mockito.mock(RestRequestInfo.class);
RestRequestResponseInfo response = Mockito.mock(RestRequestResponseInfo.class);
assertTrue(
RestRequestHttpResponseCode.BAD_REQUEST
.getAzureException(new Exception(), "Test", request, response)
.isPresent());
}

@Test
void getAzureExceptionForAllCodes() {
RestRequestInfo request = Mockito.mock(RestRequestInfo.class);
RestRequestResponseInfo response = Mockito.mock(RestRequestResponseInfo.class);

var codes =
Arrays.stream(RestRequestHttpResponseCode.values())
.filter(c -> c.getAzureExceptionClass().isPresent())
.collect(Collectors.toList());
for (RestRequestHttpResponseCode code : codes) {
assertTrue(
code.getAzureException(new Exception(), "Test", request, response).isPresent(),
"Expected an AzureException for code: " + code);
}
}

@Test
void getAzureExceptionClass() {
assertEquals(
BadRequestException.class,
RestRequestHttpResponseCode.BAD_REQUEST.getAzureExceptionClass().orElse(null));
assertEquals(
UnauthorizedException.class,
RestRequestHttpResponseCode.UNAUTHORIZED.getAzureExceptionClass().orElse(null));
}

@Test
void values() {
RestRequestHttpResponseCode[] codes = RestRequestHttpResponseCode.values();
assertTrue(codes.length > 0);
}

@Test
void valueOf() {
assertEquals(RestRequestHttpResponseCode.OK, RestRequestHttpResponseCode.valueOf("OK"));
assertEquals(
RestRequestHttpResponseCode.CREATED, RestRequestHttpResponseCode.valueOf("CREATED"));
}
}

0 comments on commit 5883131

Please sign in to comment.