Skip to content

Commit

Permalink
Fix missing Jacoco report
Browse files Browse the repository at this point in the history
  • Loading branch information
rashidi committed Dec 21, 2024
1 parent 0abaeb1 commit 7ea503a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 90 deletions.
4 changes: 2 additions & 2 deletions batch-rest-repository/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ In this article, we will explore how to implement batch operation that reads fro

== Job Configuration
Next is to implement the job that will be responsible to read from REST resource and save them into a database. `Job` consists of `Step` and `Step`
consists of `ItemReader` and `ItemWriter`. We will implement all of them in link:{url-quickref}/src/main/java/zin/rashidi/boot/batch/user/UserJobConfiguration.java[UserJobConfiguration].
consists of `ItemReader` and `ItemWriter`. We will implement all of them in link:{url-quickref}/src/main/java/zin/rashidi/boot/batch/rest/user/UserJobConfiguration.java[UserJobConfiguration].

[source,java]
----
Expand Down Expand Up @@ -139,4 +139,4 @@ class UserBatchJobTests {
}
----

Full implementation can be found in link:{url-quickref}/src/test/java/zin/rashidi/boot/batch/user/UserBatchJobTests.java[UserBatchJobTests].
Full implementation can be found in link:{url-quickref}/src/test/java/zin/rashidi/boot/batch/rest/user/UserBatchJobTests.java[UserBatchJobTests].
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zin.rashidi.boot.batch;
package zin.rashidi.boot.batch.rest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package zin.rashidi.boot.batch.user;

import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoId;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
* @author Rashidi Zin
*/
@Document
@JsonIgnoreProperties(ignoreUnknown = true)
record User(@MongoId Long id, String username) {
}
package zin.rashidi.boot.batch.rest.user;

import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoId;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
* @author Rashidi Zin
*/
@Document
@JsonIgnoreProperties(ignoreUnknown = true)
record User(@MongoId Long id, String username) {
}
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
package zin.rashidi.boot.batch.user;

import java.net.MalformedURLException;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.data.MongoItemWriter;
import org.springframework.batch.item.data.builder.MongoItemWriterBuilder;
import org.springframework.batch.item.json.JacksonJsonObjectReader;
import org.springframework.batch.item.json.JsonItemReader;
import org.springframework.batch.item.json.builder.JsonItemReaderBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.UrlResource;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.transaction.PlatformTransactionManager;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* @author Rashidi Zin
*/
@Configuration
class UserJobConfiguration {

private final JobRepository jobRepository;
private final PlatformTransactionManager transactionManager;
private final MongoOperations mongo;

UserJobConfiguration(JobRepository jobRepository, PlatformTransactionManager transactionManager, MongoOperations mongo) {
this.jobRepository = jobRepository;
this.transactionManager = transactionManager;
this.mongo = mongo;
}

@Bean
public Job userJob() throws MalformedURLException {
return new JobBuilder("userJob", jobRepository).start(step()).build();
}

private Step step() throws MalformedURLException {
return new StepBuilder("userStep", jobRepository)
.<User, User>chunk(10, transactionManager)
.reader(reader())
.writer(writer())
.build();
}

private JsonItemReader<User> reader() throws MalformedURLException {
JacksonJsonObjectReader<User> jsonObjectReader = new JacksonJsonObjectReader<>(User.class);

jsonObjectReader.setMapper(new ObjectMapper());

return new JsonItemReaderBuilder<User>()
.name("userReader")
.jsonObjectReader(jsonObjectReader)
.resource(new UrlResource("https://jsonplaceholder.typicode.com/users"))
.build();
}

private MongoItemWriter<User> writer() {
return new MongoItemWriterBuilder<User>()
.template(mongo)
.build();
}

}
package zin.rashidi.boot.batch.rest.user;

import java.net.MalformedURLException;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.data.MongoItemWriter;
import org.springframework.batch.item.data.builder.MongoItemWriterBuilder;
import org.springframework.batch.item.json.JacksonJsonObjectReader;
import org.springframework.batch.item.json.JsonItemReader;
import org.springframework.batch.item.json.builder.JsonItemReaderBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.UrlResource;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.transaction.PlatformTransactionManager;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* @author Rashidi Zin
*/
@Configuration
class UserJobConfiguration {

private final JobRepository jobRepository;
private final PlatformTransactionManager transactionManager;
private final MongoOperations mongo;

UserJobConfiguration(JobRepository jobRepository, PlatformTransactionManager transactionManager, MongoOperations mongo) {
this.jobRepository = jobRepository;
this.transactionManager = transactionManager;
this.mongo = mongo;
}

@Bean
public Job userJob() throws MalformedURLException {
return new JobBuilder("userJob", jobRepository).start(step()).build();
}

private Step step() throws MalformedURLException {
return new StepBuilder("userStep", jobRepository)
.<User, User>chunk(10, transactionManager)
.reader(reader())
.writer(writer())
.build();
}

private JsonItemReader<User> reader() throws MalformedURLException {
JacksonJsonObjectReader<User> jsonObjectReader = new JacksonJsonObjectReader<>(User.class);

jsonObjectReader.setMapper(new ObjectMapper());

return new JsonItemReaderBuilder<User>()
.name("userReader")
.jsonObjectReader(jsonObjectReader)
.resource(new UrlResource("https://jsonplaceholder.typicode.com/users"))
.build();
}

private MongoItemWriter<User> writer() {
return new MongoItemWriterBuilder<User>()
.template(mongo)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package zin.rashidi.boot.batch.user;
package zin.rashidi.boot.batch.rest.user;

import static java.time.Duration.ofSeconds;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.batch.core.ExitStatus.COMPLETED;
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
import static zin.rashidi.boot.batch.user.UserBatchJobTests.BatchTestConfiguration;
import static zin.rashidi.boot.batch.user.UserBatchJobTests.MongoTestConfiguration;
import static zin.rashidi.boot.batch.rest.user.UserBatchJobTests.BatchTestConfiguration;
import static zin.rashidi.boot.batch.rest.user.UserBatchJobTests.MongoTestConfiguration;

import javax.sql.DataSource;

Expand Down
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ subprojects {
apply {
plugin('jacoco')
}

test {
finalizedBy jacocoTestReport
}
}

tasks.named('check') {
Expand Down

0 comments on commit 7ea503a

Please sign in to comment.