Skip to content

Commit

Permalink
Sonarlint recommendations fixes and code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed-taman committed Apr 18, 2020
1 parent 19245a9 commit add5fc3
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public static void main(String[] args) {
ConfigurableApplicationContext ctx =
SpringApplication.run(ProductServiceApplication.class, args);

String mongodDbHost = ctx.getEnvironment().getProperty("spring.data.mongodb.host");
String mongodDbPort = ctx.getEnvironment().getProperty("spring.data.mongodb.port");
log.info("Connected to MongoDb: " + mongodDbHost + ":" + mongodDbPort);
var mongoDbHost = ctx.getEnvironment().getProperty("spring.data.mongodb.host");
var mongoDbPort = ctx.getEnvironment().getProperty("spring.data.mongodb.port");
log.info("Connected to MongoDb: " + mongoDbHost + ":" + mongoDbPort);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.data.domain.Sort.Direction.ASC;

// FIXME to fix all optional class check with isPresent()
@DataMongoTest
public class PersistenceTests {

Expand All @@ -42,10 +41,10 @@ public void setupDb() {
@Test
public void create() {

ProductEntity newEntity = new ProductEntity(2, "n", 2);
var newEntity = new ProductEntity(2, "n", 2);
repository.save(newEntity);

ProductEntity foundEntity = repository.findById(newEntity.getId()).get();
var foundEntity = repository.findById(newEntity.getId()).orElse(new ProductEntity());
assertEqualsProduct(newEntity, foundEntity);

assertEquals(2, repository.count());
Expand All @@ -56,7 +55,7 @@ public void update() {
savedEntity.setName("n2");
repository.save(savedEntity);

ProductEntity foundEntity = repository.findById(savedEntity.getId()).get();
var foundEntity = repository.findById(savedEntity.getId()).orElse(new ProductEntity());
assertEquals(1, (long) foundEntity.getVersion());
assertEquals("n2", foundEntity.getName());
}
Expand Down Expand Up @@ -90,26 +89,27 @@ public void duplicateError() {
public void optimisticLockError() {

// Store the saved entity in two separate entity objects
ProductEntity entity1 = repository.findById(savedEntity.getId()).get();
ProductEntity entity2 = repository.findById(savedEntity.getId()).get();
ProductEntity entity1 = repository.findById(savedEntity.getId()).orElse(new ProductEntity()),
entity2 = repository.findById(savedEntity.getId()).orElse(new ProductEntity());

// Update the entity using the first entity object
entity1.setName("n1");
repository.save(entity1);

// Update the entity using the second entity object.
// This should fail since the second entity now holds a old version number, i.e. a Optimistic
// Lock Error
/*
Update the entity using the second entity object.
This should fail since the second entity now holds a old version number,
i.e. a Optimistic Lock Error.
*/
try {
entity2.setName("n2");
repository.save(entity2);

fail("Expected an OptimisticLockingFailureException");
} catch (OptimisticLockingFailureException ignored) {
}
} catch (OptimisticLockingFailureException ignored) { }

// Get the updated entity from the database and verify its new sate
ProductEntity updatedEntity = repository.findById(savedEntity.getId()).get();
var updatedEntity = repository.findById(savedEntity.getId()).orElse(new ProductEntity());
assertEquals(1, (int) updatedEntity.getVersion());
assertEquals("n1", updatedEntity.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public static void main(String[] args) {
ConfigurableApplicationContext ctx =
SpringApplication.run(RecommendationServiceApplication.class, args);

String mongodDbHost = ctx.getEnvironment().getProperty("spring.data.mongodb.host");
String mongodDbPort = ctx.getEnvironment().getProperty("spring.data.mongodb.port");
log.info("Connected to MongoDb: " + mongodDbHost + ":" + mongodDbPort);
String mongoDbHost = ctx.getEnvironment().getProperty("spring.data.mongodb.host");
String mongoDbPort = ctx.getEnvironment().getProperty("spring.data.mongodb.port");
log.info("Connected to MongoDb: " + mongoDbHost + ":" + mongoDbPort);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.*;

// FIXME to fix all optional class check with isPresent()
@DataMongoTest
public class PersistenceTests {

Expand All @@ -37,10 +36,10 @@ public void setupDb() {
@Test
public void create() {

RecommendationEntity newEntity = new RecommendationEntity(1, 3, "a", 3, "c");
var newEntity = new RecommendationEntity(1, 3, "a", 3, "c");
repository.save(newEntity);

RecommendationEntity foundEntity = repository.findById(newEntity.getId()).get();
var foundEntity = repository.findById(newEntity.getId()).orElse(new RecommendationEntity());
assertEqualsRecommendation(newEntity, foundEntity);

assertEquals(2, repository.count());
Expand Down Expand Up @@ -75,26 +74,26 @@ public void duplicateError() {

Assertions.assertThrows(
DuplicateKeyException.class,
() -> {
RecommendationEntity entity = new RecommendationEntity(1, 2, "a", 3, "c");
repository.save(entity);
});
() -> repository.save(new RecommendationEntity(1, 2, "a", 3, "c")));
}

@Test
public void optimisticLockError() {

// Store the saved entity in two separate entity objects
RecommendationEntity entity1 = repository.findById(savedEntity.getId()).get();
RecommendationEntity entity2 = repository.findById(savedEntity.getId()).get();
RecommendationEntity
entity1 = repository.findById(savedEntity.getId()).orElse(new RecommendationEntity()),
entity2 = repository.findById(savedEntity.getId()).orElse(new RecommendationEntity());

// Update the entity using the first entity object
entity1.setAuthor("a1");
repository.save(entity1);

// Update the entity using the second entity object.
// This should fail since the second entity now holds a old version number, i.e. a Optimistic
// Lock Error
/*
Update the entity using the second entity object.
This should fail since the second entity now holds a old version number,
i.e. a Optimistic Lock Error.
*/
try {
entity2.setAuthor("a2");
repository.save(entity2);
Expand All @@ -104,7 +103,8 @@ public void optimisticLockError() {
}

// Get the updated entity from the database and verify its new sate
RecommendationEntity updatedEntity = repository.findById(savedEntity.getId()).get();
var updatedEntity = repository.findById(savedEntity.getId()).orElse(new RecommendationEntity());

assertEquals(1, (int) updatedEntity.getVersion());
assertEquals("a1", updatedEntity.getAuthor());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class PersistenceTests {
public void setupDb() {
repository.deleteAll();

ReviewEntity entity = new ReviewEntity(1, 2, "amazon", "s", "c");
var entity = new ReviewEntity(1, 2, "amazon", "s", "c");
savedEntity = repository.save(entity);

assertEquals(entity, savedEntity);
Expand All @@ -43,26 +43,23 @@ public void setupDb() {
@Test
public void create() {

ReviewEntity newEntity = new ReviewEntity(1, 3, "amazon 1", "s", "c");
var newEntity = new ReviewEntity(1, 3, "amazon 1", "s", "c");
repository.save(newEntity);

Optional<ReviewEntity> entity = repository.findById(newEntity.getId());
ReviewEntity foundEntity = new ReviewEntity();
if(entity.isPresent()) foundEntity = entity.get();

assertEquals(newEntity, foundEntity);
assertEquals(newEntity, entity.orElse(new ReviewEntity()));

assertEquals(2, repository.count());
}

@Test
public void update() {

savedEntity.setAuthor("amazon 2");
repository.save(savedEntity);

Optional<ReviewEntity> entity = repository.findById(savedEntity.getId());
ReviewEntity foundEntity = new ReviewEntity();
if(entity.isPresent()) foundEntity = entity.get();
var foundEntity = repository.findById(savedEntity.getId()).orElse(new ReviewEntity());

assertEquals(1, (long) foundEntity.getVersion());
assertEquals("amazon 2", foundEntity.getAuthor());
Expand All @@ -71,6 +68,7 @@ public void update() {
@Test
public void delete() {
repository.delete(savedEntity);

assertFalse(repository.existsById(savedEntity.getId()));
}

Expand All @@ -86,34 +84,28 @@ public void getByProductId() {
public void duplicateError() {

Assertions.assertThrows(
DataIntegrityViolationException.class,
() -> {
ReviewEntity entity = new ReviewEntity(1, 2, "amazon 1", "s", "c");
repository.save(entity);
});
DataIntegrityViolationException.class,
() -> repository.save(new ReviewEntity(
1, 2, "amazon 1",
"s", "c")));
}

@Test
public void optimisticLockError() {

ReviewEntity entity1 = new ReviewEntity(),
entity2 = new ReviewEntity();

// Store the saved entity in two separate entity objects
Optional<ReviewEntity> result = repository.findById(savedEntity.getId());
if (result.isPresent()) entity1 = result.get();

Optional<ReviewEntity> result2 = repository.findById(savedEntity.getId());
if (result2.isPresent()) entity2 = result2.get();

ReviewEntity entity1 = repository.findById(savedEntity.getId()).orElse(new ReviewEntity()),
entity2 = repository.findById(savedEntity.getId()).orElse(new ReviewEntity());

// Update the entity using the first entity object
entity1.setAuthor("amazon 1");
repository.save(entity1);

// Update the entity using the second entity object.
// This should fail since the second entity now holds a old version number, i.e. a Optimistic
// Lock Error
/*
Update the entity using the second entity object.
This should fail since the second entity now holds a old version number,
i.e. a Optimistic Lock Error
*/
try {
entity2.setAuthor("amazon 2");
repository.save(entity2);
Expand All @@ -123,9 +115,7 @@ public void optimisticLockError() {
}

// Get the updated entity from the database and verify its new sate
Optional<ReviewEntity> foundEntity = repository.findById(savedEntity.getId());
ReviewEntity updatedEntity = new ReviewEntity();
if(foundEntity.isPresent()) updatedEntity = foundEntity.get();
var updatedEntity = repository.findById(savedEntity.getId()).orElse(new ReviewEntity());

assertEquals(1, updatedEntity.getVersion());
assertEquals("amazon 1", updatedEntity.getAuthor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
@RestController
@Log4j2
public class StoreController implements StoreEndpoint {
/** Store service business logic interface. */

/**
* Store service business logic interface.
*/
private final StoreService storeService;

@Autowired
Expand Down
Loading

0 comments on commit add5fc3

Please sign in to comment.