From 6fa4a43d39e8103bfaaa767322619c9e01267fe2 Mon Sep 17 00:00:00 2001 From: chernykhalexander Date: Sat, 16 Jul 2016 00:48:09 +0300 Subject: [PATCH] added spring boot starters module --- pom.xml | 1 + spring-boot-starters/.gitignore | 1 + spring-boot-starters/README.MD | 2 + spring-boot-starters/pom.xml | 91 +++++++++++++++++++ .../main/java/org/baeldung/Application.java | 16 ++++ .../controller/GenericEntityController.java | 38 ++++++++ .../org/baeldung/domain/GenericEntity.java | 42 +++++++++ .../repository/GenericEntityRepository.java | 7 ++ .../src/main/resources/application.properties | 2 + .../baeldung/SpringBootApplicationTest.java | 54 +++++++++++ .../java/org/baeldung/SpringBootJPATest.java | 27 ++++++ .../java/org/baeldung/SpringBootMailTest.java | 82 +++++++++++++++++ .../src/test/resources/application.properties | 3 + 13 files changed, 366 insertions(+) create mode 100644 spring-boot-starters/.gitignore create mode 100644 spring-boot-starters/README.MD create mode 100644 spring-boot-starters/pom.xml create mode 100644 spring-boot-starters/src/main/java/org/baeldung/Application.java create mode 100644 spring-boot-starters/src/main/java/org/baeldung/controller/GenericEntityController.java create mode 100644 spring-boot-starters/src/main/java/org/baeldung/domain/GenericEntity.java create mode 100644 spring-boot-starters/src/main/java/org/baeldung/repository/GenericEntityRepository.java create mode 100644 spring-boot-starters/src/main/resources/application.properties create mode 100644 spring-boot-starters/src/test/java/org/baeldung/SpringBootApplicationTest.java create mode 100644 spring-boot-starters/src/test/java/org/baeldung/SpringBootJPATest.java create mode 100644 spring-boot-starters/src/test/java/org/baeldung/SpringBootMailTest.java create mode 100644 spring-boot-starters/src/test/resources/application.properties diff --git a/pom.xml b/pom.xml index 1966d1485a..0bdba095c1 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,7 @@ spring-apache-camel spring-batch spring-boot + spring-boot-starters spring-controller spring-data-cassandra spring-data-elasticsearch diff --git a/spring-boot-starters/.gitignore b/spring-boot-starters/.gitignore new file mode 100644 index 0000000000..b83d22266a --- /dev/null +++ b/spring-boot-starters/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/spring-boot-starters/README.MD b/spring-boot-starters/README.MD new file mode 100644 index 0000000000..2a87b46021 --- /dev/null +++ b/spring-boot-starters/README.MD @@ -0,0 +1,2 @@ +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring diff --git a/spring-boot-starters/pom.xml b/spring-boot-starters/pom.xml new file mode 100644 index 0000000000..87784e7690 --- /dev/null +++ b/spring-boot-starters/pom.xml @@ -0,0 +1,91 @@ + + 4.0.0 + com.baeldung + spring-boot-starters + 0.0.1-SNAPSHOT + Spring Boot Starters + This is simple boot application for Spring boot starters + + + + org.springframework.boot + spring-boot-starter-parent + 1.3.3.RELEASE + + + + UTF-8 + 1.8 + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + com.jayway.jsonpath + json-path + test + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + runtime + + + org.springframework.boot + spring-boot-starter-mail + + + org.subethamail + subethasmtp + 3.1.7 + test + + + + + spring-boot + + + src/main/resources + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-starters/src/main/java/org/baeldung/Application.java b/spring-boot-starters/src/main/java/org/baeldung/Application.java new file mode 100644 index 0000000000..453264820f --- /dev/null +++ b/spring-boot-starters/src/main/java/org/baeldung/Application.java @@ -0,0 +1,16 @@ +package org.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; + +import java.util.Queue; + +@org.springframework.boot.autoconfigure.SpringBootApplication +public class Application { + private static ApplicationContext applicationContext; + + public static void main(String[] args) { + applicationContext = SpringApplication.run(Application.class, args); + } +} diff --git a/spring-boot-starters/src/main/java/org/baeldung/controller/GenericEntityController.java b/spring-boot-starters/src/main/java/org/baeldung/controller/GenericEntityController.java new file mode 100644 index 0000000000..b6f88e7cd5 --- /dev/null +++ b/spring-boot-starters/src/main/java/org/baeldung/controller/GenericEntityController.java @@ -0,0 +1,38 @@ +package org.baeldung.controller; + +import org.baeldung.domain.GenericEntity; +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.RestController; + +import java.util.ArrayList; +import java.util.List; + +@RestController +public class GenericEntityController { + private List entityList = new ArrayList<>(); + + { + entityList.add(new GenericEntity(1l, "entity_1")); + entityList.add(new GenericEntity(2l, "entity_2")); + entityList.add(new GenericEntity(3l, "entity_3")); + entityList.add(new GenericEntity(4l, "entity_4")); + } + + @RequestMapping("/entity/all") + public List findAll() { + return entityList; + } + + @RequestMapping(value = "/entity", method = RequestMethod.POST) + public GenericEntity addEntity(GenericEntity entity) { + entityList.add(entity); + return entity; + } + + @RequestMapping("/entity/findby/{id}") + public GenericEntity findById(@PathVariable Long id) { + return entityList.stream().filter(entity -> entity.getId().equals(id)).findFirst().get(); + } +} diff --git a/spring-boot-starters/src/main/java/org/baeldung/domain/GenericEntity.java b/spring-boot-starters/src/main/java/org/baeldung/domain/GenericEntity.java new file mode 100644 index 0000000000..7b1d27cb66 --- /dev/null +++ b/spring-boot-starters/src/main/java/org/baeldung/domain/GenericEntity.java @@ -0,0 +1,42 @@ +package org.baeldung.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class GenericEntity { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String value; + + public GenericEntity() { + } + + public GenericEntity(String value) { + this.value = value; + } + + public GenericEntity(Long id, String value) { + this.id = id; + this.value = value; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/spring-boot-starters/src/main/java/org/baeldung/repository/GenericEntityRepository.java b/spring-boot-starters/src/main/java/org/baeldung/repository/GenericEntityRepository.java new file mode 100644 index 0000000000..7bb1e6dcdc --- /dev/null +++ b/spring-boot-starters/src/main/java/org/baeldung/repository/GenericEntityRepository.java @@ -0,0 +1,7 @@ +package org.baeldung.repository; + +import org.baeldung.domain.GenericEntity; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface GenericEntityRepository extends JpaRepository { +} diff --git a/spring-boot-starters/src/main/resources/application.properties b/spring-boot-starters/src/main/resources/application.properties new file mode 100644 index 0000000000..6b0388e0c3 --- /dev/null +++ b/spring-boot-starters/src/main/resources/application.properties @@ -0,0 +1,2 @@ +server.port=8080 +server.contextPath=/springbootapp diff --git a/spring-boot-starters/src/test/java/org/baeldung/SpringBootApplicationTest.java b/spring-boot-starters/src/test/java/org/baeldung/SpringBootApplicationTest.java new file mode 100644 index 0000000000..fea5cbd057 --- /dev/null +++ b/spring-boot-starters/src/test/java/org/baeldung/SpringBootApplicationTest.java @@ -0,0 +1,54 @@ +package org.baeldung; + +import org.baeldung.domain.GenericEntity; +import org.baeldung.repository.GenericEntityRepository; +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.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.nio.charset.Charset; + +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +@WebAppConfiguration +public class SpringBootApplicationTest { + @Autowired + private WebApplicationContext webApplicationContext; + private MockMvc mockMvc; + + + @Before + public void setupMockMvc() { + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) + .build(); + } + + @Test + public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect() throws Exception { + MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), + MediaType.APPLICATION_JSON.getSubtype(), + Charset.forName("utf8")); + + mockMvc.perform(MockMvcRequestBuilders.get("/entity/all")). + andExpect(MockMvcResultMatchers.status().isOk()). + andExpect(MockMvcResultMatchers.content().contentType(contentType)). + andExpect(jsonPath("$", hasSize(4))); + } + + +} diff --git a/spring-boot-starters/src/test/java/org/baeldung/SpringBootJPATest.java b/spring-boot-starters/src/test/java/org/baeldung/SpringBootJPATest.java new file mode 100644 index 0000000000..8a6b5139fe --- /dev/null +++ b/spring-boot-starters/src/test/java/org/baeldung/SpringBootJPATest.java @@ -0,0 +1,27 @@ +package org.baeldung; + +import org.baeldung.domain.GenericEntity; +import org.baeldung.repository.GenericEntityRepository; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +public class SpringBootJPATest { + @Autowired + private GenericEntityRepository genericEntityRepository; + + @Test + public void givenGenericEntityRepository_whenSaveAndRetreiveEntity_thenOK() { + GenericEntity genericEntity = genericEntityRepository.save(new GenericEntity("test")); + GenericEntity foundedEntity = genericEntityRepository.findOne(genericEntity.getId()); + assertNotNull(foundedEntity); + assertEquals(genericEntity.getValue(), foundedEntity.getValue()); + } +} diff --git a/spring-boot-starters/src/test/java/org/baeldung/SpringBootMailTest.java b/spring-boot-starters/src/test/java/org/baeldung/SpringBootMailTest.java new file mode 100644 index 0000000000..2e436ece2e --- /dev/null +++ b/spring-boot-starters/src/test/java/org/baeldung/SpringBootMailTest.java @@ -0,0 +1,82 @@ +package org.baeldung; + +import org.junit.After; +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.SpringApplicationConfiguration; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.context.WebApplicationContext; +import org.subethamail.wiser.Wiser; +import org.subethamail.wiser.WiserMessage; + +import javax.mail.MessagingException; +import java.io.IOException; +import java.util.List; + +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +public class SpringBootMailTest { + @Autowired + private JavaMailSender javaMailSender; + + private Wiser wiser; + + private String userTo = "user2@localhost"; + private String userFrom = "user1@localhost"; + private String subject = "Test subject"; + private String textMail = "Text subject mail"; + + @Before + public void setUp() throws Exception { + final int TEST_PORT = 25; + wiser = new Wiser(TEST_PORT); + wiser.start(); + } + + @After + public void tearDown() throws Exception { + wiser.stop(); + } + + @Test + public void givenMail_whenSendAndReceived_thenCorrect() throws Exception { + SimpleMailMessage message = composeEmailMessage(); + javaMailSender.send(message); + List messages = wiser.getMessages(); + + assertThat(messages, hasSize(1)); + WiserMessage wiserMessage = messages.get(0); + assertEquals(userFrom, wiserMessage.getEnvelopeSender()); + assertEquals(userTo, wiserMessage.getEnvelopeReceiver()); + assertEquals(subject, getSubject(wiserMessage)); + assertEquals(textMail, getMessage(wiserMessage)); + } + + private String getMessage(WiserMessage wiserMessage) throws MessagingException, IOException { + return wiserMessage.getMimeMessage().getContent().toString().trim(); + } + + private String getSubject(WiserMessage wiserMessage) throws MessagingException { + return wiserMessage.getMimeMessage().getSubject(); + } + + + private SimpleMailMessage composeEmailMessage() { + SimpleMailMessage mailMessage = new SimpleMailMessage(); + mailMessage.setTo(userTo); + mailMessage.setReplyTo(userFrom); + mailMessage.setFrom(userFrom); + mailMessage.setSubject(subject); + mailMessage.setText(textMail); + return mailMessage; + } +} diff --git a/spring-boot-starters/src/test/resources/application.properties b/spring-boot-starters/src/test/resources/application.properties new file mode 100644 index 0000000000..bcfcf4b2fb --- /dev/null +++ b/spring-boot-starters/src/test/resources/application.properties @@ -0,0 +1,3 @@ +spring.mail.host=localhost +spring.mail.port=25 +spring.mail.properties.mail.smtp.auth=false \ No newline at end of file