Commit e3725b1a by Benedikt Ritter

Add minimal sample copied from britter/spring-boot-heroku-demo

parent 2e29c938
### https://raw.github.com/github/gitignore/14b7566ce157ce95b07006466bacee160f242284/maven.gitignore
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2016 codecentric AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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>de.codecentric</groupId>
<artifactId>springboot-sample-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Boot Sample App</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.bechte.junit</groupId>
<artifactId>junit-hierarchicalcontextrunner</artifactId>
<version>4.12.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1203-jdbc42</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<scm>
<url>https://github.com/codecentric/springboot-sample-app</url>
<connection>scm:git:git://github.com/codecentric/springboot-sample-app.git</connection>
<developerConnection>scm:git:ssh://git@github.com/codecentric/springboot-sample-app.git</developerConnection>
<tag>HEAD</tag>
</scm>
<build>
<finalName>app</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>
</plugins>
</build>
<profiles>
<profile>
<id>travis</id>
<activation>
<property>
<name>env.TRAVIS</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<configuration>
<excludes>
<exclude>**/Application.class</exclude>
</excludes>
</configuration>
<executions>
<!-- Prepares the property pointing to the JaCoCo
runtime agent which is passed as VM argument when Maven the Surefire plugin
is executed. -->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- Ensures that the code coverage report for
unit tests is created after unit tests have been run. -->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.1.0</version>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<repositories>
<repository>
<id>spring-milestone</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestone</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
/*
* Copyright 2016 codecentric AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.springbootsample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
/*
* Copyright 2016 codecentric AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.springbootsample;
import javax.validation.Valid;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class HomeController {
private RecordRepository repository;
@Autowired
public HomeController(RecordRepository repository) {
this.repository = repository;
}
@RequestMapping(method = RequestMethod.GET)
public String home(ModelMap model) {
List<Record> records = repository.findAll();
model.addAttribute("records", records);
model.addAttribute("insertRecord", new Record());
return "home";
}
@RequestMapping(method = RequestMethod.POST)
public String insertData(ModelMap model,
@ModelAttribute("insertRecord") @Valid Record record,
BindingResult result) {
if (!result.hasErrors()) {
repository.save(record);
}
return home(model);
}
}
package de.codecentric.springbootsample;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.hibernate.validator.constraints.NotEmpty;
@Entity
public class Record {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotEmpty
private String data;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
package de.codecentric.springbootsample;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface RecordRepository extends JpaRepository<Record, Long> {
}
# Database Config
# Un-comment these lines to connect to a database. When commented out, you automatically get an in-memory-database.
#spring.jpa.hibernate.ddl-auto=update
#spring.datasource.url=jdbc:postgresql://192.168.59.103:5432/spring-boot-heroku-example
#spring.datasource.username=spring-boot-heroku-example
#spring.datasource.password=spring-boot-heroku-example
#spring.datasource.driver-class-name=org.postgresql.Driver
project.version=@project.version@
project.name=@project.name@
\ No newline at end of file
<!DOCTYPE HTML>
<!--
Copyright 2016 codecentric AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="#{project.name}">Sample App</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" />
</head>
<body>
<div class="container">
<div class="page-header">
<h1 th:text="#{project.name}">Sample App</h1>
<p class="lead">Demo application for showing how to run a <a href="http://projects.spring.io/spring-boot/">Spring Boot</a> application on <a href="https://openshift.com">OpenShift</a>.</p>
<p>The code is open source at <a href="https://github.com/codecentric/springboot-sample-app">GitHub</a>. You're currently looking at version <span th:text="#{project.version}">0.0.1</span>.</p>
</div>
<div class="row">
<div class="col-md-12 col-lg-6">
<form th:object="${insertRecord}" method="POST">
<div class="form-group">
<label for="data">Record to insert</label>
<input id="data" type="text" class="form-control" placeholder="Lorem ipsum" th:field="*{data}" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
<hr />
<div class="row">
<div class="col-sm-12">
<table class="table table-striped">
<thead>
<tr>
<th>Records</th>
</tr>
</thead>
<tbody>
<tr th:each="record : ${records}">
<td th:text="${record.data}">Some text</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>
\ No newline at end of file
/*
* Copyright 2016 Benedikt Ritter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.springbootsample;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import de.bechte.junit.runners.context.HierarchicalContextRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.ui.ModelMap;
import org.springframework.validation.MapBindingResult;
import org.springframework.validation.ObjectError;
@RunWith(HierarchicalContextRunner.class)
public class HomeControllerTest {
private ModelMap map;
private HomeController ctrl;
private RecordRepository repository;
@Before
public void setUp() throws Exception {
map = new ModelMap();
repository = mock(RecordRepository.class);
ctrl = new HomeController(repository);
}
public class Home {
@Test
public void shouldAddInsertRecordToModelMap() throws Exception {
ctrl.home(map);
assertThat(map, hasKey("insertRecord"));
assertTrue(map.get("insertRecord") instanceof Record);
Record insertRecord = (Record) map.get("insertRecord");
assertNull(insertRecord.getData());
}
@Test
public void shouldQueryRepositoryForAllRecords() throws Exception {
ctrl.home(map);
verify(repository, only()).findAll();
}
@Test
public void shouldAddRecordsFromRepositoryToModelMap() throws Exception {
when(repository.findAll()).thenReturn(Arrays.asList(new Record(), new Record(), new Record()));
ctrl.home(map);
assertThat(map, hasKey("records"));
assertTrue(map.get("records") instanceof List);
List<Record> records = getRecords();
assertThat(records, hasSize(3));
}
@SuppressWarnings("unchecked")
private List<Record> getRecords() {
return (List<Record>) map.get("records");
}
}
public class InsertData {
private MapBindingResult bindingResult;
@Before
public void setUp() throws Exception {
bindingResult = new MapBindingResult(new HashMap<>(), "insertRecord");
}
@Test
public void shouldSaveRecordWhenThereAreNoErrors() throws Exception {
Record record = new Record();
insertData(record);
verify(repository, times(1)).save(record);
}
@Test
public void shouldNotSaveRecordWhenThereAreErrors() throws Exception {
bindingResult.addError(new ObjectError("", ""));
insertData(new Record());
verify(repository, never()).save(any(Record.class));
}
@Test
public void shouldAddNewInsertRecordToModelMap() throws Exception {
Record record = new Record();
insertData(record);
assertThat(map, hasKey("insertRecord"));
assertThat(map.get("insertRecord"), is(not(record)));
}
@Test
public void shouldAddRecordsToModelMap() throws Exception {
insertData(new Record());
assertThat(map, hasKey("records"));
}
private void insertData(Record record) {
ctrl.insertData(map, record, bindingResult);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment