Skip to content

Commit

Permalink
Merge pull request #178 from openwms/feature/173_SpringBoot3
Browse files Browse the repository at this point in the history
Feature/173 spring boot3
  • Loading branch information
openwms authored Jun 6, 2024
2 parents a94d832 + 3500534 commit 419dba5
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 23 deletions.
38 changes: 36 additions & 2 deletions org.openwms.core.util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@
<tag>HEAD</tag>
</scm>

<properties>
<dozer.version>6.5.2</dozer.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-core</artifactId>
<version>${dozer.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- Spring Framework -->
<dependency>
Expand Down Expand Up @@ -57,8 +85,8 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -79,5 +107,11 @@
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
package org.openwms.core.http;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.ValidationException;
import org.ameba.LoggingCategories;
import org.ameba.exception.BehaviorAwareException;
import org.ameba.exception.BusinessRuntimeException;
import org.ameba.exception.TechnicalRuntimeException;
import org.ameba.http.AbstractBase;
import org.ameba.http.Response;
import org.openwms.core.exception.ExceptionCodes;
import org.openwms.core.listener.RemovalNotAllowedException;
Expand All @@ -35,9 +37,7 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.util.UriTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.io.Serializable;
import java.net.URI;
import java.util.Locale;

Expand Down Expand Up @@ -73,7 +73,7 @@ protected ResponseEntity<Response<?>> handleRemovalNotAllowedException(RemovalNo
.withMessageKey(rnae.getMessageKey())
.withObj(rnae.getData())
.build(),
HttpStatus.UNAUTHORIZED
HttpStatus.FORBIDDEN
);
}

Expand Down Expand Up @@ -195,7 +195,7 @@ protected String translate(String key, Object... objects) {
* @param params A set of Serializable objects that are passed to the caller
* @return A ResponseEntity with status {@code code}
*/
protected <T extends AbstractBase> ResponseEntity<Response<T>> buildResponse(HttpStatus code, String msg, String msgKey, T... params) {
protected <T extends Serializable> ResponseEntity<Response<T>> buildResponse(HttpStatus code, String msg, String msgKey, T... params) {
Response result = Response.newBuilder().withMessage(msg).withMessageKey(msgKey).withHttpStatus(code.toString()).withObj(params).build();
return new ResponseEntity<>(result, code);
}
Expand All @@ -211,7 +211,7 @@ protected <T extends AbstractBase> ResponseEntity<Response<T>> buildResponse(Htt
* @param params A set of Serializable objects that are passed to the caller
* @return A ResponseEntity with status {@code code}
*/
protected <T extends AbstractBase> ResponseEntity<Response<T>> buildResponse(HttpStatus code, String msg, String msgKey, MultiValueMap<String, String> headers, T... params) {
protected <T extends Serializable> ResponseEntity<Response<T>> buildResponse(HttpStatus code, String msg, String msgKey, MultiValueMap<String, String> headers, T... params) {
Response result = Response.newBuilder().withMessage(msg).withMessageKey(msgKey).withHttpStatus(code.toString()).withObj(params).build();
return new ResponseEntity<>(result, headers, code);
}
Expand All @@ -223,7 +223,7 @@ protected <T extends AbstractBase> ResponseEntity<Response<T>> buildResponse(Htt
* @param params A set of Serializable objects that are passed to the caller
* @return A ResponseEntity with status {@link HttpStatus#OK}
*/
protected <T extends AbstractBase> ResponseEntity<Response<T>> buildOKResponse(T... params) {
protected <T extends Serializable> ResponseEntity<Response<T>> buildOKResponse(T... params) {
return buildResponse(HttpStatus.OK, "", "", params);
}

Expand All @@ -237,7 +237,7 @@ protected <T extends AbstractBase> ResponseEntity<Response<T>> buildOKResponse(T
* @param params A set of Serializable objects that are passed to the caller
* @return A ResponseEntity with status {@code code}
*/
protected <T extends AbstractBase> ResponseEntity<Response<T>> buildNOKResponseWithKey(HttpStatus code, String msg, String msgKey, T... params) {
protected <T extends Serializable> ResponseEntity<Response<T>> buildNOKResponseWithKey(HttpStatus code, String msg, String msgKey, T... params) {
return buildResponse(code, msg, msgKey, params);
}

Expand All @@ -250,7 +250,7 @@ protected <T extends AbstractBase> ResponseEntity<Response<T>> buildNOKResponseW
* @param params A set of Serializable objects that are passed to the caller
* @return A ResponseEntity with status {@code code}
*/
protected <T extends AbstractBase> ResponseEntity<Response<T>> buildNOKResponse(HttpStatus code, String msg, T... params) {
protected <T extends Serializable> ResponseEntity<Response<T>> buildNOKResponse(HttpStatus code, String msg, T... params) {
return buildResponse(code, msg, "", params);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Date;

Expand Down Expand Up @@ -45,35 +46,62 @@ public interface TimeProvider {
*
* @return Timezone aware Date
*/
default Date nowAsDate() {
return Date.from(ZonedDateTime.of(LocalDateTime.now(), ZoneId.systemDefault()).toInstant());
default Date nowAsCurrentDate() {
return Date.from(ZonedDateTime.of(LocalDateTime.now(ZoneId.systemDefault()), ZoneId.systemDefault()).toInstant());
}

/**
* Returns the current date and time in UTC timezone.
*
* @return UTC Date
*/
default Date nowAsZuluDate() {
return Date.from(ZonedDateTime.now(ZoneId.of("Z")).toInstant());
}

/**
* Returns the current date and time considering the configured timezone.
*
* @return Timezone aware DateTime
* @return Timezone aware ZonedDateTime
*/
default ZonedDateTime nowAsZonedDateTime() {
default ZonedDateTime nowAsCurrentZonedDateTime() {
return nowAsZonedDateTime(ZoneId.systemDefault());
}

/**
* Returns the current date and time considering the configured timezone.
*
* @param zoneId ZoneId
* @return Timezone aware DateTime
* @return Timezone aware ZonedDateTime
*/
default ZonedDateTime nowAsZuluZonedDateTime() {
return nowAsZonedDateTime(ZoneOffset.UTC);
}

/**
* Returns the current date and time considering the given timezone.
*
* @param zoneId ZoneId to consider
* @return Timezone aware ZonedDateTime
*/
default ZonedDateTime nowAsZonedDateTime(ZoneId zoneId) {
return ZonedDateTime.of(LocalDateTime.now(), zoneId);
return ZonedDateTime.now(zoneId);
}

/**
* Returns the current date and time of the system considering the configured timezone.
*
* @return Timezone aware Date
* @return Timezone aware Instant
*/
default Instant nowAsCurrentInstant() {
return ZonedDateTime.of(LocalDateTime.now(ZoneId.systemDefault()), ZoneId.systemDefault()).toInstant();
}

/**
* Returns the date and time in UTC timezone.
*
* @return Timezone aware Instant
*/
default Instant now() {
return ZonedDateTime.of(LocalDateTime.now(), ZoneId.systemDefault()).toInstant();
default Instant nowAsZuluInstant() {
return ZonedDateTime.of(LocalDateTime.now(ZoneOffset.UTC), ZoneOffset.UTC).toInstant();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2005-2024 the original author or authors.
*
* 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 org.openwms.core.time;

import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.spy;

/**
* A TimeProviderTest.
*
* @author Heiko Scherrer
*/
class TimeProviderTest {


/**
* This test verifies if the nowAsCurrentDate method returns the current date
* and time considering the system's configured timezone. The date generation
* process by the method is replicated inside the test to compare and validate
* the actual result.
*/
@Test
public void testNowAsCurrentDate() {
TimeProvider timeProvider = new TimeProvider() {};

// Call the method under test
Date actualDate = timeProvider.nowAsCurrentDate();

// Get the expected current date
Date expectedDate = Date.from(ZonedDateTime.of(LocalDateTime.now(ZoneId.systemDefault()), ZoneId.systemDefault()).toInstant());

// Since the date creation and method call can't happen simultaneously,
// there might be slight microseconds difference between actual and expected dates.
// The comparison should be upto the seconds to avoid test failure
long actualTimeInSec = actualDate.getTime() / 1000;
long expectedTimeInSec = expectedDate.getTime() / 1000;

assertEquals(expectedTimeInSec, actualTimeInSec, "The expected and actual dates should be equal");
}

@Test
void nowAsZuluZonedDateTime() {
var tp = spy(TimeProvider.class);


var zuluTime = tp.nowAsZuluZonedDateTime();
System.out.println(zuluTime);

ZonedDateTime nowGMTPlus1 = ZonedDateTime.now(ZoneId.of("GMT+1"));
Date nowZulu = Date.from(nowGMTPlus1.toInstant());
System.out.println(nowGMTPlus1);

assertTrue(zuluTime.isBefore(nowGMTPlus1));
assertTrue(zuluTime.plusMinutes(60).isAfter(nowGMTPlus1));
}

/**
* Trivial implementation of TimeProvider for testing purposes.
*/
private final TimeProvider timeProvider = new TimeProvider() {};

@Test
void testNowAsCurrentInstant() {
Instant testInstant = Instant.now();
Instant methodInstant = timeProvider.nowAsCurrentInstant();

// We only check minutes as the precision of the test might result in a difference in milliseconds
assertEquals(testInstant.atZone(ZoneId.systemDefault()).getMinute(), methodInstant.atZone(ZoneId.systemDefault()).getMinute());
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.openwms</groupId>
<artifactId>org.openwms.parent</artifactId>
<version>25</version>
<version>26</version>
<relativePath />
</parent>
<artifactId>org.openwms</artifactId>
Expand Down

0 comments on commit 419dba5

Please sign in to comment.