Skip to content

Commit

Permalink
[Blazebit#1132] Quarkus extension classloading fixes and JsonIgnore t…
Browse files Browse the repository at this point in the history
…est for Quarkus
  • Loading branch information
Mobe91 authored and beikov committed Nov 4, 2024
1 parent 874bef3 commit 0d4bd81
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.blazebit.persistence.examples.quarkus.base.entity.Document;
import com.blazebit.persistence.examples.quarkus.base.view.DocumentUpdateView;
import com.blazebit.persistence.examples.quarkus.base.view.DocumentView;
import com.blazebit.persistence.examples.quarkus.base.view.DocumentWithJsonIgnoredNameView;
import com.blazebit.persistence.integration.jaxrs.EntityViewId;
import com.blazebit.persistence.view.EntityViewManager;
import com.blazebit.persistence.view.EntityViewSetting;
Expand All @@ -29,6 +30,7 @@
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
Expand All @@ -48,11 +50,11 @@
public class DocumentResource {

@Inject
EntityManager em;
private EntityManager em;
@Inject
EntityViewManager evm;
private EntityViewManager evm;
@Inject
CriteriaBuilderFactory cbf;
private CriteriaBuilderFactory cbf;

@Transactional
@PUT
Expand Down Expand Up @@ -80,4 +82,19 @@ public List<DocumentView> getDocuments(@QueryParam("age") List<Long> ages) {
CriteriaBuilder<Document> cb = cbf.create(em, Document.class).where("age").in().fromValues(Long.class, "age", ages).end();
return evm.applySetting(EntityViewSetting.create(DocumentView.class), cb).getResultList();
}

@GET
@Transactional
@Produces("application/vnd.blazebit.noname+json")
public List<DocumentWithJsonIgnoredNameView> getDocumentsWithJsonIgnoredName(@QueryParam("age") List<Long> ages) {
CriteriaBuilder<Document> cb = cbf.create(em, Document.class).where("age").in().fromValues(Long.class, "age", ages).end();
return evm.applySetting(EntityViewSetting.create(DocumentWithJsonIgnoredNameView.class), cb).getResultList();
}

@DELETE
@Transactional
public Response clearDocuments() {
cbf.delete(em, Document.class).executeUpdate();
return Response.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.blazebit.persistence.examples.quarkus.base.resource;

import com.blazebit.persistence.CriteriaBuilderFactory;
import com.blazebit.persistence.examples.quarkus.base.entity.DocumentType;
import com.blazebit.persistence.examples.quarkus.base.view.DocumentTypeCreateView;
import com.blazebit.persistence.examples.quarkus.base.view.DocumentTypeUpdateView;
import com.blazebit.persistence.examples.quarkus.base.view.DocumentTypeView;
Expand All @@ -25,6 +27,7 @@
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
Expand All @@ -41,9 +44,11 @@
public class DocumentTypeResource {

@Inject
EntityManager em;
private EntityManager em;
@Inject
EntityViewManager evm;
private EntityViewManager evm;
@Inject
private CriteriaBuilderFactory cbf;

@Transactional
@POST
Expand All @@ -63,4 +68,11 @@ public DocumentTypeView updateDocumentType(@EntityViewId("id") DocumentTypeUpdat
evm.save(em, documentTypeUpdateView);
return evm.find(em, DocumentTypeView.class, documentTypeUpdateView.getId());
}

@DELETE
@Transactional
public Response clearDocumentTypes() {
cbf.delete(em, DocumentType.class).executeUpdate();
return Response.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.blazebit.persistence.examples.quarkus.base.resource;

import com.blazebit.persistence.CriteriaBuilderFactory;
import com.blazebit.persistence.examples.quarkus.base.entity.Person;
import com.blazebit.persistence.examples.quarkus.base.view.PersonCreateView;
import com.blazebit.persistence.examples.quarkus.base.view.PersonUpdateView;
import com.blazebit.persistence.examples.quarkus.base.view.PersonView;
Expand All @@ -26,6 +28,7 @@
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
Expand All @@ -42,9 +45,11 @@
public class PersonResource {

@Inject
EntityManager em;
private EntityManager em;
@Inject
EntityViewManager evm;
private EntityViewManager evm;
@Inject
private CriteriaBuilderFactory cbf;

@Transactional
@PUT
Expand All @@ -63,4 +68,11 @@ public Response addPerson(PersonCreateView view) {
evm.save(em, view);
return Response.created(URI.create("/persons/" + view.getId())).build();
}

@DELETE
@Transactional
public Response clearPersons() {
cbf.delete(em, Person.class).executeUpdate();
return Response.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2014 - 2020 Blazebit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blazebit.persistence.examples.quarkus.base.view;

import com.blazebit.persistence.examples.quarkus.base.entity.Document;
import com.blazebit.persistence.view.EntityView;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
* @author Moritz Becker
* @since 1.5.0
*/
@EntityView(Document.class)
public interface DocumentWithJsonIgnoredNameView extends DocumentView {

@JsonIgnore
String getName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@

import io.quarkus.test.common.http.TestHTTPResource;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import java.net.URI;
import java.util.Map;
import java.util.UUID;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.assertFalse;

/**
* @author Moritz Becker
Expand All @@ -35,6 +40,13 @@ public abstract class AbstractQuarkusExampleTest {
@TestHTTPResource
protected URI apiBaseUri;

@AfterEach
public void clearData() {
given().when().delete("/documents");
given().when().delete("/document-types");
given().when().delete("/persons");
}

@Test
public void createDocument() {
given()
Expand All @@ -58,12 +70,35 @@ public void getDocuments() {

given()
.queryParam("age", 1)
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
.when().get("/documents")
.then()
.statusCode(200)
.body("size()", is(1));
}

@Test
public void getDocumentsWithJsonIgnoredName() {
given()
.body("{\"name\": \"Doc1\", \"age\": 1}")
.contentType(ContentType.JSON)
.when().post("/documents")
.then()
.statusCode(201)
.header("Location", matchesPattern("http://localhost:" + apiBaseUri.getPort() + "/documents/.*"));

Map<String, String> document = given()
.queryParam("age", 1)
.header(HttpHeaders.ACCEPT, "application/vnd.blazebit.noname+json")
.when().get("/documents")
.then()
.statusCode(200)
.body("size()", is(1))
.extract().jsonPath().getMap("[0]");

assertFalse(document.containsKey("name"));
}

@Test
public void updateDocumentType() {
given()
Expand Down
Loading

0 comments on commit 0d4bd81

Please sign in to comment.