Skip to content

Commit

Permalink
Updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Feb 3, 2025
1 parent b05610e commit f44db31
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import it.pagopa.selfcare.party.registry_proxy.connector.exception.InternalException;
import it.pagopa.selfcare.party.registry_proxy.connector.exception.InvalidRequestException;
import it.pagopa.selfcare.party.registry_proxy.connector.exception.ResourceNotFoundException;
import java.io.IOException;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;

import java.io.IOException;

@Configuration
public class RestTemplateErrorHandler implements ResponseErrorHandler {
Expand All @@ -25,12 +23,9 @@ public void handleError(ClientHttpResponse response) throws IOException {
throw new InternalException("Unknown error: " + response.getStatusText());
}
switch (status) {
case BAD_REQUEST:
throw new InvalidRequestException(response.getStatusText());
case NOT_FOUND:
throw new ResourceNotFoundException(response.getStatusText());
default:
throw new InternalException(response.getStatusText());
case BAD_REQUEST -> throw new InvalidRequestException(response.getStatusText());
case NOT_FOUND -> throw new ResourceNotFoundException(response.getStatusText());
default -> throw new InternalException(response.getStatusText());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package it.pagopa.selfcare.party.registry_proxy.connector.rest.decoder;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

import it.pagopa.selfcare.party.registry_proxy.connector.exception.InternalException;
import it.pagopa.selfcare.party.registry_proxy.connector.exception.InvalidRequestException;
import it.pagopa.selfcare.party.registry_proxy.connector.exception.ResourceNotFoundException;
import java.io.IOException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

class RestTemplateErrorHandlerTest {
@Mock
private ClientHttpResponse response;
Expand All @@ -28,6 +28,12 @@ void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void handleError_shouldThrowInternalException_whenStatusNull() throws IOException {
when(response.getStatusCode()).thenReturn(HttpStatusCode.valueOf(999));
assertThrows(InternalException.class, () -> errorHandler.handleError(response));
}

@Test
void hasError_shouldReturnTrue_whenResponseIsError() throws IOException {
when(response.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST);
Expand Down

0 comments on commit f44db31

Please sign in to comment.