Skip to content

Commit

Permalink
improve coverage on entities
Browse files Browse the repository at this point in the history
  • Loading branch information
bthreader committed Dec 16, 2023
1 parent 987e532 commit 2bfd4f6
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package servicecourse;
package servicecourse.configuration;

import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package servicecourse.repo;

import jakarta.annotation.Nullable;
import jakarta.persistence.*;
import lombok.*;
import servicecourse.generated.types.Bike;
Expand All @@ -12,7 +13,6 @@
@Entity
@Table(name = "bikes")
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -31,6 +31,7 @@ public class BikeEntity {
@JoinColumn(name = "groupset_name")
private GroupsetEntity groupset;

@Nullable
@Convert(converter = URLConverter.class)
private URL heroImageUrl;

Expand All @@ -55,4 +56,12 @@ public void apply(UpdateBikeParams input) {
input.groupset().ifPresent(this::setGroupset);
input.heroImageUrl().ifPresent(this::setHeroImageUrl);
}

private void setGroupset(GroupsetEntity groupset) {
this.groupset = groupset;
}

private void setHeroImageUrl(URL heroImageUrl) {
this.heroImageUrl = heroImageUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import servicecourse.CacheConfiguration;
import servicecourse.configuration.CacheConfiguration;
import servicecourse.generated.types.BikeBrand;
import servicecourse.generated.types.CreateBikeBrandInput;
import servicecourse.repo.BikeBrandEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Bike updateBike(UpdateBikeInput input) {

// Apply the input
entity.apply(UpdateBikeParams.builder()
.groupset(groupsetEntity)
.groupset(groupsetEntity.orElse(null))
.heroImageUrl(input.getHeroImageUrl())
.build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@

@Builder
public class UpdateBikeParams {
private Optional<GroupsetEntity> groupset;
/** Set to null if no update */
private GroupsetEntity groupset;
/** Set to null if no update */
private URL heroImageUrl;

/**
* @return the new groupset if there is an update, empty otherwise
*/
public Optional<GroupsetEntity> groupset() {
return groupset;
return Optional.ofNullable(groupset);
}

/**
* @return the new hero image URL if there is an update, empty otherwise
*/
public Optional<URL> heroImageUrl() {
return Optional.ofNullable(heroImageUrl);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package servicecourse.repo;

import org.junit.jupiter.api.Test;
import servicecourse.generated.types.BikeBrand;

import static org.assertj.core.api.Assertions.assertThat;

public class BikeBrandEntityTest {
@Test
void asBikeBrand() {
// Given a bike brand
String name = "name";
BikeBrandEntity entity = BikeBrandEntity.ofName(name);

// Then the conversion should be correct
assertThat(entity.asBikeBrand()).isEqualTo(BikeBrand.newBuilder().name(name).build());
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package servicecourse.repo;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import servicecourse.URLFactory;
import servicecourse.generated.types.Bike;
import servicecourse.services.EntityFactory;
import servicecourse.services.bikes.UpdateBikeParams;

import java.net.URL;

Expand Down Expand Up @@ -40,4 +43,81 @@ void asBike() {
// Then asBike should return a Bike object with the expected values
assertThat(bikeEntity.asBike()).isEqualTo(expectedBike);
}

@Nested
class apply {
// The immutable base reference
final BikeEntity baseBikeEntity = BikeEntity.builder()
.id(0L)
.size("medium")
.heroImageUrl(null)
.model(EntityFactory.newModelEntityWithId(0L))
.groupset(EntityFactory.newGroupsetEntityWithName("old"))
.build();
// The mutable entity to compare with the base reference for side effects
BikeEntity bikeEntity;

@BeforeEach
void setup() {
// Reset the bike entity to the base
bikeEntity = BikeEntity.builder()
.id(baseBikeEntity.getId())
.size(baseBikeEntity.getSize())
.heroImageUrl(baseBikeEntity.getHeroImageUrl())
.model(baseBikeEntity.getModel())
.groupset(baseBikeEntity.getGroupset())
.build();
}

@Test
void hero_image_URL_update() {
// Given a new URL
URL newURL = URLFactory.newUrl().orElseThrow();

// When the apply method is called with that URL
bikeEntity.apply(UpdateBikeParams.builder()
.heroImageUrl(newURL)
.build());

// Then the single update should have been applied
assertThat(baseBikeEntity.getHeroImageUrl()).isNotEqualTo(newURL);
assertThat(bikeEntity.getHeroImageUrl()).isEqualTo(newURL);

// Then no other attributes should have been changed
assertThat(bikeEntity.getId()).isEqualTo(baseBikeEntity.getId());
assertThat(bikeEntity.getSize()).isEqualTo(baseBikeEntity.getSize());
assertThat(bikeEntity.getModel()).isEqualTo(baseBikeEntity.getModel());
assertThat(bikeEntity.getGroupset()).isEqualTo(baseBikeEntity.getGroupset());
}

@Test
void groupset_update() {
// Given a new groupset
GroupsetEntity newGroupset = EntityFactory.newGroupsetEntityWithName("new!");

// When the apply method is called with that URL
bikeEntity.apply(UpdateBikeParams.builder()
.groupset(newGroupset)
.build());

// Then a single update should have been applied
assertThat(baseBikeEntity.getGroupset()).isNotEqualTo(newGroupset);
assertThat(bikeEntity.getGroupset()).isEqualTo(newGroupset);

// Then no other attributes should have been changed
assertThat(bikeEntity.getId()).isEqualTo(baseBikeEntity.getId());
assertThat(bikeEntity.getSize()).isEqualTo(baseBikeEntity.getSize());
assertThat(bikeEntity.getModel()).isEqualTo(baseBikeEntity.getModel());
assertThat(bikeEntity.getHeroImageUrl()).isEqualTo(baseBikeEntity.getHeroImageUrl());
}

@Test
void no_update() {
// When the apply method is called with an empty update
bikeEntity.apply(UpdateBikeParams.builder().build());

// There should have been no changes
assertThat(bikeEntity).isEqualTo(baseBikeEntity);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package servicecourse.repo;

import org.junit.jupiter.api.Test;
import servicecourse.generated.types.Groupset;
import servicecourse.generated.types.GroupsetBrand;

import static org.assertj.core.api.Assertions.assertThat;

public class GroupsetEntityTest {
@Test
void asGroupset() {
// Given some attributes
String name = "name";
GroupsetBrand brand = GroupsetBrand.SHIMANO;
boolean isElectronic = false;

// Given an entity with those attributes
GroupsetEntity entity = GroupsetEntity.builder()
.name(name)
.brand(brand)
.isElectronic(isElectronic)
.build();

// Given the equivalent model
Groupset groupset = Groupset.newBuilder()
.name(name)
.brand(brand)
.isElectronic(isElectronic)
.build();

// Then the method applied on the entity should return the groupset equivalent
assertThat(entity.asGroupset()).isEqualTo(groupset);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package servicecourse.repo;

import org.junit.jupiter.api.Test;
import servicecourse.generated.types.BikeBrand;
import servicecourse.generated.types.Model;
import servicecourse.services.models.ModelId;

import static org.assertj.core.api.Assertions.assertThat;

public class ModelEntityTest {
@Test
void asModel() {
// Given some attributes
Long id = 0L;
String name = "name";
int modelYear = 2023;
String brandName = "brandName";

// Given an entity with those attributes
ModelEntity entity = ModelEntity.builder()
.id(id)
.name(name)
.modelYear(modelYear)
.brandName(brandName)
.build();

// Given the equivalent model
Model model = Model.newBuilder()
.id(ModelId.serialize(id))
.name(name)
.modelYear(modelYear)
.brand(BikeBrand.newBuilder()
.name(brandName)
.build())
.build();

// Then the method applied on the entity should return the model equivalent
assertThat(entity.asModel()).isEqualTo(model);
}
}

0 comments on commit 2bfd4f6

Please sign in to comment.