Skip to content

Commit e127a02

Browse files
BENCH-231 Refactored CategoryServiceTest to use builder & remove magic values
1 parent b4810c3 commit e127a02

File tree

9 files changed

+281
-134
lines changed

9 files changed

+281
-134
lines changed

src/main/java/com/answerdigital/answerking/mapper/CategoryMapper.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,21 @@
44
import com.answerdigital.answerking.request.AddCategoryRequest;
55
import com.answerdigital.answerking.request.UpdateCategoryRequest;
66
import com.answerdigital.answerking.response.CategoryResponse;
7+
import com.answerdigital.answerking.util.DateTimeUtility;
78
import org.mapstruct.Mapper;
89
import org.mapstruct.Mapping;
910
import org.mapstruct.MappingTarget;
10-
import java.time.ZonedDateTime;
11-
import java.time.format.DateTimeFormatter;
12-
import java.time.temporal.ChronoUnit;
13-
import java.time.ZoneOffset;
1411
import java.util.stream.Collectors;
1512

1613
@Mapper(componentModel = "spring",
17-
imports = {DateTimeFormatter.class, ZoneOffset.class, ZonedDateTime.class, ChronoUnit.class, Collectors.class})
14+
imports = {DateTimeUtility.class, Collectors.class})
1815
public interface CategoryMapper {
1916

20-
@Mapping(target = "createdOn", expression = "java( ZonedDateTime.now(ZoneOffset.UTC)" +
21-
".truncatedTo( ChronoUnit.SECONDS )" +
22-
".format( DateTimeFormatter.ofPattern( \"yyyy-MM-dd HH:mm:ss\" ) ) )")
23-
@Mapping(target = "lastUpdated", expression = "java( ZonedDateTime.now(ZoneOffset.UTC)" +
24-
".truncatedTo( ChronoUnit.SECONDS )" +
25-
".format( DateTimeFormatter.ofPattern( \"yyyy-MM-dd HH:mm:ss\" ) ) )")
17+
@Mapping(target = "createdOn", expression = "java(DateTimeUtility.getDateTimeAsString())")
18+
@Mapping(target = "lastUpdated", expression = "java(DateTimeUtility.getDateTimeAsString())")
2619
Category addRequestToCategory(AddCategoryRequest addCategoryRequest);
2720

28-
@Mapping(target = "lastUpdated", expression = "java( ZonedDateTime.now(ZoneOffset.UTC)" +
29-
".truncatedTo( ChronoUnit.SECONDS )" +
30-
".format( DateTimeFormatter.ofPattern( \"yyyy-MM-dd HH:mm:ss\" ) ) )")
21+
@Mapping(target = "lastUpdated", expression = "java(DateTimeUtility.getDateTimeAsString())")
3122
Category updateRequestToCategory(@MappingTarget Category category, UpdateCategoryRequest updateCategoryRequest);
3223

3324
@Mapping(target = "productIds",

src/main/java/com/answerdigital/answerking/model/Category.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919
import javax.persistence.ManyToMany;
2020
import javax.validation.constraints.NotBlank;
2121
import javax.validation.constraints.Pattern;
22-
import java.time.ZoneOffset;
23-
import java.time.ZonedDateTime;
24-
import java.time.format.DateTimeFormatter;
25-
import java.time.temporal.ChronoUnit;
2622
import java.util.HashSet;
2723
import java.util.Objects;
2824
import java.util.Set;
2925

26+
import static com.answerdigital.answerking.util.DateTimeUtility.getDateTimeAsString;
27+
3028
@Entity
3129
@Getter
3230
@Setter
@@ -68,9 +66,7 @@ public Category(final String name, final String description) {
6866
this.name = name;
6967
this.description = description;
7068
this.retired = false;
71-
this.createdOn = ZonedDateTime.now(ZoneOffset.UTC)
72-
.truncatedTo( ChronoUnit.SECONDS )
73-
.format( DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm::ss" ) );
69+
this.createdOn = getDateTimeAsString();
7470
this.lastUpdated = this.createdOn;
7571
}
7672

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.answerdigital.answerking.util;
2+
3+
import java.time.ZoneOffset;
4+
import java.time.ZonedDateTime;
5+
import java.time.format.DateTimeFormatter;
6+
import java.time.temporal.ChronoUnit;
7+
8+
public final class DateTimeUtility {
9+
10+
private DateTimeUtility() {}
11+
12+
public static String getDateTimeAsString() {
13+
return ZonedDateTime.now(ZoneOffset.UTC)
14+
.truncatedTo(ChronoUnit.SECONDS)
15+
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.answerdigital.answerking.builder;
2+
3+
import com.answerdigital.answerking.request.AddCategoryRequest;
4+
5+
public class AddCategoryRequestTestBuilder {
6+
7+
private String name = "Burgers";
8+
private String description = "A selection of delicious burgers.";
9+
10+
public AddCategoryRequestTestBuilder withName(String name) {
11+
this.name = name;
12+
return this;
13+
}
14+
15+
public AddCategoryRequestTestBuilder withDescription(String description) {
16+
this.description = description;
17+
return this;
18+
}
19+
20+
public AddCategoryRequest build() {
21+
return new AddCategoryRequest(name, description);
22+
}
23+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.answerdigital.answerking.builder;
2+
3+
import com.answerdigital.answerking.model.Category;
4+
import com.answerdigital.answerking.model.Product;
5+
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
import static com.answerdigital.answerking.util.DateTimeUtility.getDateTimeAsString;
10+
11+
public class CategoryTestBuilder {
12+
13+
private Long id = 1L;
14+
private String name = "Burgers";
15+
private String description = "A selection of delicious burgers.";
16+
private String createdOn = getDateTimeAsString();
17+
private String lastUpdated = getDateTimeAsString();;
18+
private boolean retired = false;
19+
private Set<Product> products = new HashSet<>();
20+
21+
public CategoryTestBuilder withId(Long id) {
22+
this.id = id;
23+
return this;
24+
}
25+
26+
public CategoryTestBuilder withName(String name) {
27+
this.name = name;
28+
return this;
29+
}
30+
31+
public CategoryTestBuilder withDescription(String description) {
32+
this.description = description;
33+
return this;
34+
}
35+
36+
public CategoryTestBuilder withCreatedOn(String createdOn) {
37+
this.createdOn = createdOn;
38+
return this;
39+
}
40+
41+
public CategoryTestBuilder withLastUpdated(String lastUpdated) {
42+
this.lastUpdated = lastUpdated;
43+
return this;
44+
}
45+
46+
public CategoryTestBuilder withRetired(boolean retired) {
47+
this.retired = retired;
48+
return this;
49+
}
50+
51+
public CategoryTestBuilder withProducts(Set<Product> products) {
52+
this.products = products;
53+
return this;
54+
}
55+
56+
// creates a HashSet of one product, to remove boilerplate
57+
public CategoryTestBuilder withProduct(Product product) {
58+
Set<Product> products = new HashSet<>();
59+
products.add(product);
60+
this.products = products;
61+
return this;
62+
}
63+
64+
public Category build() {
65+
return new Category(id, name, description, createdOn, lastUpdated, retired, products);
66+
}
67+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.answerdigital.answerking.builder;
2+
3+
import com.answerdigital.answerking.model.Category;
4+
import com.answerdigital.answerking.model.LineItem;
5+
import com.answerdigital.answerking.model.Product;
6+
7+
import java.math.BigDecimal;
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
11+
public class ProductTestBuilder {
12+
13+
private Long id = 1L;
14+
private String name = "Cheeseburger";
15+
private String description = "A beef patty with cheddar cheese.";
16+
private BigDecimal price = BigDecimal.valueOf(5.00D);
17+
private boolean retired = false;
18+
private Set<Category> categories = new HashSet<>();
19+
private Set<LineItem> lineItems = new HashSet<>();
20+
21+
public ProductTestBuilder withId(Long id) {
22+
this.id = id;
23+
return this;
24+
}
25+
26+
public ProductTestBuilder withName(String name) {
27+
this.name = name;
28+
return this;
29+
}
30+
31+
public ProductTestBuilder withDescription(String description) {
32+
this.description = description;
33+
return this;
34+
}
35+
36+
public ProductTestBuilder withPrice(BigDecimal price) {
37+
this.price = price;
38+
return this;
39+
}
40+
41+
public ProductTestBuilder withRetired(boolean retired) {
42+
this.retired = retired;
43+
return this;
44+
}
45+
46+
public ProductTestBuilder withCategories(Set<Category> categories) {
47+
this.categories = categories;
48+
return this;
49+
}
50+
51+
public ProductTestBuilder withLineItems(Set<LineItem> lineItems) {
52+
this.lineItems = lineItems;
53+
return this;
54+
}
55+
56+
public Product build() {
57+
return new Product(id, name, description, price, retired, categories, lineItems);
58+
}
59+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.answerdigital.answerking.builder;
2+
3+
import com.answerdigital.answerking.request.UpdateCategoryRequest;
4+
5+
public class UpdateCategoryRequestTestBuilder {
6+
7+
private String name = "Pizzas";
8+
private String description = "Italian style stone baked pizzas.";
9+
10+
public UpdateCategoryRequestTestBuilder withName(String name) {
11+
this.name = name;
12+
return this;
13+
}
14+
15+
public UpdateCategoryRequestTestBuilder withDescription(String description) {
16+
this.description = description;
17+
return this;
18+
}
19+
20+
public UpdateCategoryRequest build() {
21+
return new UpdateCategoryRequest(name, description);
22+
}
23+
}

src/test/java/com/answerdigital/answerking/controller/CategoryControllerTest.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616
import org.springframework.test.context.junit.jupiter.SpringExtension;
1717
import org.springframework.test.web.servlet.MockMvc;
1818

19-
import java.time.ZoneId;
20-
import java.time.ZonedDateTime;
21-
import java.time.format.DateTimeFormatter;
22-
import java.time.temporal.ChronoUnit;
23-
19+
import static com.answerdigital.answerking.util.DateTimeUtility.getDateTimeAsString;
2420
import static org.junit.jupiter.api.Assertions.assertTrue;
2521
import static org.junit.jupiter.api.Assertions.assertFalse;
2622
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -67,9 +63,7 @@ void removeProductFromCategoryTest() throws Exception {
6763
void addCategoryTest() throws Exception {
6864
ObjectMapper mapper = new ObjectMapper();
6965

70-
final String testDate = ZonedDateTime.now( ZoneId.of( "Etc/UTC" ) )
71-
.truncatedTo( ChronoUnit.SECONDS )
72-
.format( DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss" ) );
66+
final String testDate = getDateTimeAsString();
7367
final var addCategoryRequest = new AddCategoryRequest("random name", "random description");
7468
final var categoryResponse = CategoryResponse.builder()
7569
.name(addCategoryRequest.name())
@@ -130,9 +124,7 @@ void updateCategoryTest() throws Exception {
130124
final var category = new Category(newRandomName, newRandomDesc);
131125
final var categoryId = 112L;
132126
final var updateCategoryRequestJson = "{\"name\": \"random name\",\"description\": \"random description\"}";
133-
String testDate = ZonedDateTime.now( ZoneId.of( "Etc/UTC" ) )
134-
.truncatedTo( ChronoUnit.SECONDS )
135-
.format( DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss" ) );
127+
String testDate = getDateTimeAsString();
136128

137129
doReturn(category).when(categoryService).updateCategory(updateCategoryRequest, categoryId);
138130
final var response = mvc.perform(put("/categories/{categoryId}", categoryId)

0 commit comments

Comments
 (0)