Skip to content

Commit 7b6ce5c

Browse files
committed
Merge branch 'v1.0.0-preview.1' into BENCH-255
# Conflicts: # src/main/java/com/answerdigital/answerking/mapper/CategoryMapper.java # src/main/java/com/answerdigital/answerking/model/Category.java # src/main/java/com/answerdigital/answerking/response/CategoryResponse.java # src/test/java/com/answerdigital/answerking/controller/CategoryControllerTest.java
2 parents 76429df + c965364 commit 7b6ce5c

File tree

7 files changed

+64
-40
lines changed

7 files changed

+64
-40
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: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
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;
@@ -12,19 +11,15 @@
1211
import java.util.stream.Collectors;
1312

1413
@Mapper(componentModel = "spring",
15-
imports = {DateTimeUtility.class, Collectors.class, Collections.class})
14+
imports = {Collectors.class, Collections.class})
1615
public interface CategoryMapper {
1716

18-
@Mapping(target = "createdOn", expression = "java(DateTimeUtility.getDateTimeAsString())")
19-
@Mapping(target = "lastUpdated", expression = "java(DateTimeUtility.getDateTimeAsString())")
2017
@Mapping(target = "products", expression = "java(Collections.EMPTY_SET)")
2118
Category addRequestToCategory(CategoryRequest addCategoryRequest);
2219

23-
@Mapping(target = "lastUpdated", expression = "java(DateTimeUtility.getDateTimeAsString())")
2420
Category updateRequestToCategory(@MappingTarget Category category, CategoryRequest updateCategoryRequest);
2521

2622
@Mapping(target = "products",
2723
expression = "java(category.getProducts().stream().map(product -> product.getId()).collect(Collectors.toList()) )")
2824
CategoryResponse convertCategoryEntityToCategoryResponse(Category category);
29-
3025
}

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

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

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
35
import lombok.AccessLevel;
46
import lombok.AllArgsConstructor;
57
import lombok.Builder;
68
import lombok.Getter;
79
import lombok.NoArgsConstructor;
810
import lombok.Setter;
911

10-
import javax.persistence.Entity;
12+
import javax.persistence.OneToMany;
1113
import javax.persistence.FetchType;
14+
import javax.persistence.Entity;
1215
import javax.persistence.GeneratedValue;
1316
import javax.persistence.GenerationType;
1417
import javax.persistence.Id;
15-
import javax.persistence.OneToMany;
1618
import javax.validation.constraints.NotBlank;
1719
import javax.validation.constraints.Pattern;
1820
import java.util.HashSet;
@@ -42,9 +44,13 @@ public class Category {
4244
message = "Category description can only contain letters, numbers, spaces and !?-.,' punctuation")
4345
private String description;
4446

45-
private String createdOn;
47+
@CreationTimestamp
48+
@Column(name = "created_on")
49+
private LocalDateTime createdOn;
4650

47-
private String lastUpdated;
51+
@UpdateTimestamp
52+
@Column(name = "last_updated")
53+
private LocalDateTime lastUpdated;
4854

4955
private boolean retired;
5056

@@ -83,9 +89,13 @@ public int hashCode() {
8389
@Override
8490
public String toString() {
8591
return "Category{" +
86-
"id=" + id +
87-
", name='" + name + '\'' +
88-
", description='" + description + '\'' +
89-
'}';
92+
"id=" + id +
93+
", name='" + name + '\'' +
94+
", description='" + description + '\'' +
95+
", createdOn=" + createdOn +
96+
", lastUpdated=" + lastUpdated +
97+
", retired=" + retired +
98+
", products=" + products +
99+
'}';
90100
}
91101
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import lombok.Data;
77
import lombok.NoArgsConstructor;
88

9+
import java.time.LocalDateTime;
910
import java.util.List;
1011

1112
@Data
@@ -23,9 +24,9 @@ public class CategoryResponse {
2324

2425
private List<Long> products;
2526

26-
private String createdOn;
27+
private LocalDateTime createdOn;
2728

28-
private String lastUpdated;
29+
private LocalDateTime lastUpdated;
2930

3031
private boolean retired;
3132

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: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.answerdigital.answerking.service.CategoryService;
88
import com.answerdigital.answerking.service.ProductService;
99
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.SerializationFeature;
11+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
1012
import org.junit.jupiter.api.Test;
1113
import org.junit.jupiter.api.extension.ExtendWith;
1214
import org.springframework.beans.factory.annotation.Autowired;
@@ -17,19 +19,24 @@
1719
import org.springframework.test.context.junit.jupiter.SpringExtension;
1820
import org.springframework.test.web.servlet.MockMvc;
1921

22+
import java.time.LocalDateTime;
23+
import java.time.temporal.ChronoUnit;
24+
import java.util.HashSet;
2025
import java.util.List;
2126

22-
import static com.answerdigital.answerking.util.DateTimeUtility.getDateTimeAsString;
2327
import static org.junit.jupiter.api.Assertions.assertAll;
2428
import static org.junit.jupiter.api.Assertions.assertEquals;
2529
import static org.junit.jupiter.api.Assertions.assertFalse;
2630
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
2733
import static org.mockito.Mockito.doReturn;
2834
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2935
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
3036
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
3137
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
3238
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
39+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
3340

3441
@AutoConfigureMockMvc(addFilters = false)
3542
@ExtendWith(SpringExtension.class)
@@ -76,7 +83,7 @@ void removeProductFromCategoryTest() throws Exception {
7683
void addCategoryTest() throws Exception {
7784
final ObjectMapper mapper = new ObjectMapper();
7885

79-
final String testDate = getDateTimeAsString();
86+
final LocalDateTime testDate = LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS);
8087
final var addCategoryRequest = new CategoryRequest("random name", "random description");
8188
final var categoryResponse = CategoryResponse.builder()
8289
.name(addCategoryRequest.name())
@@ -97,7 +104,7 @@ void addCategoryTest() throws Exception {
97104
assertFalse(response.getContentAsString().isEmpty());
98105
assertEquals(addCategoryRequest.name(), resultJsonNode.get("name").textValue());
99106
assertEquals(addCategoryRequest.description(), resultJsonNode.get("description").textValue());
100-
assertEquals(testDate.split(" ")[0], resultJsonNode.get("createdOn").textValue().split(" ")[0]);
107+
assertEquals(testDate.toString(), resultJsonNode.get("createdOn").textValue().split(" ")[0]);
101108
}
102109

103110
@Test
@@ -159,19 +166,22 @@ void addCategoryWithInvalidCategoryRequestDescTest() throws Exception {
159166
@Test
160167
void updateCategoryTest() throws Exception {
161168

162-
final ObjectMapper mapper = new ObjectMapper();
169+
final ObjectMapper mapper = new ObjectMapper()
170+
.registerModule(new JavaTimeModule())
171+
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
163172
final var updateCategoryRequest = new CategoryRequest("random name", "random description");
164-
165173
final var newRandomName = "new random name";
166174
final var newRandomDesc = "new random description";
167-
final var category = CategoryResponse.builder()
168-
.name(newRandomName)
169-
.description(newRandomDesc)
170-
.lastUpdated(getDateTimeAsString())
171-
.build();
172175
final var categoryId = 112L;
173176
final var updateCategoryRequestJson = "{\"name\": \"random name\",\"description\": \"random description\"}";
174-
final String testDate = getDateTimeAsString();
177+
final var testDate = LocalDateTime.now();
178+
final var category = CategoryResponse.builder()
179+
.id(categoryId)
180+
.name(newRandomName)
181+
.description(newRandomDesc)
182+
.createdOn(testDate)
183+
.lastUpdated(testDate)
184+
.build();
175185

176186
doReturn(category).when(categoryService).updateCategory(updateCategoryRequest, categoryId);
177187
final var response = mvc.perform(put("/categories/{categoryId}", categoryId)
@@ -185,7 +195,6 @@ void updateCategoryTest() throws Exception {
185195
assertFalse(response.getContentAsString().isEmpty());
186196
assertEquals(newRandomName, resultJsonNode.get("name").textValue());
187197
assertEquals(newRandomDesc, resultJsonNode.get("description").textValue());
188-
assertEquals(testDate.split(" ")[0], resultJsonNode.get("lastUpdated").textValue().split(" ")[0]);
189198
}
190199

191200
@Test

src/test/resources/test_sql_scripts/init_db_for_tests.sql

Lines changed: 4 additions & 4 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-
`createdOn` 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

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

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

0 commit comments

Comments
 (0)