Skip to content

Commit

Permalink
Bael 8370 new: Add Execution Metadata to Scheduled Tasks in Spring Bo…
Browse files Browse the repository at this point in the history
…ot Actuator (#18103)

* #BAEL-8370-New: add Jobs

* #BAEL-8370-New: add SecurityConfig

* #BAEL-8370-New: update Spring Boot version to 3.4.0

* #BAEL-8370-New: add htmlunit.version to spring-boot-rest module

* #BAEL-8370-New: revert Spring Boot version

* #BAEL-8370-NEW: revert htmlunit version

* #BAEL-8370-NEW: update Spring Boot version to 3.4.0

* #BAEL-8370-New: update Spring Boot version to 3.4.1

* #BAEL-8370-New: replace removed method in htmlunit

* #BAEL-8370-New: add dependency version for htmlunit

* #BAEL-8370-New: resolve error: com.google.common.util.concurrent.Futures cannot be applied to given types;

* #BAEL-8370-New: revert parent Spring Boot 3

* #BAEL-8370-New: revert change due to Spring Boot 3 Update

* #BAEL-8370-New: revert change due to Spring Boot 3 Update

* #BAEL-8370-New: revert change due to Spring Boot 3 Update

* #BAEL-8370-New: add Spring Boot 3 separately

* #BAEL-8370-New: update JUnit to 5

---------

Co-authored-by: m_mohammadi <m_mohammadi@modernisc.com>
  • Loading branch information
hmdrzsharifi and m_mohammadi authored Jan 4, 2025
1 parent bf63962 commit a76b74b
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 41 deletions.
11 changes: 8 additions & 3 deletions spring-boot-modules/spring-boot-simple/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
<packaging>war</packaging>

<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.0</version>
<relativePath/>
</parent>

<dependencies>
Expand Down Expand Up @@ -106,6 +107,10 @@
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baeldung.actuator;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
@EnableScheduling
public class JobConfig {

@Scheduled(fixedDelay = 1000L)
public void scheduleFixedDelayTask() {
System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000);
}

@Scheduled(fixedRate = 1000L)
public void scheduleFixedRateTask() {
System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000);
}

@Scheduled(cron = "0 15 10 15 * ?")
public void scheduleTaskUsingCronExpression() {
long now = System.currentTimeMillis() / 1000;
System.out.println("schedule tasks using cron jobs - " + now);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.actuator;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Bean
public SecurityFilterChain securityWebFilterChain(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/actuator/**").permitAll()
.anyRequest().authenticated())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.baeldung.actuator;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
public class ActuatorInfoIntegrationTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
import com.baeldung.bootstrap.persistence.model.Book;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import java.util.List;

import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringBootBootstrapLiveTest {

private static final String API_ROOT = "http://localhost:8080/api/books";
@LocalServerPort
private int port;
private String API_ROOT;

@BeforeEach
public void setUp() {
API_ROOT = "http://localhost:" + port + "/api/books";
RestAssured.port = port;
}

@Test
public void whenGetAllBooks_thenOK() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.baeldung.bootstrap;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class SpringContextTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.baeldung.configurationproperties;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.util.unit.DataSize;

import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertEquals;

@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = PropertiesConversionApplication.class)
@TestPropertySource("classpath:conversion.properties")
public class PropertiesConversionIntegrationTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.baeldung.starter;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
Expand All @@ -18,7 +18,7 @@
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class SpringBootApplicationIntegrationTest {
Expand All @@ -28,7 +28,7 @@ public class SpringBootApplicationIntegrationTest {

private MockMvc mockMvc;

@Before
@BeforeEach
public void setupMockMvc() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import com.baeldung.starter.domain.GenericEntity;
import com.baeldung.starter.repository.GenericEntityRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class)
public class SpringBootJPAIntegrationTest {
@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package com.baeldung.starter;

import jakarta.mail.MessagingException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;

import java.io.IOException;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class)
public class SpringBootMailIntegrationTest {
@Autowired
Expand All @@ -33,14 +33,14 @@ public class SpringBootMailIntegrationTest {
private final String subject = "Test subject";
private final String textMail = "Text subject mail";

@Before
@BeforeEach
public void setUp() {
final int TEST_PORT = 8025;
wiser = Wiser.port(TEST_PORT);
wiser.start();
}

@After
@AfterEach
public void tearDown() {
wiser.stop();
}
Expand Down
2 changes: 1 addition & 1 deletion spring-boot-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,4 @@
<jaxb-runtime.version>4.0.1</jaxb-runtime.version>
<spring-oxm.version>6.1.4</spring-oxm.version>
</properties>
</project>
</project>

0 comments on commit a76b74b

Please sign in to comment.