Skip to content

Commit c965364

Browse files
Merge pull request #108 from AnswerConsulting/BENCH-249
BENCH-49 CATEGORY SWITCHED TO LOCALDATETIME
2 parents bfb667f + f806e0e commit c965364

File tree

7 files changed

+67
-45
lines changed

7 files changed

+67
-45
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE IF NOT EXISTS category (
2+
id BIGINT NOT NULL AUTO_INCREMENT,
3+
name VARCHAR(255),
4+
description TIMESTAMP NOT NULL,
5+
createdOn TIMESTAMP NOT NULL,
6+
lastUpdated VARCHAR(255) NOT NULL,
7+
retired BIT(1) NOT NULL,
8+
9+
PRIMARY KEY (id)
10+
);

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,22 @@
33
import com.answerdigital.answerking.model.Category;
44
import com.answerdigital.answerking.request.CategoryRequest;
55
import com.answerdigital.answerking.response.CategoryResponse;
6-
import com.answerdigital.answerking.util.DateTimeUtility;
76
import org.mapstruct.Mapper;
87
import org.mapstruct.Mapping;
98
import org.mapstruct.MappingTarget;
9+
10+
import java.util.Collections;
1011
import java.util.stream.Collectors;
1112

1213
@Mapper(componentModel = "spring",
13-
imports = {DateTimeUtility.class, Collectors.class})
14+
imports = {Collectors.class, Collections.class})
1415
public interface CategoryMapper {
15-
16-
@Mapping(target = "createdOn", expression = "java(DateTimeUtility.getDateTimeAsString())")
17-
@Mapping(target = "lastUpdated", expression = "java(DateTimeUtility.getDateTimeAsString())")
16+
@Mapping(target = "products", expression = "java(Collections.EMPTY_SET)")
1817
Category addRequestToCategory(CategoryRequest addCategoryRequest);
1918

20-
@Mapping(target = "lastUpdated", expression = "java(DateTimeUtility.getDateTimeAsString())")
2119
Category updateRequestToCategory(@MappingTarget Category category, CategoryRequest updateCategoryRequest);
2220

2321
@Mapping(target = "products",
2422
expression = "java(category.getProducts().stream().map(product -> product.getId()).collect(Collectors.toList()) )")
2523
CategoryResponse convertCategoryEntityToCategoryResponse(Category category);
26-
2724
}

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
package com.answerdigital.answerking.model;
22

33
import com.fasterxml.jackson.annotation.JsonIgnore;
4-
import com.fasterxml.jackson.annotation.JsonProperty;
54
import lombok.AccessLevel;
65
import lombok.AllArgsConstructor;
76
import lombok.Builder;
87
import lombok.Getter;
98
import lombok.NoArgsConstructor;
109
import lombok.Setter;
10+
import org.hibernate.annotations.CreationTimestamp;
11+
import org.hibernate.annotations.UpdateTimestamp;
1112

1213
import javax.persistence.Column;
13-
import javax.persistence.FetchType;
14-
import javax.persistence.OneToMany;
1514
import javax.persistence.Entity;
15+
import javax.persistence.FetchType;
1616
import javax.persistence.GeneratedValue;
1717
import javax.persistence.GenerationType;
1818
import javax.persistence.Id;
19+
import javax.persistence.OneToMany;
1920
import javax.validation.constraints.NotBlank;
2021
import javax.validation.constraints.Pattern;
22+
import java.time.LocalDateTime;
2123
import java.util.HashSet;
2224
import java.util.Objects;
2325
import java.util.Set;
2426

25-
import static com.answerdigital.answerking.util.DateTimeUtility.getDateTimeAsString;
26-
2727
@Entity
2828
@Getter
2929
@Setter
@@ -45,13 +45,13 @@ public class Category {
4545
message = "Category description can only contain letters, numbers, spaces and !?-.,' punctuation")
4646
private String description;
4747

48-
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
48+
@CreationTimestamp
4949
@Column(name = "created_on")
50-
private String createdOn;
50+
private LocalDateTime createdOn;
5151

52-
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
52+
@UpdateTimestamp
5353
@Column(name = "last_updated")
54-
private String lastUpdated;
54+
private LocalDateTime lastUpdated;
5555

5656
private boolean retired;
5757

@@ -63,8 +63,6 @@ public Category(final String name, final String description) {
6363
this.name = name;
6464
this.description = description;
6565
this.retired = false;
66-
this.createdOn = getDateTimeAsString();
67-
this.lastUpdated = this.createdOn;
6866
}
6967

7068
public void addProduct(final Product product) {
@@ -91,9 +89,13 @@ public int hashCode() {
9189
@Override
9290
public String toString() {
9391
return "Category{" +
94-
"id=" + id +
95-
", name='" + name + '\'' +
96-
", description='" + description + '\'' +
97-
'}';
92+
"id=" + id +
93+
", name='" + name + '\'' +
94+
", description='" + description + '\'' +
95+
", createdOn=" + createdOn +
96+
", lastUpdated=" + lastUpdated +
97+
", retired=" + retired +
98+
", products=" + products +
99+
'}';
98100
}
99101
}

src/main/java/com/answerdigital/answerking/response/CategoryResponse.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import lombok.Builder;
66
import lombok.Data;
77
import lombok.NoArgsConstructor;
8+
9+
import java.time.LocalDateTime;
810
import java.util.List;
911

1012
@Data
@@ -24,10 +26,10 @@ public class CategoryResponse {
2426
private List<Long> products;
2527

2628
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
27-
private String createdOn;
29+
private LocalDateTime createdOn;
2830

2931
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
30-
private String lastUpdated;
32+
private LocalDateTime lastUpdated;
3133

3234
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
3335
private boolean retired;

src/test/java/com/answerdigital/answerking/builder/CategoryTestBuilder.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
import com.answerdigital.answerking.model.Category;
44
import com.answerdigital.answerking.model.Product;
55

6+
import java.time.LocalDateTime;
67
import java.util.HashSet;
78
import java.util.Set;
89

9-
import static com.answerdigital.answerking.util.DateTimeUtility.getDateTimeAsString;
10-
1110
public class CategoryTestBuilder {
1211

1312
private Long id = 1L;
@@ -16,9 +15,9 @@ public class CategoryTestBuilder {
1615

1716
private String description;
1817

19-
private String createdOn;
18+
private LocalDateTime createdOn;
2019

21-
private String lastUpdated;
20+
private LocalDateTime lastUpdated;
2221

2322
private boolean retired;
2423

@@ -28,8 +27,8 @@ public CategoryTestBuilder withDefaultValues() {
2827
this.id = 1L;
2928
this.name = "Burgers";
3029
this.description = "A selection of delicious burgers.";
31-
this.createdOn = getDateTimeAsString();
32-
this.lastUpdated = getDateTimeAsString();
30+
this.createdOn = LocalDateTime.now();
31+
this.lastUpdated = LocalDateTime.now();
3332
this.retired = false;
3433
this.products = new HashSet<>();
3534
return this;
@@ -50,12 +49,12 @@ public CategoryTestBuilder withDescription(final String description) {
5049
return this;
5150
}
5251

53-
public CategoryTestBuilder withCreatedOn(final String createdOn) {
52+
public CategoryTestBuilder withCreatedOn(final LocalDateTime createdOn) {
5453
this.createdOn = createdOn;
5554
return this;
5655
}
5756

58-
public CategoryTestBuilder withLastUpdated(final String lastUpdated) {
57+
public CategoryTestBuilder withLastUpdated(final LocalDateTime lastUpdated) {
5958
this.lastUpdated = lastUpdated;
6059
return this;
6160
}

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.answerdigital.answerking.service.CategoryService;
1010
import com.answerdigital.answerking.service.ProductService;
1111
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import com.fasterxml.jackson.databind.SerializationFeature;
13+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
1214
import org.junit.jupiter.api.Test;
1315
import org.junit.jupiter.api.extension.ExtendWith;
1416
import org.springframework.beans.factory.annotation.Autowired;
@@ -18,13 +20,16 @@
1820
import org.springframework.http.MediaType;
1921
import org.springframework.test.context.junit.jupiter.SpringExtension;
2022
import org.springframework.test.web.servlet.MockMvc;
23+
24+
import java.time.LocalDateTime;
25+
import java.time.temporal.ChronoUnit;
26+
import java.util.HashSet;
2127
import java.util.List;
22-
import static com.answerdigital.answerking.util.DateTimeUtility.getDateTimeAsString;
28+
2329
import static org.junit.jupiter.api.Assertions.assertAll;
30+
import static org.junit.jupiter.api.Assertions.assertEquals;
2431
import static org.junit.jupiter.api.Assertions.assertFalse;
2532
import static org.junit.jupiter.api.Assertions.assertTrue;
26-
import static org.junit.jupiter.api.Assertions.assertEquals;
27-
2833
import static org.mockito.Mockito.doReturn;
2934
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
3035
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
@@ -77,7 +82,7 @@ void removeProductFromCategoryTest() throws Exception {
7782
void addCategoryTest() throws Exception {
7883
final ObjectMapper mapper = new ObjectMapper();
7984

80-
final String testDate = getDateTimeAsString();
85+
final LocalDateTime testDate = LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS);
8186
final var addCategoryRequest = new CategoryRequest("random name", "random description");
8287
final var categoryResponse = CategoryResponse.builder()
8388
.name(addCategoryRequest.name())
@@ -98,7 +103,7 @@ void addCategoryTest() throws Exception {
98103
assertFalse(response.getContentAsString().isEmpty());
99104
assertEquals(addCategoryRequest.name(), resultJsonNode.get("name").textValue());
100105
assertEquals(addCategoryRequest.description(), resultJsonNode.get("description").textValue());
101-
assertEquals(testDate.split(" ")[0], resultJsonNode.get("createdOn").textValue().split(" ")[0]);
106+
assertEquals(testDate.toString(), resultJsonNode.get("createdOn").textValue().split(" ")[0]);
102107
}
103108

104109
@Test
@@ -154,15 +159,23 @@ void addCategoryWithInvalidCategoryRequestDescTest() throws Exception {
154159
@Test
155160
void updateCategoryTest() throws Exception {
156161

157-
final ObjectMapper mapper = new ObjectMapper();
162+
final ObjectMapper mapper = new ObjectMapper()
163+
.registerModule(new JavaTimeModule())
164+
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
158165
final var updateCategoryRequest = new CategoryRequest("random name", "random description");
159-
160166
final var newRandomName = "new random name";
161167
final var newRandomDesc = "new random description";
162-
final var category = new Category(newRandomName, newRandomDesc);
163168
final var categoryId = 112L;
164169
final var updateCategoryRequestJson = "{\"name\": \"random name\",\"description\": \"random description\"}";
165-
final String testDate = getDateTimeAsString();
170+
final var testDate = LocalDateTime.now();
171+
final var category = Category.builder()
172+
.id(categoryId)
173+
.name(newRandomName)
174+
.description(newRandomDesc)
175+
.createdOn(testDate)
176+
.lastUpdated(testDate)
177+
.products(new HashSet<>())
178+
.build();
166179

167180
doReturn(category).when(categoryService).updateCategory(updateCategoryRequest, categoryId);
168181
final var response = mvc.perform(put("/categories/{categoryId}", categoryId)
@@ -176,7 +189,6 @@ void updateCategoryTest() throws Exception {
176189
assertFalse(response.getContentAsString().isEmpty());
177190
assertEquals(newRandomName, resultJsonNode.get("name").textValue());
178191
assertEquals(newRandomDesc, resultJsonNode.get("description").textValue());
179-
assertEquals(testDate.split(" ")[0], resultJsonNode.get("lastUpdated").textValue().split(" ")[0]);
180192
}
181193

182194
@Test

src/test/resources/test_sql_scripts/init_db_for_tests.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ CREATE TABLE `category` (
1010
`id` bigint(20) NOT NULL AUTO_INCREMENT,
1111
`name` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
1212
`description` varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
13-
`created_on` VARCHAR(255) DEFAULT NULL,
13+
`created_on` TIMESTAMP NOT NULL,
1414
`retired` bit(1) NOT NULL,
15-
`last_updated` VARCHAR(255) DEFAULT NULL,
15+
`last_updated` TIMESTAMP NOT NULL,
1616
PRIMARY KEY (`id`) USING BTREE
1717
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
1818

1919
INSERT INTO category (name, description, created_on, retired, last_updated)
20-
VALUES('CatOne', 'CatOne description', '', 0, '');
20+
VALUES('CatOne', 'CatOne description', CURRENT_TIMESTAMP, 0, CURRENT_TIMESTAMP);
2121

2222
-- ----------------------------
2323
-- Records of category

0 commit comments

Comments
 (0)