added tests and enabled the schema validation for upload

This commit is contained in:
verboomp
2026-01-28 10:46:38 +01:00
parent 98764dc51e
commit ca514eea67
10 changed files with 228 additions and 24 deletions

View File

@@ -1,17 +1,18 @@
package marketing.heyday.hartmann.fotodocumentation.core.service;
import java.util.List;
import java.util.Optional;
import java.text.ParseException;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import jakarta.annotation.security.PermitAll;
import jakarta.ejb.LocalBean;
import jakarta.ejb.Stateless;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import jakarta.persistence.criteria.*;
import marketing.heyday.hartmann.fotodocumentation.core.model.Customer;
import marketing.heyday.hartmann.fotodocumentation.core.model.Picture;
import marketing.heyday.hartmann.fotodocumentation.core.query.Param;
@@ -32,6 +33,7 @@ import marketing.heyday.hartmann.fotodocumentation.rest.vo.CustomerValue;
@LocalBean
@PermitAll
public class CustomerPictureService extends AbstractService {
private static final Log LOG = LogFactory.getLog(CustomerPictureService.class);
public boolean addCustomerPicture(CustomerPictureValue customerPictureValue) {
Optional<Customer> customerOpt = queryService.callNamedQuerySingleResult(Customer.FIND_BY_NUMBER, new Param(Customer.PARAM_NUMBER, customerPictureValue.customerNumber()));
@@ -52,26 +54,66 @@ public class CustomerPictureService extends AbstractService {
// query = search for name, number and date
public List<CustomerListValue> getAll(String queryStr, String startsWith) {
// FIXME: do query on date
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Customer> criteriaQuery = builder.createQuery(Customer.class);
Root<Customer> customerRoot = criteriaQuery.from(Customer.class);
criteriaQuery = criteriaQuery.select(customerRoot);
criteriaQuery = criteriaQuery.select(customerRoot).distinct(true);
List<Predicate> predicates = new ArrayList<>();
if (StringUtils.isNotBlank(startsWith)) {
String param = startsWith.toLowerCase() + "%";
criteriaQuery = criteriaQuery.where(builder.like(builder.lower(customerRoot.get("name")), param));
var pred = builder.like(builder.lower(customerRoot.get("name")), param);
predicates.add(pred);
}
if (StringUtils.isNotBlank(queryStr)) {
// check if it contains a date
Date date = null;
try {
date = DateUtils.parseDate(queryStr, Locale.GERMAN, "dd.MM.yyyy", "d.M.yyyy", "dd.MM.yy", "d. MMMM yyyy", "dd MMMM yyyy", "dd-MM-yyyy");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date startOfDay = cal.getTime();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 999);
Date endOfDay = cal.getTime();
Fetch<Customer, Picture> picturesFetch = customerRoot.fetch("pictures", JoinType.LEFT);
@SuppressWarnings("unchecked")
Join<Customer, Picture> pictures = (Join<Customer, Picture>) picturesFetch;
var predicateDate = builder.between(pictures.get("pictureDate"), startOfDay, endOfDay);
predicates.add(predicateDate);
} catch (ParseException e) {
LOG.trace("Failed to find date in queryStr " + queryStr);
}
if (date == null) {
String param = "%" + StringUtils.trimToEmpty(queryStr).toLowerCase() + "%";
var predicateName = builder.like(builder.lower(customerRoot.get("name")), param);
var predicateNr = builder.like(builder.lower(customerRoot.get("customerNumber")), param);
var predicate = builder.or(predicateName, predicateNr);
criteriaQuery = criteriaQuery.where(predicate);
var pred = builder.or(predicateName, predicateNr);
predicates.add(pred);
}
}
if (predicates.size() == 1) {
criteriaQuery = criteriaQuery.where(predicates.getFirst());
} else if (predicates.size() > 1) {
criteriaQuery = criteriaQuery.where(builder.and(predicates.toArray(new Predicate[0])));
}
TypedQuery<Customer> typedQuery = entityManager.createQuery(criteriaQuery);

View File

@@ -3,6 +3,8 @@ package marketing.heyday.hartmann.fotodocumentation.rest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -54,6 +56,128 @@ public class CustomerResourceTest extends AbstractRestTest {
jsonAssert(expected, text);
}
@Test
@Order(1)
public void doGetAllStartWith() throws IOException {
LOG.info("doGetAllStartWith");
String authorization = getAuthorization();
LOG.info("authorization: " + authorization);
String path = deploymentURL + PATH + "?startsWith=M";
Request request = Request.Get(path).addHeader("Accept", "application/json; charset=utf-8")
.addHeader("Authorization", authorization);
HttpResponse httpResponse = executeRequest(request);
int code = httpResponse.getStatusLine().getStatusCode();
assertEquals(200, code);
String text = getResponseText(httpResponse, "doGetAllStartWith");
String expected = fileToString(BASE_DOWNLOAD + "doGetAllStartWith.json");
jsonAssert(expected, text);
}
@Test
@Order(1)
public void doGetAllQueryText() throws IOException {
LOG.info("doGetAllQueryText");
String authorization = getAuthorization();
LOG.info("authorization: " + authorization);
String path = deploymentURL + PATH + "?query=2345";
Request request = Request.Get(path).addHeader("Accept", "application/json; charset=utf-8")
.addHeader("Authorization", authorization);
HttpResponse httpResponse = executeRequest(request);
int code = httpResponse.getStatusLine().getStatusCode();
assertEquals(200, code);
String text = getResponseText(httpResponse, "doGetAllQueryText");
String expected = fileToString(BASE_DOWNLOAD + "doGetAllQueryText.json");
jsonAssert(expected, text);
}
@Test
@Order(1)
public void doGetAllQueryTextWithStart() throws IOException {
LOG.info("doGetAllQueryTextWithStart");
String authorization = getAuthorization();
LOG.info("authorization: " + authorization);
String path = deploymentURL + PATH + "?query=45&startsWith=M";
Request request = Request.Get(path).addHeader("Accept", "application/json; charset=utf-8")
.addHeader("Authorization", authorization);
HttpResponse httpResponse = executeRequest(request);
int code = httpResponse.getStatusLine().getStatusCode();
assertEquals(200, code);
String text = getResponseText(httpResponse, "doGetAllQueryTextWithStart");
String expected = fileToString(BASE_DOWNLOAD + "doGetAllQueryTextWithStart.json");
jsonAssert(expected, text);
}
@Test
@Order(1)
public void doGetAllQueryDate1() throws IOException {
LOG.info("doGetAllQueryDate");
String authorization = getAuthorization();
LOG.info("authorization: " + authorization);
String path = deploymentURL + PATH+ "?query=12.01.2026";
Request request = Request.Get(path).addHeader("Accept", "application/json; charset=utf-8")
.addHeader("Authorization", authorization);
HttpResponse httpResponse = executeRequest(request);
int code = httpResponse.getStatusLine().getStatusCode();
assertEquals(200, code);
String text = getResponseText(httpResponse, "doGetAllQueryDate");
String expected = fileToString(BASE_DOWNLOAD + "doGetAllQueryDate.json");
jsonAssert(expected, text);
}
@Test
@Order(1)
public void doGetAllQueryDate2() throws IOException {
LOG.info("doGetAllQueryDate");
String authorization = getAuthorization();
LOG.info("authorization: " + authorization);
String query = URLEncoder.encode("12 Januar 2026", Charset.forName("utf-8"));
String path = deploymentURL + PATH+ "?query="+ query;
Request request = Request.Get(path).addHeader("Accept", "application/json; charset=utf-8")
.addHeader("Authorization", authorization);
HttpResponse httpResponse = executeRequest(request);
int code = httpResponse.getStatusLine().getStatusCode();
assertEquals(200, code);
String text = getResponseText(httpResponse, "doGetAllQueryDate");
String expected = fileToString(BASE_DOWNLOAD + "doGetAllQueryDate.json");
jsonAssert(expected, text);
}
@Test
@Order(1)
public void doGetAllQueryDate3() throws IOException {
LOG.info("doGetAllQueryDate");
String authorization = getAuthorization();
LOG.info("authorization: " + authorization);
String query = URLEncoder.encode("12. Januar 2026", Charset.forName("utf-8"));
String path = deploymentURL + PATH+ "?query="+ query;
Request request = Request.Get(path).addHeader("Accept", "application/json; charset=utf-8")
.addHeader("Authorization", authorization);
HttpResponse httpResponse = executeRequest(request);
int code = httpResponse.getStatusLine().getStatusCode();
assertEquals(200, code);
String text = getResponseText(httpResponse, "doGetAllQueryDate");
String expected = fileToString(BASE_DOWNLOAD + "doGetAllQueryDate.json");
jsonAssert(expected, text);
}
@Test
@Order(1)
public void doGetCustomer() throws IOException {

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,8 @@
[
{
"id": 3,
"name": "Schmidt Apotheke",
"customerNumber": "3456",
"lastUpdateDate": 1768212570000
}
]

View File

@@ -0,0 +1,8 @@
[
{
"id": 2,
"name": "Meier Apotheke",
"customerNumber": "2345",
"lastUpdateDate": 1767607770000
}
]

View File

@@ -0,0 +1,8 @@
[
{
"id": 2,
"name": "Meier Apotheke",
"customerNumber": "2345",
"lastUpdateDate": 1767607770000
}
]

View File

@@ -0,0 +1,14 @@
[
{
"id": 1,
"name": "Müller Apotheke",
"customerNumber": "1234",
"lastUpdateDate": 1767348570000
},
{
"id": 2,
"name": "Meier Apotheke",
"customerNumber": "2345",
"lastUpdateDate": 1767607770000
}
]

View File

@@ -3,18 +3,18 @@
"id": 1,
"name": "Müller Apotheke",
"customerNumber": "1234",
"lastUpdateDate": 1729764570000
"lastUpdateDate": 1767348570000
},
{
"id": 2,
"name": "Meier Apotheke",
"customerNumber": "2345",
"lastUpdateDate": 1729764570000
"lastUpdateDate": 1767607770000
},
{
"id": 3,
"name": "Schmidt Apotheke",
"customerNumber": "3456",
"lastUpdateDate": 1729764570000
"lastUpdateDate": 1768212570000
}
]