Skip to content

Commit

Permalink
fix: test coverage (#32)
Browse files Browse the repository at this point in the history
Co-authored-by: milan.horvath <[email protected]>
  • Loading branch information
milanhorvath and milan.horvath authored Mar 21, 2024
1 parent b957dfe commit f79295b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ public class ImagesRestController implements ImagesInternalApiService {

@Override
public Response getImage(String refId, RefTypeDTO refType) {
Response.ResponseBuilder responseBuilder;
try (Response response = imageApi.getImage(refId, imageMapper.map(refType))) {
var contentType = response.getHeaderString(HttpHeaders.CONTENT_TYPE);
var contentLength = response.getHeaderString(HttpHeaders.CONTENT_LENGTH);
var body = response.readEntity(byte[].class);
if (contentType != null && contentLength != null && body != null && body.length != 0) {
return Response.status(response.getStatus())
if (contentType != null && body.length != 0) {
responseBuilder = Response.status(response.getStatus())
.header(HttpHeaders.CONTENT_TYPE, contentType)
.header(HttpHeaders.CONTENT_LENGTH, contentLength)
.entity(body)
.build();
.entity(body);
} else {
return Response.status(Response.Status.BAD_REQUEST).build();
responseBuilder = Response.status(Response.Status.BAD_REQUEST);
}

return responseBuilder.build();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public Response getProductByName(String name) {
var result = workspaceResponse.readEntity(WorkspacePageResult.class);
workspaceNames = mapper.workspaceNames(result);
} catch (WebApplicationException ex) {
// ignore exception
}

ProductAndWorkspacesDTO resultProductDTO = mapper.mapProductWithWorkspaceNames(resultProduct, workspaceNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
@Provider
@Singleton
public class RefTypePathParamMapper implements ParamConverterProvider {
public RefTypePathParamMapper() {

}

@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
Expand All @@ -24,8 +21,6 @@ public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, An
}

public static class RefTypeParamConverter implements ParamConverter<RefType> {
public RefTypeParamConverter() {
}

public RefType fromString(String value) {
return value != null && !value.isBlank() ? RefType.fromString(value) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ void getImage_shouldReturnBadRequest_whenBodyEmpty() {
mockServerClient.when(request()
.withPath("/internal/images/" + refId + "/" + RefType.FAVICON)
.withMethod(HttpMethod.GET))
.withId(mockId)
.withPriority(100)
.respond(httpRequest -> response().withStatusCode(OK.getStatusCode())
.withHeaders(
Expand All @@ -190,6 +191,62 @@ void getImage_shouldReturnBadRequest_whenBodyEmpty() {

}

@Test
void getImage_shouldReturnBadRequest_whenContentTypeEmpty() {

var refId = "themeName";
var refType = RefTypeDTO.FAVICON;
byte[] bytesRes = new byte[] { (byte) 0xe0, 0x4f, (byte) 0xd0,
0x20, (byte) 0xea, 0x3a, 0x69, 0x10, (byte) 0xa2, (byte) 0xd8, 0x08, 0x00, 0x2b,
0x30, 0x30, (byte) 0x9d };

mockServerClient.when(request()
.withPath("/internal/images/" + refId + "/" + RefType.FAVICON)
.withMethod(HttpMethod.GET))
.withId(mockId)
.withPriority(100)
.respond(httpRequest -> response().withStatusCode(OK.getStatusCode())
.withBody(bytesRes));

given()
.when()
.auth().oauth2(keycloakClient.getAccessToken(ADMIN))
.header(APM_HEADER_PARAM, ADMIN)
.contentType(APPLICATION_JSON)
.pathParam("refId", refId)
.pathParam("refType", refType)
.get()
.then()
.statusCode(BAD_REQUEST.getStatusCode());

}

@Test
void getImage_shouldReturnBadRequest_whenAllEmpty() {

var refId = "themeName";
var refType = RefTypeDTO.FAVICON;

mockServerClient.when(request()
.withPath("/internal/images/" + refId + "/" + RefType.FAVICON)
.withMethod(HttpMethod.GET))
.withId(mockId)
.withPriority(100)
.respond(httpRequest -> response().withStatusCode(OK.getStatusCode()));

given()
.when()
.auth().oauth2(keycloakClient.getAccessToken(ADMIN))
.header(APM_HEADER_PARAM, ADMIN)
.contentType(APPLICATION_JSON)
.pathParam("refId", refId)
.pathParam("refType", refType)
.get()
.then()
.statusCode(BAD_REQUEST.getStatusCode());

}

@Test
void uploadImage() {

Expand Down

0 comments on commit f79295b

Please sign in to comment.