added spring boot starters module
This commit is contained in:
parent
1a812c42ba
commit
6fa4a43d39
1
pom.xml
1
pom.xml
|
@ -42,6 +42,7 @@
|
|||
<module>spring-apache-camel</module>
|
||||
<module>spring-batch</module>
|
||||
<module>spring-boot</module>
|
||||
<module>spring-boot-starters</module>
|
||||
<module>spring-controller</module>
|
||||
<module>spring-data-cassandra</module>
|
||||
<module>spring-data-elasticsearch</module>
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
/target/
|
|
@ -0,0 +1,2 @@
|
|||
###The Course
|
||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|
@ -0,0 +1,91 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-boot-starters</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>Spring Boot Starters</name>
|
||||
<description>This is simple boot application for Spring boot starters</description>
|
||||
|
||||
<!-- Inherit defaults from Spring Boot -->
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.3.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.jsonpath</groupId>
|
||||
<artifactId>json-path</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.subethamail</groupId>
|
||||
<artifactId>subethasmtp</artifactId>
|
||||
<version>3.1.7</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-boot</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<GenericEntity> 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<GenericEntity> 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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<GenericEntity, Long> {
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
server.port=8080
|
||||
server.contextPath=/springbootapp
|
|
@ -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)));
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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<WiserMessage> 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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
spring.mail.host=localhost
|
||||
spring.mail.port=25
|
||||
spring.mail.properties.mail.smtp.auth=false
|
Loading…
Reference in New Issue