Skip to content

Commit

Permalink
Made refactorings and added more explicit annotations to OrderController
Browse files Browse the repository at this point in the history
  • Loading branch information
albanoj2 committed Sep 20, 2017
1 parent ec46508 commit 92abd44
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 32 deletions.
Expand Up @@ -36,7 +36,7 @@ public ResponseEntity<Collection<OrderResource>> findAllOrders() {
return new ResponseEntity<>(assembler.toResourceCollection(orders), HttpStatus.OK);
}

@RequestMapping(method = RequestMethod.POST)
@RequestMapping(method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<OrderResource> createOrder(@RequestBody Order order) {
Order createdOrder = repository.create(order);
return new ResponseEntity<>(assembler.toResource(createdOrder), HttpStatus.CREATED);
Expand All @@ -61,7 +61,7 @@ public ResponseEntity<Void> deleteOrder(@PathVariable Long id) {
return new ResponseEntity<>(responseStatus);
}

@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = "application/json")
public ResponseEntity<OrderResource> updateOrder(@PathVariable Long id, @RequestBody Order updatedOrder) {
boolean wasUpdated = repository.update(id, updatedOrder);

Expand Down
@@ -1,13 +1,18 @@
package com.dzone.albanoj2.example.rest.repository;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class IdGenerator {

private long nextId = 1;
private AtomicLong nextId = new AtomicLong(1);

public long getNextId() {
return nextId++;
return nextId.getAndIncrement();
}
}
@@ -1,6 +1,7 @@
package com.dzone.albanoj2.example.rest.repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

Expand All @@ -14,7 +15,7 @@ public abstract class InMemoryRepository<T extends Identifiable> {
@Autowired
private IdGenerator idGenerator;

private List<T> elements = new ArrayList<>();
private List<T> elements = Collections.synchronizedList(new ArrayList<>());

public T create(T element) {
elements.add(element);
Expand Down
@@ -1,12 +1,10 @@
package com.dzone.albanoj2.example.rest.repository;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.dzone.albanoj2.example.rest.domain.Order;

@Repository
@Scope("singleton")
public class OrderRepository extends InMemoryRepository<Order> {

protected void updateIfExists(Order original, Order updated) {
Expand Down
@@ -1,12 +1,17 @@
package com.dzone.albanoj2.example.rest.resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.EntityLinks;
import org.springframework.hateoas.Link;
import org.springframework.stereotype.Component;

import com.dzone.albanoj2.example.rest.domain.Order;

@Component
public class OrderResourceAssembler extends ResourceAssembler<Order, OrderResource> {

@Autowired
protected EntityLinks entityLinks;

private static final String UPDATE_REL = "update";
private static final String DELETE_REL = "delete";
Expand All @@ -16,7 +21,7 @@ public OrderResource toResource(Order order) {

OrderResource resource = new OrderResource(order);

final Link selfLink = entityLinks.linkToSingleResource(Order.class, order.getId());
final Link selfLink = entityLinks.linkToSingleResource(order);

resource.add(selfLink.withSelfRel());
resource.add(selfLink.withRel(UPDATE_REL));
Expand Down
@@ -1,20 +1,13 @@
package com.dzone.albanoj2.example.rest.resource;

import java.util.Collection;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.EntityLinks;

public abstract class ResourceAssembler<DomainType, ResourceType> {

@Autowired
protected EntityLinks entityLinks;

public abstract ResourceType toResource(DomainType domainObject);

public Collection<ResourceType> toResourceCollection(Collection<DomainType> domainObjects) {
return domainObjects.stream().map(o -> toResource(o)).collect(Collectors.toList());
}

}
package com.dzone.albanoj2.example.rest.resource;

import java.util.Collection;
import java.util.stream.Collectors;

public abstract class ResourceAssembler<DomainType, ResourceType> {

public abstract ResourceType toResource(DomainType domainObject);

public Collection<ResourceType> toResourceCollection(Collection<DomainType> domainObjects) {
return domainObjects.stream().map(o -> toResource(o)).collect(Collectors.toList());
}
}
@@ -0,0 +1,33 @@
package com.dzone.albanoj2.example.rest.test.unit.repository;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.junit4.SpringRunner;

import com.dzone.albanoj2.example.rest.repository.IdGenerator;


@RunWith(SpringRunner.class)
@SpringBootTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class IdGeneratorTest {

@Autowired
private IdGenerator generator1;

@Autowired
private IdGenerator generator2;

@Test
public void testMultipleGeneratorsEnsureGeneratorsDoNotInterfere() throws Exception {
Assert.assertEquals(1, generator1.getNextId());
Assert.assertEquals(2, generator1.getNextId());
Assert.assertEquals(1, generator2.getNextId());
Assert.assertEquals(2, generator2.getNextId());
}
}
@@ -1,4 +1,4 @@
package com.dzone.albanoj2.example.rest.test.unit;
package com.dzone.albanoj2.example.rest.test.unit.repository;

import static com.dzone.albanoj2.example.rest.test.util.OrderTestUtils.*;

Expand All @@ -22,14 +22,14 @@
public class OrderRepositoryTest {

private static final long NONEXISTENT_ID = 1000;

@Autowired
private OrderRepository repository;

@Before
public void setUp() {
repository.clear();
}

@Autowired
private OrderRepository repository;

@Test
public void testFindNonexistentOrderEnsureOptionalIsNotPresent() throws Exception {
Expand Down

0 comments on commit 92abd44

Please sign in to comment.