From 9b1e6819c398a2d68fef89a9e3d9ca39127a96b6 Mon Sep 17 00:00:00 2001 From: "devender.kumar" Date: Mon, 11 Nov 2019 07:57:48 +0100 Subject: [PATCH] Remove controller --- .../controller/UserController.java | 53 ++++++------ .../PersistenceContextIntegrationTest.java | 83 +++++++++---------- 2 files changed, 68 insertions(+), 68 deletions(-) diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/persistencecontext/controller/UserController.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/persistencecontext/controller/UserController.java index d2746ee6a8..adb3c97cfa 100644 --- a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/persistencecontext/controller/UserController.java +++ b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/persistencecontext/controller/UserController.java @@ -1,51 +1,56 @@ package com.baeldung.persistencecontext.controller; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - import com.baeldung.persistencecontext.entity.User; import com.baeldung.persistencecontext.service.ExtendedPersistenceContextUserService; import com.baeldung.persistencecontext.service.TransctionPersistenceContextUserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; @RestController -public class UserController{ +public class UserController { @Autowired - private TransctionPersistenceContextUserService transctionPersistenceContextUserService; + private TransctionPersistenceContextUserService transctionPersistenceContext; @Autowired - private ExtendedPersistenceContextUserService extendedPersistenceContextUserService; + private ExtendedPersistenceContextUserService extendedPersistenceContext; - @RequestMapping(value = "v1/user", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") + @RequestMapping(value = "v1/user", method = RequestMethod.POST, + consumes = "application/json", + produces = "application/json") public User saveUserV1(@RequestBody User user) { - return transctionPersistenceContextUserService.insertWithTransaction(user); + return transctionPersistenceContext.insertWithTransaction(user); } - @RequestMapping(value = "v2/user", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") + @RequestMapping(value = "v2/user", method = RequestMethod.POST, + consumes = "application/json", + produces = "application/json") public User saveUserV2(@RequestBody User user) { - return extendedPersistenceContextUserService.insertWithoutTransaction(user); + return extendedPersistenceContext.insertWithoutTransaction(user); } - - @RequestMapping(value = "v3/user", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") + + @RequestMapping(value = "v3/user", method = RequestMethod.POST, + consumes = "application/json", + produces = "application/json") public User saveUserV3(@RequestBody User user) { - return extendedPersistenceContextUserService.insertWithTransaction(user); + return extendedPersistenceContext.insertWithTransaction(user); } - - @RequestMapping(value = "v4/user", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") + + @RequestMapping(value = "v4/user", method = RequestMethod.POST, + consumes = "application/json", + produces = "application/json") public User saveUserV4(@RequestBody User user) { - return transctionPersistenceContextUserService.insertWithoutTransaction(user); + return transctionPersistenceContext.insertWithoutTransaction(user); } - @RequestMapping(value = "v1/user/{id}", method = RequestMethod.GET, produces = "application/json") + @RequestMapping(value = "v1/user/{id}", method = RequestMethod.GET, + produces = "application/json") public User getUserV1(@PathVariable("id") Long id) { - return transctionPersistenceContextUserService.find(id); + return transctionPersistenceContext.find(id); } - @RequestMapping(value = "v2/user/{id}", method = RequestMethod.GET, produces = "application/json") + @RequestMapping(value = "v2/user/{id}", method = RequestMethod.GET, + produces = "application/json") public User getUserV2(@PathVariable("id") Long id) { - return extendedPersistenceContextUserService.find(id); + return extendedPersistenceContext.find(id); } } diff --git a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/persistencecontext/PersistenceContextIntegrationTest.java b/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/persistencecontext/PersistenceContextIntegrationTest.java index 98a1a4a6be..0356505a73 100644 --- a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/persistencecontext/PersistenceContextIntegrationTest.java +++ b/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/persistencecontext/PersistenceContextIntegrationTest.java @@ -1,92 +1,87 @@ package com.baeldung.persistencecontext; +import com.baeldung.persistencecontext.entity.User; +import com.baeldung.persistencecontext.service.ExtendedPersistenceContextUserService; +import com.baeldung.persistencecontext.service.TransctionPersistenceContextUserService; + +import javax.persistence.EntityExistsException; +import javax.persistence.TransactionRequiredException; + import org.hamcrest.core.Is; import org.hamcrest.core.IsNull; -import org.junit.Before; 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.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.web.client.HttpServerErrorException; -import org.springframework.web.client.RestTemplate; - -import com.baeldung.persistencecontext.PersistenceContextDemoApplication; -import com.baeldung.persistencecontext.entity.User; import static org.junit.Assert.assertThat; @RunWith(SpringRunner.class) -@SpringBootTest(classes = PersistenceContextDemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) - +@SpringBootTest(classes = PersistenceContextDemoApplication.class) public class PersistenceContextIntegrationTest { - @LocalServerPort - private int port; - private RestTemplate restTemplate; - - @Before - public void setUp() { - restTemplate = new RestTemplate(); - } + + @Autowired + private TransctionPersistenceContextUserService transctionPersistenceContext; + @Autowired + private ExtendedPersistenceContextUserService extendedPersistenceContext; @Test public void testThatWhenUserSavedWithTransctionPersistenceContextThenUserShouldGetSavedInDB() { User user = new User(121L, "Devender", "admin"); - restTemplate.postForEntity(buildRequestUrl("v1/user/"), user, User.class); - User userFromTransctionPersistenceContext = restTemplate.getForObject(buildRequestUrl("v1/user/121"), User.class); - User userFromExtendedPersistenceContext = restTemplate.getForObject(buildRequestUrl("v2/user/121"), User.class); + transctionPersistenceContext.insertWithTransaction(user); + User userFromTransctionPersistenceContext = transctionPersistenceContext.find(user.getId()); + User userFromExtendedPersistenceContext = extendedPersistenceContext.find(user.getId()); assertThat(userFromTransctionPersistenceContext, Is.is(IsNull.notNullValue())); assertThat(userFromExtendedPersistenceContext, Is.is(IsNull.notNullValue())); } - @Test(expected = HttpServerErrorException.class) - public void testThatWhenUserSaveWithOutTransactionInPersistenceContextTypeTransactionThenShouldWhtowTransactionRequiredExceptionOnPersist() { + @Test(expected = TransactionRequiredException.class) + public void testThatWhenUserSaveWithOutTransactionowTransactionRequiredExceptionOnPersist() { User user = new User(122L, "Devender", "admin"); - restTemplate.postForEntity(buildRequestUrl("v4/user/"), user, User.class); + transctionPersistenceContext.insertWithoutTransaction(user); } @Test - public void testThatWhenUserSavedWithExtendedPersistenceContextWithoutTransactionThenUserShouldGetBeCached() { + public void testThatWhenUserSavedWithExtendedPersistenceContextWithoutTransactionThenUserShouldGetCached() { User user = new User(123L, "Devender", "admin"); - restTemplate.postForEntity(buildRequestUrl("v2/user/"), user, User.class); - User userFromExtendedPersistenceContext = restTemplate.getForObject(buildRequestUrl("v2/user/123"), User.class); - User userFromTransctionPersistenceContext = restTemplate.getForObject(buildRequestUrl("v1/user/123"), User.class); + extendedPersistenceContext.insertWithoutTransaction(user); + User userFromExtendedPersistenceContext = extendedPersistenceContext.find(user.getId()); + User userFromTransctionPersistenceContext = transctionPersistenceContext.find(user.getId()); assertThat(userFromExtendedPersistenceContext, Is.is(IsNull.notNullValue())); assertThat(userFromTransctionPersistenceContext, Is.is(IsNull.nullValue())); } - @Test(expected = HttpServerErrorException.class) + @Test(expected = EntityExistsException.class) public void testThatWhenAddUserWithSameIdentifierInPersistenceContextThenShouldThrowException() { - User user = new User(126L, "Devender", "admin"); - restTemplate.postForEntity(buildRequestUrl("v2/user/"), user, User.class); - restTemplate.postForEntity(buildRequestUrl("v2/user/"), user, User.class); + User user1 = new User(126L, "Devender", "admin"); + User user2 = new User(126L, "Devender", "admin"); + extendedPersistenceContext.insertWithoutTransaction(user1); + extendedPersistenceContext.insertWithoutTransaction(user2); } @Test public void testThatWhenUserSavedWithExtendedPersistenceContextWithTransactionThenUserShouldSaveEntityIntoDB() { User user = new User(127L, "Devender", "admin"); - restTemplate.postForEntity(buildRequestUrl("v3/user/"), user, User.class); - User userFromDB = restTemplate.getForObject(buildRequestUrl("v1/user/127"), User.class); + extendedPersistenceContext.insertWithTransaction(user); + User userFromDB = transctionPersistenceContext.find(user.getId()); assertThat(userFromDB, Is.is(IsNull.notNullValue())); } @Test public void testThatWhenUserSavedWithExtendedPersistenceContextWithTransactionThenUserShouldFlushCachedEntityIntoDB() { - User user = new User(124L, "Devender", "admin"); - restTemplate.postForEntity(buildRequestUrl("v2/user/"), user, User.class); - user = new User(125L, "Devender", "admin"); - restTemplate.postForEntity(buildRequestUrl("v3/user/"), user, User.class); - User userFromExtendedPersistenceContextuser1 = restTemplate.getForObject(buildRequestUrl("v2/user/124"), User.class); - User userFromExtendedPersistenceContextuser2 = restTemplate.getForObject(buildRequestUrl("v2/user/125"), User.class); - User userFromTransctionPersistenceContextuser1 = restTemplate.getForObject(buildRequestUrl("v1/user/124"), User.class); - User userFromTransctionPersistenceContextuser2 = restTemplate.getForObject(buildRequestUrl("v1/user/125"), User.class); + User user1 = new User(124L, "Devender", "admin"); + extendedPersistenceContext.insertWithoutTransaction(user1); + User user2 = new User(125L, "Devender", "admin"); + extendedPersistenceContext.insertWithTransaction(user2); + User userFromExtendedPersistenceContextuser1 = extendedPersistenceContext.find(user1.getId()); + User userFromExtendedPersistenceContextuser2 = extendedPersistenceContext.find(user2.getId()); + User userFromTransctionPersistenceContextuser1 = transctionPersistenceContext.find(user1.getId()); + User userFromTransctionPersistenceContextuser2 = transctionPersistenceContext.find(user2.getId()); assertThat(userFromExtendedPersistenceContextuser1, Is.is(IsNull.notNullValue())); assertThat(userFromExtendedPersistenceContextuser2, Is.is(IsNull.notNullValue())); assertThat(userFromTransctionPersistenceContextuser1, Is.is(IsNull.notNullValue())); assertThat(userFromTransctionPersistenceContextuser2, Is.is(IsNull.notNullValue())); } - private String buildRequestUrl(String path) { - return "http://localhost:" + port + path; - } }