Skip to content

Commit

Permalink
fix(impl): [#199] tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmf committed Mar 28, 2024
1 parent 21b107c commit e2ba964
Showing 1 changed file with 110 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@

import static io.restassured.RestAssured.given;
import static org.eclipse.tractusx.irs.util.TestMother.registerJobWithoutDepthAndAspect;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.web.client.HttpServerErrorException.InternalServerError;

import io.restassured.RestAssured;
Expand All @@ -40,6 +42,7 @@
import org.eclipse.tractusx.irs.services.IrsItemGraphQueryService;
import org.eclipse.tractusx.irs.services.SemanticHubService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -54,13 +57,18 @@

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = { "digitalTwinRegistry.type=central" })
@ActiveProfiles(profiles = { "test", "local" })
@ActiveProfiles(profiles = { "test",
"local"
})
@Import(TestConfig.class)
@ExtendWith({ MockitoExtension.class, SpringExtension.class })
@ExtendWith({ MockitoExtension.class,
SpringExtension.class
})
class IrsExceptionHandlerTest extends ControllerTest {

@MockBean
private IrsItemGraphQueryService service;

@MockBean
private SemanticHubService semanticHubService;

Expand All @@ -73,63 +81,122 @@ public void configureRestAssured() {
RestAssured.port = port;
}

@Test
void handleAll() {
authenticateWith(IrsRoles.VIEW_IRS);
@Nested
class PoliciesTests {

@Test
void getPolicies_forbidden() {
authenticateWith(IrsRoles.VIEW_IRS);
given().port(port).get("/irs/policies").then().statusCode(FORBIDDEN.value());
}

@Test
void getPolicies_success() {
authenticateWith(IrsRoles.ADMIN_IRS);
given().port(port).get("/irs/policies").then().statusCode(OK.value());
}

@Test
void getPolicies_withInvalidBpn() {
authenticateWith(IrsRoles.ADMIN_IRS);
given().port(port)
.get("/irs/policies?businessPartnerNumbers=invalid")
.then()
.statusCode(BAD_REQUEST.value())
.body("messages[0]", containsString("The business partner number at index 0 is invalid"));
}

@Test
void getPolicies_withValidAndInvalidBpn() {
authenticateWith(IrsRoles.ADMIN_IRS);
given().port(port)
.get("/irs/policies" //
+ "?businessPartnerNumbers=BPNL1234567890AB" //
+ "&businessPartnerNumbers=__invalid__BPN__" //
+ "&businessPartnerNumbers=BPNL1234567890AB")
.then()
.statusCode(BAD_REQUEST.value())
.body("messages[0]", containsString("The business partner number at index 1 is invalid"));
}

@Test
void getPolicies_withEmptyBpn() {
authenticateWith(IrsRoles.ADMIN_IRS);
given().port(port)
.get("/irs/policies" //
+ "?businessPartnerNumbers=BPNL1234567890AB" //
+ "&businessPartnerNumbers=__invalid__BPN__" //
+ "&businessPartnerNumbers=BPNL1234567890AB")
.then()
.statusCode(BAD_REQUEST.value())
.body("messages[0]", containsString("The business partner number at index 1 is invalid"));
}
}

@Nested
class JobsTests {

when(service.registerItemJob(any())).thenThrow(InternalServerError.class);
@Test
void handleAll() {
authenticateWith(IrsRoles.VIEW_IRS);

given().port(port).contentType(ContentType.JSON).body(registerJobWithoutDepthAndAspect()).post("/irs/jobs")
.then().statusCode(INTERNAL_SERVER_ERROR.value());
when(service.registerItemJob(any())).thenThrow(InternalServerError.class);

given().port(port)
.contentType(ContentType.JSON)
.body(registerJobWithoutDepthAndAspect())
.post("/irs/jobs")
.then()
.statusCode(INTERNAL_SERVER_ERROR.value());
}
}

@Test
void shouldReturn500WhenGetSemanticModelsFails() throws Exception {
authenticateWith(IrsRoles.VIEW_IRS);
@Nested
class AspectModelsTests {

when(semanticHubService.getAllAspectModels()).thenThrow(InternalServerError.class);
@Test
void shouldReturn500WhenGetSemanticModelsFails() throws Exception {
authenticateWith(IrsRoles.VIEW_IRS);

given().port(port).get("/irs/aspectmodels")
.then().statusCode(INTERNAL_SERVER_ERROR.value());
}
when(semanticHubService.getAllAspectModels()).thenThrow(InternalServerError.class);

@Test
void shouldReturn400WhenProvidingBadInput() throws Exception {
authenticateWith(IrsRoles.VIEW_IRS);
given().port(port).get("/irs/aspectmodels").then().statusCode(INTERNAL_SERVER_ERROR.value());
}

when(semanticHubService.getAllAspectModels()).thenThrow(IllegalArgumentException.class);
@Test
void shouldReturn400WhenProvidingBadInput() throws Exception {
authenticateWith(IrsRoles.VIEW_IRS);

given().port(port).get("/irs/aspectmodels")
.then().statusCode(BAD_REQUEST.value());
}
when(semanticHubService.getAllAspectModels()).thenThrow(IllegalArgumentException.class);

@Test
void shouldReturn400WhenCatchingIllegalStateException() throws Exception {
authenticateWith(IrsRoles.VIEW_IRS);
given().port(port).get("/irs/aspectmodels").then().statusCode(BAD_REQUEST.value());
}

when(semanticHubService.getAllAspectModels()).thenThrow(IllegalStateException.class);
@Test
void shouldReturn400WhenCatchingIllegalStateException() throws Exception {
authenticateWith(IrsRoles.VIEW_IRS);

given().port(port).get("/irs/aspectmodels")
.then().statusCode(BAD_REQUEST.value());
}
when(semanticHubService.getAllAspectModels()).thenThrow(IllegalStateException.class);

@Test
void shouldReturn400WhenCatchingMethodArgumentTypeMismatchException() throws Exception {
authenticateWith(IrsRoles.VIEW_IRS);
given().port(port).get("/irs/aspectmodels").then().statusCode(BAD_REQUEST.value());
}

when(semanticHubService.getAllAspectModels()).thenThrow(MethodArgumentTypeMismatchException.class);
@Test
void shouldReturn400WhenCatchingMethodArgumentTypeMismatchException() throws Exception {
authenticateWith(IrsRoles.VIEW_IRS);

given().port(port).get("/irs/aspectmodels")
.then().statusCode(BAD_REQUEST.value());
}
when(semanticHubService.getAllAspectModels()).thenThrow(MethodArgumentTypeMismatchException.class);

given().port(port).get("/irs/aspectmodels").then().statusCode(BAD_REQUEST.value());
}

@Test
void shouldReturn403WhenRightsAreMissing() throws Exception {
authenticateWith(IrsRoles.VIEW_IRS);
@Test
void shouldReturn403WhenRightsAreMissing() throws Exception {
authenticateWith(IrsRoles.VIEW_IRS);

when(semanticHubService.getAllAspectModels()).thenThrow(AccessDeniedException.class);
when(semanticHubService.getAllAspectModels()).thenThrow(AccessDeniedException.class);

given().port(port).get("/irs/aspectmodels")
.then().statusCode(FORBIDDEN.value());
given().port(port).get("/irs/aspectmodels").then().statusCode(FORBIDDEN.value());
}
}
}
}

0 comments on commit e2ba964

Please sign in to comment.