Skip to content

Commit

Permalink
example code for BAEL-1961 (eugenp#5781)
Browse files Browse the repository at this point in the history
* added example code for BAEL-2366

* moved example code for BAEL-2366

* example code for BAEL-1961

* moved example code into integration test

* updated the test assertions
  • Loading branch information
SeunMatt authored and maibin committed Nov 26, 2018
1 parent a1de031 commit 7719b2e
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.baeldung.mongodb;

import com.baeldung.mongodb.daos.UserRepository;
import com.baeldung.mongodb.models.User;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.List;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baeldung.mongodb.daos;


import com.baeldung.mongodb.models.User;
import org.springframework.data.mongodb.repository.MongoRepository;


public interface UserRepository extends MongoRepository<User, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.baeldung.mongodb.events;


import com.baeldung.mongodb.models.User;
import com.baeldung.mongodb.services.SequenceGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent;
import org.springframework.stereotype.Component;


@Component
public class UserModelListener extends AbstractMongoEventListener<User> {

private SequenceGeneratorService sequenceGenerator;

@Autowired
public UserModelListener(SequenceGeneratorService sequenceGenerator) {
this.sequenceGenerator = sequenceGenerator;
}

@Override
public void onBeforeConvert(BeforeConvertEvent<User> event) {
event.getSource().setId(sequenceGenerator.generateSequence(User.SEQUENCE_NAME));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.baeldung.mongodb.models;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;


@Document(collection = "database_sequences")
public class DatabaseSequence {

@Id
private String id;

private long seq;

public DatabaseSequence() {}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public long getSeq() {
return seq;
}

public void setSeq(long seq) {
this.seq = seq;
}
}
73 changes: 73 additions & 0 deletions spring-boot/src/main/java/com/baeldung/mongodb/models/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.baeldung.mongodb.models;

import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;


@Document(collection = "users")
public class User {

@Transient
public static final String SEQUENCE_NAME = "users_sequence";

@Id
private long id;

private String firstName;

private String lastName;

private String email;

public User() { }

public User(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}


@Override
public String toString() {
return "User{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.baeldung.mongodb.services;

import com.baeldung.mongodb.models.DatabaseSequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static org.springframework.data.mongodb.core.FindAndModifyOptions.options;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;


@Service
public class SequenceGeneratorService {


private MongoOperations mongoOperations;

@Autowired
public SequenceGeneratorService(MongoOperations mongoOperations) {
this.mongoOperations = mongoOperations;
}

public long generateSequence(String seqName) {

DatabaseSequence counter = mongoOperations.findAndModify(query(where("_id").is(seqName)),
new Update().inc("seq",1), options().returnNew(true).upsert(true),
DatabaseSequence.class);
return !Objects.isNull(counter) ? counter.getSeq() : 1;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.baeldung.mongodb;

import com.baeldung.mongodb.daos.UserRepository;
import com.baeldung.mongodb.models.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;


@RunWith(SpringRunner.class)
public class MongoDbAutoGeneratedFieldIntegrationTest {

@Autowired
private UserRepository userRepository;

@Test
public void contextLoads() {}

@Test
public void givenUserObject_whenSave_thenCreateNewUser() {

User user = new User();
user.setFirstName("John");
user.setLastName("Doe");
user.setEmail("[email protected]");
userRepository.save(user);

assertThat(userRepository.findAll().size()).isGreaterThan(0);
}


}

0 comments on commit 7719b2e

Please sign in to comment.