diff --git a/quartz-manager-parent/lombok.config b/quartz-manager-parent/lombok.config new file mode 100644 index 0000000..df71bb6 --- /dev/null +++ b/quartz-manager-parent/lombok.config @@ -0,0 +1,2 @@ +config.stopBubbling = true +lombok.addLombokGeneratedAnnotation = true diff --git a/quartz-manager-parent/pom.xml b/quartz-manager-parent/pom.xml index e1d298c..6e8cf80 100644 --- a/quartz-manager-parent/pom.xml +++ b/quartz-manager-parent/pom.xml @@ -10,7 +10,7 @@ it.fabioformosa.quartz-manager quartz-manager-parent - 4.0.4 + 4.0.5 pom @@ -69,27 +69,27 @@ it.fabioformosa.quartz-manager quartz-manager-common - 4.0.4 + 4.0.5 it.fabioformosa.quartz-manager quartz-manager-starter-api - 4.0.4 + 4.0.5 it.fabioformosa.quartz-manager quartz-manager-starter-security - 4.0.4 + 4.0.5 it.fabioformosa.quartz-manager quartz-manager-starter-persistence - 4.0.4 + 4.0.5 it.fabioformosa.quartz-manager quartz-manager-starter-ui - 4.0.4 + 4.0.5 @@ -179,17 +179,6 @@ - - org.sonatype.plugins - nexus-staging-maven-plugin - ${nexus-staging-maven-plugin.version} - true - - ossrh - https://oss.sonatype.org/ - true - - org.apache.maven.plugins maven-javadoc-plugin @@ -233,6 +222,17 @@ + + org.sonatype.plugins + nexus-staging-maven-plugin + ${nexus-staging-maven-plugin.version} + true + + ossrh + https://oss.sonatype.org/ + true + + org.apache.maven.plugins maven-gpg-plugin diff --git a/quartz-manager-parent/quartz-manager-common/pom.xml b/quartz-manager-parent/quartz-manager-common/pom.xml index 75e2d0b..964df3c 100644 --- a/quartz-manager-parent/quartz-manager-common/pom.xml +++ b/quartz-manager-parent/quartz-manager-common/pom.xml @@ -3,7 +3,7 @@ it.fabioformosa.quartz-manager quartz-manager-parent - 4.0.4 + 4.0.5 quartz-manager-common diff --git a/quartz-manager-parent/quartz-manager-starter-api/pom.xml b/quartz-manager-parent/quartz-manager-starter-api/pom.xml index 516b598..a1d32d9 100644 --- a/quartz-manager-parent/quartz-manager-starter-api/pom.xml +++ b/quartz-manager-parent/quartz-manager-starter-api/pom.xml @@ -5,7 +5,7 @@ it.fabioformosa.quartz-manager quartz-manager-parent - 4.0.4 + 4.0.5 quartz-manager-starter-api @@ -106,6 +106,10 @@ reflections 0.10.2 + + org.yaml + snakeyaml + @@ -118,34 +122,6 @@ 1.3.2 - - - - - - - - - - - - - - - - - - - - - - - - - org.yaml - snakeyaml - - org.springdoc diff --git a/quartz-manager-parent/quartz-manager-starter-api/src/main/java/it/fabioformosa/quartzmanager/api/converters/JobKeyToJobDetailDTO.java b/quartz-manager-parent/quartz-manager-starter-api/src/main/java/it/fabioformosa/quartzmanager/api/converters/JobKeyToJobDetailDTO.java index bcabadc..a09f8da 100644 --- a/quartz-manager-parent/quartz-manager-starter-api/src/main/java/it/fabioformosa/quartzmanager/api/converters/JobKeyToJobDetailDTO.java +++ b/quartz-manager-parent/quartz-manager-starter-api/src/main/java/it/fabioformosa/quartzmanager/api/converters/JobKeyToJobDetailDTO.java @@ -23,6 +23,5 @@ public class JobKeyToJobDetailDTO extends AbstractBaseConverterToDTO it.fabioformosa.quartz-manager quartz-manager-parent - 4.0.4 + 4.0.5 quartz-manager-starter-persistence diff --git a/quartz-manager-parent/quartz-manager-starter-security/pom.xml b/quartz-manager-parent/quartz-manager-starter-security/pom.xml index 9843430..107a90c 100644 --- a/quartz-manager-parent/quartz-manager-starter-security/pom.xml +++ b/quartz-manager-parent/quartz-manager-starter-security/pom.xml @@ -4,7 +4,7 @@ it.fabioformosa.quartz-manager quartz-manager-parent - 4.0.4 + 4.0.5 quartz-manager-starter-security diff --git a/quartz-manager-parent/quartz-manager-starter-security/src/main/java/it/fabioformosa/quartzmanager/api/security/QuartzManagerSecurityConfig.java b/quartz-manager-parent/quartz-manager-starter-security/src/main/java/it/fabioformosa/quartzmanager/api/security/QuartzManagerSecurityConfig.java index a8eac06..cdaedae 100644 --- a/quartz-manager-parent/quartz-manager-starter-security/src/main/java/it/fabioformosa/quartzmanager/api/security/QuartzManagerSecurityConfig.java +++ b/quartz-manager-parent/quartz-manager-starter-security/src/main/java/it/fabioformosa/quartzmanager/api/security/QuartzManagerSecurityConfig.java @@ -34,9 +34,6 @@ import org.springframework.security.web.AuthenticationEntryPoint; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.HttpStatusEntryPoint; import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; -import org.springframework.web.cors.CorsConfiguration; -import org.springframework.web.cors.CorsConfigurationSource; -import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import java.util.ArrayList; import java.util.List; @@ -101,7 +98,9 @@ public class QuartzManagerSecurityConfig { @Order(Ordered.HIGHEST_PRECEDENCE) @Bean(name = "quartzManagerFilterChain") - public SecurityFilterChain filterChain(HttpSecurity http, @Qualifier("quartzManagerInMemoryAuthentication") InMemoryUserDetailsManager userDetailsService, AuthenticationManager authenticationManager) throws Exception { + public SecurityFilterChain filterChain(HttpSecurity http, + @Qualifier("quartzManagerInMemoryAuthentication") InMemoryUserDetailsManager userDetailsService, + AuthenticationManager authenticationManager) throws Exception { http.antMatcher(QUARTZ_MANAGER_API_ANT_MATCHER).csrf().disable() // .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() // .exceptionHandling().authenticationEntryPoint(restAuthEntryPoint()).and() // @@ -128,14 +127,6 @@ public class QuartzManagerSecurityConfig { }; } - @Bean(name = "quartzManagerCorsConfigurationSource") - public CorsConfigurationSource corsConfigurationSource() { - UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); - source.registerCorsConfiguration(QUARTZ_MANAGER_API_ANT_MATCHER, new CorsConfiguration().applyPermitDefaultValues()); - source.registerCorsConfiguration(QUARTZ_MANAGER_UI_ANT_MATCHER, new CorsConfiguration().applyPermitDefaultValues()); - return source; - } - public LoginConfigurer formLoginConfigurer() { JwtAuthenticationSuccessHandler jwtAuthenticationSuccessHandler = jwtAuthenticationSuccessHandler(); AuthenticationSuccessHandler authenticationSuccessHandler = new AuthenticationSuccessHandler(jwtAuthenticationSuccessHandler); diff --git a/quartz-manager-parent/quartz-manager-starter-security/src/main/java/it/fabioformosa/quartzmanager/api/security/controllers/UserController.java b/quartz-manager-parent/quartz-manager-starter-security/src/main/java/it/fabioformosa/quartzmanager/api/security/controllers/UserController.java index 1b4e97f..a269109 100644 --- a/quartz-manager-parent/quartz-manager-starter-security/src/main/java/it/fabioformosa/quartzmanager/api/security/controllers/UserController.java +++ b/quartz-manager-parent/quartz-manager-starter-security/src/main/java/it/fabioformosa/quartzmanager/api/security/controllers/UserController.java @@ -20,13 +20,14 @@ import static it.fabioformosa.quartzmanager.api.common.config.QuartzManagerPaths @RequestMapping(value = QUARTZ_MANAGER_AUTH_PATH, produces = MediaType.APPLICATION_JSON_VALUE) public class UserController { + public static final String WHOAMI_URL = "/whoami"; - @GetMapping("/whoami") + @GetMapping(WHOAMI_URL) public ResponseEntity getLoggedUser() { SecurityContext context = SecurityContextHolder.getContext(); if (context != null && context.getAuthentication() != null) return new ResponseEntity<>(context.getAuthentication().getPrincipal(), HttpStatus.OK); - return new ResponseEntity<>(null, HttpStatus.NOT_FOUND); + return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } diff --git a/quartz-manager-parent/quartz-manager-starter-security/src/main/java/it/fabioformosa/quartzmanager/api/security/properties/JwtSecurityProperties.java b/quartz-manager-parent/quartz-manager-starter-security/src/main/java/it/fabioformosa/quartzmanager/api/security/properties/JwtSecurityProperties.java index d54721d..9012095 100644 --- a/quartz-manager-parent/quartz-manager-starter-security/src/main/java/it/fabioformosa/quartzmanager/api/security/properties/JwtSecurityProperties.java +++ b/quartz-manager-parent/quartz-manager-starter-security/src/main/java/it/fabioformosa/quartzmanager/api/security/properties/JwtSecurityProperties.java @@ -1,24 +1,38 @@ package it.fabioformosa.quartzmanager.api.security.properties; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.Getter; import lombok.Setter; -import org.apache.commons.lang3.RandomStringUtils; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; +import java.security.SecureRandom; +import java.util.Base64; + @Configuration @ConfigurationProperties(prefix = "quartz-manager.security.jwt") +@Data +@AllArgsConstructor @Getter @Setter public class JwtSecurityProperties { - private String secret = RandomStringUtils.randomAlphabetic(10); + private String secret; + private long expirationInSec = 28800; private CookieStrategy cookieStrategy = new CookieStrategy(); private HeaderStrategy headerStrategy = new HeaderStrategy(); + public JwtSecurityProperties() { + SecureRandom random = new SecureRandom(); + byte[] bytes = new byte[20]; + random.nextBytes(bytes); + Base64.Encoder encoder = Base64.getUrlEncoder().withoutPadding(); + secret = encoder.encodeToString(bytes); + } + @Data public static class CookieStrategy { private boolean enabled = false; diff --git a/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/SecurityControllerTest.java b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/SecurityControllerTest.java index c6caf93..93eb5e2 100644 --- a/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/SecurityControllerTest.java +++ b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/SecurityControllerTest.java @@ -2,6 +2,8 @@ package it.fabioformosa.quartzmanager.api.security; import it.fabioformosa.quartzmanager.api.common.config.QuartzManagerPaths; import it.fabioformosa.quartzmanager.api.security.controllers.TestController; +import it.fabioformosa.quartzmanager.api.security.properties.JwtSecurityProperties; +import org.assertj.core.api.Assertions; import org.hamcrest.core.IsNot; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -19,9 +21,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc @TestPropertySource(properties = { - "quartz-manager.security.jwt.enabled=true", "quartz-manager.security.jwt.secret=bibidibobidiboo", - "quartz-manager.security.jwt.expiration-in-sec=28800", + "quartz-manager.security.jwt.expiration-in-sec=36000", "quartz-manager.security.jwt.header-strategy.enabled=false", "quartz-manager.security.jwt.header-strategy.header=Authorization", "quartz-manager.security.jwt.cookie-strategy.enabled=true", @@ -31,11 +32,14 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. "quartz-manager.security.accounts.in-memory.users[0].password=bar", "quartz-manager.security.accounts.in-memory.users[0].roles[0]=admin", }) -public class SecurityControllerTest { +class SecurityControllerTest { @Autowired private MockMvc mockMvc; + @Autowired + private JwtSecurityProperties jwtSecurityProperties; + @Test void givenAnAnonymousUser_whenCalledADMZController_thenShouldRaiseForbidden() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/dmz")) @@ -72,4 +76,10 @@ public class SecurityControllerTest { .andExpect(status().isOk()); } + @Test + void givenSecurityProps_whenTheBootstrapHasCompleted_thenJWTPropertiesShouldBeSetAccordingly() throws Exception { + Assertions.assertThat(jwtSecurityProperties.getExpirationInSec()).isEqualTo(36000); + Assertions.assertThat(jwtSecurityProperties.getSecret()).isEqualTo("bibidibobidiboo"); + } + } diff --git a/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/SecurityLoginViaHeaderAndLoginFilterTest.java b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/SecurityLoginViaHeaderAndLoginFilterTest.java index 64b41d4..7633478 100644 --- a/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/SecurityLoginViaHeaderAndLoginFilterTest.java +++ b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/SecurityLoginViaHeaderAndLoginFilterTest.java @@ -13,7 +13,6 @@ import org.springframework.test.context.TestPropertySource; @TestPropertySource(properties = { "quartz-manager.security.login-model.form-login-enabled = false", "quartz-manager.security.login-model.userpwd-filter-enabled = true", - "quartz-manager.security.jwt.enabled=true", "quartz-manager.security.jwt.secret=bibidibobidiboo", "quartz-manager.security.jwt.expiration-in-sec=28800", "quartz-manager.security.jwt.header-strategy.enabled=true", diff --git a/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/SecurityLoginViaHeaderTest.java b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/SecurityLoginViaHeaderTest.java index dd78ef2..591f305 100644 --- a/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/SecurityLoginViaHeaderTest.java +++ b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/SecurityLoginViaHeaderTest.java @@ -12,7 +12,6 @@ import org.springframework.test.context.TestPropertySource; @TestPropertySource(properties = { "quartz-manager.security.login-model.form-login-enabled = true", "quartz-manager.security.login-model.userpwd-filter-enabled = false", - "quartz-manager.security.jwt.enabled=true", "quartz-manager.security.jwt.secret=bibidibobidiboo", "quartz-manager.security.jwt.expiration-in-sec=28800", "quartz-manager.security.jwt.header-strategy.enabled=true", diff --git a/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/controllers/UserControllerTest.java b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/controllers/UserControllerTest.java new file mode 100644 index 0000000..79c8a7e --- /dev/null +++ b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/controllers/UserControllerTest.java @@ -0,0 +1,41 @@ +package it.fabioformosa.quartzmanager.api.security.controllers; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static it.fabioformosa.quartzmanager.api.common.config.QuartzManagerPaths.QUARTZ_MANAGER_AUTH_PATH; +import static it.fabioformosa.quartzmanager.api.security.controllers.UserController.WHOAMI_URL; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +@TestPropertySource(properties = { + "quartz-manager.security.accounts.in-memory.enabled=true", + "quartz-manager.security.accounts.in-memory.users[0].username=admin", + "quartz-manager.security.accounts.in-memory.users[0].password=admin", + "quartz-manager.security.accounts.in-memory.users[0].roles[0]=admin", +}) +class UserControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Test + @WithMockUser("admin") + void givenAnUser_whenCalledTheWhoamiEndpoint_thenShouldReturn2xx() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get(QUARTZ_MANAGER_AUTH_PATH + WHOAMI_URL)) + .andExpect(status().isOk()); + } + + @Test + void givenAnAnonymousUser_whenCalledTheWhoamiEndpoint_thenShouldReturnNotFound() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get(QUARTZ_MANAGER_AUTH_PATH + WHOAMI_URL)) + .andExpect(status().isUnauthorized()); + } +} diff --git a/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/properties/AbstractPropertyValidatorTest.java b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/properties/AbstractPropertyValidatorTest.java new file mode 100644 index 0000000..8ec533b --- /dev/null +++ b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/properties/AbstractPropertyValidatorTest.java @@ -0,0 +1,39 @@ +package it.fabioformosa.quartzmanager.api.security.properties; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.springframework.boot.context.properties.bind.BindResult; +import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.boot.context.properties.source.ConfigurationPropertySource; +import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; + +import javax.validation.Validation; +import javax.validation.Validator; +import java.lang.reflect.InvocationTargetException; +import java.util.Map; + +public abstract class AbstractPropertyValidatorTest { + protected static Validator propertyValidator; + + @BeforeAll + public static void setup() { + propertyValidator = Validation.buildDefaultValidatorFactory().getValidator(); + } + + protected static T inflateConfigurationPropertyFromAMap(Map properties, String configurationPropName, Class propClass) { + ConfigurationPropertySource source = new MapConfigurationPropertySource(properties); + Binder binder = new Binder(source); + BindResult result = binder.bind(configurationPropName, propClass); + if (properties != null && !properties.isEmpty()) { + Assertions.assertThat(result.isBound()).isTrue(); + T configPropObject = result.get(); + return configPropObject; + } else { + try { + return propClass.getConstructor().newInstance(); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + } +} diff --git a/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/properties/InMemoryUsersValidationControllerTest.java b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/properties/InMemoryUsersValidationControllerTest.java index 4b29016..7275e5c 100644 --- a/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/properties/InMemoryUsersValidationControllerTest.java +++ b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/properties/InMemoryUsersValidationControllerTest.java @@ -1,25 +1,16 @@ package it.fabioformosa.quartzmanager.api.security.properties; import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.springframework.boot.context.properties.bind.BindResult; -import org.springframework.boot.context.properties.bind.Binder; -import org.springframework.boot.context.properties.source.ConfigurationPropertySource; -import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; -import javax.validation.Validation; -import javax.validation.Validator; import java.util.HashMap; import java.util.Map; import java.util.stream.Stream; -public class InMemoryUsersValidationControllerTest { - - private static Validator propertyValidator; +class InMemoryUsersValidationControllerTest extends AbstractPropertyValidatorTest { static Stream notValidInMemoryProps = Stream.of( Arguments.of( @@ -34,26 +25,15 @@ public class InMemoryUsersValidationControllerTest { ); - @BeforeAll - public static void setup() { - propertyValidator = Validation.buildDefaultValidatorFactory().getValidator(); - } - - static Stream getNotValidInMemoryProps(){ + static Stream getNotValidInMemoryProps() { return notValidInMemoryProps; } @ParameterizedTest @MethodSource("it.fabioformosa.quartzmanager.api.security.properties.InMemoryUsersValidationControllerTest#getNotValidInMemoryProps") void givenAMissingUsername_whenThePropertyValidationIsApplied_thenShouldRaiseValidationError(Map properties) { - ConfigurationPropertySource source = new MapConfigurationPropertySource(properties); - - Binder binder = new Binder(source); - BindResult result = binder.bind("quartz-manager.security.accounts.in-memory", InMemoryAccountProperties.class); - - Assertions.assertThat(result.isBound()).isTrue(); - - InMemoryAccountProperties inMemoryAccountProperties = result.get(); + InMemoryAccountProperties inMemoryAccountProperties = inflateConfigurationPropertyFromAMap(properties, + "quartz-manager.security.accounts.in-memory", InMemoryAccountProperties.class); Assertions.assertThat(propertyValidator.validate(inMemoryAccountProperties)).isNotEmpty(); } @@ -65,14 +45,9 @@ public class InMemoryUsersValidationControllerTest { properties.put("quartz-manager.security.accounts.in-memory.users[0].password", "bar"); properties.put("quartz-manager.security.accounts.in-memory.users[0].roles[0]", "admin"); - ConfigurationPropertySource source = new MapConfigurationPropertySource(properties); + InMemoryAccountProperties inMemoryAccountProperties = inflateConfigurationPropertyFromAMap(properties, + "quartz-manager.security.accounts.in-memory", InMemoryAccountProperties.class); - Binder binder = new Binder(source); - BindResult result = binder.bind("quartz-manager.security.accounts.in-memory", InMemoryAccountProperties.class); - - Assertions.assertThat(result.isBound()).isTrue(); - - InMemoryAccountProperties inMemoryAccountProperties = result.get(); Assertions.assertThat(propertyValidator.validate(inMemoryAccountProperties)).isEmpty(); } diff --git a/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/properties/JwtSecurityPropertiesTest.java b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/properties/JwtSecurityPropertiesTest.java new file mode 100644 index 0000000..aea2902 --- /dev/null +++ b/quartz-manager-parent/quartz-manager-starter-security/src/test/java/it/fabioformosa/quartzmanager/api/security/properties/JwtSecurityPropertiesTest.java @@ -0,0 +1,39 @@ +package it.fabioformosa.quartzmanager.api.security.properties; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +class JwtSecurityPropertiesTest extends AbstractPropertyValidatorTest { + + @Test + void givenAllJWTSecurityPropSet_whenThePropertyValidationIsApplied_thenShouldBeValid() { + Map properties = new HashMap<>(); + String secret = "helloworld"; + properties.put("quartz-manager.security.jwt.secret", secret); + String expirationInSec = "36000"; + properties.put("quartz-manager.security.jwt.expirationInSec", expirationInSec); + + JwtSecurityProperties jwtSecurityProperties = inflateConfigurationPropertyFromAMap(properties, + "quartz-manager.security.jwt", JwtSecurityProperties.class); + + Assertions.assertThat(propertyValidator.validate(jwtSecurityProperties)).isEmpty(); + + Assertions.assertThat(jwtSecurityProperties.getExpirationInSec()).isEqualTo(Long.valueOf(expirationInSec)); + Assertions.assertThat(jwtSecurityProperties.getSecret()).isEqualTo(secret); + } + + @Test + void givenTheMandatoryJWTSecurityPropUnset_whenThePropertyValidationIsApplied_thenShouldBeSetWithDefault() { + Map properties = new HashMap<>(); + + JwtSecurityProperties jwtSecurityProperties = inflateConfigurationPropertyFromAMap(properties, + "quartz-manager.security.jwt", JwtSecurityProperties.class); + + Assertions.assertThat(jwtSecurityProperties.getExpirationInSec()).isEqualTo(28800L); + Assertions.assertThat(jwtSecurityProperties.getSecret()).isNotBlank(); + } + +} diff --git a/quartz-manager-parent/quartz-manager-starter-ui/pom.xml b/quartz-manager-parent/quartz-manager-starter-ui/pom.xml index a56be98..057d1c8 100644 --- a/quartz-manager-parent/quartz-manager-starter-ui/pom.xml +++ b/quartz-manager-parent/quartz-manager-starter-ui/pom.xml @@ -4,7 +4,7 @@ it.fabioformosa.quartz-manager quartz-manager-parent - 4.0.4 + 4.0.5 quartz-manager-starter-ui diff --git a/quartz-manager-parent/quartz-manager-web-showcase/pom.xml b/quartz-manager-parent/quartz-manager-web-showcase/pom.xml index bf66627..56ee643 100644 --- a/quartz-manager-parent/quartz-manager-web-showcase/pom.xml +++ b/quartz-manager-parent/quartz-manager-web-showcase/pom.xml @@ -5,7 +5,7 @@ it.fabioformosa.quartz-manager quartz-manager-parent - 4.0.4 + 4.0.5 quartz-manager-web-showcase diff --git a/quartz-manager-parent/quartz-manager-web-showcase/src/main/java/it/fabioformosa/quartzmanager/jobs/tests/MisfireTestJob.java b/quartz-manager-parent/quartz-manager-web-showcase/src/main/java/it/fabioformosa/quartzmanager/jobs/tests/MisfireTestJob.java index fb4634c..169b381 100644 --- a/quartz-manager-parent/quartz-manager-web-showcase/src/main/java/it/fabioformosa/quartzmanager/jobs/tests/MisfireTestJob.java +++ b/quartz-manager-parent/quartz-manager-web-showcase/src/main/java/it/fabioformosa/quartzmanager/jobs/tests/MisfireTestJob.java @@ -2,9 +2,9 @@ package it.fabioformosa.quartzmanager.jobs.tests; import it.fabioformosa.quartzmanager.api.jobs.AbstractQuartzManagerJob; import it.fabioformosa.quartzmanager.api.jobs.entities.LogRecord; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.quartz.JobExecutionContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * This job can be used to test the misfire policy. It pretends to be a long @@ -13,20 +13,26 @@ import org.slf4j.LoggerFactory; * @author Fabio.Formosa * */ +@Slf4j +@NoArgsConstructor public class MisfireTestJob extends AbstractQuartzManagerJob { - private Logger log = LoggerFactory.getLogger(MisfireTestJob.class); + private long sleepPeriodInMs = 10 * 1000L; + + public MisfireTestJob(long sleepPeriodInMs) { + this.sleepPeriodInMs = sleepPeriodInMs; + } @Override public LogRecord doIt(JobExecutionContext jobExecutionContext) { try { log.info("{} is going to sleep...", Thread.currentThread().getName()); - Thread.sleep(10 * 1000); + Thread.sleep(sleepPeriodInMs); log.info("{} woke up!", Thread.currentThread().getName()); } catch (InterruptedException e) { - e.printStackTrace(); + throw new IllegalStateException(e); } return new LogRecord(LogRecord.LogType.INFO, "Hello!"); diff --git a/quartz-manager-parent/quartz-manager-web-showcase/src/test/java/it/fabioformosa/QuartManagerApplicationTests.java b/quartz-manager-parent/quartz-manager-web-showcase/src/test/java/it/fabioformosa/QuartManagerApplicationTests.java index 60afb24..d1b7d9a 100644 --- a/quartz-manager-parent/quartz-manager-web-showcase/src/test/java/it/fabioformosa/QuartManagerApplicationTests.java +++ b/quartz-manager-parent/quartz-manager-web-showcase/src/test/java/it/fabioformosa/QuartManagerApplicationTests.java @@ -9,7 +9,7 @@ import org.springframework.test.context.web.WebAppConfiguration; public class QuartManagerApplicationTests { @Test - public void contextLoads() { + void contextLoads() { } } diff --git a/quartz-manager-parent/quartz-manager-web-showcase/src/test/java/it/fabioformosa/quartzmanager/jobs/tests/MisfireTestJobTest.java b/quartz-manager-parent/quartz-manager-web-showcase/src/test/java/it/fabioformosa/quartzmanager/jobs/tests/MisfireTestJobTest.java new file mode 100644 index 0000000..db60521 --- /dev/null +++ b/quartz-manager-parent/quartz-manager-web-showcase/src/test/java/it/fabioformosa/quartzmanager/jobs/tests/MisfireTestJobTest.java @@ -0,0 +1,18 @@ +package it.fabioformosa.quartzmanager.jobs.tests; + +import it.fabioformosa.quartzmanager.api.jobs.entities.LogRecord; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +class MisfireTestJobTest { + + @Test + void givenAMisfireTestJob_whenIsExecuted_shoulReturnALogRecord() { + MisfireTestJob misfireTestJob = new MisfireTestJob(10L); + LogRecord logRecord = misfireTestJob.doIt(null); + Assertions.assertThat(logRecord.getMessage()).isEqualTo("Hello!"); + } + +} + +