Skip to content

Commit 2ac99f2

Browse files
Merge pull request #101 from AnswerConsulting/BENCH-231
BENCH-231 Refactored CategoryServiceTest to use builder & remove magic values
2 parents 6f46ebd + 8c09e90 commit 2ac99f2

File tree

9 files changed

+318
-134
lines changed

9 files changed

+318
-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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.answerdigital.answerking.builder;
2+
3+
import com.answerdigital.answerking.request.AddCategoryRequest;
4+
5+
public class AddCategoryRequestTestBuilder {
6+
7+
private String name;
8+
private String description;
9+
10+
public AddCategoryRequestTestBuilder withDefaultValues() {
11+
this.name = "Burgers";
12+
this.description = "A selection of delicious burgers.";
13+
return this;
14+
}
15+
16+
public AddCategoryRequestTestBuilder withName(final String name) {
17+
this.name = name;
18+
return this;
19+
}
20+
21+
public AddCategoryRequestTestBuilder withDescription(final String description) {
22+
this.description = description;
23+
return this;
24+
}
25+
26+
public AddCategoryRequest build() {
27+
return new AddCategoryRequest(name, description);
28+
}
29+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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;
15+
private String description;
16+
private String createdOn;
17+
private String lastUpdated;
18+
private boolean retired;
19+
private Set<Product> products;
20+
21+
public CategoryTestBuilder withDefaultValues() {
22+
this.id = 1L;
23+
this.name = "Burgers";
24+
this.description = "A selection of delicious burgers.";
25+
this.createdOn = getDateTimeAsString();
26+
this.lastUpdated = getDateTimeAsString();
27+
this.retired = false;
28+
this.products = new HashSet<>();
29+
return this;
30+
}
31+
32+
public CategoryTestBuilder withId(final Long id) {
33+
this.id = id;
34+
return this;
35+
}
36+
37+
public CategoryTestBuilder withName(final String name) {
38+
this.name = name;
39+
return this;
40+
}
41+
42+
public CategoryTestBuilder withDescription(final String description) {
43+
this.description = description;
44+
return this;
45+
}
46+
47+
public CategoryTestBuilder withCreatedOn(final String createdOn) {
48+
this.createdOn = createdOn;
49+
return this;
50+
}
51+
52+
public CategoryTestBuilder withLastUpdated(final String lastUpdated) {
53+
this.lastUpdated = lastUpdated;
54+
return this;
55+
}
56+
57+
public CategoryTestBuilder withRetired(final boolean retired) {
58+
this.retired = retired;
59+
return this;
60+
}
61+
62+
public CategoryTestBuilder withProducts(final Set<Product> products) {
63+
this.products = products;
64+
return this;
65+
}
66+
67+
public CategoryTestBuilder withProduct(final Product product) {
68+
products.add(product);
69+
return this;
70+
}
71+
72+
public Category build() {
73+
return new Category(id, name, description, createdOn, lastUpdated, retired, products);
74+
}
75+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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;
14+
private String name;
15+
private String description;
16+
private BigDecimal price;
17+
private boolean retired;
18+
private Set<Category> categories;
19+
private Set<LineItem> lineItems;
20+
21+
public ProductTestBuilder withDefaultValues() {
22+
this.id = 1L;
23+
this.name = "Cheeseburger";
24+
this.description = "A beef patty with cheddar cheese.";
25+
this.price = BigDecimal.valueOf(5.00D);
26+
this.retired = false;
27+
this.categories = new HashSet<>();
28+
this.lineItems = new HashSet<>();
29+
return this;
30+
}
31+
32+
public ProductTestBuilder withId(final Long id) {
33+
this.id = id;
34+
return this;
35+
}
36+
37+
public ProductTestBuilder withName(final String name) {
38+
this.name = name;
39+
return this;
40+
}
41+
42+
public ProductTestBuilder withDescription(final String description) {
43+
this.description = description;
44+
return this;
45+
}
46+
47+
public ProductTestBuilder withPrice(final BigDecimal price) {
48+
this.price = price;
49+
return this;
50+
}
51+
52+
public ProductTestBuilder withRetired(final boolean retired) {
53+
this.retired = retired;
54+
return this;
55+
}
56+
57+
public ProductTestBuilder withCategories(final Set<Category> categories) {
58+
this.categories = categories;
59+
return this;
60+
}
61+
62+
public ProductTestBuilder withLineItems(final Set<LineItem> lineItems) {
63+
this.lineItems = lineItems;
64+
return this;
65+
}
66+
67+
public Product build() {
68+
return new Product(id, name, description, price, retired, categories, lineItems);
69+
}
70+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.answerdigital.answerking.builder;
2+
3+
import com.answerdigital.answerking.request.UpdateCategoryRequest;
4+
5+
public class UpdateCategoryRequestTestBuilder {
6+
7+
private String name;
8+
private String description;
9+
10+
public UpdateCategoryRequestTestBuilder withDefaultValues() {
11+
this.name = "Pizzas";
12+
this.description = "Italian style stone baked pizzas.";
13+
return this;
14+
}
15+
16+
public UpdateCategoryRequestTestBuilder withName(final String name) {
17+
this.name = name;
18+
return this;
19+
}
20+
21+
public UpdateCategoryRequestTestBuilder withDescription(final String description) {
22+
this.description = description;
23+
return this;
24+
}
25+
26+
public UpdateCategoryRequest build() {
27+
return new UpdateCategoryRequest(name, description);
28+
}
29+
}

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)