Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Optimized Entity With Lombok and Added Lombok Dependency #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ResponseEntity<?> getAllTodoItems() {
@RequestMapping(value = "/api/todolist", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> addNewTodoItem(@RequestBody TodoItem item) {
try {
item.setID(UUID.randomUUID().toString());
item.setId(UUID.randomUUID().toString());
todoItemRepository.save(item);
return new ResponseEntity<String>("Entity created", HttpStatus.CREATED);
} catch (Exception e) {
Expand All @@ -77,7 +77,7 @@ public ResponseEntity<String> addNewTodoItem(@RequestBody TodoItem item) {
@RequestMapping(value = "/api/todolist", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> updateTodoItem(@RequestBody TodoItem item) {
try {
todoItemRepository.deleteById(item.getID());
todoItemRepository.deleteById(item.getId());
todoItemRepository.save(item);
return new ResponseEntity<String>("Entity updated", HttpStatus.OK);
} catch (Exception e) {
Expand Down
66 changes: 15 additions & 51 deletions src/main/java/com/microsoft/azure/sample/model/TodoItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,39 @@
package com.microsoft.azure.sample.model;

import com.microsoft.azure.spring.data.cosmosdb.core.mapping.Document;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document
public class TodoItem {
private String id;
private String description;
private String owner;
private boolean finished;

public TodoItem() {
}

public TodoItem(String id, String description, String owner) {
this.description = description;
this.id = id;
this.owner = owner;
this.finished = false;
}

public boolean isFinished() {
return finished;
}

public void setFinish(boolean finished) {
this.finished = finished;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getOwner() {
return owner;
}

public void setOwner(String owner) {
this.owner = owner;
}

public String getID() {
return id;
}

public void setID(String id) {
this.id = id;
public TodoItem(String s, String s1, String s2) {
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof TodoItem)) {
return false;
}
final TodoItem group = (TodoItem) o;
return Objects.equals(this.getDescription(), group.getDescription())
&& Objects.equals(this.getOwner(), group.getOwner())
&& Objects.equals(this.getID(), group.getID());
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TodoItem todoItem = (TodoItem) o;
return finished == todoItem.finished &&
Objects.equals(id, todoItem.id) &&
Objects.equals(description, todoItem.description) &&
Objects.equals(owner, todoItem.owner);
}

@Override
public int hashCode() {
return Objects.hash(description, id, owner);
return Objects.hash(id, description, owner, finished);
}
}

28 changes: 14 additions & 14 deletions src/test/java/com/microsoft/azure/sample/TodoApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public class TodoApplicationTest {
@Before
public void setUp() {
repository.clear();
repository.put(mockItemA.getID(), mockItemA);
repository.put(mockItemB.getID(), mockItemB);
repository.put(mockItemA.getId(), mockItemA);
repository.put(mockItemB.getId(), mockItemB);

given(this.todoItemRepository.save(any(TodoItem.class))).willAnswer((InvocationOnMock invocation) -> {
final TodoItem item = invocation.getArgument(0);
if (repository.containsKey(item.getID())) {
if (repository.containsKey(item.getId())) {
throw new Exception("Conflict.");
}
repository.put(item.getID(), item);
repository.put(item.getId(), item);
return item;
});

Expand Down Expand Up @@ -101,16 +101,16 @@ public void shouldRenderDefaultTemplate() throws Exception {

@Test
public void canGetTodoItem() throws Exception {
mockMvc.perform(get(String.format("/api/todolist/%s", mockItemA.getID()))).andDo(print())
mockMvc.perform(get(String.format("/api/todolist/%s", mockItemA.getId()))).andDo(print())
.andExpect(status().isOk())
.andExpect(content().json(String.format("{\"id\":\"%s\",\"description\":\"%s\",\"owner\":\"%s\"}",
mockItemA.getID(), mockItemA.getDescription(), mockItemA.getOwner())));
mockItemA.getId(), mockItemA.getDescription(), mockItemA.getOwner())));
}

@Test
public void canGetAllTodoItems() throws Exception {
mockMvc.perform(get("/api/todolist")).andDo(print()).andExpect(status().isOk()).andExpect(content()
.json(String.format("[{\"id\":\"%s\"}, {\"id\":\"%s\"}]", mockItemA.getID(), mockItemB.getID())));
.json(String.format("[{\"id\":\"%s\"}, {\"id\":\"%s\"}]", mockItemA.getId(), mockItemB.getId())));
}

@Test
Expand All @@ -126,19 +126,19 @@ public void canSaveTodoItems() throws Exception {
@Test
public void canDeleteTodoItems() throws Exception {
final int size = repository.size();
mockMvc.perform(delete(String.format("/api/todolist/%s", mockItemA.getID()))).andDo(print())
mockMvc.perform(delete(String.format("/api/todolist/%s", mockItemA.getId()))).andDo(print())
.andExpect(status().isOk());
assertTrue(size - 1 == repository.size());
assertFalse(repository.containsKey(mockItemA.getID()));
assertFalse(repository.containsKey(mockItemA.getId()));
}

@Test
public void canUpdateTodoItems() throws Exception {
final String newItemJsonString = String.format("{\"id\":\"%s\",\"description\":\"%s\",\"owner\":\"%s\"}",
mockItemA.getID(), mockItemA.getDescription(), "New Owner");
mockItemA.getId(), mockItemA.getDescription(), "New Owner");
mockMvc.perform(put("/api/todolist").contentType(MediaType.APPLICATION_JSON_VALUE).content(newItemJsonString))
.andDo(print()).andExpect(status().isOk());
assertTrue(repository.get(mockItemA.getID()).getOwner().equals("New Owner"));
assertTrue(repository.get(mockItemA.getId()).getOwner().equals("New Owner"));
}

@Test
Expand All @@ -155,13 +155,13 @@ public void canNotDeleteNonExistingTodoItems() throws Exception {
@Test
public void idempotenceOfPut() throws Exception {
final String newItemJsonString = String.format("{\"id\":\"%s\",\"description\":\"%s\",\"owner\":\"%s\"}",
mockItemA.getID(), mockItemA.getDescription(), "New Owner");
mockItemA.getId(), mockItemA.getDescription(), "New Owner");
mockMvc.perform(put("/api/todolist").contentType(MediaType.APPLICATION_JSON_VALUE).content(newItemJsonString))
.andDo(print()).andExpect(status().isOk());
final TodoItem firstRes = repository.get(mockItemA.getID());
final TodoItem firstRes = repository.get(mockItemA.getId());
mockMvc.perform(put("/api/todolist").contentType(MediaType.APPLICATION_JSON_VALUE).content(newItemJsonString))
.andDo(print()).andExpect(status().isOk());
final TodoItem secondRes = repository.get(mockItemA.getID());
final TodoItem secondRes = repository.get(mockItemA.getId());
assertTrue(firstRes.equals(secondRes));
}
}