├── .gitignore ├── LICENSE ├── README.md ├── completablefuture-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── completablefuturepoc │ │ └── Sleep.java │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── completablefuturepoc │ ├── CompletableFutureAdvanceTest.java │ └── CompletableFutureTest.java ├── dropwizard-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── dropwizard │ │ ├── DropwizardApplication.java │ │ ├── config │ │ └── DropwizardConfig.java │ │ ├── model │ │ └── Message.java │ │ └── resource │ │ └── MessageResource.java │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── dropwizard │ └── resource │ └── MessageResourceTest.java ├── dropwizard-series-configuration-class ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── pocs │ │ │ └── dropwizardseries │ │ │ └── configurationclass │ │ │ ├── DropwizardApplication.java │ │ │ ├── config │ │ │ └── DropwizardConfig.java │ │ │ ├── model │ │ │ └── Message.java │ │ │ └── resource │ │ │ └── MessageResource.java │ └── resources │ │ └── config.yaml │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── dropwizardseries │ └── configurationclass │ └── resource │ └── MessageResourceTest.java ├── future-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── futurepoc │ │ └── SomeBigProcess.java │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── futurepoc │ └── FuturesTest.java ├── integrationtests-maven-example ├── README.md ├── pom.xml └── src │ ├── integration-test │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── integrationtestsmaven │ │ └── controller │ │ └── InfoControllerIT.java │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── pocs │ │ │ └── integrationtestsmaven │ │ │ ├── Application.java │ │ │ ├── controller │ │ │ └── InfoController.java │ │ │ ├── entity │ │ │ └── Info.java │ │ │ ├── repository │ │ │ ├── InfoRepository.java │ │ │ └── impl │ │ │ │ └── InfoRepositoryMock.java │ │ │ └── service │ │ │ ├── InfoService.java │ │ │ └── impl │ │ │ └── InfoServiceImpl.java │ └── resources │ │ └── application.properties │ └── test │ ├── java │ └── com │ │ └── edwise │ │ └── pocs │ │ └── integrationtestsmaven │ │ └── InfoControllerTest.java │ └── resources │ └── application.properties ├── integrationtests-rest-example ├── README.md ├── pom.xml └── src │ ├── integration-test │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── integrationtestsrest │ │ └── controller │ │ └── InfoControllerIT.java │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── pocs │ │ │ └── integrationtestsrest │ │ │ ├── Application.java │ │ │ ├── controller │ │ │ └── InfoController.java │ │ │ ├── entity │ │ │ └── Info.java │ │ │ ├── repository │ │ │ ├── InfoRepository.java │ │ │ └── impl │ │ │ │ └── InfoRepositoryMock.java │ │ │ └── service │ │ │ ├── InfoService.java │ │ │ └── impl │ │ │ └── InfoServiceImpl.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── integrationtestsrest │ └── InfoControllerTest.java ├── integrationtests-restassured-example ├── README.md ├── pom.xml └── src │ ├── integration-test │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── itrestassured │ │ └── controller │ │ └── InfoControllerIT.java │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── pocs │ │ │ └── itrestassured │ │ │ ├── Application.java │ │ │ ├── controller │ │ │ └── InfoController.java │ │ │ ├── entity │ │ │ └── Info.java │ │ │ ├── repository │ │ │ ├── InfoRepository.java │ │ │ └── impl │ │ │ │ └── InfoRepositoryMock.java │ │ │ └── service │ │ │ ├── InfoService.java │ │ │ └── impl │ │ │ └── InfoServiceImpl.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── itrestassured │ └── controller │ └── InfoControllerTest.java ├── java8-collectingandthen-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── java8collectingandthen │ │ └── CollectorsCollectingAndThen.java │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── java8collectingandthen │ └── CollectorsCollectingAndThenTest.java ├── java8-function-methods-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── java8functionmethods │ │ ├── BookCharacterFunction.java │ │ └── model │ │ └── BookCharacter.java │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── java8functionmethods │ └── FunctionMethodsTest.java ├── java8-predicate-methods-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── java8predicatemethods │ │ ├── BookCharacterChecker.java │ │ ├── BookCharacterPredicate.java │ │ └── model │ │ └── BookCharacter.java │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── java8predicatemethods │ └── PredicateMethodsTest.java ├── jaxrs-wildcards-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── jaxrswildcards │ │ ├── Application.java │ │ └── resource │ │ └── FooResource.java │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── jaxrswildcards │ └── resource │ └── FooResourceTest.java ├── jersey-rest-client-example ├── README.md ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── edwise │ └── pocs │ └── jerseyrestclient │ ├── AppBookRestApiary.java │ ├── AppInfoRestLocal.java │ └── model │ ├── Book.java │ └── Info.java ├── jersey-rest-example ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── pocs │ │ │ └── jerseyrest │ │ │ ├── RestApplication.java │ │ │ ├── config │ │ │ └── JsonMapperProvider.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ ├── resource │ │ │ └── UserController.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ └── UserServiceMock.java │ └── webapp │ │ └── WEB-INF │ │ └── beans.xml │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── jerseyrest │ ├── entity │ └── UserTest.java │ ├── resource │ └── UserControllerTest.java │ └── service │ └── impl │ └── UserServiceMockTest.java ├── jooq-example ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── pocs │ │ │ └── jooq │ │ │ ├── Application.java │ │ │ └── jdbc │ │ │ └── DBConnector.java │ └── resources │ │ └── populate_db.sql │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── jooq │ └── JOOQExampleTest.java ├── lombok-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── lombok │ │ ├── withlombok │ │ ├── Book.java │ │ ├── FooService.java │ │ └── User.java │ │ └── withoutlombok │ │ ├── Book.java │ │ ├── FooService.java │ │ └── User.java │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── lombok │ ├── withlombok │ ├── BookTest.java │ ├── FooServiceTest.java │ └── UserTest.java │ └── withoutlombok │ ├── BookTest.java │ ├── FooServiceTest.java │ └── UserTest.java ├── orika-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── orikapoc │ │ ├── OrikaExample.java │ │ ├── config │ │ ├── LocalDateTimeToLocalDateConverter.java │ │ └── OrikaConfig.java │ │ ├── dto │ │ └── DestinationDTO.java │ │ └── entity │ │ ├── EntityType.java │ │ └── SourceEntity.java │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── orikapoc │ └── OrikaExampleTest.java ├── passbyvalue-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── edwise │ │ └── example │ │ └── passbyvalue │ │ ├── PassByValueExample.java │ │ └── User.java │ └── test │ └── java │ └── com │ └── edwise │ └── example │ └── passbyvalue │ └── PassByValueExampleTest.java ├── spring-data-example ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── pocs │ │ │ └── springdata │ │ │ ├── Application.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ ├── repository │ │ │ └── UserRepository.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ └── UserServiceImpl.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── springdata │ ├── ApplicationTest.java │ ├── controller │ └── UserControllerTest.java │ ├── entity │ └── UserTest.java │ └── service │ └── impl │ └── UserServiceImplTest.java ├── spring-rest-client-example ├── README.md ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── edwise │ └── pocs │ └── springrestclient │ ├── AppBookRestApiary.java │ ├── AppInfoRestLocal.java │ └── model │ ├── Book.java │ └── Info.java ├── spring-rest-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── springrest │ │ ├── Application.java │ │ ├── controllers │ │ └── UserController.java │ │ ├── entities │ │ └── User.java │ │ └── services │ │ ├── UserService.java │ │ └── impl │ │ └── UserServiceMock.java │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── springrest │ ├── ApplicationTest.java │ ├── controllers │ └── UserControllerTest.java │ ├── entities │ └── UserTest.java │ └── impl │ └── UserServiceMockTest.java ├── springboot-devtools-test ├── README.md ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── edwise │ └── pocs │ └── springbootdevtoolstest │ ├── Application.java │ └── controllers │ ├── HelloWorldController.java │ └── OtherController.java ├── springboot-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── edwise │ │ └── pocs │ │ └── springboot │ │ ├── Application.java │ │ └── controllers │ │ └── HelloWorldController.java │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── springboot │ └── controllers │ └── HelloWorldControllerTest.java ├── springboot-series-actuator ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── springbootseries │ │ │ └── actuator │ │ │ ├── Application.java │ │ │ ├── checkers │ │ │ └── FileManager.java │ │ │ ├── controller │ │ │ └── HelloWorldController.java │ │ │ ├── endpoints │ │ │ ├── BetterHealthIndicator.java │ │ │ └── BugsEndpoint.java │ │ │ ├── entity │ │ │ └── Bug.java │ │ │ └── service │ │ │ ├── BugsService.java │ │ │ └── impl │ │ │ └── BugsServiceMock.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── edwise │ └── springbootseries │ └── actuator │ ├── checkers │ └── FileManagerTest.java │ ├── controller │ └── HelloWorldControllerTest.java │ └── service │ └── impl │ └── BugsServiceMockTest.java ├── springboot-series-jackson ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── springbootseries │ │ │ └── jackson │ │ │ ├── Application.java │ │ │ ├── controller │ │ │ └── InfoController.java │ │ │ └── entity │ │ │ └── Info.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── edwise │ └── springbootseries │ └── jackson │ ├── ApplicationTest.java │ └── controller │ └── InfoControllerTest.java ├── springboot-series-jooq ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── springbootseries │ │ │ └── jooq │ │ │ ├── Application.java │ │ │ └── dao │ │ │ └── BooksDao.java │ └── resources │ │ ├── data.sql │ │ ├── delete.sql │ │ └── schema.sql │ └── test │ └── java │ └── com │ └── edwise │ └── springbootseries │ └── jooq │ └── dao │ └── BooksDaoIT.java ├── springboot-series-war ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── edwise │ │ └── springbootseries │ │ └── war │ │ ├── Application.java │ │ └── controllers │ │ └── HelloWorldController.java │ └── test │ └── java │ └── com │ └── edwise │ └── springbootseries │ └── war │ └── controllers │ └── HelloWorldControllerTest.java ├── springbootseries-banner ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── springbootseries │ │ │ └── banner │ │ │ └── Application.java │ └── resources │ │ └── banner.txt │ └── test │ └── java │ └── com │ └── edwise │ └── springbootseries │ └── banner │ └── ApplicationTest.java ├── springbootseries-integrationtests ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── springbootseries │ │ │ └── integrationtests │ │ │ ├── Application.java │ │ │ ├── controller │ │ │ └── InfoController.java │ │ │ ├── entity │ │ │ └── Info.java │ │ │ ├── repository │ │ │ ├── InfoRepository.java │ │ │ └── impl │ │ │ │ └── InfoRepositoryMock.java │ │ │ └── service │ │ │ ├── InfoService.java │ │ │ └── impl │ │ │ └── InfoServiceImpl.java │ └── resources │ │ └── application.properties │ └── test │ ├── java │ └── com │ │ └── edwise │ │ └── springbootseries │ │ └── integrationtests │ │ ├── InfoControllerIT.java │ │ └── InfoControllerTest.java │ └── resources │ └── application.properties ├── springfox-swagger2-example ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── pocs │ │ │ └── swagger2 │ │ │ ├── Application.java │ │ │ ├── config │ │ │ └── SwaggerConfig.java │ │ │ ├── controller │ │ │ └── InfoController.java │ │ │ ├── entity │ │ │ └── Info.java │ │ │ ├── repository │ │ │ ├── InfoRepository.java │ │ │ └── impl │ │ │ │ └── InfoRepositoryMock.java │ │ │ └── service │ │ │ ├── InfoService.java │ │ │ └── impl │ │ │ └── InfoServiceImpl.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── swagger2 │ └── controller │ └── InfoControllerTest.java ├── springmvc-swagger-example ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── edwise │ │ │ └── pocs │ │ │ └── swagger │ │ │ ├── Application.java │ │ │ ├── config │ │ │ └── SwaggerSpringMvcConfig.java │ │ │ ├── controllers │ │ │ └── UserController.java │ │ │ ├── entities │ │ │ └── User.java │ │ │ └── services │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ └── UserServiceMock.java │ └── webapp │ │ └── swagger │ │ ├── css │ │ ├── reset.css │ │ └── screen.css │ │ ├── images │ │ ├── explorer_icons.png │ │ ├── logo_small.png │ │ ├── pet_store_api.png │ │ ├── throbber.gif │ │ └── wordnik_api.png │ │ ├── index.html │ │ ├── lib │ │ ├── backbone-min.js │ │ ├── handlebars-1.0.0.js │ │ ├── highlight.7.3.pack.js │ │ ├── jquery-1.8.0.min.js │ │ ├── jquery.ba-bbq.min.js │ │ ├── jquery.slideto.min.js │ │ ├── jquery.wiggle.min.js │ │ ├── shred.bundle.js │ │ ├── shred │ │ │ └── content.js │ │ ├── swagger-client.js │ │ ├── swagger-oauth.js │ │ ├── swagger.js │ │ └── underscore-min.js │ │ ├── o2c.html │ │ ├── swagger-ui.js │ │ └── swagger-ui.min.js │ └── test │ └── java │ └── com │ └── edwise │ └── pocs │ └── swagger │ ├── ApplicationTest.java │ ├── controllers │ └── UserControllerTest.java │ ├── entities │ └── UserTest.java │ └── impl │ └── UserServiceMockTest.java └── spy-mockito-example ├── README.md ├── pom.xml └── src ├── main └── java │ └── com │ └── edwise │ └── pocs │ └── spymockito │ ├── Bar.java │ ├── BarWellDesigned.java │ └── Foo.java └── test └── java └── com └── edwise └── pocs └── spymockito ├── BarTest.java └── BarWellDesignedTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | .classpath 4 | .project 5 | .settings 6 | target 7 | out -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Eduardo Antón 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /completablefuture-example/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de uso de la clase CompletableFuture: https://anotherdayanotherbug.wordpress.com/2016/03/07/futuros-en-java-parte-3-completablefuture/ 2 | https://anotherdayanotherbug.wordpress.com/2016/03/29/futuros-en-java-parte-4-completablefuture-uso-avanzado/ -------------------------------------------------------------------------------- /completablefuture-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.pocs.completablefuturepoc 7 | completablefuture-example 8 | 1.1.0 9 | jar 10 | 11 | completablefuture-example 12 | https://github.com/edwise/java-pocs/ 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | 21 | org.slf4j 22 | slf4j-api 23 | 1.7.18 24 | 25 | 26 | ch.qos.logback 27 | logback-classic 28 | 1.1.6 29 | 30 | 31 | 32 | junit 33 | junit 34 | 4.12 35 | test 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.apache.maven.plugins 43 | maven-compiler-plugin 44 | 3.3 45 | 46 | ${java.version} 47 | ${java.version} 48 | ${java.version} 49 | ${java.version} 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /completablefuture-example/src/main/java/com/edwise/pocs/completablefuturepoc/Sleep.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.completablefuturepoc; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import java.util.concurrent.TimeUnit; 7 | 8 | public final class Sleep { 9 | private static final Logger LOGGER = LoggerFactory.getLogger(Sleep.class); 10 | 11 | private Sleep() { 12 | } 13 | 14 | public static void sleepSeconds(int seconds) { 15 | try { 16 | TimeUnit.SECONDS.sleep(seconds); 17 | } catch (InterruptedException e) { 18 | LOGGER.error("Error in sleep of TimeUnit: {}", e.getMessage(), e); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /dropwizard-example/README.md: -------------------------------------------------------------------------------- 1 | POC con Dropwizard, creado para el post https://anotherdayanotherbug.wordpress.com/2016/05/31/poc-con-dropwizard/ -------------------------------------------------------------------------------- /dropwizard-example/src/main/java/com/edwise/pocs/dropwizard/DropwizardApplication.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.dropwizard; 2 | 3 | import com.edwise.pocs.dropwizard.config.DropwizardConfig; 4 | import com.edwise.pocs.dropwizard.resource.MessageResource; 5 | import io.dropwizard.Application; 6 | import io.dropwizard.setup.Environment; 7 | 8 | public class DropwizardApplication extends Application { 9 | 10 | public static void main(String[] args) throws Exception { 11 | new DropwizardApplication().run(args); 12 | } 13 | 14 | @Override 15 | public void run(DropwizardConfig dropwizardConfig, Environment environment) { 16 | environment.jersey().register(new MessageResource()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /dropwizard-example/src/main/java/com/edwise/pocs/dropwizard/config/DropwizardConfig.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.dropwizard.config; 2 | 3 | import io.dropwizard.Configuration; 4 | 5 | public class DropwizardConfig extends Configuration { 6 | } 7 | -------------------------------------------------------------------------------- /dropwizard-example/src/main/java/com/edwise/pocs/dropwizard/model/Message.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.dropwizard.model; 2 | 3 | public class Message { 4 | 5 | private final String message; 6 | 7 | public Message(String message) { 8 | this.message = message; 9 | } 10 | 11 | public String getMessage() { 12 | return message; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /dropwizard-example/src/main/java/com/edwise/pocs/dropwizard/resource/MessageResource.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.dropwizard.resource; 2 | 3 | import com.edwise.pocs.dropwizard.model.Message; 4 | 5 | import javax.ws.rs.GET; 6 | import javax.ws.rs.Path; 7 | import javax.ws.rs.Produces; 8 | import javax.ws.rs.QueryParam; 9 | import javax.ws.rs.core.MediaType; 10 | 11 | @Path("/hello-world") 12 | @Produces(MediaType.APPLICATION_JSON) 13 | public class MessageResource { 14 | private static final String HELLO_WORLD_SENTENCE = "Hello world %s!"; 15 | private static final String DEFAULT_NAME = "Nobody"; 16 | 17 | @GET 18 | public Message getHelloWorldMessage(@QueryParam("name") String name) { 19 | return new Message(String.format(HELLO_WORLD_SENTENCE, name != null ? name : DEFAULT_NAME)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /dropwizard-example/src/test/java/com/edwise/pocs/dropwizard/resource/MessageResourceTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.dropwizard.resource; 2 | 3 | import com.edwise.pocs.dropwizard.model.Message; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import static org.assertj.core.api.Assertions.assertThat; 8 | 9 | public class MessageResourceTest { 10 | private static final String HELLO_WORLD_NOBODY = "Hello world Nobody!"; 11 | private static final String HELLO_WORLD_NAME123 = "Hello world Name123!"; 12 | private static final String NAME_123 = "Name123"; 13 | 14 | private MessageResource messageResource; 15 | 16 | @Before 17 | public void setUp() { 18 | messageResource = new MessageResource(); 19 | } 20 | 21 | @Test 22 | public void getHelloWorldMessageShouldReturnDefaultNameIfNameNull() { 23 | Message result = messageResource.getHelloWorldMessage(null); 24 | 25 | assertThat(result).isNotNull(); 26 | assertThat(result.getMessage()).isEqualTo(HELLO_WORLD_NOBODY); 27 | } 28 | 29 | @Test 30 | public void getHelloWorldMessageShouldReturnNameIfNameNotNull() { 31 | Message result = messageResource.getHelloWorldMessage(NAME_123); 32 | 33 | assertThat(result).isNotNull(); 34 | assertThat(result.getMessage()).isEqualTo(HELLO_WORLD_NAME123); 35 | } 36 | } -------------------------------------------------------------------------------- /dropwizard-series-configuration-class/README.md: -------------------------------------------------------------------------------- 1 | Ejemplo de configuración de Dropwizard, para el post: http://anotherdayanotherbug.wordpress.com/2016/07/12/dropwizard-series-clase-configuration/ -------------------------------------------------------------------------------- /dropwizard-series-configuration-class/src/main/java/com/edwise/pocs/dropwizardseries/configurationclass/DropwizardApplication.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.dropwizardseries.configurationclass; 2 | 3 | import com.edwise.pocs.dropwizardseries.configurationclass.config.DropwizardConfig; 4 | import com.edwise.pocs.dropwizardseries.configurationclass.resource.MessageResource; 5 | import io.dropwizard.Application; 6 | import io.dropwizard.setup.Environment; 7 | 8 | public class DropwizardApplication extends Application { 9 | 10 | public static void main(String[] args) throws Exception { 11 | new DropwizardApplication().run(args); 12 | } 13 | 14 | @Override 15 | public void run(DropwizardConfig dropwizardConfig, Environment environment) { 16 | environment.jersey() 17 | .register(new MessageResource(dropwizardConfig.getHost(), dropwizardConfig.getDefaultName())); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /dropwizard-series-configuration-class/src/main/java/com/edwise/pocs/dropwizardseries/configurationclass/config/DropwizardConfig.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.dropwizardseries.configurationclass.config; 2 | 3 | import io.dropwizard.Configuration; 4 | import org.hibernate.validator.constraints.NotEmpty; 5 | 6 | public class DropwizardConfig extends Configuration { 7 | 8 | @NotEmpty 9 | private String host; 10 | 11 | private String defaultName; 12 | 13 | public String getHost() { 14 | return host; 15 | } 16 | 17 | public void setHost(String host) { 18 | this.host = host; 19 | } 20 | 21 | public String getDefaultName() { 22 | return defaultName; 23 | } 24 | 25 | public void setDefaultName(String defaultName) { 26 | this.defaultName = defaultName; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /dropwizard-series-configuration-class/src/main/java/com/edwise/pocs/dropwizardseries/configurationclass/model/Message.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.dropwizardseries.configurationclass.model; 2 | 3 | public class Message { 4 | 5 | private final String message; 6 | 7 | public Message(String message) { 8 | this.message = message; 9 | } 10 | 11 | public String getMessage() { 12 | return message; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /dropwizard-series-configuration-class/src/main/java/com/edwise/pocs/dropwizardseries/configurationclass/resource/MessageResource.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.dropwizardseries.configurationclass.resource; 2 | 3 | import com.edwise.pocs.dropwizardseries.configurationclass.model.Message; 4 | 5 | import javax.ws.rs.GET; 6 | import javax.ws.rs.Path; 7 | import javax.ws.rs.Produces; 8 | import javax.ws.rs.QueryParam; 9 | import javax.ws.rs.core.MediaType; 10 | 11 | @Path("/hello-world") 12 | @Produces(MediaType.APPLICATION_JSON) 13 | public class MessageResource { 14 | private static final String HELLO_WORLD_SENTENCE = "Hello world %s, from %s!"; 15 | 16 | private final String host; 17 | private final String defaultName; 18 | 19 | public MessageResource(String host, String defaultName) { 20 | this.host = host; 21 | this.defaultName = defaultName; 22 | } 23 | 24 | @GET 25 | public Message getHelloWorldMessage(@QueryParam("name") String name) { 26 | return new Message(String.format(HELLO_WORLD_SENTENCE, name != null ? name : defaultName, host)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /dropwizard-series-configuration-class/src/main/resources/config.yaml: -------------------------------------------------------------------------------- 1 | host: localhost 2 | defaultName: Nobody 3 | -------------------------------------------------------------------------------- /dropwizard-series-configuration-class/src/test/java/com/edwise/pocs/dropwizardseries/configurationclass/resource/MessageResourceTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.dropwizardseries.configurationclass.resource; 2 | 3 | import com.edwise.pocs.dropwizardseries.configurationclass.model.Message; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import static org.assertj.core.api.Assertions.assertThat; 8 | 9 | public class MessageResourceTest { 10 | private static final String HELLO_WORLD_NOBODY = "Hello world %s, from %s!"; 11 | private static final String HELLO_WORLD_NAME123 = "Hello world Name123, from %s!"; 12 | private static final String NAME_123 = "Name123"; 13 | 14 | private MessageResource messageResource; 15 | 16 | @Before 17 | public void setUp() { 18 | messageResource = new MessageResource("localhost", "default"); 19 | } 20 | 21 | @Test 22 | public void getHelloWorldMessageShouldReturnDefaultNameIfNameNull() { 23 | Message result = messageResource.getHelloWorldMessage(null); 24 | 25 | assertThat(result).isNotNull(); 26 | assertThat(result.getMessage()).isEqualTo(String.format(HELLO_WORLD_NOBODY, "default", "localhost")); 27 | } 28 | 29 | @Test 30 | public void getHelloWorldMessageShouldReturnNameIfNameNotNull() { 31 | Message result = messageResource.getHelloWorldMessage(NAME_123); 32 | 33 | assertThat(result).isNotNull(); 34 | assertThat(result.getMessage()).isEqualTo(String.format(HELLO_WORLD_NAME123, "localhost")); 35 | } 36 | } -------------------------------------------------------------------------------- /future-example/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de uso de la interfaz Future: https://anotherdayanotherbug.wordpress.com/2016/02/10/futuros-en-java-parte-2-interfaz-future/ -------------------------------------------------------------------------------- /future-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.pocs.futurepoc 7 | future-example 8 | 1.0.0 9 | jar 10 | 11 | future-example 12 | https://github.com/edwise/java-pocs/ 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | 21 | org.slf4j 22 | slf4j-api 23 | 1.7.13 24 | 25 | 26 | ch.qos.logback 27 | logback-classic 28 | 1.1.3 29 | 30 | 31 | 32 | junit 33 | junit 34 | 4.12 35 | test 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.apache.maven.plugins 43 | maven-compiler-plugin 44 | 3.3 45 | 46 | ${java.version} 47 | ${java.version} 48 | ${java.version} 49 | ${java.version} 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /future-example/src/main/java/com/edwise/pocs/futurepoc/SomeBigProcess.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.futurepoc; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Future; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | public class SomeBigProcess { 11 | private final static Logger LOG = LoggerFactory.getLogger(SomeBigProcess.class); 12 | 13 | private final ExecutorService executor; 14 | 15 | public SomeBigProcess(ExecutorService executor) { 16 | this.executor = executor; 17 | } 18 | 19 | public Future processVeryLong(String param) throws InterruptedException { 20 | return executor.submit(() -> { 21 | LOG.info("Comenzando processVeryLong..."); 22 | TimeUnit.SECONDS.sleep(5); 23 | LOG.info("Terminando processVeryLong..."); 24 | return param.concat(" result"); 25 | }); 26 | } 27 | 28 | public Future processVeryLongThatThrowsException(String parama) throws InterruptedException { 29 | return executor.submit(() -> { 30 | LOG.info("Comenzando processVeryLongThatThrowsException..."); 31 | TimeUnit.SECONDS.sleep(5); 32 | LOG.info("Terminando processVeryLongThatThrowsException..."); 33 | throw new RuntimeException("Error!"); 34 | }); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /future-example/src/test/java/com/edwise/pocs/futurepoc/FuturesTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.futurepoc; 2 | 3 | import org.junit.AfterClass; 4 | import org.junit.BeforeClass; 5 | import org.junit.Test; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | 9 | import java.util.concurrent.ExecutionException; 10 | import java.util.concurrent.ExecutorService; 11 | import java.util.concurrent.Executors; 12 | import java.util.concurrent.Future; 13 | 14 | import static org.junit.Assert.assertEquals; 15 | 16 | public class FuturesTest { 17 | private final static Logger LOG = LoggerFactory.getLogger(FuturesTest.class); 18 | 19 | private static ExecutorService executor; 20 | 21 | @BeforeClass 22 | public static void setUp() { 23 | executor = Executors.newFixedThreadPool(5); 24 | } 25 | 26 | @AfterClass 27 | public static void shutDown() { 28 | executor.shutdown(); 29 | } 30 | 31 | @Test 32 | public void testFuture() throws InterruptedException, ExecutionException { 33 | SomeBigProcess someBigProcess = new SomeBigProcess(executor); 34 | 35 | LOG.info("Lanzamos el futuro..."); 36 | Future future = someBigProcess.processVeryLong("test"); 37 | LOG.info("Futuro lanzado."); 38 | 39 | assertEquals("test result", future.get()); 40 | } 41 | 42 | @Test(expected = ExecutionException.class) 43 | public void testFutureWithException() throws InterruptedException, ExecutionException { 44 | SomeBigProcess someBigProcess = new SomeBigProcess(executor); 45 | 46 | LOG.info("Lanzamos el futuro..."); 47 | Future future = someBigProcess.processVeryLongThatThrowsException("test"); 48 | LOG.info("Futuro lanzado."); 49 | 50 | future.get(); 51 | } 52 | } -------------------------------------------------------------------------------- /integrationtests-maven-example/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de configuración de tests de integración con maven, creado para el post: https://anotherdayanotherbug.wordpress.com/2015/02/23/como-configurar-tus-tests-de-integracion-con-maven/ -------------------------------------------------------------------------------- /integrationtests-maven-example/src/main/java/com/edwise/pocs/integrationtestsmaven/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsmaven; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /integrationtests-maven-example/src/main/java/com/edwise/pocs/integrationtestsmaven/controller/InfoController.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsmaven.controller; 2 | 3 | import com.edwise.pocs.integrationtestsmaven.entity.Info; 4 | import com.edwise.pocs.integrationtestsmaven.service.InfoService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.MediaType; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.PathVariable; 10 | import org.springframework.web.bind.annotation.RequestBody; 11 | import org.springframework.web.bind.annotation.RequestMapping; 12 | import org.springframework.web.bind.annotation.RequestMethod; 13 | import org.springframework.web.bind.annotation.ResponseStatus; 14 | import org.springframework.web.bind.annotation.RestController; 15 | 16 | @RestController 17 | @RequestMapping("/api/info/") 18 | public class InfoController { 19 | 20 | @Autowired 21 | private InfoService infoService; 22 | 23 | @ResponseStatus(HttpStatus.OK) 24 | @RequestMapping(method = RequestMethod.GET, value = "{id}", 25 | produces = MediaType.APPLICATION_JSON_VALUE) 26 | public Info getInfo(@PathVariable Long id) { 27 | return infoService.findOne(id); 28 | } 29 | 30 | @RequestMapping(method = RequestMethod.POST, 31 | produces = MediaType.APPLICATION_JSON_VALUE) 32 | public ResponseEntity createInfo(@RequestBody Info info) { 33 | Info infoCreated = infoService.save(info); 34 | return new ResponseEntity<>(infoCreated, HttpStatus.CREATED); 35 | } 36 | 37 | @ResponseStatus(HttpStatus.NO_CONTENT) 38 | @RequestMapping(method = RequestMethod.DELETE, value = "{id}") 39 | public void deleteInfo(@PathVariable Long id) { 40 | infoService.delete(id); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /integrationtests-maven-example/src/main/java/com/edwise/pocs/integrationtestsmaven/entity/Info.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsmaven.entity; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | public class Info { 6 | 7 | private Long id; 8 | private String info; 9 | private LocalDateTime creationDateTime; 10 | 11 | public Long getId() { 12 | return id; 13 | } 14 | 15 | public Info setId(Long id) { 16 | this.id = id; 17 | return this; 18 | } 19 | 20 | public String getInfo() { 21 | return info; 22 | } 23 | 24 | public Info setInfo(String info) { 25 | this.info = info; 26 | return this; 27 | } 28 | 29 | public LocalDateTime getCreationDateTime() { 30 | return creationDateTime; 31 | } 32 | 33 | public Info setCreationDateTime(LocalDateTime creationDateTime) { 34 | this.creationDateTime = creationDateTime; 35 | return this; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /integrationtests-maven-example/src/main/java/com/edwise/pocs/integrationtestsmaven/repository/InfoRepository.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsmaven.repository; 2 | 3 | 4 | import com.edwise.pocs.integrationtestsmaven.entity.Info; 5 | 6 | public interface InfoRepository { 7 | 8 | Info save(Info info); 9 | 10 | Info findOne(Long id); 11 | 12 | void delete(Long id); 13 | } 14 | -------------------------------------------------------------------------------- /integrationtests-maven-example/src/main/java/com/edwise/pocs/integrationtestsmaven/repository/impl/InfoRepositoryMock.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsmaven.repository.impl; 2 | 3 | 4 | import com.edwise.pocs.integrationtestsmaven.entity.Info; 5 | import com.edwise.pocs.integrationtestsmaven.repository.InfoRepository; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import java.time.LocalDateTime; 9 | 10 | @Repository 11 | public class InfoRepositoryMock implements InfoRepository { 12 | 13 | @Override 14 | public Info save(Info info) { 15 | return info.setId(1234L); 16 | } 17 | 18 | @Override 19 | public Info findOne(Long id) { 20 | return new Info() 21 | .setId(id) 22 | .setInfo("Info 1234") 23 | .setCreationDateTime(LocalDateTime.of(2001, 12, 12, 13, 40, 30)); 24 | } 25 | 26 | @Override 27 | public void delete(Long id) { 28 | // delete info by id 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /integrationtests-maven-example/src/main/java/com/edwise/pocs/integrationtestsmaven/service/InfoService.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsmaven.service; 2 | 3 | 4 | import com.edwise.pocs.integrationtestsmaven.entity.Info; 5 | 6 | public interface InfoService { 7 | 8 | Info save(Info entity); 9 | 10 | Info findOne(Long id); 11 | 12 | void delete(Long id); 13 | } 14 | -------------------------------------------------------------------------------- /integrationtests-maven-example/src/main/java/com/edwise/pocs/integrationtestsmaven/service/impl/InfoServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsmaven.service.impl; 2 | 3 | 4 | import com.edwise.pocs.integrationtestsmaven.entity.Info; 5 | import com.edwise.pocs.integrationtestsmaven.repository.InfoRepository; 6 | import com.edwise.pocs.integrationtestsmaven.service.InfoService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | @Service 11 | public class InfoServiceImpl implements InfoService { 12 | 13 | @Autowired 14 | private InfoRepository infoRepository; 15 | 16 | @Override 17 | public Info save(Info info) { 18 | return infoRepository.save(info); 19 | } 20 | 21 | @Override 22 | public Info findOne(Long id) { 23 | return infoRepository.findOne(id); 24 | } 25 | 26 | @Override 27 | public void delete(Long id) { 28 | infoRepository.delete(id); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /integrationtests-maven-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.jackson.serialization.write_dates_as_timestamps=false -------------------------------------------------------------------------------- /integrationtests-maven-example/src/test/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.jackson.serialization.write_dates_as_timestamps=false -------------------------------------------------------------------------------- /integrationtests-rest-example/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de tests de integración de un servicio REST con Spring, creado para el post: https://anotherdayanotherbug.wordpress.com/2015/03/16/tests-de-integracion-para-un-servicio-rest-con-spring/ -------------------------------------------------------------------------------- /integrationtests-rest-example/src/main/java/com/edwise/pocs/integrationtestsrest/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsrest; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /integrationtests-rest-example/src/main/java/com/edwise/pocs/integrationtestsrest/controller/InfoController.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsrest.controller; 2 | 3 | import com.edwise.pocs.integrationtestsrest.entity.Info; 4 | import com.edwise.pocs.integrationtestsrest.service.InfoService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.MediaType; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import java.util.List; 12 | 13 | @RestController 14 | @RequestMapping("/api/info/") 15 | public class InfoController { 16 | 17 | @Autowired 18 | private InfoService infoService; 19 | 20 | @ResponseStatus(HttpStatus.OK) 21 | @RequestMapping(method = RequestMethod.GET, 22 | produces = MediaType.APPLICATION_JSON_VALUE) 23 | public List getAllInfos() { 24 | return infoService.findAll(); 25 | } 26 | 27 | @ResponseStatus(HttpStatus.OK) 28 | @RequestMapping(method = RequestMethod.GET, value = "{id}", 29 | produces = MediaType.APPLICATION_JSON_VALUE) 30 | public Info getInfo(@PathVariable Long id) { 31 | return infoService.findOne(id); 32 | } 33 | 34 | @RequestMapping(method = RequestMethod.POST, 35 | produces = MediaType.APPLICATION_JSON_VALUE) 36 | public ResponseEntity createInfo(@RequestBody Info info) { 37 | Info infoCreated = infoService.save(info); 38 | return new ResponseEntity<>(infoCreated, HttpStatus.CREATED); 39 | } 40 | 41 | @ResponseStatus(HttpStatus.NO_CONTENT) 42 | @RequestMapping(method = RequestMethod.PUT, value = "{id}") 43 | public void updateInfo(@PathVariable Long id, @RequestBody Info info) { 44 | infoService.update(info.setId(id)); 45 | } 46 | 47 | @ResponseStatus(HttpStatus.NO_CONTENT) 48 | @RequestMapping(method = RequestMethod.DELETE, value = "{id}") 49 | public void deleteInfo(@PathVariable Long id) { 50 | infoService.delete(id); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /integrationtests-rest-example/src/main/java/com/edwise/pocs/integrationtestsrest/entity/Info.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsrest.entity; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | public class Info { 6 | 7 | private Long id; 8 | private String infoText; 9 | private LocalDateTime creationDateTime; 10 | 11 | public Long getId() { 12 | return id; 13 | } 14 | 15 | public Info setId(Long id) { 16 | this.id = id; 17 | return this; 18 | } 19 | 20 | public String getInfoText() { 21 | return infoText; 22 | } 23 | 24 | public Info setInfoText(String infoText) { 25 | this.infoText = infoText; 26 | return this; 27 | } 28 | 29 | public LocalDateTime getCreationDateTime() { 30 | return creationDateTime; 31 | } 32 | 33 | public Info setCreationDateTime(LocalDateTime creationDateTime) { 34 | this.creationDateTime = creationDateTime; 35 | return this; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /integrationtests-rest-example/src/main/java/com/edwise/pocs/integrationtestsrest/repository/InfoRepository.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsrest.repository; 2 | 3 | import com.edwise.pocs.integrationtestsrest.entity.Info; 4 | 5 | import java.util.List; 6 | 7 | public interface InfoRepository { 8 | 9 | Info save(Info info); 10 | 11 | Info findOne(Long id); 12 | 13 | List findAll(); 14 | 15 | Info update(Info info); 16 | 17 | void delete(Long id); 18 | } 19 | -------------------------------------------------------------------------------- /integrationtests-rest-example/src/main/java/com/edwise/pocs/integrationtestsrest/repository/impl/InfoRepositoryMock.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsrest.repository.impl; 2 | 3 | import com.edwise.pocs.integrationtestsrest.entity.Info; 4 | import com.edwise.pocs.integrationtestsrest.repository.InfoRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.time.LocalDateTime; 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | @Repository 12 | public class InfoRepositoryMock implements InfoRepository { 13 | 14 | @Override 15 | public Info save(Info info) { 16 | return info.setId(1234L); 17 | } 18 | 19 | @Override 20 | public Info findOne(Long id) { 21 | return new Info() 22 | .setId(id) 23 | .setInfoText("Info 1234") 24 | .setCreationDateTime(LocalDateTime.of(2001, 12, 12, 13, 40, 30)); 25 | } 26 | 27 | @Override 28 | public List findAll() { 29 | return Arrays.asList( 30 | new Info() 31 | .setId(120L) 32 | .setInfoText("Info 1234") 33 | .setCreationDateTime(LocalDateTime.of(2001, 12, 12, 13, 40, 30)), 34 | new Info() 35 | .setId(121L) 36 | .setInfoText("Info 4567") 37 | .setCreationDateTime(LocalDateTime.of(2012, 11, 5, 10, 44, 30)), 38 | new Info() 39 | .setId(122L) 40 | .setInfoText("Info 7892") 41 | .setCreationDateTime(LocalDateTime.of(2013, 12, 10, 13, 33, 12)) 42 | ); 43 | } 44 | 45 | @Override 46 | public Info update(Info info) { 47 | return new Info() 48 | .setId(info.getId()) 49 | .setInfoText("Info 1234 Updated") 50 | .setCreationDateTime(LocalDateTime.of(2012, 11, 12, 19, 35, 10)); 51 | } 52 | 53 | @Override 54 | public void delete(Long id) { 55 | // delete info by id 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /integrationtests-rest-example/src/main/java/com/edwise/pocs/integrationtestsrest/service/InfoService.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsrest.service; 2 | 3 | import com.edwise.pocs.integrationtestsrest.entity.Info; 4 | 5 | import java.util.List; 6 | 7 | public interface InfoService { 8 | 9 | Info save(Info entity); 10 | 11 | Info findOne(Long id); 12 | 13 | List findAll(); 14 | 15 | Info update(Info info); 16 | 17 | void delete(Long id); 18 | } 19 | -------------------------------------------------------------------------------- /integrationtests-rest-example/src/main/java/com/edwise/pocs/integrationtestsrest/service/impl/InfoServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.integrationtestsrest.service.impl; 2 | 3 | 4 | import com.edwise.pocs.integrationtestsrest.entity.Info; 5 | import com.edwise.pocs.integrationtestsrest.repository.InfoRepository; 6 | import com.edwise.pocs.integrationtestsrest.service.InfoService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.List; 11 | 12 | @Service 13 | public class InfoServiceImpl implements InfoService { 14 | 15 | @Autowired 16 | private InfoRepository infoRepository; 17 | 18 | @Override 19 | public Info save(Info info) { 20 | return infoRepository.save(info); 21 | } 22 | 23 | @Override 24 | public List findAll() { 25 | return infoRepository.findAll(); 26 | } 27 | 28 | @Override 29 | public Info findOne(Long id) { 30 | return infoRepository.findOne(id); 31 | } 32 | 33 | @Override 34 | public Info update(Info info) { 35 | return infoRepository.update(info); 36 | } 37 | 38 | @Override 39 | public void delete(Long id) { 40 | infoRepository.delete(id); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /integrationtests-rest-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.jackson.serialization.write_dates_as_timestamps=false -------------------------------------------------------------------------------- /integrationtests-restassured-example/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de tests de integración de un servicio REST usando REST-Assured, creado para el post: https://anotherdayanotherbug.wordpress.com/2015/05/25/tests-de-integracion-usando-rest-assured/ 2 | -------------------------------------------------------------------------------- /integrationtests-restassured-example/src/main/java/com/edwise/pocs/itrestassured/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.itrestassured; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /integrationtests-restassured-example/src/main/java/com/edwise/pocs/itrestassured/entity/Info.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.itrestassured.entity; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | public class Info { 6 | 7 | private Long id; 8 | private String infoText; 9 | private LocalDateTime creationDateTime; 10 | 11 | public Long getId() { 12 | return id; 13 | } 14 | 15 | public Info setId(Long id) { 16 | this.id = id; 17 | return this; 18 | } 19 | 20 | public String getInfoText() { 21 | return infoText; 22 | } 23 | 24 | public Info setInfoText(String infoText) { 25 | this.infoText = infoText; 26 | return this; 27 | } 28 | 29 | public LocalDateTime getCreationDateTime() { 30 | return creationDateTime; 31 | } 32 | 33 | public Info setCreationDateTime(LocalDateTime creationDateTime) { 34 | this.creationDateTime = creationDateTime; 35 | return this; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /integrationtests-restassured-example/src/main/java/com/edwise/pocs/itrestassured/repository/InfoRepository.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.itrestassured.repository; 2 | 3 | 4 | import com.edwise.pocs.itrestassured.entity.Info; 5 | 6 | import java.util.List; 7 | 8 | public interface InfoRepository { 9 | 10 | Info save(Info info); 11 | 12 | Info findOne(Long id); 13 | 14 | List findAll(); 15 | 16 | Info update(Info info); 17 | 18 | void delete(Long id); 19 | } 20 | -------------------------------------------------------------------------------- /integrationtests-restassured-example/src/main/java/com/edwise/pocs/itrestassured/repository/impl/InfoRepositoryMock.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.itrestassured.repository.impl; 2 | 3 | import com.edwise.pocs.itrestassured.entity.Info; 4 | import com.edwise.pocs.itrestassured.repository.InfoRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.time.LocalDateTime; 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | @Repository 12 | public class InfoRepositoryMock implements InfoRepository { 13 | 14 | @Override 15 | public Info save(Info info) { 16 | return info.setId(1234L); 17 | } 18 | 19 | @Override 20 | public Info findOne(Long id) { 21 | return new Info() 22 | .setId(id) 23 | .setInfoText("Info 1234") 24 | .setCreationDateTime(LocalDateTime.of(2001, 12, 12, 13, 40, 30)); 25 | } 26 | 27 | @Override 28 | public List findAll() { 29 | return Arrays.asList( 30 | new Info() 31 | .setId(120L) 32 | .setInfoText("Info 1234") 33 | .setCreationDateTime(LocalDateTime.of(2001, 12, 12, 13, 40, 30)), 34 | new Info() 35 | .setId(121L) 36 | .setInfoText("Info 4567") 37 | .setCreationDateTime(LocalDateTime.of(2012, 11, 5, 10, 44, 30)), 38 | new Info() 39 | .setId(122L) 40 | .setInfoText("Info 7892") 41 | .setCreationDateTime(LocalDateTime.of(2013, 12, 10, 13, 33, 12)) 42 | ); 43 | } 44 | 45 | @Override 46 | public Info update(Info info) { 47 | return new Info() 48 | .setId(info.getId()) 49 | .setInfoText("Info 1234 Updated") 50 | .setCreationDateTime(LocalDateTime.of(2012, 11, 12, 19, 35, 10)); 51 | } 52 | 53 | @Override 54 | public void delete(Long id) { 55 | // delete info by id 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /integrationtests-restassured-example/src/main/java/com/edwise/pocs/itrestassured/service/InfoService.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.itrestassured.service; 2 | 3 | 4 | import com.edwise.pocs.itrestassured.entity.Info; 5 | 6 | import java.util.List; 7 | 8 | public interface InfoService { 9 | 10 | Info save(Info entity); 11 | 12 | Info findOne(Long id); 13 | 14 | List findAll(); 15 | 16 | Info update(Info info); 17 | 18 | void delete(Long id); 19 | } 20 | -------------------------------------------------------------------------------- /integrationtests-restassured-example/src/main/java/com/edwise/pocs/itrestassured/service/impl/InfoServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.itrestassured.service.impl; 2 | 3 | 4 | import com.edwise.pocs.itrestassured.entity.Info; 5 | import com.edwise.pocs.itrestassured.repository.InfoRepository; 6 | import com.edwise.pocs.itrestassured.service.InfoService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.List; 11 | 12 | @Service 13 | public class InfoServiceImpl implements InfoService { 14 | 15 | @Autowired 16 | private InfoRepository infoRepository; 17 | 18 | @Override 19 | public Info save(Info info) { 20 | return infoRepository.save(info); 21 | } 22 | 23 | @Override 24 | public List findAll() { 25 | return infoRepository.findAll(); 26 | } 27 | 28 | @Override 29 | public Info findOne(Long id) { 30 | return infoRepository.findOne(id); 31 | } 32 | 33 | @Override 34 | public Info update(Info info) { 35 | return infoRepository.update(info); 36 | } 37 | 38 | @Override 39 | public void delete(Long id) { 40 | infoRepository.delete(id); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /integrationtests-restassured-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.jackson.serialization.write_dates_as_timestamps=false -------------------------------------------------------------------------------- /java8-collectingandthen-example/README.md: -------------------------------------------------------------------------------- 1 | - Proyecto de ejemplo de uso del método collectingAndThen en Streams: https://anotherdayanotherbug.wordpress.com/2016/04/19/java-8-tips-collectingandthen-en-streams/ 2 | -------------------------------------------------------------------------------- /java8-collectingandthen-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.pocs.java8collectingandthen 7 | java8-collectingandthen-example 8 | 1.0.0 9 | jar 10 | 11 | java8-collectingandthen-example 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.12 24 | test 25 | 26 | 27 | 28 | org.assertj 29 | assertj-core 30 | 3.4.1 31 | test 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.apache.maven.plugins 39 | maven-compiler-plugin 40 | 3.5.1 41 | 42 | ${java.version} 43 | ${java.version} 44 | ${java.version} 45 | ${java.version} 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /java8-collectingandthen-example/src/main/java/com/edwise/pocs/java8collectingandthen/CollectorsCollectingAndThen.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.java8collectingandthen; 2 | 3 | public class CollectorsCollectingAndThen { 4 | 5 | public static void main(String[] args) { 6 | // The code in the test class CollectorsCollectingAndThenTest 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /java8-function-methods-example/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de uso de los métodos defaults de la interfaz Function: https://anotherdayanotherbug.wordpress.com/2016/06/21/java-8-tips-metodos-default-en-la-interfaz-funcional-function/ -------------------------------------------------------------------------------- /java8-function-methods-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.pocs.java8functionmethods 7 | java8-function-methods-example 8 | 1.0.0-SNAPSHOT 9 | jar 10 | 11 | java8-function-methods-example 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.12 24 | test 25 | 26 | 27 | org.assertj 28 | assertj-core 29 | 3.4.1 30 | test 31 | 32 | 33 | 34 | 35 | 36 | 37 | org.apache.maven.plugins 38 | maven-compiler-plugin 39 | 3.5.1 40 | 41 | ${java.version} 42 | ${java.version} 43 | ${java.version} 44 | ${java.version} 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /java8-function-methods-example/src/main/java/com/edwise/pocs/java8functionmethods/BookCharacterFunction.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.java8functionmethods; 2 | 3 | import com.edwise.pocs.java8functionmethods.model.BookCharacter; 4 | 5 | import java.util.List; 6 | import java.util.function.Function; 7 | 8 | import static com.edwise.pocs.java8functionmethods.model.BookCharacter.Weapon; 9 | 10 | public final class BookCharacterFunction { 11 | 12 | private BookCharacterFunction() { 13 | } 14 | 15 | public static Function, BookCharacter> findFirstSwordsman() { 16 | return list -> 17 | list.stream() 18 | .filter(bookChar -> Weapon.SWORD.equals(bookChar.getMainWeapon())) 19 | .findFirst() 20 | .orElse(new BookCharacter("NOBODY", 0, Weapon.SWORD, true)); 21 | } 22 | 23 | public static Function characterToCode() { 24 | return bookCharacter -> 25 | bookCharacter.getName() + "::" + bookCharacter.getMainWeapon() + "::" + 26 | (bookCharacter.isHuman() ? "human" : "nonHuman"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /java8-function-methods-example/src/main/java/com/edwise/pocs/java8functionmethods/model/BookCharacter.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.java8functionmethods.model; 2 | 3 | import java.util.Objects; 4 | 5 | public class BookCharacter { 6 | private final String name; 7 | private final Integer age; 8 | private final Weapon mainWeapon; 9 | private final boolean human; 10 | 11 | public BookCharacter(String name, Integer age, Weapon mainWeapon, boolean human) { 12 | this.name = name; 13 | this.age = age; 14 | this.mainWeapon = mainWeapon; 15 | this.human = human; 16 | } 17 | 18 | public enum Weapon {SWORD, AXE, BOW, STAFF, RING} 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public Integer getAge() { 25 | return age; 26 | } 27 | 28 | public Weapon getMainWeapon() { 29 | return mainWeapon; 30 | } 31 | 32 | public boolean isHuman() { 33 | return human; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "BookCharacter{" + 39 | "name='" + name + '\'' + 40 | '}'; 41 | } 42 | 43 | @Override 44 | public boolean equals(Object o) { 45 | if (this == o) return true; 46 | if (o == null || getClass() != o.getClass()) return false; 47 | BookCharacter that = (BookCharacter) o; 48 | return Objects.equals(name, that.name); 49 | } 50 | 51 | @Override 52 | public int hashCode() { 53 | return Objects.hash(name); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /java8-predicate-methods-example/README.md: -------------------------------------------------------------------------------- 1 | - Proyecto de ejemplo de uso de los métodos defaults de la interfaz Predicate: https://anotherdayanotherbug.wordpress.com/2016/05/10/java-8-tips-metodos-default-en-la-interfaz-funcional-predicate/ -------------------------------------------------------------------------------- /java8-predicate-methods-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.pocs.java8predicatemethods 7 | java8-predicate-methods-example 8 | 1.0.0 9 | jar 10 | 11 | java8-predicate-methods-example 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.12 24 | test 25 | 26 | 27 | org.assertj 28 | assertj-core 29 | 3.4.1 30 | test 31 | 32 | 33 | 34 | 35 | 36 | 37 | org.apache.maven.plugins 38 | maven-compiler-plugin 39 | 3.5.1 40 | 41 | ${java.version} 42 | ${java.version} 43 | ${java.version} 44 | ${java.version} 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /java8-predicate-methods-example/src/main/java/com/edwise/pocs/java8predicatemethods/BookCharacterChecker.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.java8predicatemethods; 2 | 3 | import com.edwise.pocs.java8predicatemethods.model.BookCharacter; 4 | 5 | import java.util.function.Predicate; 6 | 7 | public class BookCharacterChecker { 8 | 9 | public void doSomeStuffIfThisAndValid(BookCharacter bChar, 10 | Predicate predicate) { 11 | if (predicate.and(BookCharacterPredicate.isValid()).test(bChar)) { 12 | // do some stuff 13 | System.out.println("doing stuff with result true"); 14 | } else { 15 | // do other stuff 16 | System.out.println("doing stuff with result false"); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java8-predicate-methods-example/src/main/java/com/edwise/pocs/java8predicatemethods/BookCharacterPredicate.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.java8predicatemethods; 2 | 3 | import com.edwise.pocs.java8predicatemethods.model.BookCharacter; 4 | 5 | import java.util.function.Predicate; 6 | 7 | public class BookCharacterPredicate { 8 | public static Predicate isYoung() { 9 | return bChar -> bChar.getAge() < 90; 10 | } 11 | 12 | public static Predicate useSword() { 13 | return bChar -> BookCharacter.Weapon.SWORD.equals(bChar.getMainWeapon()); 14 | } 15 | 16 | public static Predicate isHuman() { 17 | return BookCharacter::isHuman; 18 | } 19 | 20 | public static Predicate isValid() { 21 | return bChar -> bChar.getName() != null && 22 | bChar.getAge() > 0 && 23 | bChar.getMainWeapon() != null; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java8-predicate-methods-example/src/main/java/com/edwise/pocs/java8predicatemethods/model/BookCharacter.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.java8predicatemethods.model; 2 | 3 | import java.util.Objects; 4 | 5 | public class BookCharacter { 6 | private final String name; 7 | private final Integer age; 8 | private final Weapon mainWeapon; 9 | private final boolean human; 10 | 11 | public BookCharacter(String name, Integer age, Weapon mainWeapon, boolean human) { 12 | this.name = name; 13 | this.age = age; 14 | this.mainWeapon = mainWeapon; 15 | this.human = human; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public Integer getAge() { 23 | return age; 24 | } 25 | 26 | public Weapon getMainWeapon() { 27 | return mainWeapon; 28 | } 29 | 30 | public boolean isHuman() { 31 | return human; 32 | } 33 | 34 | public enum Weapon {SWORD, AXE, BOW, STAFF, RING} 35 | 36 | @Override 37 | public String toString() { 38 | return "BookCharacter{" + 39 | "name='" + name + '\'' + 40 | '}'; 41 | } 42 | 43 | @Override 44 | public boolean equals(Object o) { 45 | if (this == o) return true; 46 | if (o == null || getClass() != o.getClass()) return false; 47 | BookCharacter that = (BookCharacter) o; 48 | return Objects.equals(name, that.name); 49 | } 50 | 51 | @Override 52 | public int hashCode() { 53 | return Objects.hash(name); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /jaxrs-wildcards-example/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de wildcards en jaxrs, creado para el post: https://anotherdayanotherbug.wordpress.com/2015/11/04/expresiones-regulares-y-wildcards-en-servicios-rest-con-jax-rs/ -------------------------------------------------------------------------------- /jaxrs-wildcards-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.pocs.jaxrswildcards 7 | jaxrs-wildcards-example 8 | 1.0 9 | jar 10 | 11 | jaxrs-wildcards-example 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 2.21.1 19 | 9.3.5.v20151012 20 | 21 | 22 | 23 | 24 | 25 | org.glassfish.jersey 26 | jersey-bom 27 | ${jersey.version} 28 | pom 29 | import 30 | 31 | 32 | 33 | 34 | 35 | 36 | org.glassfish.jersey.containers 37 | jersey-container-servlet 38 | 39 | 40 | 41 | junit 42 | junit 43 | 4.12 44 | test 45 | 46 | 47 | org.assertj 48 | assertj-core 49 | 3.2.0 50 | test 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.apache.maven.plugins 58 | maven-compiler-plugin 59 | 3.3 60 | 61 | ${java.version} 62 | ${java.version} 63 | ${java.version} 64 | ${java.version} 65 | 66 | 67 | 68 | 69 | org.eclipse.jetty 70 | jetty-maven-plugin 71 | ${jettyplugin.version} 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /jaxrs-wildcards-example/src/main/java/com/edwise/pocs/jaxrswildcards/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jaxrswildcards; 2 | 3 | import org.glassfish.jersey.server.ResourceConfig; 4 | 5 | import javax.ws.rs.ApplicationPath; 6 | 7 | @ApplicationPath("api") 8 | public class Application extends ResourceConfig { 9 | 10 | public Application() { 11 | packages("com.edwise.pocs.jaxrswildcards"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /jaxrs-wildcards-example/src/main/java/com/edwise/pocs/jaxrswildcards/resource/FooResource.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jaxrswildcards.resource; 2 | 3 | import javax.ws.rs.GET; 4 | import javax.ws.rs.Path; 5 | import javax.ws.rs.PathParam; 6 | import javax.ws.rs.Produces; 7 | import javax.ws.rs.core.MediaType; 8 | 9 | @Path("foo") 10 | public class FooResource { 11 | 12 | @GET 13 | @Produces(MediaType.TEXT_PLAIN) 14 | public String getAllFoos() { 15 | return "All foos"; 16 | } 17 | 18 | @GET 19 | @Path("{id}") 20 | @Produces(MediaType.TEXT_PLAIN) 21 | public String getFooById(@PathParam("id") String id) { 22 | return "Foo with id: " + id; 23 | } 24 | 25 | @GET 26 | @Path("twoparams/{firstname}-{surname}") 27 | @Produces(MediaType.TEXT_PLAIN) 28 | public String getFooWithNameAndSurname(@PathParam("firstname") String firstname, @PathParam("surname") String surname) { 29 | return firstname + " " + surname; 30 | } 31 | 32 | @GET 33 | @Path("onlyinteger/{id : \\d+}") 34 | @Produces(MediaType.TEXT_PLAIN) 35 | public String getFooWithIntegerId(@PathParam("id") int id) { 36 | return Integer.toString(id); 37 | } 38 | 39 | @GET 40 | @Path("wildcard/{subpath : .+}") 41 | @Produces(MediaType.TEXT_PLAIN) 42 | public String getFooWithWildcard(@PathParam("subpath") String subpath) { 43 | return subpath; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /jaxrs-wildcards-example/src/test/java/com/edwise/pocs/jaxrswildcards/resource/FooResourceTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jaxrswildcards.resource; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.assertj.core.api.Assertions.assertThat; 7 | 8 | public class FooResourceTest { 9 | 10 | private static final String ID_1234 = "ID_1234"; 11 | private static final String NAME = "name"; 12 | private static final String SURNAME = "surname"; 13 | private static final int INTEGER_ID = 1234; 14 | private static final String DIRECTORIES_PATH = "/temp/files/another/mytext.txt"; 15 | 16 | private FooResource fooResource; 17 | 18 | @Before 19 | public void setUp() { 20 | fooResource = new FooResource(); 21 | } 22 | 23 | @Test 24 | public void testWhenGetAllFoosThenReturnsAllFoos() { 25 | String result = fooResource.getAllFoos(); 26 | 27 | assertThat(result).isEqualTo("All foos"); 28 | } 29 | 30 | @Test 31 | public void testWhenGetFooByIdThenReturnsFoo() { 32 | String result = fooResource.getFooById(ID_1234); 33 | 34 | assertThat(result).isEqualTo("Foo with id: " + ID_1234); 35 | } 36 | 37 | @Test 38 | public void testWhenGetFooWithNameAndSurnameThenReturnsNameAndSurname() { 39 | String result = fooResource.getFooWithNameAndSurname(NAME, SURNAME); 40 | 41 | assertThat(result).isEqualTo(NAME + " " + SURNAME); 42 | } 43 | 44 | @Test 45 | public void testWhenGetFooWithIntegerIdThenReturnsId() { 46 | String result = fooResource.getFooWithIntegerId(INTEGER_ID); 47 | 48 | assertThat(result).isEqualTo(Integer.toString(INTEGER_ID)); 49 | } 50 | 51 | @Test 52 | public void testWhenGetFooWithWildcardThenReturnsPathVariable() { 53 | String result = fooResource.getFooWithWildcard(DIRECTORIES_PATH); 54 | 55 | assertThat(result).isEqualTo(DIRECTORIES_PATH); 56 | } 57 | } -------------------------------------------------------------------------------- /jersey-rest-client-example/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de cliente REST con JAX-RS (Jersey) creado para el post: https://anotherdayanotherbug.wordpress.com/2015/09/11/implementar-un-cliente-rest-con-jax-rs-jersey/ -------------------------------------------------------------------------------- /jersey-rest-client-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.pocs.jerseyrestclient 7 | jersey-rest-client-example 8 | 1.0 9 | jar 10 | 11 | jersey-rest-client-example 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | 21 | org.glassfish.jersey.core 22 | jersey-client 23 | 2.21 24 | 25 | 26 | com.fasterxml.jackson.jaxrs 27 | jackson-jaxrs-json-provider 28 | 2.6.1 29 | 30 | 31 | com.fasterxml.jackson.datatype 32 | jackson-datatype-jsr310 33 | 2.6.1 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-compiler-plugin 42 | 3.3 43 | 44 | ${java.version} 45 | ${java.version} 46 | ${java.version} 47 | ${java.version} 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /jersey-rest-client-example/src/main/java/com/edwise/pocs/jerseyrestclient/model/Book.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jerseyrestclient.model; 2 | 3 | public class Book { 4 | private Long id; 5 | private String title; 6 | 7 | public Long getId() { 8 | return id; 9 | } 10 | 11 | public void setId(Long id) { 12 | this.id = id; 13 | } 14 | 15 | public String getTitle() { 16 | return title; 17 | } 18 | 19 | public void setTitle(String title) { 20 | this.title = title; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return "Book{" + 26 | "id=" + id + 27 | ", title='" + title + '\'' + 28 | '}'; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /jersey-rest-client-example/src/main/java/com/edwise/pocs/jerseyrestclient/model/Info.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jerseyrestclient.model; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | public class Info { 6 | 7 | private Long id; 8 | private String infoText; 9 | private LocalDateTime creationDateTime; 10 | 11 | public Long getId() { 12 | return id; 13 | } 14 | 15 | public void setId(Long id) { 16 | this.id = id; 17 | } 18 | 19 | public String getInfoText() { 20 | return infoText; 21 | } 22 | 23 | public void setInfoText(String infoText) { 24 | this.infoText = infoText; 25 | } 26 | 27 | public LocalDateTime getCreationDateTime() { 28 | return creationDateTime; 29 | } 30 | 31 | public void setCreationDateTime(LocalDateTime creationDateTime) { 32 | this.creationDateTime = creationDateTime; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "Info{" + 38 | "id=" + id + 39 | ", infoText='" + infoText + '\'' + 40 | ", creationDateTime=" + creationDateTime + 41 | '}'; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /jersey-rest-example/README.md: -------------------------------------------------------------------------------- 1 | POC de un servicio REST con JAX-RS (Jersey), creado para el post http://anotherdayanotherbug.wordpress.com/2014/11/10/implementar-un-servicio-rest-con-jax-rs-jersey 2 | 3 | Actualizado para el post: http://anotherdayanotherbug.wordpress.com/2015/04/08/como-devolver-el-resultado-de-un-post-en-un-servicio-rest -------------------------------------------------------------------------------- /jersey-rest-example/src/main/java/com/edwise/pocs/jerseyrest/RestApplication.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jerseyrest; 2 | 3 | import com.edwise.pocs.jerseyrest.service.UserService; 4 | import com.edwise.pocs.jerseyrest.service.impl.UserServiceMock; 5 | import org.glassfish.hk2.utilities.binding.AbstractBinder; 6 | import org.glassfish.jersey.jackson.JacksonFeature; 7 | import org.glassfish.jersey.server.ResourceConfig; 8 | 9 | import javax.ws.rs.ApplicationPath; 10 | 11 | @ApplicationPath("api") 12 | public class RestApplication extends ResourceConfig { 13 | 14 | public RestApplication() { 15 | packages("com.edwise.pocs.jerseyrest"); 16 | 17 | // Si usamos un JEE container (Glassfish, TomEE, etc), este registro no es necesario. 18 | register(new AbstractBinder() { 19 | @Override 20 | protected void configure() { 21 | bind(new UserServiceMock()).to(UserService.class); 22 | } 23 | }); 24 | 25 | register(JacksonFeature.class); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /jersey-rest-example/src/main/java/com/edwise/pocs/jerseyrest/config/JsonMapperProvider.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jerseyrest.config; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.fasterxml.jackson.databind.SerializationFeature; 5 | import com.fasterxml.jackson.datatype.joda.JodaModule; 6 | 7 | import javax.ws.rs.Produces; 8 | import javax.ws.rs.core.MediaType; 9 | import javax.ws.rs.ext.ContextResolver; 10 | import javax.ws.rs.ext.Provider; 11 | 12 | @Provider 13 | @Produces(MediaType.APPLICATION_JSON) 14 | public class JsonMapperProvider implements ContextResolver { 15 | 16 | final ObjectMapper objectMapper; 17 | 18 | public JsonMapperProvider() { 19 | objectMapper = new ObjectMapper(); 20 | objectMapper.registerModule(new JodaModule()); 21 | objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); 22 | } 23 | 24 | @Override 25 | public ObjectMapper getContext(Class arg0) { 26 | return objectMapper; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /jersey-rest-example/src/main/java/com/edwise/pocs/jerseyrest/resource/UserController.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jerseyrest.resource; 2 | 3 | import com.edwise.pocs.jerseyrest.entity.User; 4 | import com.edwise.pocs.jerseyrest.service.UserService; 5 | 6 | import javax.inject.Inject; 7 | import javax.ws.rs.*; 8 | import javax.ws.rs.core.Context; 9 | import javax.ws.rs.core.MediaType; 10 | import javax.ws.rs.core.Response; 11 | import javax.ws.rs.core.UriBuilder; 12 | import javax.ws.rs.core.UriInfo; 13 | import java.net.URI; 14 | import java.util.List; 15 | 16 | @Path("users") 17 | public class UserController { 18 | 19 | @Inject 20 | private UserService userService; 21 | 22 | @Context 23 | private UriInfo uriInfo; 24 | 25 | @GET 26 | @Produces(MediaType.APPLICATION_JSON) 27 | public List getAllUsers() { 28 | return userService.findAll(); 29 | } 30 | 31 | @GET 32 | @Path("/{id}") 33 | @Produces(MediaType.APPLICATION_JSON) 34 | public Response getUserById(@PathParam("id") long userId) { 35 | Response response; 36 | User user = userService.findById(userId); 37 | if (user == null) { 38 | response = Response.status(Response.Status.NOT_FOUND).build(); 39 | } else { 40 | response = Response.ok(user).build(); 41 | } 42 | return response; 43 | } 44 | 45 | @POST 46 | @Consumes(MediaType.APPLICATION_JSON) 47 | public Response insertUser(User user) { 48 | User userSaved = userService.save(user); 49 | return Response.created(createURIFromResource(userSaved)).build(); 50 | } 51 | 52 | @PUT 53 | @Path("/{id}") 54 | @Consumes(MediaType.APPLICATION_JSON) 55 | public Response updateUser(@PathParam("id") long userId, User user) { 56 | Response response; 57 | User userOld = userService.findById(userId); 58 | if (userOld != null) { 59 | userService.update(userOld.copyFrom(user)); 60 | response = Response.status(Response.Status.NO_CONTENT).build(); 61 | } else { 62 | response = Response.status(Response.Status.NOT_FOUND).build(); 63 | } 64 | 65 | return response; 66 | } 67 | 68 | @DELETE 69 | @Path("/{id}") 70 | public Response deleteUser(@PathParam("id") long userId) { 71 | userService.delete(userId); 72 | return Response.status(Response.Status.NO_CONTENT).build(); 73 | } 74 | 75 | private URI createURIFromResource(User userSaved) { 76 | UriBuilder uriBuilder = uriInfo.getRequestUriBuilder(); 77 | return uriBuilder.path(userSaved.getId().toString()).build(); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /jersey-rest-example/src/main/java/com/edwise/pocs/jerseyrest/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jerseyrest.service; 2 | 3 | import com.edwise.pocs.jerseyrest.entity.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserService { 8 | 9 | User findById(Long id); 10 | 11 | List findAll(); 12 | 13 | User save(User user); 14 | 15 | User update(User user); 16 | 17 | void delete(Long id); 18 | } 19 | -------------------------------------------------------------------------------- /jersey-rest-example/src/main/java/com/edwise/pocs/jerseyrest/service/impl/UserServiceMock.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jerseyrest.service.impl; 2 | 3 | import com.edwise.pocs.jerseyrest.entity.User; 4 | import com.edwise.pocs.jerseyrest.service.UserService; 5 | import org.joda.time.LocalDate; 6 | 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | public class UserServiceMock implements UserService { 11 | 12 | private static final long USER_ID_12 = 12L; 13 | private static final long USER_ID_140 = 140L; 14 | private static final long USER_ID_453321 = 45332L; 15 | private static final String NAME_GANDALF = "Gandalf"; 16 | private static final String NAME_ARAGORN = "Aragorn"; 17 | private static final String NAME_FRODO = "Frodo"; 18 | private static final int TYPE_1 = 1; 19 | private static final int TYPE_2 = 2; 20 | private static final String PHONE_666554433 = "666554433"; 21 | private static final String PHONE_661534411 = "661534411"; 22 | private static final String PHONE_666222211 = "666222211"; 23 | private static final String STRING_DATE_19110102 = "1911-01-02"; 24 | private static final String STRING_DATE_19230716 = "1923-07-16"; 25 | private static final String STRING_DATE_19511124 = "1951-11-24"; 26 | 27 | @Override 28 | public User findById(Long id) { 29 | return new User() 30 | .setId(id) 31 | .setName(NAME_GANDALF) 32 | .setPhone(PHONE_666554433) 33 | .setType(TYPE_1) 34 | .setBirthDate(LocalDate.parse(STRING_DATE_19110102)); 35 | } 36 | 37 | @Override 38 | public List findAll() { 39 | return Arrays.asList( 40 | new User() 41 | .setId(USER_ID_12) 42 | .setName(NAME_GANDALF) 43 | .setPhone(PHONE_666554433) 44 | .setType(TYPE_1) 45 | .setBirthDate(LocalDate.parse(STRING_DATE_19110102)), 46 | new User() 47 | .setId(USER_ID_140) 48 | .setName(NAME_ARAGORN) 49 | .setPhone(PHONE_661534411) 50 | .setType(TYPE_1) 51 | .setBirthDate(LocalDate.parse(STRING_DATE_19230716)), 52 | new User() 53 | .setId(USER_ID_453321) 54 | .setName(NAME_FRODO) 55 | .setPhone(PHONE_666222211) 56 | .setType(TYPE_2) 57 | .setBirthDate(LocalDate.parse(STRING_DATE_19511124)) 58 | ); 59 | } 60 | 61 | @Override 62 | public User save(User user) { 63 | return user.setId(USER_ID_453321); 64 | } 65 | 66 | @Override 67 | public User update(User user) { 68 | return user; 69 | } 70 | 71 | @Override 72 | public void delete(Long id) { 73 | // delete user by id 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /jersey-rest-example/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /jersey-rest-example/src/test/java/com/edwise/pocs/jerseyrest/entity/UserTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jerseyrest.entity; 2 | 3 | import org.joda.time.LocalDate; 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class UserTest { 9 | private static final long USER_ID_12 = 12L; 10 | private static final String NAME_GANDALF = "Gandalf"; 11 | private static final int TYPE_1 = 1; 12 | private static final String PHONE_661534411 = "661534411"; 13 | private static final String STRING_DATE_19110102 = "1911-01-02"; 14 | 15 | @Test 16 | public void copyFrom() { 17 | User user = createUser(USER_ID_12, NAME_GANDALF, TYPE_1, PHONE_661534411, STRING_DATE_19110102); 18 | User userTo = createUser(USER_ID_12, null, null, null, null); 19 | 20 | userTo.copyFrom(user); 21 | 22 | assertEquals(userTo, user); 23 | } 24 | 25 | private User createUser(Long id, String name, Integer type, String phone, String stringDate) { 26 | User user = new User(); 27 | user.setId(id); 28 | user.setName(name); 29 | user.setType(type); 30 | user.setPhone(phone); 31 | if (stringDate != null) { 32 | user.setBirthDate(LocalDate.parse(stringDate)); 33 | } 34 | 35 | return user; 36 | } 37 | } -------------------------------------------------------------------------------- /jersey-rest-example/src/test/java/com/edwise/pocs/jerseyrest/service/impl/UserServiceMockTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jerseyrest.service.impl; 2 | 3 | import com.edwise.pocs.jerseyrest.entity.User; 4 | import com.edwise.pocs.jerseyrest.service.UserService; 5 | import org.joda.time.LocalDate; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import java.util.List; 10 | 11 | import static org.hamcrest.Matchers.hasSize; 12 | import static org.hamcrest.Matchers.is; 13 | import static org.junit.Assert.*; 14 | 15 | public class UserServiceMockTest { 16 | private static final long USER_ID_12 = 12l; 17 | private static final String NAME_GANDALF = "Gandalf"; 18 | private static final int TYPE_1 = 1; 19 | private static final String PHONE_661534411 = "661534411"; 20 | private static final String STRING_DATE_19110102 = "1911-01-02"; 21 | 22 | private UserService userService; 23 | 24 | @Before 25 | public void setUp() { 26 | userService = new UserServiceMock(); 27 | } 28 | 29 | @Test 30 | public void findById() { 31 | User user = userService.findById(USER_ID_12); 32 | 33 | assertNotNull(user); 34 | assertThat(user.getId(), is(USER_ID_12)); 35 | } 36 | 37 | @Test 38 | public void findAll() { 39 | List users = userService.findAll(); 40 | 41 | assertNotNull(users); 42 | assertThat(users, hasSize(3)); 43 | } 44 | 45 | @Test 46 | public void save() { 47 | User user = userService.save( 48 | createUser(null, NAME_GANDALF, TYPE_1, PHONE_661534411, STRING_DATE_19110102) 49 | ); 50 | 51 | assertNotNull(user); 52 | assertNotNull(user.getId()); 53 | } 54 | 55 | @Test 56 | public void update() { 57 | User user = userService.update( 58 | createUser(USER_ID_12, NAME_GANDALF, TYPE_1, PHONE_661534411, STRING_DATE_19110102) 59 | ); 60 | 61 | assertNotNull(user); 62 | assertThat(user.getId(), is(USER_ID_12)); 63 | } 64 | 65 | @Test 66 | public void delete() { 67 | userService.delete(USER_ID_12); 68 | } 69 | 70 | private User createUser(Long id, String name, int type, String phone, String stringDate) { 71 | User user = new User(); 72 | user.setId(id); 73 | user.setName(name); 74 | user.setType(type); 75 | user.setPhone(phone); 76 | user.setBirthDate(LocalDate.parse(stringDate)); 77 | 78 | return user; 79 | } 80 | } -------------------------------------------------------------------------------- /jooq-example/README.md: -------------------------------------------------------------------------------- 1 | POC de un proyecto maven simple configurado con jOOQ, creado para el post: https://anotherdayanotherbug.wordpress.com/2015/01/20/como-configurar-jooq-en-un-proyecto-maven/ 2 | -------------------------------------------------------------------------------- /jooq-example/src/main/java/com/edwise/pocs/jooq/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jooq; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | public class Application { 7 | private static final Logger log = LoggerFactory.getLogger(Application.class); 8 | 9 | public static void main(String[] args) { 10 | log.info("POC de jOOQ. Ejecuta los tests para probarla :)"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jooq-example/src/main/java/com/edwise/pocs/jooq/jdbc/DBConnector.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jooq.jdbc; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import java.sql.Connection; 7 | import java.sql.DriverManager; 8 | import java.sql.SQLException; 9 | 10 | public final class DBConnector { 11 | private static final Logger log = LoggerFactory.getLogger(DBConnector.class); 12 | 13 | private static final String URL = "jdbc:h2:mem:pocjooq;" + "INIT=RUNSCRIPT FROM 'classpath:populate_db.sql'"; 14 | private static final String USERNAME = "sa"; 15 | private static final String PASSWORD = ""; 16 | 17 | private static final DBConnector INSTANCE = new DBConnector(); 18 | private static Connection connection = null; 19 | 20 | private DBConnector() { 21 | } 22 | 23 | public static DBConnector getInstance() { 24 | return INSTANCE; 25 | } 26 | 27 | public Connection connection() { 28 | try { 29 | if (connection == null || connection.isClosed()) { 30 | connection = doConnection(); 31 | } 32 | } catch (SQLException e) { 33 | log.error("Error al comprobar si está cerrada la conexión: {}", e); 34 | throw new RuntimeException(e); 35 | } 36 | 37 | return connection; 38 | } 39 | 40 | public void closeConnection() { 41 | try { 42 | connection.close(); 43 | log.debug("Conexión cerrada."); 44 | } catch (SQLException e) { 45 | log.error("Error al cerrar la conexión: {}", e); 46 | } 47 | } 48 | 49 | private Connection doConnection() { 50 | Connection conn; 51 | try { 52 | conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); 53 | } catch (SQLException e) { 54 | log.error("Error al crear la conexión: {}", e); 55 | throw new RuntimeException(e); 56 | } 57 | log.debug("Conexion creada: {}", conn); 58 | 59 | return conn; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /jooq-example/src/main/resources/populate_db.sql: -------------------------------------------------------------------------------- 1 | create schema if not exists pocjooq; 2 | 3 | use schema pocjooq; 4 | 5 | create table if not exists book_character (id integer primary key, name varchar(30)); 6 | 7 | delete from book_character; 8 | insert into book_character values (1, 'Saruman'); 9 | insert into book_character values (2, 'Gandalf'); 10 | insert into book_character values (3, 'Aragorn'); 11 | insert into book_character values (4, 'Samwise'); 12 | insert into book_character values (5, 'Frodo'); 13 | 14 | commit; -------------------------------------------------------------------------------- /jooq-example/src/test/java/com/edwise/pocs/jooq/JOOQExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.jooq; 2 | 3 | import com.edwise.pocs.jooq.jdbc.DBConnector; 4 | import org.jooq.DSLContext; 5 | import org.jooq.Record; 6 | import org.jooq.Result; 7 | import org.jooq.SQLDialect; 8 | import org.jooq.impl.DSL; 9 | import org.jooq.util.maven.generated.Tables; 10 | import org.junit.After; 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.slf4j.Logger; 14 | import org.slf4j.LoggerFactory; 15 | 16 | public class JOOQExampleTest { 17 | private static final Logger log = LoggerFactory.getLogger(JOOQExampleTest.class); 18 | 19 | private DSLContext jooqDSL; 20 | 21 | @Before 22 | public void setUp() { 23 | jooqDSL = DSL.using(DBConnector.getInstance().connection(), SQLDialect.H2); 24 | } 25 | 26 | @After 27 | public void tearDown() { 28 | DBConnector.getInstance().closeConnection(); 29 | } 30 | 31 | @Test 32 | public void testSelectAll() { 33 | Result result = jooqDSL 34 | .select() 35 | .from(Tables.BOOK_CHARACTER) 36 | .fetch(); 37 | 38 | log.info("Resultado query: \n{}", result.toString()); 39 | } 40 | 41 | @Test 42 | public void testStreamForEachJava8() { 43 | jooqDSL.selectFrom(Tables.BOOK_CHARACTER) 44 | .where(Tables.BOOK_CHARACTER.ID.in(2, 3)) 45 | .orderBy(Tables.BOOK_CHARACTER.NAME) 46 | .fetch() 47 | .stream() 48 | .map(bookCharacter -> bookCharacter.getName().toUpperCase()) 49 | .forEach(s -> log.info("Name: {}", s)); 50 | } 51 | } -------------------------------------------------------------------------------- /lombok-example/README.md: -------------------------------------------------------------------------------- 1 | POC con lombok, creado para el post: http://anotherdayanotherbug.wordpress.com/2014/10/27/lombok-o-como-eliminar-codigo-boilerplate/ -------------------------------------------------------------------------------- /lombok-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.edwise.pocs.lombok 6 | lombok-example 7 | 1.0 8 | jar 9 | 10 | lombok-example 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 1.8 16 | 17 | 1.14.8 18 | 2.1 19 | 20 | 21 | 22 | 23 | org.projectlombok 24 | lombok 25 | ${lombok.version} 26 | provided 27 | 28 | 29 | 30 | org.apache.logging.log4j 31 | log4j-api 32 | ${log4j.version} 33 | 34 | 35 | org.apache.logging.log4j 36 | log4j-core 37 | ${log4j.version} 38 | 39 | 40 | 41 | junit 42 | junit 43 | 4.11 44 | test 45 | 46 | 47 | hamcrest-core 48 | org.hamcrest 49 | 50 | 51 | 52 | 53 | 54 | org.hamcrest 55 | hamcrest-all 56 | 1.3 57 | test 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /lombok-example/src/main/java/com/edwise/pocs/lombok/withlombok/Book.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.lombok.withlombok; 2 | 3 | import lombok.Data; 4 | import lombok.experimental.Accessors; 5 | import lombok.experimental.Builder; 6 | 7 | @Data 8 | @Accessors(chain = true) 9 | @Builder 10 | public class Book { 11 | 12 | private Long id; 13 | private String title; 14 | private String isbn; 15 | private Short type; 16 | } 17 | -------------------------------------------------------------------------------- /lombok-example/src/main/java/com/edwise/pocs/lombok/withlombok/FooService.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.lombok.withlombok; 2 | 3 | import lombok.extern.log4j.Log4j2; 4 | 5 | @Log4j2 6 | public class FooService { 7 | 8 | public String getFooById(Long id) { 9 | String myFoo = null; 10 | if (id != null) { 11 | myFoo = "myFoo!"; 12 | } else { 13 | log.error("id cannot be null!"); 14 | } 15 | 16 | return myFoo; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /lombok-example/src/main/java/com/edwise/pocs/lombok/withlombok/User.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.lombok.withlombok; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.NonNull; 7 | import lombok.experimental.Accessors; 8 | 9 | import java.time.LocalDate; 10 | import java.util.List; 11 | 12 | @Data 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | @Accessors(chain = true) 16 | public class User { 17 | 18 | private long id; 19 | 20 | @NonNull 21 | private String name; 22 | 23 | @NonNull 24 | private String surname; 25 | 26 | private int type; 27 | private LocalDate birthDate; 28 | private String phoneNumber; 29 | private List favoriteBooks; 30 | } 31 | -------------------------------------------------------------------------------- /lombok-example/src/main/java/com/edwise/pocs/lombok/withoutlombok/Book.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.lombok.withoutlombok; 2 | 3 | public class Book { 4 | 5 | private Long id; 6 | private String title; 7 | private String isbn; 8 | private Short type; 9 | 10 | public Book(Long id, String title, String isbn, Short type) { 11 | this.id = id; 12 | this.title = title; 13 | this.isbn = isbn; 14 | this.type = type; 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public Book setId(Long id) { 22 | this.id = id; 23 | return this; 24 | } 25 | 26 | public String getTitle() { 27 | return title; 28 | } 29 | 30 | public Book setTitle(String title) { 31 | this.title = title; 32 | return this; 33 | } 34 | 35 | public String getIsbn() { 36 | return isbn; 37 | } 38 | 39 | public Book setIsbn(String isbn) { 40 | this.isbn = isbn; 41 | return this; 42 | } 43 | 44 | public Short getType() { 45 | return type; 46 | } 47 | 48 | public Book setType(Short type) { 49 | this.type = type; 50 | return this; 51 | } 52 | 53 | public static BookBuilder builder() { 54 | return new BookBuilder(); 55 | } 56 | 57 | public static class BookBuilder { 58 | private Long id; 59 | private String title; 60 | private String isbn; 61 | private Short type; 62 | 63 | private BookBuilder() {} 64 | 65 | public BookBuilder id(Long id) { 66 | this.id = id; 67 | return this; 68 | } 69 | 70 | public BookBuilder title(String title) { 71 | this.title = title; 72 | return this; 73 | } 74 | 75 | public BookBuilder isbn(String isbn) { 76 | this.isbn = isbn; 77 | return this; 78 | } 79 | 80 | public BookBuilder type(Short type) { 81 | this.type = type; 82 | return this; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "BookBuilder(id = " + id 88 | + ", title = " + title 89 | + ", isbn = " + isbn 90 | + ", type = " + type + ")"; 91 | } 92 | 93 | public Book build() { 94 | return new Book(id, title, isbn, type); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /lombok-example/src/main/java/com/edwise/pocs/lombok/withoutlombok/FooService.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.lombok.withoutlombok; 2 | 3 | import org.apache.logging.log4j.LogManager; 4 | import org.apache.logging.log4j.Logger; 5 | 6 | public class FooService { 7 | private static final Logger log = 8 | LogManager.getLogger(FooService.class); 9 | 10 | public String getFooById(Long id) { 11 | String myFoo = null; 12 | if (id != null) { 13 | myFoo = "myFoo!"; 14 | } else { 15 | log.error("id cannot be null!"); 16 | } 17 | 18 | return myFoo; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /lombok-example/src/test/java/com/edwise/pocs/lombok/withlombok/BookTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.lombok.withlombok; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.hamcrest.Matchers.is; 6 | import static org.junit.Assert.assertThat; 7 | 8 | public class BookTest { 9 | 10 | @Test 11 | public void testBuilderBook() { 12 | Book book = Book.builder() 13 | .id(12L) 14 | .title("The Hobbit") 15 | .type((short) 2) 16 | .isbn("111-232323-4544") 17 | .build(); 18 | 19 | assertThat(book.getId(), is(12L)); 20 | assertThat(book.getTitle(), is("The Hobbit")); 21 | assertThat(book.getType(), is((short) 2)); 22 | assertThat(book.getIsbn(), is("111-232323-4544")); 23 | } 24 | } -------------------------------------------------------------------------------- /lombok-example/src/test/java/com/edwise/pocs/lombok/withlombok/FooServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.lombok.withlombok; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertNull; 6 | 7 | public class FooServiceTest { 8 | 9 | @Test 10 | public void testGetFooByIdWithNullParameter() { 11 | FooService fooService = new FooService(); 12 | 13 | // Logs ERROR in the console! 14 | String foo = fooService.getFooById(null); 15 | 16 | assertNull(foo); 17 | } 18 | } -------------------------------------------------------------------------------- /lombok-example/src/test/java/com/edwise/pocs/lombok/withoutlombok/BookTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.lombok.withoutlombok; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.hamcrest.Matchers.is; 6 | import static org.junit.Assert.*; 7 | 8 | public class BookTest { 9 | 10 | @Test 11 | public void testBuilderBook() { 12 | Book book = Book.builder() 13 | .id(12L) 14 | .title("The Hobbit") 15 | .type((short) 2) 16 | .isbn("111-232323-4544") 17 | .build(); 18 | 19 | assertThat(book.getId(), is(12L)); 20 | assertThat(book.getTitle(), is("The Hobbit")); 21 | assertThat(book.getType(), is((short) 2)); 22 | assertThat(book.getIsbn(), is("111-232323-4544")); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /lombok-example/src/test/java/com/edwise/pocs/lombok/withoutlombok/FooServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.lombok.withoutlombok; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | public class FooServiceTest { 8 | 9 | @Test 10 | public void testGetFooById() { 11 | FooService fooService = new FooService(); 12 | 13 | // Logs ERROR in the console! 14 | String foo = fooService.getFooById(null); 15 | 16 | assertNull(foo); 17 | } 18 | } -------------------------------------------------------------------------------- /orika-example/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de mapeo de beans con Orika, creado para el post: https://anotherdayanotherbug.wordpress.com/2015/06/17/como-mapear-beans-con-orika/ 2 | -------------------------------------------------------------------------------- /orika-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.pocs.orikapoc 7 | orika-example 8 | 1.0 9 | jar 10 | 11 | orika-example 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | 21 | ma.glasnost.orika 22 | orika-core 23 | 1.4.6 24 | 25 | 26 | 27 | junit 28 | junit 29 | 4.12 30 | test 31 | 32 | 33 | hamcrest-core 34 | org.hamcrest 35 | 36 | 37 | 38 | 39 | 40 | org.hamcrest 41 | hamcrest-all 42 | 1.3 43 | test 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-compiler-plugin 53 | 3.3 54 | 55 | ${java.version} 56 | ${java.version} 57 | ${java.version} 58 | ${java.version} 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /orika-example/src/main/java/com/edwise/pocs/orikapoc/OrikaExample.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.orikapoc; 2 | 3 | import com.edwise.pocs.orikapoc.config.OrikaConfig; 4 | import com.edwise.pocs.orikapoc.dto.DestinationDTO; 5 | import com.edwise.pocs.orikapoc.entity.EntityType; 6 | import com.edwise.pocs.orikapoc.entity.SourceEntity; 7 | import ma.glasnost.orika.MapperFacade; 8 | 9 | import java.time.LocalDateTime; 10 | 11 | public class OrikaExample { 12 | 13 | public static void main(String[] args) { 14 | MapperFacade mapper = OrikaConfig.getMapperFacade(); 15 | 16 | SourceEntity sourceEntity = new SourceEntity(); 17 | sourceEntity 18 | .setId(12345L) 19 | .setName("entityName") 20 | .setUserSurname("surnameEntity") 21 | .setEntityType(new EntityType().setId(9999).setType("type9")) 22 | .setOptions(new String[]{"option1", "option2", "option3"}) 23 | .setCreationDateTime(LocalDateTime.of(2015, 6, 16, 9, 10, 30)) 24 | .setNums(new Integer[]{14, 25, 67}); 25 | 26 | DestinationDTO destDTO = mapper.map(sourceEntity, DestinationDTO.class); 27 | 28 | System.out.println("Source: " + sourceEntity); 29 | System.out.println("Destination: " + destDTO); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /orika-example/src/main/java/com/edwise/pocs/orikapoc/config/LocalDateTimeToLocalDateConverter.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.orikapoc.config; 2 | 3 | import ma.glasnost.orika.CustomConverter; 4 | import ma.glasnost.orika.metadata.Type; 5 | 6 | import java.time.LocalDate; 7 | import java.time.LocalDateTime; 8 | 9 | public class LocalDateTimeToLocalDateConverter 10 | extends CustomConverter { 11 | public LocalDate convert(LocalDateTime source, 12 | Type destinationType) { 13 | return source.toLocalDate(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /orika-example/src/main/java/com/edwise/pocs/orikapoc/config/OrikaConfig.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.orikapoc.config; 2 | 3 | import com.edwise.pocs.orikapoc.dto.DestinationDTO; 4 | import com.edwise.pocs.orikapoc.entity.SourceEntity; 5 | import ma.glasnost.orika.MapperFacade; 6 | import ma.glasnost.orika.MapperFactory; 7 | import ma.glasnost.orika.converter.ConverterFactory; 8 | import ma.glasnost.orika.impl.DefaultMapperFactory; 9 | 10 | public final class OrikaConfig { 11 | 12 | private static final MapperFactory mapperFactory; 13 | 14 | static { 15 | mapperFactory = new DefaultMapperFactory.Builder().build(); 16 | 17 | ConverterFactory converterFactory = mapperFactory.getConverterFactory(); 18 | converterFactory.registerConverter(new LocalDateTimeToLocalDateConverter()); 19 | 20 | mapperFactory.classMap(SourceEntity.class, DestinationDTO.class) 21 | .exclude("id") 22 | .field("userSurname", "surname") 23 | .field("entityType.id", "type") 24 | .field("creationDateTime", "creationDate") 25 | .field("nums[0]", "firstNum") 26 | .byDefault() 27 | .register(); 28 | } 29 | 30 | private OrikaConfig() { 31 | } 32 | 33 | public static MapperFacade getMapperFacade() { 34 | return mapperFactory.getMapperFacade(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /orika-example/src/main/java/com/edwise/pocs/orikapoc/dto/DestinationDTO.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.orikapoc.dto; 2 | 3 | import java.time.LocalDate; 4 | import java.util.List; 5 | 6 | public class DestinationDTO { 7 | 8 | private Long id; 9 | private String name; 10 | private String surname; 11 | private int type; 12 | private List options; 13 | private LocalDate creationDate; 14 | private Integer firstNum; 15 | 16 | public Long getId() { 17 | return id; 18 | } 19 | 20 | public DestinationDTO setId(Long id) { 21 | this.id = id; 22 | return this; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public DestinationDTO setName(String name) { 30 | this.name = name; 31 | return this; 32 | } 33 | 34 | public String getSurname() { 35 | return surname; 36 | } 37 | 38 | public DestinationDTO setSurname(String surname) { 39 | this.surname = surname; 40 | return this; 41 | } 42 | 43 | public int getType() { 44 | return type; 45 | } 46 | 47 | public DestinationDTO setType(int type) { 48 | this.type = type; 49 | return this; 50 | } 51 | 52 | public List getOptions() { 53 | return options; 54 | } 55 | 56 | public DestinationDTO setOptions(List options) { 57 | this.options = options; 58 | return this; 59 | } 60 | 61 | public LocalDate getCreationDate() { 62 | return creationDate; 63 | } 64 | 65 | public DestinationDTO setCreationDate(LocalDate creationDate) { 66 | this.creationDate = creationDate; 67 | return this; 68 | } 69 | 70 | public Integer getFirstNum() { 71 | return firstNum; 72 | } 73 | 74 | public DestinationDTO setFirstNum(Integer firstNum) { 75 | this.firstNum = firstNum; 76 | return this; 77 | } 78 | 79 | @Override 80 | public String toString() { 81 | return "DestinationDTO{" + 82 | "id=" + id + 83 | ", name='" + name + '\'' + 84 | ", surname='" + surname + '\'' + 85 | ", type=" + type + 86 | ", options=" + options + 87 | ", creationDate=" + creationDate + 88 | ", firstNum=" + firstNum + 89 | '}'; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /orika-example/src/main/java/com/edwise/pocs/orikapoc/entity/EntityType.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.orikapoc.entity; 2 | 3 | public class EntityType { 4 | 5 | private int id; 6 | private String type; 7 | 8 | public int getId() { 9 | return id; 10 | } 11 | 12 | public EntityType setId(int id) { 13 | this.id = id; 14 | return this; 15 | } 16 | 17 | public String getType() { 18 | return type; 19 | } 20 | 21 | public EntityType setType(String type) { 22 | this.type = type; 23 | return this; 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return "EntityType{" + 29 | "id=" + id + 30 | ", type='" + type + '\'' + 31 | '}'; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /orika-example/src/main/java/com/edwise/pocs/orikapoc/entity/SourceEntity.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.orikapoc.entity; 2 | 3 | import java.time.LocalDateTime; 4 | import java.util.Arrays; 5 | 6 | public class SourceEntity { 7 | 8 | private Long id; 9 | private String name; 10 | private String userSurname; 11 | private String[] options; 12 | private EntityType entityType; 13 | private LocalDateTime creationDateTime; 14 | private Integer[] nums; 15 | 16 | public Long getId() { 17 | return id; 18 | } 19 | 20 | public SourceEntity setId(Long id) { 21 | this.id = id; 22 | return this; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public SourceEntity setName(String name) { 30 | this.name = name; 31 | return this; 32 | } 33 | 34 | public String getUserSurname() { 35 | return userSurname; 36 | } 37 | 38 | public SourceEntity setUserSurname(String userSurname) { 39 | this.userSurname = userSurname; 40 | return this; 41 | } 42 | 43 | public String[] getOptions() { 44 | return options; 45 | } 46 | 47 | public SourceEntity setOptions(String[] options) { 48 | this.options = options; 49 | return this; 50 | } 51 | 52 | public EntityType getEntityType() { 53 | return entityType; 54 | } 55 | 56 | public SourceEntity setEntityType(EntityType entityType) { 57 | this.entityType = entityType; 58 | return this; 59 | } 60 | 61 | public LocalDateTime getCreationDateTime() { 62 | return creationDateTime; 63 | } 64 | 65 | public SourceEntity setCreationDateTime(LocalDateTime creationDateTime) { 66 | this.creationDateTime = creationDateTime; 67 | return this; 68 | } 69 | 70 | public Integer[] getNums() { 71 | return nums; 72 | } 73 | 74 | public SourceEntity setNums(Integer[] nums) { 75 | this.nums = nums; 76 | return this; 77 | } 78 | 79 | @Override 80 | public String toString() { 81 | return "SourceEntity{" + 82 | "id=" + id + 83 | ", name='" + name + '\'' + 84 | ", userSurname='" + userSurname + '\'' + 85 | ", options=" + Arrays.toString(options) + 86 | ", entityType=" + entityType + 87 | ", creationDateTime=" + creationDateTime + 88 | ", nums=" + Arrays.toString(nums) + 89 | '}'; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /passbyvalue-example/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo del paso de parametros en java, creado para el post: https://anotherdayanotherbug.wordpress.com/2015/03/25/java-el-paso-de-parametros-es-por-valor/ 2 | -------------------------------------------------------------------------------- /passbyvalue-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.example.passbyvalue 7 | passbyvalue-example 8 | 1.0 9 | jar 10 | 11 | passbyvalue-example 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.12 24 | test 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.apache.maven.plugins 32 | maven-compiler-plugin 33 | 3.2 34 | 35 | ${java.version} 36 | ${java.version} 37 | ${java.version} 38 | ${java.version} 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /passbyvalue-example/src/main/java/com/edwise/example/passbyvalue/PassByValueExample.java: -------------------------------------------------------------------------------- 1 | package com.edwise.example.passbyvalue; 2 | 3 | public class PassByValueExample { 4 | 5 | public void primitiveExample() { 6 | int years = 10; 7 | System.out.println("Years before method: " + years); 8 | methodWithPrimitive(years); 9 | System.out.println("Years after method: " + years); 10 | } 11 | 12 | public void objectExample() { 13 | User user = new User("edwise"); 14 | System.out.println("User before method: " + user); 15 | methodWithObjectSet(user); 16 | System.out.println("User after method: " + user); 17 | } 18 | 19 | public void otherObjectExample() { 20 | User user = new User("edwise"); 21 | System.out.println("User before method: " + user); 22 | methodWithObjectNew(user); 23 | System.out.println("User after method: " + user); 24 | } 25 | 26 | private void methodWithPrimitive(int arg) { 27 | arg = arg + 3; 28 | System.out.println("Parameter modified: " + arg); 29 | } 30 | 31 | private void methodWithObjectSet(User arg) { 32 | arg.setName("newUser"); 33 | System.out.println("Parameter modified: " + arg); 34 | } 35 | 36 | private void methodWithObjectNew(User arg) { 37 | arg = new User("newUser"); 38 | System.out.println("Parameter modified: " + arg); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /passbyvalue-example/src/main/java/com/edwise/example/passbyvalue/User.java: -------------------------------------------------------------------------------- 1 | package com.edwise.example.passbyvalue; 2 | 3 | public class User { 4 | private String name; 5 | 6 | public User(String name) { 7 | this.name = name; 8 | } 9 | 10 | public String getName() { 11 | return name; 12 | } 13 | 14 | public void setName(String name) { 15 | this.name = name; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "User{" + 21 | "name='" + name + '\'' + 22 | '}'; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /passbyvalue-example/src/test/java/com/edwise/example/passbyvalue/PassByValueExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.example.passbyvalue; 2 | 3 | import org.junit.Test; 4 | 5 | public class PassByValueExampleTest { 6 | 7 | private PassByValueExample passByValueExample = new PassByValueExample(); 8 | 9 | @Test 10 | public void testPrimitiveExample() { 11 | passByValueExample.primitiveExample(); 12 | } 13 | 14 | @Test 15 | public void testObjectExample() { 16 | passByValueExample.objectExample(); 17 | } 18 | 19 | @Test 20 | public void testOtherObjectExample() { 21 | passByValueExample.otherObjectExample(); 22 | } 23 | } -------------------------------------------------------------------------------- /spring-data-example/README.md: -------------------------------------------------------------------------------- 1 | POC con Spring Data JPA, creado para el post sobre Spring Data: http://anotherdayanotherbug.wordpress.com/2014/12/05/implementar-la-capa-de-base-de-datos-con-spring-data/ 2 | -------------------------------------------------------------------------------- /spring-data-example/src/main/java/com/edwise/pocs/springdata/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springdata; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.fasterxml.jackson.datatype.jsr310.JSR310Module; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.ComponentScan; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | @Configuration 12 | @EnableAutoConfiguration 13 | @ComponentScan 14 | public class Application { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(Application.class, args); 18 | } 19 | 20 | @Bean 21 | public ObjectMapper configureJacksonObjectMapperWithJSR310Module() { 22 | ObjectMapper objectMapper = new ObjectMapper(); 23 | objectMapper.registerModule(new JSR310Module()); 24 | objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature. 25 | WRITE_DATES_AS_TIMESTAMPS , false); 26 | 27 | return objectMapper; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /spring-data-example/src/main/java/com/edwise/pocs/springdata/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springdata.repository; 2 | 3 | import com.edwise.pocs.springdata.entity.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.data.jpa.repository.Query; 6 | import org.springframework.data.repository.query.Param; 7 | 8 | import java.util.List; 9 | 10 | public interface UserRepository extends JpaRepository { 11 | 12 | public List findByType(Integer type); 13 | 14 | public List findByNameIgnoreCase(String name); 15 | 16 | @Query("SELECT u FROM User u WHERE u.phone LIKE CONCAT(:phonePrefix, '%')") 17 | public List findByPhonePrefix(@Param("phonePrefix") String phonePrefix); 18 | } 19 | -------------------------------------------------------------------------------- /spring-data-example/src/main/java/com/edwise/pocs/springdata/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springdata.service; 2 | 3 | import com.edwise.pocs.springdata.entity.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserService { 8 | 9 | User findById(Long id); 10 | 11 | List findAll(); 12 | 13 | User save(User user); 14 | 15 | void delete(Long id); 16 | 17 | boolean existsUser(Long id); 18 | } 19 | -------------------------------------------------------------------------------- /spring-data-example/src/main/java/com/edwise/pocs/springdata/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springdata.service.impl; 2 | 3 | import com.edwise.pocs.springdata.entity.User; 4 | import com.edwise.pocs.springdata.repository.UserRepository; 5 | import com.edwise.pocs.springdata.service.UserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | 11 | @Service 12 | public class UserServiceImpl implements UserService { 13 | 14 | @Autowired 15 | private UserRepository userRepository; 16 | 17 | @Override 18 | public User findById(Long id) { 19 | return userRepository.findOne(id); 20 | } 21 | 22 | @Override 23 | public List findAll() { 24 | return userRepository.findAll(); 25 | } 26 | 27 | @Override 28 | public User save(User user) { 29 | return userRepository.save(user); 30 | } 31 | 32 | @Override 33 | public void delete(Long id) { 34 | userRepository.delete(id); 35 | } 36 | 37 | @Override 38 | public boolean existsUser(Long id) { 39 | return userRepository.exists(id); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /spring-data-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #spring.datasource.url=jdbc:mysql://localhost/springdatapoc 2 | #spring.datasource.username=root 3 | #spring.datasource.password=root 4 | #spring.datasource.driverClassName=com.mysql.jdbc.Driver 5 | 6 | #spring.datasource.url = jdbc:h2:mem:test 7 | #spring.datasource.username = sa 8 | #spring.datasource.password = 9 | #spring.datasource.driver = org.h2.Driver 10 | -------------------------------------------------------------------------------- /spring-data-example/src/test/java/com/edwise/pocs/springdata/entity/UserTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springdata.entity; 2 | 3 | import org.junit.Test; 4 | 5 | import java.time.LocalDate; 6 | 7 | import static org.junit.Assert.assertEquals; 8 | 9 | public class UserTest { 10 | private static final long USER_ID_12 = 12L; 11 | private static final String NAME_GANDALF = "Gandalf"; 12 | private static final int TYPE_1 = 1; 13 | private static final String PHONE_661534411 = "661534411"; 14 | private static final String STRING_DATE_19110102 = "1911-01-02"; 15 | 16 | @Test 17 | public void copyFrom() { 18 | User user = createUser(USER_ID_12, NAME_GANDALF, TYPE_1, PHONE_661534411, STRING_DATE_19110102); 19 | User userTo = createUser(USER_ID_12, null, null, null, null); 20 | 21 | userTo.copyFrom(user); 22 | 23 | assertEquals(userTo, user); 24 | } 25 | 26 | private User createUser(Long id, String name, Integer type, String phone, String stringDate) { 27 | User user = new User(); 28 | user.setId(id); 29 | user.setName(name); 30 | user.setType(type); 31 | user.setPhone(phone); 32 | if (stringDate != null) { 33 | user.setBirthDate(LocalDate.parse(stringDate)); 34 | } 35 | 36 | return user; 37 | } 38 | } -------------------------------------------------------------------------------- /spring-rest-client-example/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de cliente REST con Spring, RestTemplate, creado para el post: https://anotherdayanotherbug.wordpress.com/2015/05/05/implementar-un-cliente-rest-con-spring-resttemplate/ -------------------------------------------------------------------------------- /spring-rest-client-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.pocs.springrestclient 7 | spring-rest-client-example 8 | 1.0 9 | jar 10 | 11 | spring-rest-client-example 12 | http://github.com/edwise/java-pocs 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | 21 | org.springframework 22 | spring-web 23 | 4.1.6.RELEASE 24 | 25 | 26 | com.fasterxml.jackson.core 27 | jackson-databind 28 | 2.5.3 29 | 30 | 31 | com.fasterxml.jackson.datatype 32 | jackson-datatype-jsr310 33 | 2.5.3 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-compiler-plugin 42 | 3.2 43 | 44 | ${java.version} 45 | ${java.version} 46 | ${java.version} 47 | ${java.version} 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /spring-rest-client-example/src/main/java/com/edwise/pocs/springrestclient/model/Book.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springrestclient.model; 2 | 3 | public class Book { 4 | private Long id; 5 | private String title; 6 | 7 | public Long getId() { 8 | return id; 9 | } 10 | 11 | public void setId(Long id) { 12 | this.id = id; 13 | } 14 | 15 | public String getTitle() { 16 | return title; 17 | } 18 | 19 | public void setTitle(String title) { 20 | this.title = title; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return "Book{" + 26 | "id=" + id + 27 | ", title='" + title + '\'' + 28 | '}'; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /spring-rest-client-example/src/main/java/com/edwise/pocs/springrestclient/model/Info.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springrestclient.model; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | public class Info { 6 | 7 | private Long id; 8 | private String infoText; 9 | private LocalDateTime creationDateTime; 10 | 11 | public Long getId() { 12 | return id; 13 | } 14 | 15 | public void setId(Long id) { 16 | this.id = id; 17 | } 18 | 19 | public String getInfoText() { 20 | return infoText; 21 | } 22 | 23 | public void setInfoText(String infoText) { 24 | this.infoText = infoText; 25 | } 26 | 27 | public LocalDateTime getCreationDateTime() { 28 | return creationDateTime; 29 | } 30 | 31 | public void setCreationDateTime(LocalDateTime creationDateTime) { 32 | this.creationDateTime = creationDateTime; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "Info{" + 38 | "id=" + id + 39 | ", infoText='" + infoText + '\'' + 40 | ", creationDateTime=" + creationDateTime + 41 | '}'; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /spring-rest-example/README.md: -------------------------------------------------------------------------------- 1 | POC de un servicio REST con Spring, creado para el post http://anotherdayanotherbug.wordpress.com/2014/09/08/implementar-un-servicio-rest-con-spring 2 | 3 | Actualizado para el post: http://anotherdayanotherbug.wordpress.com/2015/04/08/como-devolver-el-resultado-de-un-post-en-un-servicio-rest 4 | 5 | -------------------------------------------------------------------------------- /spring-rest-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.edwise.pocs.springrest 6 | spring-rest-example 7 | 1.1 8 | jar 9 | 10 | spring-rest-example 11 | http://maven.apache.org 12 | 13 | 14 | org.springframework.boot 15 | spring-boot-starter-parent 16 | 1.2.3.RELEASE 17 | 18 | 19 | 20 | UTF-8 21 | 1.8 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | 37 | joda-time 38 | joda-time 39 | 2.7 40 | 41 | 42 | 43 | com.fasterxml.jackson.datatype 44 | jackson-datatype-joda 45 | 2.4.5 46 | 47 | 48 | 49 | junit 50 | junit 51 | 4.12 52 | test 53 | 54 | 55 | hamcrest-core 56 | org.hamcrest 57 | 58 | 59 | 60 | 61 | 62 | org.hamcrest 63 | hamcrest-all 64 | 1.3 65 | test 66 | 67 | 68 | 69 | org.mockito 70 | mockito-core 71 | 1.10.19 72 | test 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | org.springframework.boot 81 | spring-boot-maven-plugin 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /spring-rest-example/src/main/java/com/edwise/pocs/springrest/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springrest; 2 | 3 | import com.fasterxml.jackson.databind.Module; 4 | import com.fasterxml.jackson.datatype.joda.JodaModule; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.ComponentScan; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | @Configuration 12 | @EnableAutoConfiguration 13 | @ComponentScan 14 | public class Application { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(Application.class, args); 18 | } 19 | 20 | @Bean 21 | public Module jodaModule() { 22 | return new JodaModule(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-rest-example/src/main/java/com/edwise/pocs/springrest/services/UserService.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springrest.services; 2 | 3 | import com.edwise.pocs.springrest.entities.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserService { 8 | 9 | User findById(Long id); 10 | 11 | List findAll(); 12 | 13 | User save(User user); 14 | 15 | User update(User user); 16 | 17 | void delete(Long id); 18 | } 19 | -------------------------------------------------------------------------------- /spring-rest-example/src/test/java/com/edwise/pocs/springrest/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springrest; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.SpringApplicationConfiguration; 6 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 7 | import org.springframework.test.context.web.WebAppConfiguration; 8 | 9 | @RunWith(SpringJUnit4ClassRunner.class) 10 | @SpringApplicationConfiguration(classes = Application.class) 11 | @WebAppConfiguration 12 | public class ApplicationTest { 13 | 14 | @Test 15 | public void contextLoads() { 16 | } 17 | } -------------------------------------------------------------------------------- /spring-rest-example/src/test/java/com/edwise/pocs/springrest/entities/UserTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springrest.entities; 2 | 3 | import org.joda.time.LocalDate; 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class UserTest { 9 | private static final long USER_ID_12 = 12L; 10 | private static final String NAME_GANDALF = "Gandalf"; 11 | private static final int TYPE_1 = 1; 12 | private static final String PHONE_661534411 = "661534411"; 13 | private static final String STRING_DATE_19110102 = "1911-01-02"; 14 | 15 | @Test 16 | public void copyFrom() { 17 | User user = createUser(USER_ID_12, NAME_GANDALF, TYPE_1, PHONE_661534411, STRING_DATE_19110102); 18 | User userTo = createUser(USER_ID_12, null, null, null, null); 19 | 20 | userTo.copyFrom(user); 21 | 22 | assertEquals(userTo, user); 23 | } 24 | 25 | private User createUser(Long id, String name, Integer type, String phone, String stringDate) { 26 | User user = new User(); 27 | user.setId(id); 28 | user.setName(name); 29 | user.setType(type); 30 | user.setPhone(phone); 31 | if (stringDate != null) { 32 | user.setBirthDate(LocalDate.parse(stringDate)); 33 | } 34 | 35 | return user; 36 | } 37 | } -------------------------------------------------------------------------------- /spring-rest-example/src/test/java/com/edwise/pocs/springrest/impl/UserServiceMockTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springrest.impl; 2 | 3 | import com.edwise.pocs.springrest.entities.User; 4 | import com.edwise.pocs.springrest.services.UserService; 5 | import com.edwise.pocs.springrest.services.impl.UserServiceMock; 6 | import org.joda.time.LocalDate; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | import java.util.List; 11 | 12 | import static org.hamcrest.Matchers.hasSize; 13 | import static org.hamcrest.Matchers.is; 14 | import static org.junit.Assert.assertThat; 15 | import static org.junit.Assert.assertNotNull; 16 | 17 | public class UserServiceMockTest { 18 | private static final long USER_ID_12 = 12l; 19 | private static final String NAME_GANDALF = "Gandalf"; 20 | private static final int TYPE_1 = 1; 21 | private static final String PHONE_661534411 = "661534411"; 22 | private static final String STRING_DATE_19110102 = "1911-01-02"; 23 | 24 | private UserService userService; 25 | 26 | @Before 27 | public void setUp() { 28 | userService = new UserServiceMock(); 29 | } 30 | 31 | @Test 32 | public void findById() { 33 | User user = userService.findById(USER_ID_12); 34 | 35 | assertNotNull(user); 36 | assertThat(user.getId(), is(USER_ID_12)); 37 | } 38 | 39 | @Test 40 | public void findAll() { 41 | List users = userService.findAll(); 42 | 43 | assertNotNull(users); 44 | assertThat(users, hasSize(3)); 45 | } 46 | 47 | @Test 48 | public void save() { 49 | User user = userService.save( 50 | createUser(null, NAME_GANDALF, TYPE_1, PHONE_661534411, STRING_DATE_19110102) 51 | ); 52 | 53 | assertNotNull(user); 54 | assertNotNull(user.getId()); 55 | } 56 | 57 | @Test 58 | public void update() { 59 | User user = userService.update( 60 | createUser(USER_ID_12, NAME_GANDALF, TYPE_1, PHONE_661534411, STRING_DATE_19110102) 61 | ); 62 | 63 | assertNotNull(user); 64 | assertThat(user.getId(), is(USER_ID_12)); 65 | } 66 | 67 | @Test 68 | public void delete() { 69 | userService.delete(USER_ID_12); 70 | } 71 | 72 | private User createUser(Long id, String name, int type, String phone, String stringDate) { 73 | User user = new User(); 74 | user.setId(id); 75 | user.setName(name); 76 | user.setType(type); 77 | user.setPhone(phone); 78 | user.setBirthDate(LocalDate.parse(stringDate)); 79 | 80 | return user; 81 | } 82 | } -------------------------------------------------------------------------------- /springboot-devtools-test/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de uso de las developer tools en Spring Boot, creado para el post: https://anotherdayanotherbug.wordpress.com/2015/12/02/spring-boot-series-developer-tools/ -------------------------------------------------------------------------------- /springboot-devtools-test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.pocs.springbootdevtoolstest 7 | springboot-devtools-test 8 | 1.0 9 | jar 10 | 11 | springboot-devtools-test 12 | Test project for Spring Boot new featture: devtools 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.3.0.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | 1.8 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-devtools 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-maven-plugin 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /springboot-devtools-test/src/main/java/com/edwise/pocs/springbootdevtoolstest/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springbootdevtoolstest; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-devtools-test/src/main/java/com/edwise/pocs/springbootdevtoolstest/controllers/HelloWorldController.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springbootdevtoolstest.controllers; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RequestMethod; 5 | import org.springframework.web.bind.annotation.RestController; 6 | import org.springframework.http.MediaType; 7 | 8 | @RestController 9 | @RequestMapping("/helloworld") 10 | public class HelloWorldController { 11 | 12 | @RequestMapping(method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE) 13 | public String getHelloWorld() { 14 | return "Hello World!"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /springboot-devtools-test/src/main/java/com/edwise/pocs/springbootdevtoolstest/controllers/OtherController.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springbootdevtoolstest.controllers; 2 | 3 | import org.springframework.web.bind.annotation.PathVariable; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestMethod; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @RestController 9 | @RequestMapping("/other") 10 | public class OtherController { 11 | 12 | @RequestMapping(path = "/{name}", method = RequestMethod.GET) 13 | public String otherGet(@PathVariable String name) { 14 | return "Hello" + name; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /springboot-example/README.md: -------------------------------------------------------------------------------- 1 | POC con Spring Boot, creado para el post http://anotherdayanotherbug.wordpress.com/2014/08/27/poc-con-spring-boot/ 2 | -------------------------------------------------------------------------------- /springboot-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.edwise.pocs.springboot 6 | springboot-example 7 | 0.1 8 | jar 9 | 10 | springboot-example 11 | http://maven.apache.org 12 | 13 | 14 | org.springframework.boot 15 | spring-boot-starter-parent 16 | 1.1.5.RELEASE 17 | 18 | 19 | 20 | UTF-8 21 | 1.8 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | junit 32 | junit 33 | 4.11 34 | test 35 | 36 | 37 | hamcrest-core 38 | org.hamcrest 39 | 40 | 41 | 42 | 43 | 44 | org.hamcrest 45 | hamcrest-all 46 | 1.3 47 | test 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-maven-plugin 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /springboot-example/src/main/java/com/edwise/pocs/springboot/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.context.annotation.ComponentScan; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | @Configuration 9 | @EnableAutoConfiguration 10 | @ComponentScan 11 | public class Application { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(Application.class, args); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /springboot-example/src/main/java/com/edwise/pocs/springboot/controllers/HelloWorldController.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springboot.controllers; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.ResponseBody; 6 | 7 | @Controller 8 | public class HelloWorldController { 9 | 10 | @RequestMapping("/") 11 | @ResponseBody 12 | public String sayHelloWorld() { 13 | return "Hello World in your SpringBoot Application!"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /springboot-example/src/test/java/com/edwise/pocs/springboot/controllers/HelloWorldControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.springboot.controllers; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.hamcrest.Matchers.is; 7 | import static org.junit.Assert.assertNotNull; 8 | import static org.junit.Assert.assertThat; 9 | 10 | public class HelloWorldControllerTest { 11 | 12 | private HelloWorldController helloWorldController; 13 | 14 | @Before 15 | public void setUp() { 16 | helloWorldController = new HelloWorldController(); 17 | } 18 | 19 | @Test 20 | public void testSayHelloWorld() { 21 | String msgResult = helloWorldController.sayHelloWorld(); 22 | 23 | assertNotNull(msgResult); 24 | assertThat(msgResult, is("Hello World in your SpringBoot Application!")); 25 | } 26 | } -------------------------------------------------------------------------------- /springboot-series-actuator/README.md: -------------------------------------------------------------------------------- 1 | Ejemplo de uso de Spring Boot Actuator, creado para el segundo post de la serie Spring Boot: http://anotherdayanotherbug.wordpress.com/2014/11/21/spring-boot-series-actuator-monitoriza-tu-aplicacion/ 2 | -------------------------------------------------------------------------------- /springboot-series-actuator/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.springbootseries.actuator 7 | springbootseries-actuator 8 | 1.0 9 | jar 10 | 11 | springbootseries-actuator 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-parent 22 | 1.1.9.RELEASE 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-actuator 34 | 35 | 36 | 37 | junit 38 | junit 39 | 4.11 40 | test 41 | 42 | 43 | hamcrest-core 44 | org.hamcrest 45 | 46 | 47 | 48 | 49 | 50 | org.hamcrest 51 | hamcrest-all 52 | 1.3 53 | test 54 | 55 | 56 | 57 | 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-maven-plugin 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /springboot-series-actuator/src/main/java/com/edwise/springbootseries/actuator/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.actuator; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.context.annotation.ComponentScan; 6 | 7 | @EnableAutoConfiguration 8 | @ComponentScan 9 | public class Application { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(Application.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /springboot-series-actuator/src/main/java/com/edwise/springbootseries/actuator/checkers/FileManager.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.actuator.checkers; 2 | 3 | import java.util.Random; 4 | 5 | public class FileManager { 6 | 7 | public enum Status { 8 | OK, 9 | LOW, 10 | DANGER 11 | } 12 | 13 | // This is a random mock! 14 | public static Status checkStatus() { 15 | 16 | Random r = new Random(); 17 | return Status.values()[r.nextInt(Status.values().length)]; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /springboot-series-actuator/src/main/java/com/edwise/springbootseries/actuator/controller/HelloWorldController.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.actuator.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.ResponseBody; 6 | 7 | @Controller 8 | public class HelloWorldController { 9 | 10 | @RequestMapping("/") 11 | @ResponseBody 12 | public String sayHelloWorld() { 13 | return "Hello World in your SpringBoot Application!"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /springboot-series-actuator/src/main/java/com/edwise/springbootseries/actuator/endpoints/BetterHealthIndicator.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.actuator.endpoints; 2 | 3 | import com.edwise.springbootseries.actuator.checkers.FileManager; 4 | import org.springframework.boot.actuate.health.Health; 5 | import org.springframework.boot.actuate.health.HealthIndicator; 6 | import org.springframework.stereotype.Component; 7 | 8 | 9 | @Component 10 | public class BetterHealthIndicator implements HealthIndicator { 11 | 12 | @Override 13 | public Health health() { 14 | FileManager.Status diskStatus = FileManager.checkStatus(); 15 | 16 | Health health = null; 17 | if (diskStatus.equals(FileManager.Status.OK)) { 18 | health = Health.up().build(); 19 | } else { 20 | health = Health.down().withDetail("diskStatus", diskStatus).build(); 21 | } 22 | 23 | return health; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /springboot-series-actuator/src/main/java/com/edwise/springbootseries/actuator/endpoints/BugsEndpoint.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.actuator.endpoints; 2 | 3 | import com.edwise.springbootseries.actuator.entity.Bug; 4 | import com.edwise.springbootseries.actuator.service.BugsService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.actuate.endpoint.AbstractEndpoint; 7 | import org.springframework.stereotype.Component; 8 | 9 | import java.util.List; 10 | 11 | @Component 12 | public class BugsEndpoint extends AbstractEndpoint> { 13 | 14 | @Autowired 15 | BugsService bugsService; 16 | 17 | public BugsEndpoint() { 18 | super("bugs"); 19 | } 20 | 21 | 22 | @Override 23 | public List invoke() { 24 | return bugsService.getAllBugs(); 25 | } 26 | } -------------------------------------------------------------------------------- /springboot-series-actuator/src/main/java/com/edwise/springbootseries/actuator/entity/Bug.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.actuator.entity; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | public class Bug { 6 | 7 | private long id; 8 | private String name; 9 | private String description; 10 | private LocalDateTime date; 11 | 12 | public long getId() { 13 | return id; 14 | } 15 | 16 | public Bug setId(long id) { 17 | this.id = id; 18 | return this; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public Bug setName(String name) { 26 | this.name = name; 27 | return this; 28 | } 29 | 30 | public String getDescription() { 31 | return description; 32 | } 33 | 34 | public Bug setDescription(String description) { 35 | this.description = description; 36 | return this; 37 | } 38 | 39 | public LocalDateTime getDate() { 40 | return date; 41 | } 42 | 43 | public Bug setDate(LocalDateTime date) { 44 | this.date = date; 45 | return this; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /springboot-series-actuator/src/main/java/com/edwise/springbootseries/actuator/service/BugsService.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.actuator.service; 2 | 3 | import com.edwise.springbootseries.actuator.entity.Bug; 4 | 5 | import java.util.List; 6 | 7 | public interface BugsService { 8 | 9 | public List getAllBugs(); 10 | 11 | public Bug getBug(long id); 12 | } 13 | -------------------------------------------------------------------------------- /springboot-series-actuator/src/main/java/com/edwise/springbootseries/actuator/service/impl/BugsServiceMock.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.actuator.service.impl; 2 | 3 | import com.edwise.springbootseries.actuator.entity.Bug; 4 | import com.edwise.springbootseries.actuator.service.BugsService; 5 | import org.springframework.stereotype.Service; 6 | 7 | import java.time.LocalDateTime; 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | @Service 12 | public class BugsServiceMock implements BugsService { 13 | 14 | @Override 15 | public List getAllBugs() { 16 | return Arrays.asList( 17 | createRandomBug(12345, "bug434632", "Error in system 45322352", "2009-12-08T12:35:10"), 18 | createRandomBug(34555, "bug564645", "Error in system 62357272", "2013-02-13T11:16:30"), 19 | createRandomBug(34536, "bug569565", "Error in system 47347423", "2014-12-22T10:05:20"), 20 | createRandomBug(56463, "bug675673", "Error in system 46734673", "2014-11-11T11:56:30"), 21 | createRandomBug(457345, "bug411132", "Error in system 65835655", "2014-03-25T05:22:45") 22 | ); 23 | } 24 | 25 | @Override 26 | public Bug getBug(long id) { 27 | return createRandomBug(id, "bug434631", "Error in system bla bla bla", "2014-03-25T05:22:45"); 28 | } 29 | 30 | private Bug createRandomBug(long id, String name, String description, String dateString) { 31 | return new Bug() 32 | .setId(id) 33 | .setName(name) 34 | .setDescription(description) 35 | .setDate(LocalDateTime.parse(dateString)); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /springboot-series-actuator/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | management.contextPath=/actuator-admin 2 | 3 | info.application.name=Spring Boot Series: actuator example app 4 | info.application.description=Example Spring Boot app with actuator, for my blog 5 | info.application.version=0.1.1 -------------------------------------------------------------------------------- /springboot-series-actuator/src/test/java/com/edwise/springbootseries/actuator/checkers/FileManagerTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.actuator.checkers; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertNotNull; 6 | 7 | public class FileManagerTest { 8 | 9 | @Test 10 | public void testCheckStatus() { 11 | FileManager.Status status = FileManager.checkStatus(); 12 | 13 | assertNotNull(status); 14 | } 15 | } -------------------------------------------------------------------------------- /springboot-series-actuator/src/test/java/com/edwise/springbootseries/actuator/controller/HelloWorldControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.actuator.controller; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.hamcrest.Matchers.is; 6 | import static org.junit.Assert.assertNotNull; 7 | import static org.junit.Assert.assertThat; 8 | 9 | public class HelloWorldControllerTest { 10 | 11 | private HelloWorldController helloWorldController = new HelloWorldController(); 12 | 13 | @Test 14 | public void testSayHelloWorld() { 15 | String msgResult = helloWorldController.sayHelloWorld(); 16 | 17 | assertNotNull(msgResult); 18 | assertThat(msgResult, is("Hello World in your SpringBoot Application!")); 19 | } 20 | } -------------------------------------------------------------------------------- /springboot-series-actuator/src/test/java/com/edwise/springbootseries/actuator/service/impl/BugsServiceMockTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.actuator.service.impl; 2 | 3 | import com.edwise.springbootseries.actuator.entity.Bug; 4 | import com.edwise.springbootseries.actuator.service.BugsService; 5 | import org.junit.Test; 6 | 7 | import java.util.List; 8 | 9 | import static org.hamcrest.MatcherAssert.assertThat; 10 | import static org.hamcrest.Matchers.hasSize; 11 | import static org.hamcrest.Matchers.is; 12 | import static org.junit.Assert.assertNotNull; 13 | 14 | public class BugsServiceMockTest { 15 | private static final long BUG_ID_12 = 12l; 16 | 17 | private BugsService bugsService = new BugsServiceMock(); 18 | 19 | @Test 20 | public void getById() { 21 | Bug bug = bugsService.getBug(BUG_ID_12); 22 | 23 | assertNotNull(bug); 24 | assertThat(bug.getId(), is(BUG_ID_12)); 25 | } 26 | @Test 27 | public void getAll() { 28 | List bugs = bugsService.getAllBugs(); 29 | 30 | assertNotNull(bugs); 31 | assertThat(bugs, hasSize(5)); 32 | } 33 | } -------------------------------------------------------------------------------- /springboot-series-jackson/README.md: -------------------------------------------------------------------------------- 1 | Ejemplo de configuración de Jackson en un proyecto Spring Boot, creado para el tercer post de la serie Spring Boot: http://anotherdayanotherbug.wordpress.com/2015/01/07/spring-boot-series-autoconfiguracion-de-jackson/ -------------------------------------------------------------------------------- /springboot-series-jackson/src/main/java/com/edwise/springbootseries/jackson/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.jackson; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | 7 | @SpringBootApplication 8 | public class Application { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(Application.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /springboot-series-jackson/src/main/java/com/edwise/springbootseries/jackson/controller/InfoController.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.jackson.controller; 2 | 3 | import com.edwise.springbootseries.jackson.entity.Info; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.http.MediaType; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.*; 8 | 9 | import org.joda.time.LocalDateTime; 10 | //import java.time.LocalDateTime; 11 | 12 | @RestController 13 | @RequestMapping("/api/info/") 14 | public class InfoController { 15 | 16 | @ResponseStatus(HttpStatus.OK) 17 | @RequestMapping(method = RequestMethod.GET, value = "{id}", produces = MediaType.APPLICATION_JSON_VALUE) 18 | public Info getInfo(@PathVariable long id) { 19 | return new Info() 20 | .setId(id) 21 | .setInfo("Info 1234") 22 | .setCreationDateTime(new LocalDateTime(2001, 12, 12, 13, 40, 30)); 23 | // .setCreationDateTime(LocalDateTime.of(2001, 12, 12, 13, 40, 30)); 24 | } 25 | 26 | @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) 27 | public ResponseEntity createInfo(@RequestBody Info info) { 28 | return new ResponseEntity<>(info.setId(1234), HttpStatus.CREATED); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /springboot-series-jackson/src/main/java/com/edwise/springbootseries/jackson/entity/Info.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.jackson.entity; 2 | 3 | import lombok.Data; 4 | import lombok.experimental.Accessors; 5 | 6 | //import java.time.LocalDateTime; 7 | import org.joda.time.LocalDateTime; 8 | 9 | @Data 10 | @Accessors(chain = true) 11 | public class Info { 12 | 13 | private long id; 14 | private String info; 15 | private LocalDateTime creationDateTime; 16 | } 17 | -------------------------------------------------------------------------------- /springboot-series-jackson/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.jackson.serialization.write_dates_as_timestamps=false 2 | -------------------------------------------------------------------------------- /springboot-series-jackson/src/test/java/com/edwise/springbootseries/jackson/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.jackson; 2 | 3 | import com.edwise.springbootseries.jackson.entity.Info; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.SpringApplicationConfiguration; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | import org.springframework.test.context.web.WebAppConfiguration; 11 | 12 | import static org.hamcrest.MatcherAssert.assertThat; 13 | import static org.hamcrest.Matchers.containsString; 14 | import static org.hamcrest.core.Is.is; 15 | 16 | import org.joda.time.LocalDateTime; 17 | //import java.time.LocalDateTime; 18 | 19 | @RunWith(SpringJUnit4ClassRunner.class) 20 | @SpringApplicationConfiguration(classes = Application.class) 21 | @WebAppConfiguration 22 | public class ApplicationTest { 23 | 24 | @Autowired 25 | public ObjectMapper objectMapper; 26 | 27 | @Test 28 | public void testSerializingLocalDateTimeIsCorrect() throws Exception { 29 | LocalDateTime date = new LocalDateTime(2011, 12, 9, 19, 15, 20); 30 | //LocalDateTime date = LocalDateTime.of(2011, 12, 9, 19, 15, 20); 31 | 32 | String result = objectMapper.writeValueAsString(date); 33 | assertThat(result, containsString("2011-12-09T19:15:20")); 34 | } 35 | 36 | @Test 37 | public void testDeserializingLocalDateTimeIsCorrect() throws Exception { 38 | String infoJson = "{\"id\":1234,\"info\":\"Info Test\",\"creationDateTime\":\"2011-12-09T19:15:20\"}"; 39 | 40 | Info result = objectMapper.reader(Info.class).readValue(infoJson); 41 | 42 | assertThat(result.getCreationDateTime(), is(new LocalDateTime(2011, 12, 9, 19, 15, 20))); 43 | //assertThat(result.getCreationDateTime(), is(LocalDateTime.of(2011, 12, 9, 19, 15, 20))); 44 | } 45 | } -------------------------------------------------------------------------------- /springboot-series-jackson/src/test/java/com/edwise/springbootseries/jackson/controller/InfoControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.jackson.controller; 2 | 3 | import com.edwise.springbootseries.jackson.entity.Info; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | 9 | import static org.hamcrest.Matchers.equalTo; 10 | import static org.hamcrest.Matchers.is; 11 | import static org.junit.Assert.assertNotNull; 12 | import static org.junit.Assert.assertThat; 13 | 14 | import org.joda.time.LocalDateTime; 15 | //import java.time.LocalDateTime; 16 | 17 | 18 | public class InfoControllerTest { 19 | 20 | private static final long INFO_ID_1234 = 1234; 21 | private static final String INFO_TEST = "Test 1234"; 22 | private static final LocalDateTime INFO_CREATION_DATE_TIME = new LocalDateTime(2011, 12, 9, 19, 15, 20); 23 | // private static final LocalDateTime INFO_CREATION_DATE_TIME = LocalDateTime.of(2011, 12, 9, 19, 15, 20); 24 | 25 | private InfoController infoController; 26 | 27 | @Before 28 | public void setUp() { 29 | infoController = new InfoController(); 30 | } 31 | 32 | @Test 33 | public void testGetInfo() { 34 | Info info = infoController.getInfo(INFO_ID_1234); 35 | 36 | assertNotNull(info); 37 | assertThat(info.getId(), is(INFO_ID_1234)); 38 | } 39 | 40 | @Test 41 | public void testCreateInfo() { 42 | ResponseEntity response = infoController.createInfo( 43 | new Info() 44 | .setInfo(INFO_TEST) 45 | .setCreationDateTime(INFO_CREATION_DATE_TIME) 46 | ); 47 | 48 | assertNotNull(response); 49 | assertNotNull(response.getBody()); 50 | assertThat(response.getStatusCode(), equalTo(HttpStatus.CREATED)); 51 | assertThat(response.getBody().getInfo(), is(INFO_TEST)); 52 | assertThat(response.getBody().getCreationDateTime(), is(INFO_CREATION_DATE_TIME)); 53 | } 54 | } -------------------------------------------------------------------------------- /springboot-series-jooq/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de la autoconfiguración de jOOQ en Spring Boot en la versión 1.3: 2 | https://anotherdayanotherbug.wordpress.com/2016/01/15/spring-boot-series-autoconfiguracion-con-jooq/ -------------------------------------------------------------------------------- /springboot-series-jooq/src/main/java/com/edwise/springbootseries/jooq/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.jooq; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-series-jooq/src/main/java/com/edwise/springbootseries/jooq/dao/BooksDao.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.jooq.dao; 2 | 3 | import org.jooq.DSLContext; 4 | import org.jooq.Record; 5 | import org.jooq.Result; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Repository; 10 | import sample.jooq.domain.Tables; 11 | 12 | import java.util.Optional; 13 | 14 | @Repository 15 | public class BooksDao { 16 | private static final Logger LOG = LoggerFactory.getLogger(BooksDao.class); 17 | 18 | private final DSLContext create; 19 | 20 | @Autowired 21 | public BooksDao(DSLContext dslContext) { 22 | this.create = dslContext; 23 | } 24 | 25 | public Result getAllBookCharacters() { 26 | Result result = create 27 | .select() 28 | .from(Tables.BOOK_CHARACTER) 29 | .fetch(); 30 | 31 | LOG.info("Resultado query getAllBookCharacters: \n{}", result); 32 | return result; 33 | } 34 | 35 | public Result getAllBookCharactersOrderByName() { 36 | Result result = create 37 | .select() 38 | .from(Tables.BOOK_CHARACTER) 39 | .orderBy(Tables.BOOK_CHARACTER.NAME) 40 | .fetch(); 41 | 42 | LOG.info("Resultado query getAllBookCharactersOrderByName: \n{}", result); 43 | return result; 44 | } 45 | 46 | public Optional getBookCharacterById(int id) { 47 | Optional result = create 48 | .select() 49 | .from(Tables.BOOK_CHARACTER) 50 | .where(Tables.BOOK_CHARACTER.ID.equal(id)) 51 | .orderBy(Tables.BOOK_CHARACTER.NAME) 52 | .fetchOptional(); 53 | 54 | LOG.info("Resultado query getBookCharacterById: \n{}", result); 55 | return result; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /springboot-series-jooq/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | insert into book_character values (1, 'Saruman'); 2 | insert into book_character values (2, 'Gandalf'); 3 | insert into book_character values (3, 'Aragorn'); 4 | insert into book_character values (4, 'Samwise'); 5 | insert into book_character values (5, 'Frodo'); -------------------------------------------------------------------------------- /springboot-series-jooq/src/main/resources/delete.sql: -------------------------------------------------------------------------------- 1 | drop all OBJECTS; -------------------------------------------------------------------------------- /springboot-series-jooq/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | create table book_character (id integer primary key, name varchar(30)); -------------------------------------------------------------------------------- /springboot-series-jooq/src/test/java/com/edwise/springbootseries/jooq/dao/BooksDaoIT.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.jooq.dao; 2 | 3 | import com.edwise.springbootseries.jooq.Application; 4 | import org.jooq.Record; 5 | import org.jooq.Result; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.SpringApplicationConfiguration; 12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 | import sample.jooq.domain.Tables; 14 | 15 | import java.util.Optional; 16 | 17 | import static org.assertj.core.api.Assertions.assertThat; 18 | 19 | @RunWith(SpringJUnit4ClassRunner.class) 20 | @SpringApplicationConfiguration(Application.class) 21 | public class BooksDaoIT { 22 | private static final Logger LOG = LoggerFactory.getLogger(BooksDaoIT.class); 23 | 24 | private static final int EXISTING_CHAR_ID = 4; 25 | private static final int NOT_EXISTING_CHAR_ID = 13; 26 | 27 | @Autowired 28 | private BooksDao booksDao; 29 | 30 | @Test 31 | public void getAllBookCharactersShouldReturnAllTheChars() { 32 | Result allBookCharacters = booksDao.getAllBookCharacters(); 33 | 34 | allBookCharacters.stream() 35 | .map(record -> record.getValue(Tables.BOOK_CHARACTER.NAME)) 36 | .forEach(name -> LOG.info("Char: {}", name)); 37 | 38 | assertThat(allBookCharacters) 39 | .extracting(record -> record.getValue(Tables.BOOK_CHARACTER.NAME)) 40 | .contains("Samwise", "Gandalf", "Frodo", "Saruman", "Aragorn"); 41 | } 42 | 43 | @Test 44 | public void getAllBookCharactersOrderByNameShouldReturnCharsSorted() { 45 | Result allBookCharactersByName = booksDao.getAllBookCharactersOrderByName(); 46 | 47 | assertThat(allBookCharactersByName) 48 | .extracting(record -> record.getValue(Tables.BOOK_CHARACTER.NAME)) 49 | .isSorted(); 50 | } 51 | 52 | @Test 53 | public void getBookByIdThatExistsShouldReturnValidRecord() { 54 | Optional bookCharacter = booksDao.getBookCharacterById(EXISTING_CHAR_ID); 55 | 56 | assertThat( 57 | bookCharacter 58 | .orElseThrow(() -> new AssertionError("Not existing character")) 59 | .getValue(Tables.BOOK_CHARACTER.NAME)) 60 | .isEqualTo("Samwise"); 61 | } 62 | 63 | @Test 64 | public void getBookByIdThatNotExistsShouldReturnEmptyOptionalRecord() { 65 | Optional bookCharacter = booksDao.getBookCharacterById(NOT_EXISTING_CHAR_ID); 66 | 67 | assertThat(bookCharacter.isPresent()).isFalse(); 68 | } 69 | } -------------------------------------------------------------------------------- /springboot-series-war/README.md: -------------------------------------------------------------------------------- 1 | Ejemplo Spring Boot, creado para el primer post de la serie Spring Boot: http://anotherdayanotherbug.wordpress.com/2014/10/13/spring-boot-series-generar-war-para-tomcat/ 2 | -------------------------------------------------------------------------------- /springboot-series-war/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.edwise.springbootseries.war 6 | springbootseries-war 7 | 1.0 8 | war 9 | 10 | springbootseries-war 11 | http://maven.apache.org 12 | 13 | 14 | org.springframework.boot 15 | spring-boot-starter-parent 16 | 1.1.8.RELEASE 17 | 18 | 19 | 20 | UTF-8 21 | 1.8 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | junit 32 | junit 33 | 4.11 34 | test 35 | 36 | 37 | hamcrest-core 38 | org.hamcrest 39 | 40 | 41 | 42 | 43 | 44 | org.hamcrest 45 | hamcrest-all 46 | 1.3 47 | test 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-maven-plugin 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /springboot-series-war/src/main/java/com/edwise/springbootseries/war/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.war; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.boot.builder.SpringApplicationBuilder; 6 | import org.springframework.boot.context.web.SpringBootServletInitializer; 7 | import org.springframework.context.annotation.ComponentScan; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | @Configuration 11 | @EnableAutoConfiguration 12 | @ComponentScan 13 | public class Application extends SpringBootServletInitializer { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(Application.class, args); 17 | } 18 | 19 | @Override 20 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 21 | return application.sources(Application.class); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /springboot-series-war/src/main/java/com/edwise/springbootseries/war/controllers/HelloWorldController.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.war.controllers; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.ResponseBody; 6 | 7 | @Controller 8 | public class HelloWorldController { 9 | 10 | @RequestMapping("/") 11 | @ResponseBody 12 | public String sayHelloWorld() { 13 | return "Hello World in your SpringBoot Application!"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /springboot-series-war/src/test/java/com/edwise/springbootseries/war/controllers/HelloWorldControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.war.controllers; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.hamcrest.Matchers.is; 7 | import static org.junit.Assert.assertNotNull; 8 | import static org.junit.Assert.assertThat; 9 | 10 | public class HelloWorldControllerTest { 11 | 12 | private HelloWorldController helloWorldController; 13 | 14 | @Before 15 | public void setUp() { 16 | helloWorldController = new HelloWorldController(); 17 | } 18 | 19 | @Test 20 | public void testSayHelloWorld() { 21 | String msgResult = helloWorldController.sayHelloWorld(); 22 | 23 | assertNotNull(msgResult); 24 | assertThat(msgResult, is("Hello World in your SpringBoot Application!")); 25 | } 26 | } -------------------------------------------------------------------------------- /springbootseries-banner/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo para modificar el banner de Spring Boot en el arranque, creado para el post: https://anotherdayanotherbug.wordpress.com/2015/07/20/spring-boot-series-como-cambiar-el-banner-de-arranque/ -------------------------------------------------------------------------------- /springbootseries-banner/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.springbootseries.banner 7 | springbootseries-banner 8 | 1.0 9 | jar 10 | 11 | springbootseries-banner 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-parent 22 | 1.2.5.RELEASE 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-test 34 | test 35 | 36 | 37 | 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-maven-plugin 43 | 44 | 45 | 46 | org.apache.maven.plugins 47 | maven-compiler-plugin 48 | 3.3 49 | 50 | ${java.version} 51 | ${java.version} 52 | ${java.version} 53 | ${java.version} 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /springbootseries-banner/src/main/java/com/edwise/springbootseries/banner/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.banner; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication app = new SpringApplication(Application.class); 11 | //app.setShowBanner(false); 12 | app.run(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /springbootseries-banner/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | __ __ _ __ ____ _ _ ____ ____ ____ __ _ _ __ __ _ __ ____ _ _ ____ ____ ____ _ _ ___ 2 | / _\ ( ( \ / \(_ _)/ )( \( __)( _ \ ( \ / _\ ( \/ )_ / _\ ( ( \ / \(_ _)/ )( \( __)( _ \ ( _ \/ )( \ / __) 3 | / \/ /( O ) )( ) __ ( ) _) ) / ) D (/ \ ) /( ) / \/ /( O ) )( ) __ ( ) _) ) / ) _ () \/ (( (_ \ 4 | \_/\_/\_)__) \__/ (__) \_)(_/(____)(__\_) (____/\_/\_/(__/ (/ \_/\_/\_)__) \__/ (__) \_)(_/(____)(__\_) (____/\____/ \___/ 5 | 6 | -> Spring Boot version: ${spring-boot.formatted-version} <- 7 | 8 | @edwise 9 | -------------------------------------------------------------------------------- /springbootseries-banner/src/test/java/com/edwise/springbootseries/banner/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.banner; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.SpringApplicationConfiguration; 6 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 7 | import org.springframework.test.context.web.WebAppConfiguration; 8 | 9 | @RunWith(SpringJUnit4ClassRunner.class) 10 | @SpringApplicationConfiguration(classes = Application.class) 11 | @WebAppConfiguration 12 | public class ApplicationTest { 13 | 14 | @Test 15 | public void contextLoads() { 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /springbootseries-integrationtests/README.md: -------------------------------------------------------------------------------- 1 | Ejemplo de test de integración en un proyecto Spring Boot, creado para el cuarto post de la serie Spring Boot: 2 | https://anotherdayanotherbug.wordpress.com/2015/02/04/spring-boot-series-como-configurar-tus-tests-de-integracion/ -------------------------------------------------------------------------------- /springbootseries-integrationtests/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.springbootseries.integrationtests 7 | springbootseries-integrationtests 8 | 1.0 9 | jar 10 | 11 | springbootseries-integrationtests 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-parent 22 | 1.2.1.RELEASE 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | com.fasterxml.jackson.datatype 33 | jackson-datatype-jsr310 34 | 2.5.0 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /springbootseries-integrationtests/src/main/java/com/edwise/springbootseries/integrationtests/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.integrationtests; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springbootseries-integrationtests/src/main/java/com/edwise/springbootseries/integrationtests/controller/InfoController.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.integrationtests.controller; 2 | 3 | import com.edwise.springbootseries.integrationtests.entity.Info; 4 | import com.edwise.springbootseries.integrationtests.service.InfoService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.MediaType; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | @RestController 12 | @RequestMapping("/api/info/") 13 | public class InfoController { 14 | 15 | @Autowired 16 | private InfoService infoService; 17 | 18 | @ResponseStatus(HttpStatus.OK) 19 | @RequestMapping(method = RequestMethod.GET, value = "{id}", 20 | produces = MediaType.APPLICATION_JSON_VALUE) 21 | public Info getInfo(@PathVariable Long id) { 22 | return infoService.findOne(id); 23 | } 24 | 25 | @RequestMapping(method = RequestMethod.POST, 26 | produces = MediaType.APPLICATION_JSON_VALUE) 27 | public ResponseEntity createInfo(@RequestBody Info info) { 28 | Info infoCreated = infoService.save(info); 29 | return new ResponseEntity<>(infoCreated, HttpStatus.CREATED); 30 | } 31 | 32 | @ResponseStatus(HttpStatus.NO_CONTENT) 33 | @RequestMapping(method = RequestMethod.DELETE, value = "{id}") 34 | public void deleteInfo(@PathVariable Long id) { 35 | infoService.delete(id); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /springbootseries-integrationtests/src/main/java/com/edwise/springbootseries/integrationtests/entity/Info.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.integrationtests.entity; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | public class Info { 6 | 7 | private Long id; 8 | private String info; 9 | private LocalDateTime creationDateTime; 10 | 11 | public Long getId() { 12 | return id; 13 | } 14 | 15 | public Info setId(Long id) { 16 | this.id = id; 17 | return this; 18 | } 19 | 20 | public String getInfo() { 21 | return info; 22 | } 23 | 24 | public Info setInfo(String info) { 25 | this.info = info; 26 | return this; 27 | } 28 | 29 | public LocalDateTime getCreationDateTime() { 30 | return creationDateTime; 31 | } 32 | 33 | public Info setCreationDateTime(LocalDateTime creationDateTime) { 34 | this.creationDateTime = creationDateTime; 35 | return this; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /springbootseries-integrationtests/src/main/java/com/edwise/springbootseries/integrationtests/repository/InfoRepository.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.integrationtests.repository; 2 | 3 | import com.edwise.springbootseries.integrationtests.entity.Info; 4 | 5 | public interface InfoRepository { 6 | 7 | Info save(Info info); 8 | 9 | Info findOne(Long id); 10 | 11 | void delete(Long id); 12 | } 13 | -------------------------------------------------------------------------------- /springbootseries-integrationtests/src/main/java/com/edwise/springbootseries/integrationtests/repository/impl/InfoRepositoryMock.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.integrationtests.repository.impl; 2 | 3 | import com.edwise.springbootseries.integrationtests.entity.Info; 4 | import com.edwise.springbootseries.integrationtests.repository.InfoRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.time.LocalDateTime; 8 | 9 | @Repository 10 | public class InfoRepositoryMock implements InfoRepository { 11 | 12 | @Override 13 | public Info save(Info info) { 14 | return info.setId(1234L); 15 | } 16 | 17 | @Override 18 | public Info findOne(Long id) { 19 | return new Info() 20 | .setId(id) 21 | .setInfo("Info 1234") 22 | .setCreationDateTime(LocalDateTime.of(2001, 12, 12, 13, 40, 30)); 23 | } 24 | 25 | @Override 26 | public void delete(Long id) { 27 | // delete info by id 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /springbootseries-integrationtests/src/main/java/com/edwise/springbootseries/integrationtests/service/InfoService.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.integrationtests.service; 2 | 3 | import com.edwise.springbootseries.integrationtests.entity.Info; 4 | 5 | public interface InfoService { 6 | Info save(Info entity); 7 | 8 | Info findOne(Long id); 9 | 10 | void delete(Long id); 11 | } 12 | -------------------------------------------------------------------------------- /springbootseries-integrationtests/src/main/java/com/edwise/springbootseries/integrationtests/service/impl/InfoServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.edwise.springbootseries.integrationtests.service.impl; 2 | 3 | import com.edwise.springbootseries.integrationtests.entity.Info; 4 | import com.edwise.springbootseries.integrationtests.repository.InfoRepository; 5 | import com.edwise.springbootseries.integrationtests.service.InfoService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | @Service 10 | public class InfoServiceImpl implements InfoService { 11 | 12 | @Autowired 13 | private InfoRepository infoRepository; 14 | 15 | @Override 16 | public Info save(Info info) { 17 | return infoRepository.save(info); 18 | } 19 | 20 | @Override 21 | public Info findOne(Long id) { 22 | return infoRepository.findOne(id); 23 | } 24 | 25 | @Override 26 | public void delete(Long id) { 27 | infoRepository.delete(id); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /springbootseries-integrationtests/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.jackson.serialization.write_dates_as_timestamps=false -------------------------------------------------------------------------------- /springbootseries-integrationtests/src/test/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.jackson.serialization.write_dates_as_timestamps=false -------------------------------------------------------------------------------- /springfox-swagger2-example/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de documentar un servicio REST Spring con Swagger 2, usando la librería SpringFox: https://anotherdayanotherbug.wordpress.com/2015/12/21/documentar-un-servicio-rest-con-swagger-2-springfox/ -------------------------------------------------------------------------------- /springfox-swagger2-example/src/main/java/com/edwise/pocs/swagger2/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.swagger2; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springfox-swagger2-example/src/main/java/com/edwise/pocs/swagger2/config/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.swagger2.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import springfox.documentation.builders.ApiInfoBuilder; 6 | import springfox.documentation.service.ApiInfo; 7 | import springfox.documentation.spi.DocumentationType; 8 | import springfox.documentation.spring.web.plugins.Docket; 9 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 10 | 11 | import java.time.LocalDateTime; 12 | import java.util.Date; 13 | 14 | import static springfox.documentation.builders.PathSelectors.regex; 15 | 16 | @Configuration 17 | @EnableSwagger2 18 | public class SwaggerConfig { 19 | 20 | @Bean 21 | public Docket newsApi() { 22 | return new Docket(DocumentationType.SWAGGER_2) 23 | .groupName("api-infos") 24 | .apiInfo(apiInfo()) 25 | .directModelSubstitute(LocalDateTime.class, Date.class) 26 | .select() 27 | .paths(regex("/api.*")) 28 | .build(); 29 | } 30 | 31 | private ApiInfo apiInfo() { 32 | return new ApiInfoBuilder() 33 | .title("Infos REST api") 34 | .description("PoC of a REST api, Infos") 35 | .termsOfServiceUrl("http://en.wikipedia.org/wiki/Terms_of_service") 36 | .contact("edwise.null@gmail.com") 37 | .license("Apache License Version 2.0") 38 | .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") 39 | .version("2.0") 40 | .build(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /springfox-swagger2-example/src/main/java/com/edwise/pocs/swagger2/entity/Info.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.swagger2.entity; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | 6 | import java.time.LocalDateTime; 7 | 8 | @ApiModel(value = "Info entity", description = "Complete data of a entity Info") 9 | public class Info { 10 | 11 | @ApiModelProperty(value = "The id of the info", required = false) 12 | private Long id; 13 | 14 | @ApiModelProperty(value = "The text of the info", required = true) 15 | private String infoText; 16 | 17 | @ApiModelProperty(value = "The date of the info", required = true) 18 | private LocalDateTime creationDateTime; 19 | 20 | public Long getId() { 21 | return id; 22 | } 23 | 24 | public Info setId(Long id) { 25 | this.id = id; 26 | return this; 27 | } 28 | 29 | public String getInfoText() { 30 | return infoText; 31 | } 32 | 33 | public Info setInfoText(String infoText) { 34 | this.infoText = infoText; 35 | return this; 36 | } 37 | 38 | public LocalDateTime getCreationDateTime() { 39 | return creationDateTime; 40 | } 41 | 42 | public Info setCreationDateTime(LocalDateTime creationDateTime) { 43 | this.creationDateTime = creationDateTime; 44 | return this; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /springfox-swagger2-example/src/main/java/com/edwise/pocs/swagger2/repository/InfoRepository.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.swagger2.repository; 2 | 3 | import com.edwise.pocs.swagger2.entity.Info; 4 | 5 | import java.util.List; 6 | 7 | public interface InfoRepository { 8 | 9 | Info save(Info info); 10 | 11 | Info findOne(Long id); 12 | 13 | List findAll(); 14 | 15 | Info update(Info info); 16 | 17 | void delete(Long id); 18 | } 19 | -------------------------------------------------------------------------------- /springfox-swagger2-example/src/main/java/com/edwise/pocs/swagger2/repository/impl/InfoRepositoryMock.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.swagger2.repository.impl; 2 | 3 | import com.edwise.pocs.swagger2.entity.Info; 4 | import com.edwise.pocs.swagger2.repository.InfoRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.time.LocalDateTime; 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | @Repository 12 | public class InfoRepositoryMock implements InfoRepository { 13 | 14 | @Override 15 | public Info save(Info info) { 16 | return info.setId(1234L); 17 | } 18 | 19 | @Override 20 | public Info findOne(Long id) { 21 | return new Info() 22 | .setId(id) 23 | .setInfoText("Info 1234") 24 | .setCreationDateTime(LocalDateTime.of(2001, 12, 12, 13, 40, 30)); 25 | } 26 | 27 | @Override 28 | public List findAll() { 29 | return Arrays.asList( 30 | new Info() 31 | .setId(120L) 32 | .setInfoText("Info 1234") 33 | .setCreationDateTime(LocalDateTime.of(2001, 12, 12, 13, 40, 30)), 34 | new Info() 35 | .setId(121L) 36 | .setInfoText("Info 4567") 37 | .setCreationDateTime(LocalDateTime.of(2012, 11, 5, 10, 44, 30)), 38 | new Info() 39 | .setId(122L) 40 | .setInfoText("Info 7892") 41 | .setCreationDateTime(LocalDateTime.of(2013, 12, 10, 13, 33, 12)) 42 | ); 43 | } 44 | 45 | @Override 46 | public Info update(Info info) { 47 | return new Info() 48 | .setId(info.getId()) 49 | .setInfoText("Info 1234 Updated") 50 | .setCreationDateTime(LocalDateTime.of(2012, 11, 12, 19, 35, 10)); 51 | } 52 | 53 | @Override 54 | public void delete(Long id) { 55 | // delete info by id 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /springfox-swagger2-example/src/main/java/com/edwise/pocs/swagger2/service/InfoService.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.swagger2.service; 2 | 3 | import com.edwise.pocs.swagger2.entity.Info; 4 | 5 | import java.util.List; 6 | 7 | public interface InfoService { 8 | 9 | Info save(Info entity); 10 | 11 | Info findOne(Long id); 12 | 13 | List findAll(); 14 | 15 | Info update(Info info); 16 | 17 | void delete(Long id); 18 | } 19 | -------------------------------------------------------------------------------- /springfox-swagger2-example/src/main/java/com/edwise/pocs/swagger2/service/impl/InfoServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.swagger2.service.impl; 2 | 3 | import com.edwise.pocs.swagger2.entity.Info; 4 | import com.edwise.pocs.swagger2.repository.InfoRepository; 5 | import com.edwise.pocs.swagger2.service.InfoService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | 11 | @Service 12 | public class InfoServiceImpl implements InfoService { 13 | 14 | @Autowired 15 | private InfoRepository infoRepository; 16 | 17 | @Override 18 | public Info save(Info info) { 19 | return infoRepository.save(info); 20 | } 21 | 22 | @Override 23 | public List findAll() { 24 | return infoRepository.findAll(); 25 | } 26 | 27 | @Override 28 | public Info findOne(Long id) { 29 | return infoRepository.findOne(id); 30 | } 31 | 32 | @Override 33 | public Info update(Info info) { 34 | return infoRepository.update(info); 35 | } 36 | 37 | @Override 38 | public void delete(Long id) { 39 | infoRepository.delete(id); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /springfox-swagger2-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.jackson.serialization.write_dates_as_timestamps=false -------------------------------------------------------------------------------- /springmvc-swagger-example/README.md: -------------------------------------------------------------------------------- 1 | POC de Swagger, documentando un servicio REST implementado en Spring, creado para el post http://anotherdayanotherbug.wordpress.com/2014/09/27/documentar-un-servicio-rest-con-swagger/ 2 | 3 | 4 | -------------------------------------------------------------------------------- /springmvc-swagger-example/src/main/java/com/edwise/pocs/swagger/Application.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.swagger; 2 | 3 | import com.fasterxml.jackson.databind.Module; 4 | import com.fasterxml.jackson.datatype.joda.JodaModule; 5 | import com.mangofactory.swagger.plugin.EnableSwagger; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.ComponentScan; 10 | import org.springframework.context.annotation.Configuration; 11 | 12 | @Configuration 13 | @EnableAutoConfiguration 14 | @ComponentScan 15 | @EnableSwagger 16 | public class Application { 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(Application.class, args); 20 | } 21 | 22 | @Bean 23 | public Module jodaModule() { 24 | return new JodaModule(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /springmvc-swagger-example/src/main/java/com/edwise/pocs/swagger/config/SwaggerSpringMvcConfig.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.swagger.config; 2 | 3 | import com.mangofactory.swagger.configuration.SpringSwaggerConfig; 4 | import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin; 5 | import com.wordnik.swagger.model.ApiInfo; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | @Configuration 11 | public class SwaggerSpringMvcConfig { 12 | 13 | private SpringSwaggerConfig springSwaggerConfig; 14 | 15 | @Autowired 16 | public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) { 17 | this.springSwaggerConfig = springSwaggerConfig; 18 | } 19 | 20 | @Bean 21 | public SwaggerSpringMvcPlugin customImplementation() { 22 | return new SwaggerSpringMvcPlugin(this.springSwaggerConfig) 23 | .apiInfo(apiInfo()) 24 | .includePatterns(".*api/*.*"); 25 | } 26 | 27 | private ApiInfo apiInfo() { 28 | return new ApiInfo( 29 | "Users API", 30 | "Your user database!", 31 | "http://userweb.userapi.com/Terms_of_service", 32 | "userapi.manager@gmail.com", 33 | "Apache 2.0", 34 | "http://www.apache.org/licenses/LICENSE-2.0.html" 35 | ); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /springmvc-swagger-example/src/main/java/com/edwise/pocs/swagger/services/UserService.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.swagger.services; 2 | 3 | import com.edwise.pocs.swagger.entities.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserService { 8 | 9 | User findById(Long id); 10 | 11 | List findAll(); 12 | 13 | User save(User user); 14 | 15 | User update(User user); 16 | 17 | void delete(Long id); 18 | } 19 | -------------------------------------------------------------------------------- /springmvc-swagger-example/src/main/webapp/swagger/css/reset.css: -------------------------------------------------------------------------------- 1 | /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 */ 2 | html, 3 | body, 4 | div, 5 | span, 6 | applet, 7 | object, 8 | iframe, 9 | h1, 10 | h2, 11 | h3, 12 | h4, 13 | h5, 14 | h6, 15 | p, 16 | blockquote, 17 | pre, 18 | a, 19 | abbr, 20 | acronym, 21 | address, 22 | big, 23 | cite, 24 | code, 25 | del, 26 | dfn, 27 | em, 28 | img, 29 | ins, 30 | kbd, 31 | q, 32 | s, 33 | samp, 34 | small, 35 | strike, 36 | strong, 37 | sub, 38 | sup, 39 | tt, 40 | var, 41 | b, 42 | u, 43 | i, 44 | center, 45 | dl, 46 | dt, 47 | dd, 48 | ol, 49 | ul, 50 | li, 51 | fieldset, 52 | form, 53 | label, 54 | legend, 55 | table, 56 | caption, 57 | tbody, 58 | tfoot, 59 | thead, 60 | tr, 61 | th, 62 | td, 63 | article, 64 | aside, 65 | canvas, 66 | details, 67 | embed, 68 | figure, 69 | figcaption, 70 | footer, 71 | header, 72 | hgroup, 73 | menu, 74 | nav, 75 | output, 76 | ruby, 77 | section, 78 | summary, 79 | time, 80 | mark, 81 | audio, 82 | video { 83 | margin: 0; 84 | padding: 0; 85 | border: 0; 86 | font-size: 100%; 87 | font: inherit; 88 | vertical-align: baseline; 89 | } 90 | /* HTML5 display-role reset for older browsers */ 91 | article, 92 | aside, 93 | details, 94 | figcaption, 95 | figure, 96 | footer, 97 | header, 98 | hgroup, 99 | menu, 100 | nav, 101 | section { 102 | display: block; 103 | } 104 | body { 105 | line-height: 1; 106 | } 107 | ol, 108 | ul { 109 | list-style: none; 110 | } 111 | blockquote, 112 | q { 113 | quotes: none; 114 | } 115 | blockquote:before, 116 | blockquote:after, 117 | q:before, 118 | q:after { 119 | content: ''; 120 | content: none; 121 | } 122 | table { 123 | border-collapse: collapse; 124 | border-spacing: 0; 125 | } 126 | -------------------------------------------------------------------------------- /springmvc-swagger-example/src/main/webapp/swagger/images/explorer_icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edwise/java-pocs/ef99aa7f9c0c84552aa7507a4f993f226aaf2b5b/springmvc-swagger-example/src/main/webapp/swagger/images/explorer_icons.png -------------------------------------------------------------------------------- /springmvc-swagger-example/src/main/webapp/swagger/images/logo_small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edwise/java-pocs/ef99aa7f9c0c84552aa7507a4f993f226aaf2b5b/springmvc-swagger-example/src/main/webapp/swagger/images/logo_small.png -------------------------------------------------------------------------------- /springmvc-swagger-example/src/main/webapp/swagger/images/pet_store_api.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edwise/java-pocs/ef99aa7f9c0c84552aa7507a4f993f226aaf2b5b/springmvc-swagger-example/src/main/webapp/swagger/images/pet_store_api.png -------------------------------------------------------------------------------- /springmvc-swagger-example/src/main/webapp/swagger/images/throbber.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edwise/java-pocs/ef99aa7f9c0c84552aa7507a4f993f226aaf2b5b/springmvc-swagger-example/src/main/webapp/swagger/images/throbber.gif -------------------------------------------------------------------------------- /springmvc-swagger-example/src/main/webapp/swagger/images/wordnik_api.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/edwise/java-pocs/ef99aa7f9c0c84552aa7507a4f993f226aaf2b5b/springmvc-swagger-example/src/main/webapp/swagger/images/wordnik_api.png -------------------------------------------------------------------------------- /springmvc-swagger-example/src/main/webapp/swagger/lib/jquery.slideto.min.js: -------------------------------------------------------------------------------- 1 | (function(b){b.fn.slideto=function(a){a=b.extend({slide_duration:"slow",highlight_duration:3E3,highlight:true,highlight_color:"#FFFF99"},a);return this.each(function(){obj=b(this);b("body").animate({scrollTop:obj.offset().top},a.slide_duration,function(){a.highlight&&b.ui.version&&obj.effect("highlight",{color:a.highlight_color},a.highlight_duration)})})}})(jQuery); 2 | -------------------------------------------------------------------------------- /springmvc-swagger-example/src/main/webapp/swagger/lib/jquery.wiggle.min.js: -------------------------------------------------------------------------------- 1 | /* 2 | jQuery Wiggle 3 | Author: WonderGroup, Jordan Thomas 4 | URL: http://labs.wondergroup.com/demos/mini-ui/index.html 5 | License: MIT (http://en.wikipedia.org/wiki/MIT_License) 6 | */ 7 | jQuery.fn.wiggle=function(o){var d={speed:50,wiggles:3,travel:5,callback:null};var o=jQuery.extend(d,o);return this.each(function(){var cache=this;var wrap=jQuery(this).wrap('
').css("position","relative");var calls=0;for(i=1;i<=o.wiggles;i++){jQuery(this).animate({left:"-="+o.travel},o.speed).animate({left:"+="+o.travel*2},o.speed*2).animate({left:"-="+o.travel},o.speed,function(){calls++;if(jQuery(cache).parent().hasClass('wiggle-wrap')){jQuery(cache).parent().replaceWith(cache);} 8 | if(calls==o.wiggles&&jQuery.isFunction(o.callback)){o.callback();}});}});}; -------------------------------------------------------------------------------- /springmvc-swagger-example/src/main/webapp/swagger/o2c.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /springmvc-swagger-example/src/test/java/com/edwise/pocs/swagger/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.swagger; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.SpringApplicationConfiguration; 6 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 7 | import org.springframework.test.context.web.WebAppConfiguration; 8 | 9 | @RunWith(SpringJUnit4ClassRunner.class) 10 | @SpringApplicationConfiguration(classes = Application.class) 11 | @WebAppConfiguration 12 | public class ApplicationTest { 13 | 14 | @Test 15 | public void contextLoads() { 16 | } 17 | } -------------------------------------------------------------------------------- /springmvc-swagger-example/src/test/java/com/edwise/pocs/swagger/entities/UserTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.swagger.entities; 2 | 3 | import org.joda.time.LocalDate; 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class UserTest { 9 | private static final long USER_ID_12 = 12L; 10 | private static final String NAME_GANDALF = "Gandalf"; 11 | private static final int TYPE_1 = 1; 12 | private static final String PHONE_661534411 = "661534411"; 13 | private static final String STRING_DATE_19110102 = "1911-01-02"; 14 | 15 | @Test 16 | public void copyFrom() { 17 | User user = createUser(USER_ID_12, NAME_GANDALF, TYPE_1, PHONE_661534411, STRING_DATE_19110102); 18 | User userTo = createUser(USER_ID_12, null, null, null, null); 19 | 20 | userTo.copyFrom(user); 21 | 22 | assertEquals(userTo, user); 23 | } 24 | 25 | private User createUser(Long id, String name, Integer type, String phone, String stringDate) { 26 | User user = new User(); 27 | user.setId(id); 28 | user.setName(name); 29 | user.setType(type); 30 | user.setPhone(phone); 31 | if (stringDate != null) { 32 | user.setBirthDate(LocalDate.parse(stringDate)); 33 | } 34 | 35 | return user; 36 | } 37 | } -------------------------------------------------------------------------------- /springmvc-swagger-example/src/test/java/com/edwise/pocs/swagger/impl/UserServiceMockTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.swagger.impl; 2 | 3 | import com.edwise.pocs.swagger.entities.User; 4 | import com.edwise.pocs.swagger.services.UserService; 5 | import com.edwise.pocs.swagger.services.impl.UserServiceMock; 6 | import org.joda.time.LocalDate; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | import java.util.List; 11 | 12 | import static org.hamcrest.Matchers.hasSize; 13 | import static org.hamcrest.Matchers.is; 14 | import static org.junit.Assert.assertThat; 15 | import static org.junit.Assert.assertNotNull; 16 | 17 | public class UserServiceMockTest { 18 | private static final long USER_ID_12 = 12l; 19 | private static final String NAME_GANDALF = "Gandalf"; 20 | private static final int TYPE_1 = 1; 21 | private static final String PHONE_661534411 = "661534411"; 22 | private static final String STRING_DATE_19110102 = "1911-01-02"; 23 | 24 | private UserService userService; 25 | 26 | @Before 27 | public void setUp() { 28 | userService = new UserServiceMock(); 29 | } 30 | 31 | @Test 32 | public void findById() { 33 | User user = userService.findById(USER_ID_12); 34 | 35 | assertNotNull(user); 36 | assertThat(user.getId(), is(USER_ID_12)); 37 | } 38 | 39 | @Test 40 | public void findAll() { 41 | List users = userService.findAll(); 42 | 43 | assertNotNull(users); 44 | assertThat(users, hasSize(3)); 45 | } 46 | 47 | @Test 48 | public void save() { 49 | User user = userService.save( 50 | createUser(null, NAME_GANDALF, TYPE_1, PHONE_661534411, STRING_DATE_19110102) 51 | ); 52 | 53 | assertNotNull(user); 54 | assertNotNull(user.getId()); 55 | } 56 | 57 | @Test 58 | public void update() { 59 | User user = userService.update( 60 | createUser(USER_ID_12, NAME_GANDALF, TYPE_1, PHONE_661534411, STRING_DATE_19110102) 61 | ); 62 | 63 | assertNotNull(user); 64 | assertThat(user.getId(), is(USER_ID_12)); 65 | } 66 | 67 | @Test 68 | public void delete() { 69 | userService.delete(USER_ID_12); 70 | } 71 | 72 | private User createUser(Long id, String name, int type, String phone, String stringDate) { 73 | User user = new User(); 74 | user.setId(id); 75 | user.setName(name); 76 | user.setType(type); 77 | user.setPhone(phone); 78 | user.setBirthDate(LocalDate.parse(stringDate)); 79 | 80 | return user; 81 | } 82 | } -------------------------------------------------------------------------------- /spy-mockito-example/README.md: -------------------------------------------------------------------------------- 1 | Proyecto de ejemplo de uso de Spy de mockito, creado para el post: https://anotherdayanotherbug.wordpress.com/2015/10/13/unit-testing-como-mockear-un-metodo-de-una-clase-padre/ -------------------------------------------------------------------------------- /spy-mockito-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.edwise.pocs.spymockito 7 | spy-mockito-example 8 | 1.0 9 | jar 10 | 11 | spy-mockito-example 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.12 24 | test 25 | 26 | 27 | 28 | org.mockito 29 | mockito-core 30 | 1.10.19 31 | test 32 | 33 | 34 | 35 | org.assertj 36 | assertj-core 37 | 3.2.0 38 | test 39 | 40 | 41 | 42 | 43 | 44 | 45 | org.apache.maven.plugins 46 | maven-compiler-plugin 47 | 3.3 48 | 49 | ${java.version} 50 | ${java.version} 51 | ${java.version} 52 | ${java.version} 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /spy-mockito-example/src/main/java/com/edwise/pocs/spymockito/Bar.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.spymockito; 2 | 3 | public class Bar extends Foo { 4 | 5 | public int myMethod() { 6 | System.out.println("Method to test!"); 7 | String msg = this.doSomethingWithDatabase(); 8 | System.out.println("Msg: ".concat(msg)); 9 | 10 | return 1; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spy-mockito-example/src/main/java/com/edwise/pocs/spymockito/BarWellDesigned.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.spymockito; 2 | 3 | public class BarWellDesigned { 4 | 5 | private Foo foo; 6 | 7 | public BarWellDesigned(Foo foo) { 8 | this.foo = foo; 9 | } 10 | 11 | public int myMethod() { 12 | System.out.println("Method to test!"); 13 | String msg = foo.doSomethingWithDatabase(); 14 | System.out.println("Msg: ".concat(msg)); 15 | 16 | return 1; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /spy-mockito-example/src/main/java/com/edwise/pocs/spymockito/Foo.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.spymockito; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | public class Foo { 6 | 7 | public String doSomethingWithDatabase() { 8 | System.out.println("Method that connect with DB..."); 9 | try { 10 | TimeUnit.SECONDS.sleep(3); 11 | } catch (InterruptedException e) { 12 | e.printStackTrace(); 13 | } 14 | System.out.println("Finished connection whit DB."); 15 | 16 | return "OK real Foo"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /spy-mockito-example/src/test/java/com/edwise/pocs/spymockito/BarTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.spymockito; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | import static org.mockito.Mockito.doReturn; 7 | import static org.mockito.Mockito.spy; 8 | 9 | public class BarTest { 10 | 11 | private Bar bar; 12 | 13 | @Test 14 | public void testMyMethodWithoutMockito() { 15 | bar = new Bar(); 16 | 17 | int result = bar.myMethod(); 18 | 19 | assertThat(result).isEqualTo(1); 20 | } 21 | 22 | @Test 23 | public void testMyMethodWithMockitoSpy() { 24 | bar = spy(new Bar()); 25 | doReturn("OK Foo mocked").when(bar).doSomethingWithDatabase(); 26 | // when(Foo.doSomethingWithDatabase()).thenReturn("OK"); 27 | 28 | int result = bar.myMethod(); 29 | 30 | assertThat(result).isEqualTo(1); 31 | } 32 | } -------------------------------------------------------------------------------- /spy-mockito-example/src/test/java/com/edwise/pocs/spymockito/BarWellDesignedTest.java: -------------------------------------------------------------------------------- 1 | package com.edwise.pocs.spymockito; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | import static org.mockito.Mockito.mock; 7 | import static org.mockito.Mockito.when; 8 | 9 | public class BarWellDesignedTest { 10 | 11 | private BarWellDesigned barWellDesigned; 12 | 13 | @Test 14 | public void testMyMethodWithoutMockito() { 15 | Foo foo = mock(Foo.class); 16 | barWellDesigned = new BarWellDesigned(foo); 17 | when(foo.doSomethingWithDatabase()).thenReturn("OK Foo mocked"); 18 | 19 | int result = barWellDesigned.myMethod(); 20 | 21 | assertThat(result).isEqualTo(1); 22 | } 23 | } --------------------------------------------------------------------------------