Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made refactorings and added more explicit annotations to OrderController
- Loading branch information
Showing
8 changed files
with
67 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
src/main/java/com/dzone/albanoj2/example/rest/repository/IdGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
src/main/java/com/dzone/albanoj2/example/rest/repository/OrderRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 13 additions & 20 deletions
33
src/main/java/com/dzone/albanoj2/example/rest/resource/ResourceAssembler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/test/java/com/dzone/albanoj2/example/rest/test/unit/repository/IdGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters