├── .editorconfig ├── .gitignore ├── README.md ├── pom.xml ├── run-dev.sh └── src ├── main ├── java │ └── org │ │ └── jug │ │ └── algeria │ │ ├── Application.java │ │ ├── controller │ │ └── HomeController.java │ │ ├── domain │ │ └── AppUser.java │ │ └── repository │ │ └── UserRepository.java └── resources │ ├── application-dev.yml │ └── static │ └── index.html └── test ├── java └── org │ └── jug │ └── algeria │ └── controller │ └── HomeControllerIntegrationTests.java └── resources └── application-test.yml /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | indent_style = space 7 | indent_size = 2 8 | charset = utf-8 9 | trim_trailing_whitespace = true -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | pom.xml.bak 3 | *.script 4 | blood.properties 5 | blood.log 6 | 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Welcome to spring-boot-postgres Sample project 2 | 3 | ## Pre requisites 4 | Java 8 5 | Maven 3.3.x 6 | PostGreSql running instance (H2 for dev profile) 7 | 8 | ## Postgres Instance Configuration 9 | In order to use your instance please update the [ Database Configuration Section ] section in ```src/main/resources/application.yml``` 10 | 11 | ```yaml 12 | # 13 | # [ Database Configuration Section ] 14 | # 15 | spring: 16 | datasource: 17 | url: jdbc:postgresql://localhost:5432/blood 18 | username: postgres 19 | password: postgres 20 | platform: POSTGRESQL 21 | jpa: 22 | hibernate: 23 | ddl-auto: create-drop 24 | show-sql: true 25 | server: 26 | port: 9095 27 | 28 | # 29 | # [ Other Configuration Attributes ] 30 | # 31 | ``` 32 | 33 | ## Run Application Locally 34 | ```mvn -Dspring.profiles.active={dev-prod} spring-boot:run``` 35 | 36 | ## Run Integration Tests 37 | ```mvn test``` 38 | 39 | ## Load Sample Data 40 | schema and data are initialized using ```schema-${platform}.sql``` and ```data-${platform}.sql``` 41 | 42 | ## Invoke Application 43 | 44 | ### through browser 45 | to make POST requests from your browser use tools like POSTMAN : https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop 46 | 47 | 48 | to add a user make a POST like this example : ```http://localhost:9095/user/Tarik Ibn Ziad``` 49 | to list all application users : ```http://localhost:9095/user``` 50 | 51 | ### Add a user 52 | ```curl -X POST "http://localhost:9095/user/Abderrazak%20BOUADMA"``` 53 | running the above POST request will result to an 200 Ok HTTP response and JSON Content-Type of Application/json of the new created object. 54 | the url must be URL_ENCODED before making the request (notice the %20 which encodes the white space character). 55 | 56 | ### List All Users 57 | ```curl "http://localhost:9095/user"``` 58 | running the above GET request will result to an 200 Ok HTTP response and JSON Content-Type of Application/json and a list (maybe empty) of all users in DB 59 | 60 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | 7 | spring-boot-starter-parent 8 | org.springframework.boot 9 | 1.4.3.RELEASE 10 | 11 | 12 | org.jug.algeria 13 | doctor-blood 14 | 0.0.1-SNAPSHOT 15 | jar 16 | doctor-blood Maven Webapp 17 | 18 | 19 | 1.8 20 | UTF-8 21 | UTF-8 22 | 23 | 24 | 25 | 26 | 27 | javax.inject 28 | javax.inject 29 | 1 30 | 31 | 32 | org.projectlombok 33 | lombok 34 | 35 | 36 | org.assertj 37 | assertj-core 38 | test 39 | 40 | 41 | com.h2database 42 | h2 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-actuator 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter-data-jpa 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-starter-data-rest 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-starter-test 59 | 60 | 61 | org.springframework.boot 62 | spring-boot-starter-web 63 | 64 | 65 | org.testng 66 | testng 67 | 6.8.8 68 | test 69 | 70 | 71 | postgresql 72 | postgresql 73 | 9.1-901-1.jdbc4 74 | 75 | 76 | 77 | 78 | 79 | blood-${project.version} 80 | 81 | 82 | org.springframework.boot 83 | spring-boot-maven-plugin 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | devTest8 93 | 94 | 95 | hsqldbIntegrationTest 96 | true 97 | 98 | 99 | 100 | 101 | 102 | ${project.build.outputDirectory} 103 | src/test/resources/config 104 | 105 | application.properties 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | dev8 115 | 116 | true 117 | 118 | 119 | 120 | 121 | ${project.build.outputDirectory} 122 | src/main/resources/config/dev 123 | 124 | application.properties 125 | 126 | 127 | 128 | 129 | 130 | com.google.code.sortpom 131 | maven-sortpom-plugin 132 | 2.3.0 133 | 134 | 135 | verify 136 | 137 | sort 138 | 139 | 140 | 141 | 142 | true 143 | 4 144 | groupId,artifactId 145 | groupId,artifactId 146 | true 147 | false 148 | 149 | 150 | 151 | maven-compiler-plugin 152 | 153 | ${java.version} 154 | ${java.version} 155 | 156 | 157 | 158 | org.apache.maven.plugins 159 | maven-enforcer-plugin 160 | 1.3.1 161 | 162 | 163 | enforce-versions 164 | 165 | enforce 166 | 167 | 168 | 169 | 170 | 171 | 172 | You are running an older version of Maven. requires at least Maven 3.1 173 | [3.1.0,) 174 | 175 | 176 | You are running an older version of Java. requires at least JDK ${java.version} 177 | [${java.version}.0,) 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | -------------------------------------------------------------------------------- /run-dev.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | mvn clean -Dspring.profiles.active-dev spring-boot:run 4 | -------------------------------------------------------------------------------- /src/main/java/org/jug/algeria/Application.java: -------------------------------------------------------------------------------- 1 | package org.jug.algeria; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | public static void main(String[] args) { 9 | SpringApplication.run(Application.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/org/jug/algeria/controller/HomeController.java: -------------------------------------------------------------------------------- 1 | package org.jug.algeria.controller; 2 | 3 | import org.jug.algeria.domain.AppUser; 4 | import org.jug.algeria.repository.UserRepository; 5 | import org.springframework.http.MediaType; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.*; 8 | import org.springframework.web.servlet.ModelAndView; 9 | 10 | import javax.inject.Inject; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | import java.util.function.Consumer; 14 | 15 | @RestController 16 | @RequestMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE) 17 | public class HomeController { 18 | 19 | final UserRepository userRepository; 20 | 21 | @Inject 22 | public HomeController(UserRepository userRepository) { 23 | this.userRepository = userRepository; 24 | } 25 | 26 | @GetMapping 27 | public ModelAndView home() { 28 | return new ModelAndView("index"); 29 | } 30 | 31 | @GetMapping(value = "/hello") 32 | public ResponseEntity sayHello() { 33 | return ResponseEntity.ok().body("Hello there !"); 34 | } 35 | 36 | @PostMapping(value = "/user/{username}", produces = MediaType.APPLICATION_JSON_VALUE) 37 | public ResponseEntity create(@PathVariable String username) { 38 | AppUser appUser = new AppUser(); 39 | appUser.setUsername(username); 40 | AppUser saved = userRepository.save(appUser); 41 | return ResponseEntity.ok().body(saved); 42 | } 43 | 44 | @GetMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE) 45 | public ResponseEntity> findAll() { 46 | final List resultList = new ArrayList<>(); 47 | final Iterable all = userRepository.findAll(); 48 | all.forEach(resultList::add); 49 | return ResponseEntity.ok().body(resultList); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/jug/algeria/domain/AppUser.java: -------------------------------------------------------------------------------- 1 | package org.jug.algeria.domain; 2 | 3 | import lombok.Data; 4 | import lombok.RequiredArgsConstructor; 5 | 6 | import javax.persistence.Entity; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.validation.constraints.NotNull; 11 | 12 | @Entity 13 | @Data 14 | @RequiredArgsConstructor 15 | public class AppUser { 16 | 17 | @Id @GeneratedValue(strategy = GenerationType.AUTO) 18 | private Long id; 19 | 20 | @NotNull 21 | private String username; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/jug/algeria/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package org.jug.algeria.repository; 2 | 3 | import org.jug.algeria.domain.AppUser; 4 | import org.springframework.data.repository.CrudRepository; 5 | 6 | public interface UserRepository extends CrudRepository { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | # 2 | # [ Database Configuration Section ] 3 | # 4 | 5 | spring: 6 | datasource: 7 | url: jdbc:h2:mem:APP_USERS;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false 8 | username: sa 9 | password: 10 | server: 11 | port: 9095 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/resources/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /src/test/java/org/jug/algeria/controller/HomeControllerIntegrationTests.java: -------------------------------------------------------------------------------- 1 | package org.jug.algeria.controller; 2 | 3 | 4 | import org.assertj.core.api.Assertions; 5 | import org.jug.algeria.domain.AppUser; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.mock.web.MockHttpServletRequest; 11 | import org.springframework.test.context.TestPropertySource; 12 | import org.springframework.test.context.junit4.SpringRunner; 13 | import org.springframework.web.client.RestTemplate; 14 | 15 | @RunWith(SpringRunner.class) 16 | @SpringBootTest 17 | @TestPropertySource(locations = {"classpath:application-test.yml"}) 18 | public class HomeControllerIntegrationTests { 19 | 20 | RestTemplate restTemplate = new RestTemplate(); 21 | 22 | @Test 23 | public void shouldAdd_AppUser_ToDb() { 24 | 25 | ResponseEntity responseEntity = restTemplate 26 | .postForEntity("http://localhost:9000/user/Abderrazak BOUADMA", 27 | MockHttpServletRequest.DEFAULT_PROTOCOL, 28 | AppUser.class); 29 | 30 | final AppUser appUser = responseEntity.getBody(); 31 | 32 | Assertions.assertThat(appUser).isNotNull(); 33 | Assertions.assertThat(appUser.getUsername()).isNotNull().isEqualTo("Abderrazak BOUADMA"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/resources/application-test.yml: -------------------------------------------------------------------------------- 1 | # 2 | # [ Database Configuration Section ] 3 | # 4 | 5 | spring: 6 | datasource: 7 | url: jdbc:h2:mem:USERS;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false 8 | username: sa 9 | password: 10 | server: 11 | port: 9095 12 | 13 | # 14 | # [ Other Configuration Attributes ] 15 | # 16 | --------------------------------------------------------------------------------