Skip to content

Commit

Permalink
Add more Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Jan 16, 2024
1 parent 2367e0a commit 0450708
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @param <T> The type of the azure exception that is produced.
*/
@FunctionalInterface
public interface AzureExceptionInstantiator<T extends AzureException> {
public interface AzureExceptionInstantiation<T extends AzureException> {

/**
* Creates a new instance of the class {@code T}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.brenoepics.at4j.util.rest;

import com.github.brenoepics.at4j.core.exceptions.AzureException;
import com.github.brenoepics.at4j.core.exceptions.AzureExceptionInstantiator;
import com.github.brenoepics.at4j.core.exceptions.AzureExceptionInstantiation;
import com.github.brenoepics.at4j.core.exceptions.BadRequestException;
import com.github.brenoepics.at4j.core.exceptions.NotFoundException;
import java.util.Arrays;
Expand Down Expand Up @@ -87,7 +87,7 @@ public enum RestRequestHttpResponseCode {
/**
* The azure exception instantiated that produces instances to throw for this kind of result code.
*/
private final AzureExceptionInstantiator<?> azureExceptionInstantiator;
private final AzureExceptionInstantiation<?> azureExceptionInstantiation;

/** The azure exception class to throw for this kind of result code. */
private final Class<? extends AzureException> azureExceptionClass;
Expand Down Expand Up @@ -133,11 +133,11 @@ public enum RestRequestHttpResponseCode {
<T extends AzureException> RestRequestHttpResponseCode(
int code,
String meaning,
AzureExceptionInstantiator<T> exceptionInstantiator,
AzureExceptionInstantiation<T> exceptionInstantiator,
Class<T> azureExceptionClass) {
this.code = code;
this.meaning = meaning;
this.azureExceptionInstantiator = exceptionInstantiator;
this.azureExceptionInstantiation = exceptionInstantiator;
this.azureExceptionClass = azureExceptionClass;

if ((exceptionInstantiator == null) && (azureExceptionClass != null)
Expand Down Expand Up @@ -211,7 +211,7 @@ public Optional<? extends AzureException> getAzureException(
String message,
RestRequestInformation request,
RestRequestResponseInformation response) {
return Optional.ofNullable(azureExceptionInstantiator)
return Optional.ofNullable(azureExceptionInstantiation)
.map(instantiator -> instantiator.createInstance(origin, message, request, response));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public enum RestRequestResultErrorCode {
/**
* The azure exception instantiate that produces instances to throw for this kind of result code.
*/
private final AzureExceptionInstantiator<AzureException> azureExceptionInstantiator;
private final AzureExceptionInstantiation<AzureException> azureExceptionInstantiation;

/** The response code for which the given instantiating should be used. */
private final RestRequestHttpResponseCode responseCode;
Expand Down Expand Up @@ -253,18 +253,18 @@ public enum RestRequestResultErrorCode {
*
* @param code The actual numeric close code.
* @param meaning The textual meaning.
* @param azureExceptionInstantiator The azure exception instantiator that produces instances to
* @param azureExceptionInstantiation The azure exception instantiator that produces instances to
* throw for this kind of result code.
* @param responseCode The response code for which the given instantiator should be used.
*/
RestRequestResultErrorCode(
int code,
String meaning,
AzureExceptionInstantiator<AzureException> azureExceptionInstantiator,
AzureExceptionInstantiation<AzureException> azureExceptionInstantiation,
RestRequestHttpResponseCode responseCode) {
this.code = code;
this.meaning = meaning;
this.azureExceptionInstantiator = azureExceptionInstantiator;
this.azureExceptionInstantiation = azureExceptionInstantiation;
this.responseCode = responseCode;
}

Expand Down Expand Up @@ -313,7 +313,7 @@ public Optional<AzureException> getAzureException(
String message,
RestRequestInformation request,
RestRequestResponseInformation response) {
return Optional.ofNullable(azureExceptionInstantiator)
return Optional.ofNullable(azureExceptionInstantiation)
.map(instantiate -> instantiate.createInstance(origin, message, request, response))
.filter(
exception ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.brenoepics.at4j.core.exceptions;

import com.github.brenoepics.at4j.util.rest.RestRequestInformation;
import com.github.brenoepics.at4j.util.rest.RestRequestResponseInformation;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

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

class AzureExceptionInstantiationTest {

private final Exception origin = new Exception();
private final RestRequestInformation request = Mockito.mock(RestRequestInformation.class);
private final RestRequestResponseInformation response = Mockito.mock(RestRequestResponseInformation.class);
private final AzureExceptionInstantiation<AzureException> azureExceptionInstantiation = AzureException::new;

@Test
void createInstance_withValidInputs_returnsNewInstance() {
String message = "Test message";
AzureException result = azureExceptionInstantiation.createInstance(origin, message, request, response);

assertNotNull(result);
assertTrue(result.getRequest().isPresent());
assertTrue(result.getResponse().isPresent());

assertSame(origin, result.getCause());
assertEquals(message, result.getMessage());
assertEquals(request, result.getRequest().get());
assertEquals(response, result.getResponse().get());

}
}

0 comments on commit 0450708

Please sign in to comment.