diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 65b0f247f8..7a305322a6 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -106,6 +106,12 @@ ${jquery.version} + + com.google.guava + guava + 18.0 + + org.apache.tomcat tomcat-servlet-api diff --git a/spring-boot/src/main/java/org/baeldung/boot/components/FooComponent.java b/spring-boot/src/main/java/org/baeldung/boot/components/FooComponent.java new file mode 100644 index 0000000000..b327958c16 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/boot/components/FooComponent.java @@ -0,0 +1,58 @@ +package org.baeldung.boot.components; + +import org.baeldung.boot.exceptions.CommonException; +import org.baeldung.boot.exceptions.FooNotFoundException; +import org.baeldung.boot.model.Foo; +import org.baeldung.boot.repository.FooRepository; +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.google.common.base.Preconditions; + +@Component +public class FooComponent { + + @Autowired + private FooRepository fooRepository; + + private final static Logger logger = org.slf4j.LoggerFactory.getLogger(FooComponent.class); + + public Foo getFooWithId(Integer id) { + + Foo foo = null; + + try { + foo = fooRepository.findOne(id); + Preconditions.checkNotNull(foo); + logger.info("Foo with Id {} found",id); + }catch(NullPointerException ex){ + logger.error("Foo with Id {} was not found",id); + throw new FooNotFoundException("The given foo Id was not found"); + } catch (Exception ex) { + logger.error("Error while retrieving Foo with Id {} found",id,ex); + throw new CommonException("Error while retrieving foo"); + } + + return foo; + } + + public Foo getFooWithName(String name) { + + Foo foo = null; + + try { + foo = fooRepository.findByName(name); + Preconditions.checkNotNull(foo); + logger.info("Foo with name {} found",name); + }catch(NullPointerException ex){ + logger.error("Foo with name {} was not found",name); + throw new FooNotFoundException("The given foo name was not found"); + } catch (Exception ex) { + logger.error("Error while retrieving Foo with name {} found",name,ex); + throw new CommonException("Error while retrieving foo"); + } + + return foo; + } +} \ No newline at end of file diff --git a/spring-boot/src/main/java/org/baeldung/boot/exceptions/CommonException.java b/spring-boot/src/main/java/org/baeldung/boot/exceptions/CommonException.java new file mode 100644 index 0000000000..1f008440e6 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/boot/exceptions/CommonException.java @@ -0,0 +1,13 @@ +package org.baeldung.boot.exceptions; + +public class CommonException extends RuntimeException{ + + /** + * + */ + private static final long serialVersionUID = 3080004140659213332L; + + public CommonException(String message){ + super(message); + } +} diff --git a/spring-boot/src/main/java/org/baeldung/boot/exceptions/FooNotFoundException.java b/spring-boot/src/main/java/org/baeldung/boot/exceptions/FooNotFoundException.java new file mode 100644 index 0000000000..68ef3fa389 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/boot/exceptions/FooNotFoundException.java @@ -0,0 +1,13 @@ +package org.baeldung.boot.exceptions; + +public class FooNotFoundException extends RuntimeException{ + + /** + * + */ + private static final long serialVersionUID = 9042200028456133589L; + + public FooNotFoundException(String message){ + super(message); + } +} diff --git a/spring-boot/src/main/java/org/baeldung/boot/model/Foo.java b/spring-boot/src/main/java/org/baeldung/boot/model/Foo.java index 6a36459e3c..ac8a8fe429 100644 --- a/spring-boot/src/main/java/org/baeldung/boot/model/Foo.java +++ b/spring-boot/src/main/java/org/baeldung/boot/model/Foo.java @@ -21,6 +21,13 @@ public class Foo implements Serializable { this.name = name; } + + public Foo(Integer id, String name) { + super(); + this.id = id; + this.name = name; + } + public Integer getId() { return id; } diff --git a/spring-boot/src/main/java/org/baeldung/boot/service/FooService.java b/spring-boot/src/main/java/org/baeldung/boot/service/FooService.java new file mode 100644 index 0000000000..2cb64ea139 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/boot/service/FooService.java @@ -0,0 +1,52 @@ +package org.baeldung.boot.service; + +import org.baeldung.boot.components.FooComponent; +import org.baeldung.boot.exceptions.CommonException; +import org.baeldung.boot.exceptions.FooNotFoundException; +import org.baeldung.boot.model.Foo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class FooService { + + @Autowired + private FooComponent fooComponent; + + @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity getFooWithId(@PathVariable Integer id) { + + Foo foo = fooComponent.getFooWithId(id); + + return new ResponseEntity(foo, HttpStatus.OK); + } + + @RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity getFooWithName(@RequestParam String name) { + + Foo foo = fooComponent.getFooWithName(name); + + return new ResponseEntity(foo, HttpStatus.OK); + } + + @ExceptionHandler(value = FooNotFoundException.class) + @ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Foo not found") + public void handleFooNotFoundException() { + + } + + @ExceptionHandler(value = CommonException.class) + @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR, reason = "Common exception") + public void handleCommonException() { + + } +} \ No newline at end of file diff --git a/spring-boot/src/test/java/org/baeldung/boot/FooComponentTests.java b/spring-boot/src/test/java/org/baeldung/boot/FooComponentTests.java new file mode 100644 index 0000000000..775f7c1ef0 --- /dev/null +++ b/spring-boot/src/test/java/org/baeldung/boot/FooComponentTests.java @@ -0,0 +1,68 @@ +package org.baeldung.boot; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Mockito.doReturn; + +import java.util.HashMap; +import java.util.Map; + +import org.baeldung.boot.components.FooComponent; +import org.baeldung.boot.model.Foo; +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.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = DemoApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) +public class FooComponentTests { + + @Autowired + private TestRestTemplate testRestTemplate; + + @SpyBean + private FooComponent fooComponent; + + @Before + public void init() { + Foo foo = new Foo(); + foo.setId(5); + foo.setName("MOCKED_FOO"); + + doReturn(foo).when(fooComponent).getFooWithId(anyInt()); + + // doCallRealMethod().when(fooComponent).getFooWithName(anyString()); + } + + @Test + public void givenInquiryingFooWithId_whenFooComponentIsMocked_thenAssertMockedResult() { + Map pathVariables = new HashMap(); + pathVariables.put("id", "1"); + ResponseEntity fooResponse = testRestTemplate.getForEntity("/{id}", Foo.class, pathVariables); + + assertNotNull(fooResponse); + assertEquals(HttpStatus.OK, fooResponse.getStatusCode()); + assertEquals(5, fooResponse.getBody().getId().longValue()); + assertEquals("MOCKED_FOO", fooResponse.getBody().getName()); + } + + @Test + public void givenInquiryingFooWithName_whenFooComponentIsMocked_thenAssertMockedResult() { + Map pathVariables = new HashMap(); + pathVariables.put("name", "Foo_Name"); + ResponseEntity fooResponse = testRestTemplate.getForEntity("/?name={name}", Foo.class, pathVariables); + + assertNotNull(fooResponse); + assertEquals(HttpStatus.OK, fooResponse.getStatusCode()); + assertEquals(1, fooResponse.getBody().getId().longValue()); + } +} \ No newline at end of file diff --git a/spring-boot/src/test/java/org/baeldung/boot/FooIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/boot/FooIntegrationTest.java new file mode 100644 index 0000000000..932cce26d5 --- /dev/null +++ b/spring-boot/src/test/java/org/baeldung/boot/FooIntegrationTest.java @@ -0,0 +1,43 @@ +package org.baeldung.boot; +import java.util.HashMap; +import java.util.Map; + +import org.baeldung.boot.DemoApplication; +import org.baeldung.boot.model.Foo; +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.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes=DemoApplication.class,webEnvironment = WebEnvironment.RANDOM_PORT) +public class FooIntegrationTest { + + @Autowired + private TestRestTemplate testRestTemplate; + + + @Test + public void givenInquiryingFooWithId_whenIdIsValid_thenHttpStatusOK(){ + Map pathVariables = new HashMap(); + pathVariables.put("id", "1"); + ResponseEntity fooResponse = testRestTemplate.getForEntity("/{id}", Foo.class, pathVariables); + Assert.assertNotNull(fooResponse); + Assert.assertEquals(HttpStatus.OK,fooResponse.getStatusCode()); + } + + @Test + public void givenInquiryingFooWithName_whenNameIsValid_thenHttpStatusOK(){ + Map pathVariables = new HashMap(); + pathVariables.put("name", "Foo_Name"); + ResponseEntity fooResponse = testRestTemplate.getForEntity("/?name={name}", Foo.class, pathVariables); + Assert.assertNotNull(fooResponse); + Assert.assertEquals(HttpStatus.OK,fooResponse.getStatusCode()); + } +} \ No newline at end of file diff --git a/spring-boot/src/test/java/org/baeldung/boot/FooJPATest.java b/spring-boot/src/test/java/org/baeldung/boot/FooJPATest.java new file mode 100644 index 0000000000..c29aa64e6c --- /dev/null +++ b/spring-boot/src/test/java/org/baeldung/boot/FooJPATest.java @@ -0,0 +1,34 @@ +package org.baeldung.boot; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.baeldung.boot.model.Foo; +import org.baeldung.boot.repository.FooRepository; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@DataJpaTest +public class FooJPATest { + + @Autowired + private TestEntityManager entityManager; + + @Autowired + private FooRepository repository; + + @Test + public void findFooByName() { + this.entityManager.persist(new Foo("Foo_Name_2")); + Foo foo = this.repository.findByName("Foo_Name_2"); + assertNotNull(foo); + assertEquals("Foo_Name_2",foo.getName()); + // Due to having Insert query for Foo with Id 1, so TestEntityManager generates new Id of 2 + assertEquals(2l,foo.getId().longValue()); + } +} \ No newline at end of file diff --git a/spring-boot/src/test/java/org/baeldung/boot/FooJsonTest.java b/spring-boot/src/test/java/org/baeldung/boot/FooJsonTest.java new file mode 100644 index 0000000000..2789ed0a8c --- /dev/null +++ b/spring-boot/src/test/java/org/baeldung/boot/FooJsonTest.java @@ -0,0 +1,35 @@ +package org.baeldung.boot; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.baeldung.boot.model.Foo; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.JsonTest; +import org.springframework.boot.test.json.JacksonTester; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@JsonTest +public class FooJsonTest { + + @Autowired + private JacksonTester json; + + + @Test + public void testSerialize() throws Exception { + Foo foo = new Foo(3, "Foo_Name_3"); + assertThat(this.json.write(foo)).isEqualToJson("expected.json"); + assertThat(this.json.write(foo)).hasJsonPathStringValue("@.name"); + assertThat(this.json.write(foo)).extractingJsonPathStringValue("@.name").isEqualTo("Foo_Name_3"); + } + + @Test + public void testDeserialize() throws Exception { + String content = "{\"id\":4,\"name\":\"Foo_Name_4\"}"; + assertThat(this.json.parseObject(content).getName()).isEqualTo("Foo_Name_4"); + assertThat(this.json.parseObject(content).getId()==4); + } +} \ No newline at end of file diff --git a/spring-boot/src/test/resources/application.properties b/spring-boot/src/test/resources/application.properties index 14b190629e..0e6cb86bc5 100644 --- a/spring-boot/src/test/resources/application.properties +++ b/spring-boot/src/test/resources/application.properties @@ -1,3 +1,5 @@ spring.mail.host=localhost spring.mail.port=8025 -spring.mail.properties.mail.smtp.auth=false \ No newline at end of file +spring.mail.properties.mail.smtp.auth=false + +security.basic.enabled=false \ No newline at end of file diff --git a/spring-boot/src/test/resources/import.sql b/spring-boot/src/test/resources/import.sql new file mode 100644 index 0000000000..a382410271 --- /dev/null +++ b/spring-boot/src/test/resources/import.sql @@ -0,0 +1 @@ +Insert into Foo values(1,'Foo_Name'); \ No newline at end of file diff --git a/spring-boot/src/test/resources/org/baeldung/boot/expected.json b/spring-boot/src/test/resources/org/baeldung/boot/expected.json new file mode 100644 index 0000000000..f5409421a6 --- /dev/null +++ b/spring-boot/src/test/resources/org/baeldung/boot/expected.json @@ -0,0 +1,4 @@ +{ + "id":3, + "name":"Foo_Name_3" +} \ No newline at end of file