From 012c924afb48d588274ee9aedce37bd7724e7629 Mon Sep 17 00:00:00 2001 From: apeterlic Date: Sat, 16 Jul 2022 07:46:52 +0200 Subject: [PATCH 01/21] Add Maven Snapshot Repository vs Release Repository --- .../maven-download-artifacts/pom.xml | 52 +++++++++++++++++++ .../maven-release-repository/pom.xml | 38 ++++++++++++++ .../maven-snapshot-repository/pom.xml | 38 ++++++++++++++ maven-modules/maven-repositories/pom.xml | 23 ++++++++ maven-modules/pom.xml | 1 + 5 files changed, 152 insertions(+) create mode 100644 maven-modules/maven-repositories/maven-download-artifacts/pom.xml create mode 100644 maven-modules/maven-repositories/maven-release-repository/pom.xml create mode 100644 maven-modules/maven-repositories/maven-snapshot-repository/pom.xml create mode 100644 maven-modules/maven-repositories/pom.xml diff --git a/maven-modules/maven-repositories/maven-download-artifacts/pom.xml b/maven-modules/maven-repositories/maven-download-artifacts/pom.xml new file mode 100644 index 0000000000..678fc32bec --- /dev/null +++ b/maven-modules/maven-repositories/maven-download-artifacts/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + maven-repository-download + Maven Release Repository + 1.0.0-SNAPSHOT + + + com.baeldung + maven-repositories + 1.0.0-SNAPSHOT + + + + + nexus-snapshot + nexus-snapshot + http://localhost:8081/repository/maven-snapshots/ + + true + + + false + + + + nexus-release + nexus-release + http://localhost:8081/repository/maven-releases/ + + false + + + + + + + com.baeldung + maven-release-repository + 1.0.0 + + + + com.baeldung + maven-snapshot-repository + 1.0.0-SNAPSHOT + + + + \ No newline at end of file diff --git a/maven-modules/maven-repositories/maven-release-repository/pom.xml b/maven-modules/maven-repositories/maven-release-repository/pom.xml new file mode 100644 index 0000000000..bd7adcbcce --- /dev/null +++ b/maven-modules/maven-repositories/maven-release-repository/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + maven-release-repository + Maven Release Repository + 1.0.0 + + + com.baeldung + maven-repositories + 1.0.0-SNAPSHOT + + + + + nexus + nexus-release + http://localhost:8081/repository/maven-releases/ + + false + + + true + + + + + + + nexus + nexus-release + http://localhost:8081/repository/maven-releases/ + + + + \ No newline at end of file diff --git a/maven-modules/maven-repositories/maven-snapshot-repository/pom.xml b/maven-modules/maven-repositories/maven-snapshot-repository/pom.xml new file mode 100644 index 0000000000..5409e47b8e --- /dev/null +++ b/maven-modules/maven-repositories/maven-snapshot-repository/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + maven-snapshot-repository + Maven Snapshot Repository + 1.0.0-SNAPSHOT + + + com.baeldung + maven-repositories + 1.0.0-SNAPSHOT + + + + + nexus + nexus-snapshot + http://localhost:8081/repository/maven-snapshots/ + + true + + + false + + + + + + + nexus + nexus-snapshot + http://localhost:8081/repository/maven-snapshots/ + + + + \ No newline at end of file diff --git a/maven-modules/maven-repositories/pom.xml b/maven-modules/maven-repositories/pom.xml new file mode 100644 index 0000000000..ba39d00d65 --- /dev/null +++ b/maven-modules/maven-repositories/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + maven-repositories + 1.0.0-SNAPSHOT + Maven Repositories + pom + + + com.baeldung + maven-modules + 0.0.1-SNAPSHOT + + + + maven-release-repository + maven-snapshot-repository + maven-download-artifacts + + + \ No newline at end of file diff --git a/maven-modules/pom.xml b/maven-modules/pom.xml index 253f5d9fa0..412e26f041 100644 --- a/maven-modules/pom.xml +++ b/maven-modules/pom.xml @@ -41,6 +41,7 @@ maven-parent-pom-resolution maven-simple maven-classifier + maven-repositories From 27a3dd08d9d4f1ab30563f8de50e7a4651e34b1f Mon Sep 17 00:00:00 2001 From: panagiotiskakos Date: Sat, 16 Jul 2022 20:30:06 +0300 Subject: [PATCH 02/21] [JAVA-12192] Renamed to *ManualTest --- ...ationTest.java => MicrometerAtlasIntegrationManualTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename metrics/src/test/java/com/baeldung/metrics/micrometer/{MicrometerAtlasIntegrationTest.java => MicrometerAtlasIntegrationManualTest.java} (99%) diff --git a/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationTest.java b/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationManualTest.java similarity index 99% rename from metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationTest.java rename to metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationManualTest.java index 02ef926794..cb679b73fb 100644 --- a/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationTest.java +++ b/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationManualTest.java @@ -37,7 +37,7 @@ import com.netflix.spectator.atlas.AtlasConfig; /** * @author aiet */ -public class MicrometerAtlasIntegrationTest { +public class MicrometerAtlasIntegrationManualTest { private AtlasConfig atlasConfig; From 4b80c468f1bfb585f3c05e82a5093626a02159be Mon Sep 17 00:00:00 2001 From: lucaCambi77 Date: Thu, 21 Jul 2022 02:13:46 +0200 Subject: [PATCH 03/21] multiple compose use same network (#12512) --- .../docker-compose-my-app-service.yaml | 13 +++++++++++++ .../docker-compose-redis-service.yaml | 13 +++++++++++++ .../docker-compose-my-app-service.yaml | 12 ++++++++++++ .../docker-compose-redis-service.yaml | 12 ++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 docker-modules/docker-compose/two-compose-share-same-network/create-network-from-service/docker-compose-my-app-service.yaml create mode 100644 docker-modules/docker-compose/two-compose-share-same-network/create-network-from-service/docker-compose-redis-service.yaml create mode 100644 docker-modules/docker-compose/two-compose-share-same-network/join-existing-network/docker-compose-my-app-service.yaml create mode 100644 docker-modules/docker-compose/two-compose-share-same-network/join-existing-network/docker-compose-redis-service.yaml diff --git a/docker-modules/docker-compose/two-compose-share-same-network/create-network-from-service/docker-compose-my-app-service.yaml b/docker-modules/docker-compose/two-compose-share-same-network/create-network-from-service/docker-compose-my-app-service.yaml new file mode 100644 index 0000000000..42e9eec8a1 --- /dev/null +++ b/docker-modules/docker-compose/two-compose-share-same-network/create-network-from-service/docker-compose-my-app-service.yaml @@ -0,0 +1,13 @@ +services: + my_app: + image: web-app:latest + container_name: web-app + ports: + - "8080:8080" + networks: + - my-app + +networks: + my-app: + name: redis_network + external: true \ No newline at end of file diff --git a/docker-modules/docker-compose/two-compose-share-same-network/create-network-from-service/docker-compose-redis-service.yaml b/docker-modules/docker-compose/two-compose-share-same-network/create-network-from-service/docker-compose-redis-service.yaml new file mode 100644 index 0000000000..ef9573a803 --- /dev/null +++ b/docker-modules/docker-compose/two-compose-share-same-network/create-network-from-service/docker-compose-redis-service.yaml @@ -0,0 +1,13 @@ +services: + redis: + image: redis:latest + container_name: redis + ports: + - '6379:6379' + networks: + - network + +networks: + network: + driver: bridge + name: redis_network diff --git a/docker-modules/docker-compose/two-compose-share-same-network/join-existing-network/docker-compose-my-app-service.yaml b/docker-modules/docker-compose/two-compose-share-same-network/join-existing-network/docker-compose-my-app-service.yaml new file mode 100644 index 0000000000..f3310108ce --- /dev/null +++ b/docker-modules/docker-compose/two-compose-share-same-network/join-existing-network/docker-compose-my-app-service.yaml @@ -0,0 +1,12 @@ +services: + my_app: + image: web-app:latest + container_name: web-app + ports: + - "8080:8080" + networks: + - network-example + +networks: + network-example: + external: true \ No newline at end of file diff --git a/docker-modules/docker-compose/two-compose-share-same-network/join-existing-network/docker-compose-redis-service.yaml b/docker-modules/docker-compose/two-compose-share-same-network/join-existing-network/docker-compose-redis-service.yaml new file mode 100644 index 0000000000..2cf44a05d6 --- /dev/null +++ b/docker-modules/docker-compose/two-compose-share-same-network/join-existing-network/docker-compose-redis-service.yaml @@ -0,0 +1,12 @@ +services: + redis: + image: redis:latest + container_name: redis + ports: + - '6379:6379' + networks: + - network-example + +networks: + network-example: + external: true From 86b509da2a243adc6cb3510838743342ff14add7 Mon Sep 17 00:00:00 2001 From: panagiotiskakos Date: Thu, 21 Jul 2022 07:01:24 +0300 Subject: [PATCH 04/21] [JAVA-12192] Renamed to *ManualTest --- ...ntegrationManualTest.java => MicrometerAtlasManualTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename metrics/src/test/java/com/baeldung/metrics/micrometer/{MicrometerAtlasIntegrationManualTest.java => MicrometerAtlasManualTest.java} (99%) diff --git a/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationManualTest.java b/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasManualTest.java similarity index 99% rename from metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationManualTest.java rename to metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasManualTest.java index cb679b73fb..8341befb83 100644 --- a/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationManualTest.java +++ b/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasManualTest.java @@ -37,7 +37,7 @@ import com.netflix.spectator.atlas.AtlasConfig; /** * @author aiet */ -public class MicrometerAtlasIntegrationManualTest { +public class MicrometerAtlasManualTest { private AtlasConfig atlasConfig; From 85637639447cffa746215886419c70628422baca Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Thu, 21 Jul 2022 12:13:17 +0500 Subject: [PATCH 05/21] Updated README.md added link back to the article: https://www.baeldung.com/spring-boot-redis-testcontainers --- spring-boot-modules/spring-boot-testing-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-boot-modules/spring-boot-testing-2/README.md b/spring-boot-modules/spring-boot-testing-2/README.md index a32469fb2f..03ff1ab607 100644 --- a/spring-boot-modules/spring-boot-testing-2/README.md +++ b/spring-boot-modules/spring-boot-testing-2/README.md @@ -11,4 +11,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Setting the Log Level in Spring Boot when Testing](https://www.baeldung.com/spring-boot-testing-log-level) - [Failed to Load ApplicationContext for JUnit Test of Spring Controller](https://www.baeldung.com/spring-junit-failed-to-load-applicationcontext) - [Spring Web Service Integration Tests with @WebServiceServerTest](https://www.baeldung.com/spring-webserviceservertest) +- [Spring Boot – Testing Redis With Testcontainers](https://www.baeldung.com/spring-boot-redis-testcontainers) - More articles: [[<-- prev]](../spring-boot-testing) From d576113964420f486d3c8706d0181af00fcd3e77 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Thu, 21 Jul 2022 12:20:38 +0500 Subject: [PATCH 06/21] Updated README.md added link back to the article: https://www.baeldung.com/spring-boot-keycloak-integration-testing --- spring-boot-modules/spring-boot-testing-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-boot-modules/spring-boot-testing-2/README.md b/spring-boot-modules/spring-boot-testing-2/README.md index 03ff1ab607..bb504aeee0 100644 --- a/spring-boot-modules/spring-boot-testing-2/README.md +++ b/spring-boot-modules/spring-boot-testing-2/README.md @@ -12,4 +12,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Failed to Load ApplicationContext for JUnit Test of Spring Controller](https://www.baeldung.com/spring-junit-failed-to-load-applicationcontext) - [Spring Web Service Integration Tests with @WebServiceServerTest](https://www.baeldung.com/spring-webserviceservertest) - [Spring Boot – Testing Redis With Testcontainers](https://www.baeldung.com/spring-boot-redis-testcontainers) +- [Spring Boot – Keycloak Integration Testing with Testcontainers](https://www.baeldung.com/spring-boot-keycloak-integration-testing) - More articles: [[<-- prev]](../spring-boot-testing) From 1be9c468a47e36471278ec63e29f963be3656a5e Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Thu, 21 Jul 2022 13:51:43 +0500 Subject: [PATCH 07/21] Updated README.md added link back to the article: https://www.baeldung.com/java-validate-xml-xsd --- xml-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/xml-2/README.md b/xml-2/README.md index bc599f8480..cfbd9d9911 100644 --- a/xml-2/README.md +++ b/xml-2/README.md @@ -5,3 +5,4 @@ This module contains articles about eXtensible Markup Language (XML) ### Relevant Articles: - [Pretty-Print XML in Java](https://www.baeldung.com/java-pretty-print-xml) +- [Validate an XML File against an XSD File](https://www.baeldung.com/java-validate-xml-xsd) From 318f2fc9737384d579fcc6fd1467f74b47f7f2cb Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Thu, 21 Jul 2022 13:59:02 +0500 Subject: [PATCH 08/21] Updated README.md added link back to the article: https://www.baeldung.com/spring-data-rest-serialize-entity-id --- persistence-modules/spring-data-rest-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/persistence-modules/spring-data-rest-2/README.md b/persistence-modules/spring-data-rest-2/README.md index 04a54291b0..1f0191cac9 100644 --- a/persistence-modules/spring-data-rest-2/README.md +++ b/persistence-modules/spring-data-rest-2/README.md @@ -6,6 +6,7 @@ This module contains articles about Spring Data REST - [Guide to Spring Data REST Validators](https://www.baeldung.com/spring-data-rest-validators) - [Spring Data Web Support](https://www.baeldung.com/spring-data-web-support) - [Spring REST and HAL Browser](https://www.baeldung.com/spring-rest-hal) +- [Spring Data Rest – Serializing the Entity ID](https://www.baeldung.com/spring-data-rest-serialize-entity-id) ### The Course The "REST With Spring" Classes: http://bit.ly/restwithspring From c46c863dc4acee9f86848e16558b9cd53dec6c9e Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Thu, 21 Jul 2022 14:02:56 +0500 Subject: [PATCH 09/21] Created README.md added link back to the article: https://www.baeldung.com/ops/docker-compose-links-depends-on --- docker-modules/docker-compose/depends-on_vs_links/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 docker-modules/docker-compose/depends-on_vs_links/README.md diff --git a/docker-modules/docker-compose/depends-on_vs_links/README.md b/docker-modules/docker-compose/depends-on_vs_links/README.md new file mode 100644 index 0000000000..4496bda1a7 --- /dev/null +++ b/docker-modules/docker-compose/depends-on_vs_links/README.md @@ -0,0 +1,3 @@ + +### Relevant Articles: +- [Difference Between links and depends_on in Docker Compose](https://www.baeldung.com/ops/docker-compose-links-depends-on) From 24e8e8f3427825a6e3c2726e321a1faa42815bce Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Thu, 21 Jul 2022 14:06:29 +0500 Subject: [PATCH 10/21] Updated README.md added link back to the article: https://www.baeldung.com/mongodb-query-documents-id --- persistence-modules/java-mongodb-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/persistence-modules/java-mongodb-3/README.md b/persistence-modules/java-mongodb-3/README.md index 3ffe5980da..d76d53b06a 100644 --- a/persistence-modules/java-mongodb-3/README.md +++ b/persistence-modules/java-mongodb-3/README.md @@ -1,3 +1,4 @@ ### Relevant Artilces: - [Guide to Find in MongoDB](https://www.baeldung.com/mongodb-find) +- [Query Documents using Document ID in MongoDB](https://www.baeldung.com/mongodb-query-documents-id) From f678e8b0341d8899175ef91bb65f4dd9fb402c6a Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Thu, 21 Jul 2022 14:10:40 +0500 Subject: [PATCH 11/21] Updated README.md added link back to the article: https://www.baeldung.com/java-notnull-method-parameter --- javaxval/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/javaxval/README.md b/javaxval/README.md index 8fc5b07ccf..346c4268c1 100644 --- a/javaxval/README.md +++ b/javaxval/README.md @@ -13,3 +13,4 @@ This module contains articles about Bean Validation. - [Guide to ParameterMessageInterpolator](https://www.baeldung.com/hibernate-parametermessageinterpolator) - [Hibernate Validator Annotation Processor in Depth](https://www.baeldung.com/hibernate-validator-annotation-processor) - [Constraint Composition with Bean Validation](https://www.baeldung.com/java-bean-validation-constraint-composition) +- [Using @NotNull on a Method Parameter](https://www.baeldung.com/java-notnull-method-parameter) From dc085496a4e5c08db9e222fd09740723b094e339 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Thu, 21 Jul 2022 14:14:58 +0500 Subject: [PATCH 12/21] Updated README.md added link back to the article: https://www.baeldung.com/spring-data-mongodb-count --- persistence-modules/spring-boot-persistence-mongodb-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/README.md b/persistence-modules/spring-boot-persistence-mongodb-2/README.md index f3be63c8b5..6a98f05520 100644 --- a/persistence-modules/spring-boot-persistence-mongodb-2/README.md +++ b/persistence-modules/spring-boot-persistence-mongodb-2/README.md @@ -4,4 +4,5 @@ - [Configure MongoDB Collection Name for a Class in Spring Data](https://www.baeldung.com/spring-data-mongodb-collection-name) - [MongoDB Composite Key With Spring Data](https://www.baeldung.com/spring-data-mongodb-composite-key) - [Unique Field in MongoDB Document in Spring Data](https://www.baeldung.com/spring-data-mongodb-unique) +- [Count Documents Using Spring Data MongoDB Repository](https://www.baeldung.com/spring-data-mongodb-count) - More articles: [[<--prev]](../spring-boot-persistence-mongodb) From 450019f48048372fc5b77d65268de6331cb4b806 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Thu, 21 Jul 2022 14:17:28 +0500 Subject: [PATCH 13/21] Updated README.md added link back to the article: https://www.baeldung.com/ops/docker-compose-communication --- docker-modules/docker-compose/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-modules/docker-compose/README.md b/docker-modules/docker-compose/README.md index fbb58107d3..17b993ae6b 100644 --- a/docker-modules/docker-compose/README.md +++ b/docker-modules/docker-compose/README.md @@ -2,4 +2,5 @@ - [Introduction to Docker Compose](https://www.baeldung.com/ops/docker-compose) - [How to Get Docker-Compose to Always Use the Latest Image](https://www.baeldung.com/ops/docker-compose-latest-image) +- [Communication Between Multiple Docker Compose Projects](https://www.baeldung.com/ops/docker-compose-communication) From 299348e8595451210fa52f6f0ede9e587cd0ee06 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Thu, 21 Jul 2022 21:39:20 +0500 Subject: [PATCH 14/21] Removed new README file Removed new README file from /depends-on_vs_links --- docker-modules/docker-compose/depends-on_vs_links/README.md | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 docker-modules/docker-compose/depends-on_vs_links/README.md diff --git a/docker-modules/docker-compose/depends-on_vs_links/README.md b/docker-modules/docker-compose/depends-on_vs_links/README.md deleted file mode 100644 index 4496bda1a7..0000000000 --- a/docker-modules/docker-compose/depends-on_vs_links/README.md +++ /dev/null @@ -1,3 +0,0 @@ - -### Relevant Articles: -- [Difference Between links and depends_on in Docker Compose](https://www.baeldung.com/ops/docker-compose-links-depends-on) From 310c77817d43dcaa75d24277789cda3522ba0a04 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Thu, 21 Jul 2022 21:40:44 +0500 Subject: [PATCH 15/21] Updated README.md added link back to the article: https://www.baeldung.com/ops/docker-compose-links-depends-on --- docker-modules/docker-compose/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-modules/docker-compose/README.md b/docker-modules/docker-compose/README.md index 17b993ae6b..10554a319d 100644 --- a/docker-modules/docker-compose/README.md +++ b/docker-modules/docker-compose/README.md @@ -3,4 +3,5 @@ - [Introduction to Docker Compose](https://www.baeldung.com/ops/docker-compose) - [How to Get Docker-Compose to Always Use the Latest Image](https://www.baeldung.com/ops/docker-compose-latest-image) - [Communication Between Multiple Docker Compose Projects](https://www.baeldung.com/ops/docker-compose-communication) +- [Difference Between links and depends_on in Docker Compose](https://www.baeldung.com/ops/docker-compose-links-depends-on) From b23fcdc4c4cd7cfbc23f6adf4944b6b8a9da7596 Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Thu, 21 Jul 2022 23:10:50 +0530 Subject: [PATCH 16/21] JAVA-13336 Refactor mockito modules (#12517) * JAVA-13336 Refactor mockito modules * JAVA-13336 Fix Jenkins build --- testing-modules/mockito-2/.gitignore | 14 ---- testing-modules/mockito-2/README.md | 10 --- testing-modules/mockito-2/pom.xml | 31 --------- .../mockito-2/src/main/resources/logback.xml | 13 ---- testing-modules/mockito/.gitignore | 7 +- testing-modules/mockito/README.md | 14 ++-- testing-modules/mockito/pom.xml | 67 +++---------------- .../mockito/additionalanswers/Book.java | 0 .../additionalanswers/BookRepository.java | 0 .../additionalanswers/BookService.java | 0 .../com/baeldung/mockito/fluentapi/Pizza.java | 0 .../mockito/fluentapi/PizzaService.java | 0 .../baeldung/mockito/java8/JobPosition.java | 0 .../baeldung/mockito/java8/JobService.java | 0 .../com/baeldung/mockito/java8/Person.java | 0 .../mockito/java8/UnemploymentService.java | 0 .../java8/UnemploymentServiceImpl.java | 0 .../mockito/mocksettings/AbstractCoffee.java | 0 .../mockito/mocksettings/SimpleService.java | 0 .../mocksettings/SpecialInterface.java | 0 .../baeldung/mockito/objectmapper/Flower.java | 0 .../FlowerJsonStringValidator.java | 0 .../mockito/whenvsdomethods/Employee.java | 0 .../IAmOnHolidayException.java | 0 .../mockito/src/main/resources/logback.xml | 8 +-- .../BookServiceUnitTest.java | 0 .../bddmockito/BDDMockitoUnitTest.java | 0 .../bddmockito/PhoneBookRepository.java | 0 .../mockito/bddmockito/PhoneBookService.java | 0 .../fluentapi/PizzaServiceUnitTest.java | 0 .../mockito/fluentapi/PizzaUnitTest.java | 0 .../ArgumentMatcherWithLambdaUnitTest.java | 0 .../ArgumentMatcherWithoutLambdaUnitTest.java | 0 .../java8/CustomAnswerWithLambdaUnitTest.java | 0 .../CustomAnswerWithoutLambdaUnitTest.java | 0 .../mockito/java8/JobServiceUnitTest.java | 0 .../UnemploymentServiceImplUnitTest.java | 0 .../LazyVerificationUnitTest.java | 0 .../misusing/ExpectedTestFailureRule.java | 0 .../MockitoUnecessaryStubUnitTest.java | 0 .../mocksettings/MockSettingsUnitTest.java | 0 .../FlowerJsonStringValidatorUnitTest.java | 0 .../WhenVsDoMethodsUnitTest.java | 0 testing-modules/pom.xml | 1 - 44 files changed, 24 insertions(+), 141 deletions(-) delete mode 100644 testing-modules/mockito-2/.gitignore delete mode 100644 testing-modules/mockito-2/README.md delete mode 100644 testing-modules/mockito-2/pom.xml delete mode 100644 testing-modules/mockito-2/src/main/resources/logback.xml rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/additionalanswers/Book.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/additionalanswers/BookRepository.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/additionalanswers/BookService.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/fluentapi/Pizza.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/fluentapi/PizzaService.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/java8/JobPosition.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/java8/JobService.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/java8/Person.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/java8/UnemploymentService.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/java8/UnemploymentServiceImpl.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/mocksettings/AbstractCoffee.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/mocksettings/SimpleService.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/mocksettings/SpecialInterface.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/objectmapper/Flower.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/objectmapper/FlowerJsonStringValidator.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/whenvsdomethods/Employee.java (100%) rename testing-modules/{mockito-2 => mockito}/src/main/java/com/baeldung/mockito/whenvsdomethods/IAmOnHolidayException.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/additionalanswers/BookServiceUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/bddmockito/BDDMockitoUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/bddmockito/PhoneBookRepository.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/bddmockito/PhoneBookService.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/fluentapi/PizzaServiceUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/fluentapi/PizzaUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithLambdaUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/java8/JobServiceUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/java8/UnemploymentServiceImplUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/lazyverification/LazyVerificationUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/misusing/ExpectedTestFailureRule.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/misusing/MockitoUnecessaryStubUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/mocksettings/MockSettingsUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/objectmapper/FlowerJsonStringValidatorUnitTest.java (100%) rename testing-modules/{mockito-2 => mockito}/src/test/java/com/baeldung/mockito/whenvsdomethods/WhenVsDoMethodsUnitTest.java (100%) diff --git a/testing-modules/mockito-2/.gitignore b/testing-modules/mockito-2/.gitignore deleted file mode 100644 index 7f300600e6..0000000000 --- a/testing-modules/mockito-2/.gitignore +++ /dev/null @@ -1,14 +0,0 @@ -*.class - -.settings -.project - -#folders# -/target -/src/main/webapp/WEB-INF/classes -*/META-INF/* - -# Packaged files # -*.jar -*.war -*.ear diff --git a/testing-modules/mockito-2/README.md b/testing-modules/mockito-2/README.md deleted file mode 100644 index 6fd71bf305..0000000000 --- a/testing-modules/mockito-2/README.md +++ /dev/null @@ -1,10 +0,0 @@ -### Relevant articles - -- [Mockito’s Java 8 Features](https://www.baeldung.com/mockito-2-java-8) -- [Lazy Verification with Mockito 2](https://www.baeldung.com/mockito-2-lazy-verification) -- [Mockito Strict Stubbing and The UnnecessaryStubbingException](https://www.baeldung.com/mockito-unnecessary-stubbing-exception) -- [Mockito and Fluent APIs](https://www.baeldung.com/mockito-fluent-apis) -- [Mocking the ObjectMapper readValue() Method](https://www.baeldung.com/mockito-mock-jackson-read-value) -- [Introduction to Mockito’s AdditionalAnswers](https://www.baeldung.com/mockito-additionalanswers) -- [Difference Between when() and doXxx() Methods in Mockito](https://www.baeldung.com/java-mockito-when-vs-do) -- [Overview of Mockito MockSettings](https://www.baeldung.com/mockito-mocksettings) diff --git a/testing-modules/mockito-2/pom.xml b/testing-modules/mockito-2/pom.xml deleted file mode 100644 index e5812222eb..0000000000 --- a/testing-modules/mockito-2/pom.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - 4.0.0 - mockito-2 - 0.0.1-SNAPSHOT - mockito-2 - jar - - - com.baeldung - testing-modules - 1.0.0-SNAPSHOT - - - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - - - org.mockito - mockito-junit-jupiter - ${mockito.version} - test - - - - \ No newline at end of file diff --git a/testing-modules/mockito-2/src/main/resources/logback.xml b/testing-modules/mockito-2/src/main/resources/logback.xml deleted file mode 100644 index 7d900d8ea8..0000000000 --- a/testing-modules/mockito-2/src/main/resources/logback.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - \ No newline at end of file diff --git a/testing-modules/mockito/.gitignore b/testing-modules/mockito/.gitignore index 83c05e60c8..7f300600e6 100644 --- a/testing-modules/mockito/.gitignore +++ b/testing-modules/mockito/.gitignore @@ -1,13 +1,14 @@ *.class +.settings +.project + #folders# /target -/neoDb* -/data /src/main/webapp/WEB-INF/classes */META-INF/* # Packaged files # *.jar *.war -*.ear \ No newline at end of file +*.ear diff --git a/testing-modules/mockito/README.md b/testing-modules/mockito/README.md index 6cc6bb8955..2870d1afcf 100644 --- a/testing-modules/mockito/README.md +++ b/testing-modules/mockito/README.md @@ -1,8 +1,12 @@ -========= +### Relevant articles -## Mockito Cookbooks and Examples - - -### Relevant Articles: +- [Mockito’s Java 8 Features](https://www.baeldung.com/mockito-2-java-8) +- [Lazy Verification with Mockito 2](https://www.baeldung.com/mockito-2-lazy-verification) +- [Mockito Strict Stubbing and The UnnecessaryStubbingException](https://www.baeldung.com/mockito-unnecessary-stubbing-exception) +- [Mockito and Fluent APIs](https://www.baeldung.com/mockito-fluent-apis) +- [Mocking the ObjectMapper readValue() Method](https://www.baeldung.com/mockito-mock-jackson-read-value) +- [Introduction to Mockito’s AdditionalAnswers](https://www.baeldung.com/mockito-additionalanswers) +- [Difference Between when() and doXxx() Methods in Mockito](https://www.baeldung.com/java-mockito-when-vs-do) +- [Overview of Mockito MockSettings](https://www.baeldung.com/mockito-mocksettings) - [Testing Callbacks with Mockito](https://www.baeldung.com/mockito-callbacks) - [Quick Guide to BDDMockito](https://www.baeldung.com/bdd-mockito) diff --git a/testing-modules/mockito/pom.xml b/testing-modules/mockito/pom.xml index 36f4a56dff..b55245f20c 100644 --- a/testing-modules/mockito/pom.xml +++ b/testing-modules/mockito/pom.xml @@ -4,75 +4,28 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 mockito - 0.1-SNAPSHOT + 0.0.1-SNAPSHOT mockito + jar com.baeldung - parent-java - 0.0.1-SNAPSHOT - ../../parent-java + testing-modules + 1.0.0-SNAPSHOT - org.springframework - spring-core - ${spring-framework.version} + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} - org.springframework - spring-context - ${spring-framework.version} - - - org.springframework.data - spring-data-jpa - ${spring-data.version} - - - org.eclipse.persistence - javax.persistence - ${javax.persistence.version} - - - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - - - - org.springframework.boot - spring-boot-starter - ${spring-boot.version} - test - - - org.springframework.boot - spring-boot-starter-test - ${spring-boot.version} + org.mockito + mockito-junit-jupiter + ${mockito.version} test - - mockito - - - src/main/resources - true - - - - - - 2.0.4.RELEASE - 5.0.8.RELEASE - 2.0.9.RELEASE - - 2.0.2 - 2.1.1 - - \ No newline at end of file diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/Book.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/additionalanswers/Book.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/Book.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/additionalanswers/Book.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/BookRepository.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/additionalanswers/BookRepository.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/BookRepository.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/additionalanswers/BookRepository.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/BookService.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/additionalanswers/BookService.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/BookService.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/additionalanswers/BookService.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/fluentapi/Pizza.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/fluentapi/Pizza.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/fluentapi/Pizza.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/fluentapi/Pizza.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/fluentapi/PizzaService.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/fluentapi/PizzaService.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/fluentapi/PizzaService.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/fluentapi/PizzaService.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/java8/JobPosition.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/java8/JobPosition.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/java8/JobPosition.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/java8/JobPosition.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/java8/JobService.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/java8/JobService.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/java8/JobService.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/java8/JobService.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/java8/Person.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/java8/Person.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/java8/Person.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/java8/Person.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/java8/UnemploymentService.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/java8/UnemploymentService.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/java8/UnemploymentService.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/java8/UnemploymentService.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/java8/UnemploymentServiceImpl.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/java8/UnemploymentServiceImpl.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/java8/UnemploymentServiceImpl.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/java8/UnemploymentServiceImpl.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/mocksettings/AbstractCoffee.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/mocksettings/AbstractCoffee.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/mocksettings/AbstractCoffee.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/mocksettings/AbstractCoffee.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/mocksettings/SimpleService.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/mocksettings/SimpleService.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/mocksettings/SimpleService.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/mocksettings/SimpleService.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/mocksettings/SpecialInterface.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/mocksettings/SpecialInterface.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/mocksettings/SpecialInterface.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/mocksettings/SpecialInterface.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/objectmapper/Flower.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/objectmapper/Flower.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/objectmapper/Flower.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/objectmapper/Flower.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/objectmapper/FlowerJsonStringValidator.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/objectmapper/FlowerJsonStringValidator.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/objectmapper/FlowerJsonStringValidator.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/objectmapper/FlowerJsonStringValidator.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/whenvsdomethods/Employee.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/whenvsdomethods/Employee.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/whenvsdomethods/Employee.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/whenvsdomethods/Employee.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/whenvsdomethods/IAmOnHolidayException.java b/testing-modules/mockito/src/main/java/com/baeldung/mockito/whenvsdomethods/IAmOnHolidayException.java similarity index 100% rename from testing-modules/mockito-2/src/main/java/com/baeldung/mockito/whenvsdomethods/IAmOnHolidayException.java rename to testing-modules/mockito/src/main/java/com/baeldung/mockito/whenvsdomethods/IAmOnHolidayException.java diff --git a/testing-modules/mockito/src/main/resources/logback.xml b/testing-modules/mockito/src/main/resources/logback.xml index ec0dc2469a..7d900d8ea8 100644 --- a/testing-modules/mockito/src/main/resources/logback.xml +++ b/testing-modules/mockito/src/main/resources/logback.xml @@ -2,17 +2,11 @@ - web - %date [%thread] %-5level %logger{36} - %message%n + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/additionalanswers/BookServiceUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/additionalanswers/BookServiceUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/additionalanswers/BookServiceUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/additionalanswers/BookServiceUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/bddmockito/BDDMockitoUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/bddmockito/BDDMockitoUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/bddmockito/BDDMockitoUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/bddmockito/BDDMockitoUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/bddmockito/PhoneBookRepository.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/bddmockito/PhoneBookRepository.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/bddmockito/PhoneBookRepository.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/bddmockito/PhoneBookRepository.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/bddmockito/PhoneBookService.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/bddmockito/PhoneBookService.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/bddmockito/PhoneBookService.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/bddmockito/PhoneBookService.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/fluentapi/PizzaServiceUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/fluentapi/PizzaServiceUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/fluentapi/PizzaServiceUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/fluentapi/PizzaServiceUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/fluentapi/PizzaUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/fluentapi/PizzaUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/fluentapi/PizzaUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/fluentapi/PizzaUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithLambdaUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithLambdaUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithLambdaUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithLambdaUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/java8/JobServiceUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/java8/JobServiceUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/java8/JobServiceUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/java8/JobServiceUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/java8/UnemploymentServiceImplUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/java8/UnemploymentServiceImplUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/java8/UnemploymentServiceImplUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/java8/UnemploymentServiceImplUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/lazyverification/LazyVerificationUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/lazyverification/LazyVerificationUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/lazyverification/LazyVerificationUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/lazyverification/LazyVerificationUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/misusing/ExpectedTestFailureRule.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/misusing/ExpectedTestFailureRule.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/misusing/ExpectedTestFailureRule.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/misusing/ExpectedTestFailureRule.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/misusing/MockitoUnecessaryStubUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/misusing/MockitoUnecessaryStubUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/misusing/MockitoUnecessaryStubUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/misusing/MockitoUnecessaryStubUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/mocksettings/MockSettingsUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/mocksettings/MockSettingsUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/mocksettings/MockSettingsUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/mocksettings/MockSettingsUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/objectmapper/FlowerJsonStringValidatorUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/objectmapper/FlowerJsonStringValidatorUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/objectmapper/FlowerJsonStringValidatorUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/objectmapper/FlowerJsonStringValidatorUnitTest.java diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/whenvsdomethods/WhenVsDoMethodsUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/whenvsdomethods/WhenVsDoMethodsUnitTest.java similarity index 100% rename from testing-modules/mockito-2/src/test/java/com/baeldung/mockito/whenvsdomethods/WhenVsDoMethodsUnitTest.java rename to testing-modules/mockito/src/test/java/com/baeldung/mockito/whenvsdomethods/WhenVsDoMethodsUnitTest.java diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index f931cf6947..1601bfb804 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -29,7 +29,6 @@ junit5-annotations junit5-migration load-testing-comparison - mockito-2 mockito mockito-simple mocks From 50955510ca2395a6c7c5893023e7677f782dd8db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?El=C3=A7im=20Duran?= <49587041+elcim@users.noreply.github.com> Date: Thu, 21 Jul 2022 22:17:26 +0300 Subject: [PATCH 17/21] BAEL-5645 - Scanning Java Annotations at Runtime (#12511) * BAEL-5645 - Annotation scanners using Spring context, Spring core, reflections, java reflection, and Jandex libraries are implemented * BAEL-5645 - Library versions are incremented in pom.xml Co-authored-by: elcimduran --- .../spring-boot-libraries-2/pom.xml | 31 ++++++++ .../scanner/SampleAnnotatedClass.java | 14 ++++ .../annotation/scanner/SampleAnnotation.java | 14 ++++ .../scanner/SampleAnnotationScanner.java | 13 ++++ .../scanner/ScanNotSupportedException.java | 4 + .../scanner/UnexpectedScanException.java | 7 ++ .../jandexlib/JandexScannerService.java | 76 +++++++++++++++++++ .../JavaReflectionsScannerService.java | 58 ++++++++++++++ .../ReflectionsScannerService.java | 45 +++++++++++ .../SpringBeanAnnotationScannerService.java | 52 +++++++++++++ .../SpringCoreAnnotationScannerService.java | 42 ++++++++++ .../SampleAnnotationScannerUnitTest.java | 47 ++++++++++++ 12 files changed, 403 insertions(+) create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/SampleAnnotatedClass.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/SampleAnnotation.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/SampleAnnotationScanner.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/ScanNotSupportedException.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/UnexpectedScanException.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/jandexlib/JandexScannerService.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/javareflectionlib/JavaReflectionsScannerService.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/reflectionslib/ReflectionsScannerService.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcontextlib/SpringBeanAnnotationScannerService.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcorelib/SpringCoreAnnotationScannerService.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/test/java/com/baeldung/annotation/scanner/SampleAnnotationScannerUnitTest.java diff --git a/spring-boot-modules/spring-boot-libraries-2/pom.xml b/spring-boot-modules/spring-boot-libraries-2/pom.xml index f62a341efc..79c090dde5 100644 --- a/spring-boot-modules/spring-boot-libraries-2/pom.xml +++ b/spring-boot-modules/spring-boot-libraries-2/pom.xml @@ -62,6 +62,16 @@ spqr 0.11.2 + + org.reflections + reflections + 0.10.2 + + + org.jboss + jandex + 2.4.3.Final + @@ -92,6 +102,27 @@ + + org.jboss.jandex + jandex-maven-plugin + 1.2.3 + + + compile + make-index + + jandex + + + + + ${project.build.outputDirectory} + + + + + + diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/SampleAnnotatedClass.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/SampleAnnotatedClass.java new file mode 100644 index 0000000000..64849d6a3a --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/SampleAnnotatedClass.java @@ -0,0 +1,14 @@ +package com.baeldung.annotation.scanner; + +@SampleAnnotation(name = "SampleAnnotatedClass") +public class SampleAnnotatedClass { + + @SampleAnnotation(name = "annotatedMethod") + public void annotatedMethod() { + //Do something + } + + public void notAnnotatedMethod() { + //Do something + } +} diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/SampleAnnotation.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/SampleAnnotation.java new file mode 100644 index 0000000000..212e12e910 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/SampleAnnotation.java @@ -0,0 +1,14 @@ +package com.baeldung.annotation.scanner; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Target({ METHOD, TYPE }) +@Retention(RUNTIME) +public @interface SampleAnnotation { + String name(); +} diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/SampleAnnotationScanner.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/SampleAnnotationScanner.java new file mode 100644 index 0000000000..5cc9a2e8b3 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/SampleAnnotationScanner.java @@ -0,0 +1,13 @@ +package com.baeldung.annotation.scanner; + +import java.util.List; + +public interface SampleAnnotationScanner { + List scanAnnotatedMethods(); + + List scanAnnotatedClasses(); + + boolean supportsMethodScan(); + + boolean supportsClassScan(); +} diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/ScanNotSupportedException.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/ScanNotSupportedException.java new file mode 100644 index 0000000000..1566dc3fad --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/ScanNotSupportedException.java @@ -0,0 +1,4 @@ +package com.baeldung.annotation.scanner; + +public class ScanNotSupportedException extends RuntimeException{ +} diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/UnexpectedScanException.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/UnexpectedScanException.java new file mode 100644 index 0000000000..bd7c129bfd --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/UnexpectedScanException.java @@ -0,0 +1,7 @@ +package com.baeldung.annotation.scanner; + +public class UnexpectedScanException extends RuntimeException { + public UnexpectedScanException(Throwable ex) { + super(ex); + } +} diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/jandexlib/JandexScannerService.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/jandexlib/JandexScannerService.java new file mode 100644 index 0000000000..fc10db223e --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/jandexlib/JandexScannerService.java @@ -0,0 +1,76 @@ +package com.baeldung.annotation.scanner.jandexlib; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.jboss.jandex.AnnotationInstance; +import org.jboss.jandex.AnnotationTarget; +import org.jboss.jandex.DotName; +import org.jboss.jandex.Index; +import org.jboss.jandex.IndexReader; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.Resource; +import org.springframework.stereotype.Service; + +import com.baeldung.annotation.scanner.SampleAnnotationScanner; +import com.baeldung.annotation.scanner.UnexpectedScanException; + +@Service +public class JandexScannerService implements SampleAnnotationScanner { + @Value("classpath:META-INF/jandex.idx") + private Resource appFile; + + @Override + public List scanAnnotatedMethods() { + try { + final IndexReader reader = new IndexReader(appFile.getInputStream()); + Index jandexFile = reader.read(); + final List appAnnotationList = jandexFile.getAnnotations(DotName.createSimple("com.baeldung.annotation.scanner.SampleAnnotation")); + List annotatedMethods = new ArrayList<>(); + for (AnnotationInstance annotationInstance : appAnnotationList) { + if (annotationInstance.target() + .kind() == AnnotationTarget.Kind.METHOD) { + annotatedMethods.add(annotationInstance.value("name") + .value() + .toString()); + } + } + return Collections.unmodifiableList(annotatedMethods); + } catch (IOException e) { + throw new UnexpectedScanException(e); + } + } + + @Override + public List scanAnnotatedClasses() { + try { + final IndexReader reader = new IndexReader(appFile.getInputStream()); + Index jandexFile = reader.read(); + final List appAnnotationList = jandexFile.getAnnotations(DotName.createSimple("com.baeldung.annotation.scanner.SampleAnnotation")); + List annotatedClasses = new ArrayList<>(); + for (AnnotationInstance annotationInstance : appAnnotationList) { + if (annotationInstance.target() + .kind() == AnnotationTarget.Kind.CLASS) { + annotatedClasses.add(annotationInstance.value("name") + .value() + .toString()); + } + } + return Collections.unmodifiableList(annotatedClasses); + } catch (IOException e) { + throw new UnexpectedScanException(e); + } + } + + @Override + public boolean supportsMethodScan() { + return true; + } + + @Override + public boolean supportsClassScan() { + return true; + } +} diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/javareflectionlib/JavaReflectionsScannerService.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/javareflectionlib/JavaReflectionsScannerService.java new file mode 100644 index 0000000000..2833bb1326 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/javareflectionlib/JavaReflectionsScannerService.java @@ -0,0 +1,58 @@ +package com.baeldung.annotation.scanner.javareflectionlib; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.springframework.stereotype.Service; + +import com.baeldung.annotation.scanner.SampleAnnotation; +import com.baeldung.annotation.scanner.SampleAnnotationScanner; +import com.baeldung.annotation.scanner.UnexpectedScanException; + +@Service +public class JavaReflectionsScannerService implements SampleAnnotationScanner { + @Override + public List scanAnnotatedMethods() { + try { + Class clazz = ClassLoader.getSystemClassLoader() + .loadClass("com.baeldung.annotation.scanner.SampleAnnotatedClass"); + Method[] methods = clazz.getMethods(); + List annotatedMethods = new ArrayList<>(); + for (Method method : methods) { + SampleAnnotation annotation = method.getAnnotation(SampleAnnotation.class); + if (annotation != null) { + annotatedMethods.add(annotation.name()); + } + } + return Collections.unmodifiableList(annotatedMethods); + } catch (ClassNotFoundException e) { + throw new UnexpectedScanException(e); + } + } + + @Override + public List scanAnnotatedClasses() { + try { + Class clazz = ClassLoader.getSystemClassLoader() + .loadClass("com.baeldung.annotation.scanner.SampleAnnotatedClass"); + SampleAnnotation classAnnotation = clazz.getAnnotation(SampleAnnotation.class); + List annotatedClasses = new ArrayList<>(); + annotatedClasses.add(classAnnotation.name()); + return Collections.unmodifiableList(annotatedClasses); + } catch (ClassNotFoundException e) { + throw new UnexpectedScanException(e); + } + } + + @Override + public boolean supportsMethodScan() { + return true; + } + + @Override + public boolean supportsClassScan() { + return true; + } +} diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/reflectionslib/ReflectionsScannerService.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/reflectionslib/ReflectionsScannerService.java new file mode 100644 index 0000000000..82a10e21aa --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/reflectionslib/ReflectionsScannerService.java @@ -0,0 +1,45 @@ +package com.baeldung.annotation.scanner.reflectionslib; + +import java.lang.reflect.Method; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import org.reflections.Reflections; +import org.springframework.stereotype.Service; + +import com.baeldung.annotation.scanner.SampleAnnotation; +import com.baeldung.annotation.scanner.SampleAnnotationScanner; + +@Service +public class ReflectionsScannerService implements SampleAnnotationScanner { + @Override + public List scanAnnotatedMethods() { + Reflections reflections = new Reflections("com.baeldung.annotation.scanner"); + Set methods = reflections.getMethodsAnnotatedWith(SampleAnnotation.class); + return methods.stream() + .map(method -> method.getAnnotation(SampleAnnotation.class) + .name()) + .collect(Collectors.toList()); + } + + @Override + public List scanAnnotatedClasses() { + Reflections reflections = new Reflections("com.baeldung.annotation.scanner"); + Set> types = reflections.getTypesAnnotatedWith(SampleAnnotation.class); + return types.stream() + .map(clazz -> clazz.getAnnotation(SampleAnnotation.class) + .name()) + .collect(Collectors.toList()); + } + + @Override + public boolean supportsMethodScan() { + return true; + } + + @Override + public boolean supportsClassScan() { + return true; + } +} diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcontextlib/SpringBeanAnnotationScannerService.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcontextlib/SpringBeanAnnotationScannerService.java new file mode 100644 index 0000000000..cd31ae686e --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcontextlib/SpringBeanAnnotationScannerService.java @@ -0,0 +1,52 @@ +package com.baeldung.annotation.scanner.springcontextlib; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; +import org.springframework.core.type.filter.AnnotationTypeFilter; +import org.springframework.stereotype.Service; + +import com.baeldung.annotation.scanner.SampleAnnotation; +import com.baeldung.annotation.scanner.SampleAnnotationScanner; +import com.baeldung.annotation.scanner.ScanNotSupportedException; + +@Service +public class SpringBeanAnnotationScannerService implements SampleAnnotationScanner { + @Override + public List scanAnnotatedMethods() { + throw new ScanNotSupportedException(); + } + + @Override + public List scanAnnotatedClasses() { + ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); + provider.addIncludeFilter(new AnnotationTypeFilter(SampleAnnotation.class)); + Set beanDefs = provider.findCandidateComponents("com.baeldung.annotation.scanner"); + List annotatedBeans = new ArrayList<>(); + for (BeanDefinition bd : beanDefs) { + if (bd instanceof AnnotatedBeanDefinition) { + Map annotAttributeMap = ((AnnotatedBeanDefinition) bd).getMetadata() + .getAnnotationAttributes(SampleAnnotation.class.getCanonicalName()); + annotatedBeans.add(annotAttributeMap.get("name") + .toString()); + } + } + return Collections.unmodifiableList(annotatedBeans); + } + + @Override + public boolean supportsMethodScan() { + return false; + } + + @Override + public boolean supportsClassScan() { + return true; + } +} diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcorelib/SpringCoreAnnotationScannerService.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcorelib/SpringCoreAnnotationScannerService.java new file mode 100644 index 0000000000..f3421fe46d --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcorelib/SpringCoreAnnotationScannerService.java @@ -0,0 +1,42 @@ +package com.baeldung.annotation.scanner.springcorelib; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.stereotype.Service; +import org.springframework.util.ClassUtils; + +import com.baeldung.annotation.scanner.SampleAnnotationScanner; +import com.baeldung.annotation.scanner.SampleAnnotatedClass; +import com.baeldung.annotation.scanner.SampleAnnotation; +import com.baeldung.annotation.scanner.ScanNotSupportedException; + +@Service +public class SpringCoreAnnotationScannerService implements SampleAnnotationScanner { + @Override + public List scanAnnotatedMethods() { + final Class userClass = ClassUtils.getUserClass(SampleAnnotatedClass.class); + return Arrays.stream(userClass.getMethods()) + .filter(method -> AnnotationUtils.getAnnotation(method, SampleAnnotation.class) != null) + .map(method -> method.getAnnotation(SampleAnnotation.class) + .name()) + .collect(Collectors.toList()); + } + + @Override + public List scanAnnotatedClasses() { + throw new ScanNotSupportedException(); + } + + @Override + public boolean supportsMethodScan() { + return true; + } + + @Override + public boolean supportsClassScan() { + return false; + } +} diff --git a/spring-boot-modules/spring-boot-libraries-2/src/test/java/com/baeldung/annotation/scanner/SampleAnnotationScannerUnitTest.java b/spring-boot-modules/spring-boot-libraries-2/src/test/java/com/baeldung/annotation/scanner/SampleAnnotationScannerUnitTest.java new file mode 100644 index 0000000000..80eca2b4c5 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/test/java/com/baeldung/annotation/scanner/SampleAnnotationScannerUnitTest.java @@ -0,0 +1,47 @@ +package com.baeldung.annotation.scanner; + +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.*; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SampleAnnotationScannerUnitTest { + @Autowired + private List scannerList; + + @Test + public void givenPackage_whenScanAnnotatedClasses_thenAnnotationValues() { + final List annotatedClasses = scannerList.stream() + .filter(SampleAnnotationScanner::supportsClassScan) + .map(SampleAnnotationScanner::scanAnnotatedClasses) + .flatMap(Collection::stream) + .collect(Collectors.toList()); + + assertNotNull(annotatedClasses); + assertEquals(4, annotatedClasses.size()); + annotatedClasses.forEach(annotValue -> assertEquals("SampleAnnotatedClass", annotValue)); + } + + @Test + public void givenPackage_whenScanAnnotatedMethods_thenAnnotationValues() { + final List annotatedMethods = scannerList.stream() + .filter(SampleAnnotationScanner::supportsMethodScan) + .map(SampleAnnotationScanner::scanAnnotatedMethods) + .flatMap(Collection::stream) + .collect(Collectors.toList()); + + assertNotNull(annotatedMethods); + assertEquals(3, annotatedMethods.size()); + annotatedMethods.forEach(annotValue -> assertEquals("annotatedMethod", annotValue)); + } + +} From 8d894df26adc574c051abf68d45c946e062ddf8e Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Thu, 21 Jul 2022 17:47:35 -0300 Subject: [PATCH 18/21] BAEL-5365 Count in Spring Data MongoDB Repository (#12481) * BAEL-5370 - MongoDB Composite Key First Draft. * removing comments * BAEL-5370 Test could fail if ran in a different order: givenCompositeId_whenSearchingByIdObject_thenFound * BAEL-5370 removing compound index related stuff * removing first insert from assertThrows * first draft * removing Customer2 * first draft * adjustments * removed controlled * fixed formatting on Car --- .../count/SpringBootCountApplication.java | 13 ++ .../boot/count/dao/CarRepository.java | 16 +++ .../com/baeldung/boot/count/data/Car.java | 33 +++++ .../boot/count/service/CountCarService.java | 69 ++++++++++ .../CountCarServiceIntegrationTest.java | 118 ++++++++++++++++++ 5 files changed, 249 insertions(+) create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/SpringBootCountApplication.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/dao/CarRepository.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/data/Car.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/service/CountCarService.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/count/service/CountCarServiceIntegrationTest.java diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/SpringBootCountApplication.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/SpringBootCountApplication.java new file mode 100644 index 0000000000..bb7351383c --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/SpringBootCountApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.boot.count; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; + +@SpringBootApplication +@EnableMongoRepositories(basePackages = { "com.baeldung.boot.count" }) +public class SpringBootCountApplication { + public static void main(String... args) { + SpringApplication.run(SpringBootCountApplication.class, args); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/dao/CarRepository.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/dao/CarRepository.java new file mode 100644 index 0000000000..b03298852e --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/dao/CarRepository.java @@ -0,0 +1,16 @@ +package com.baeldung.boot.count.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; + +import com.baeldung.boot.count.data.Car; + +public interface CarRepository extends MongoRepository { + @Query(value = "{brand: ?0}", count = true) + public long countBrand(String brand); + + Long countByBrand(String brand); + + @Query(value = "{}", count = true) + Long countWithAnnotation(); +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/data/Car.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/data/Car.java new file mode 100644 index 0000000000..42b80d70b0 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/data/Car.java @@ -0,0 +1,33 @@ +package com.baeldung.boot.count.data; + +import org.springframework.data.mongodb.core.mapping.Document; + +@Document +public class Car { + private String name; + + private String brand; + + public Car() { + } + + public Car(String brand) { + this.brand = brand; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/service/CountCarService.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/service/CountCarService.java new file mode 100644 index 0000000000..d4685847fb --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/count/service/CountCarService.java @@ -0,0 +1,69 @@ +package com.baeldung.boot.count.service; + +import java.util.List; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Example; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.stereotype.Service; + +import com.baeldung.boot.count.dao.CarRepository; +import com.baeldung.boot.count.data.Car; + +@Service +public class CountCarService { + + @Autowired + private CarRepository repo; + + @Autowired + private MongoTemplate mongo; + + public List findCars() { + return repo.findAll(); + } + + public Optional findCar(String id) { + return repo.findById(id); + } + + public Car insertCar(Car item) { + return repo.insert(item); + } + + public long getCountWithQueryAnnotation() { + return repo.countWithAnnotation(); + } + + public long getCountWithCrudRepository() { + return repo.count(); + } + + public long getCountBrandWithQueryMethod(String brand) { + return repo.countByBrand(brand); + } + + public long getCountWithExample(Car item) { + return repo.count(Example.of(item)); + } + + public long getCountWithExampleCriteria(Car item) { + Query query = new Query(); + query.addCriteria(Criteria.byExample(item)); + return mongo.count(query, Car.class); + } + + public long getCountBrandWithQueryAnnotation(String brand) { + return repo.countBrand(brand); + } + + public long getCountBrandWithCriteria(String brand) { + Query query = new Query(); + query.addCriteria(Criteria.where("brand") + .is(brand)); + return mongo.count(query, Car.class); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/count/service/CountCarServiceIntegrationTest.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/count/service/CountCarServiceIntegrationTest.java new file mode 100644 index 0000000000..421ecd3a34 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/count/service/CountCarServiceIntegrationTest.java @@ -0,0 +1,118 @@ +package com.baeldung.boot.count.service; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.boot.count.data.Car; + +@SpringBootTest +@DirtiesContext +@RunWith(SpringRunner.class) +public class CountCarServiceIntegrationTest { + @Autowired + private CountCarService service; + + Car car1 = new Car("B-A"); + + @Before + public void init() { + service.insertCar(car1); + service.insertCar(new Car("B-B")); + service.insertCar(new Car("B-C")); + } + + @Test + public void givenAllDocs_whenQueryAnnotationCount_thenCountEqualsSize() { + List all = service.findCars(); + + long count = service.getCountWithQueryAnnotation(); + + assertEquals(count, all.size()); + } + + @Test + public void givenAllDocs_whenCrudRepositoryCount_thenCountEqualsSize() { + List all = service.findCars(); + + long count = service.getCountWithCrudRepository(); + + assertEquals(count, all.size()); + } + + @Test + public void givenFilteredDocs_whenCriteriaCountByBrand_thenCountEqualsSize() { + String filter = "B-A"; + long all = service.findCars() + .stream() + .filter(car -> car.getBrand() + .equals(filter)) + .count(); + + long count = service.getCountBrandWithCriteria(filter); + + assertEquals(count, all); + } + + @Test + public void givenQueryAnnotation_whenCountingByBrand_thenCountEqualsSize() { + String filter = "B-A"; + long all = service.findCars() + .stream() + .filter(car -> car.getBrand() + .equals(filter)) + .count(); + + long count = service.getCountBrandWithQueryAnnotation(filter); + + assertEquals(count, all); + } + + @Test + public void givenFilteredDocs_whenQueryMethodCountByBrand_thenCountEqualsSize() { + String filter = "B-A"; + long all = service.findCars() + .stream() + .filter(car -> car.getBrand() + .equals(filter)) + .count(); + + long count = service.getCountBrandWithQueryMethod(filter); + + assertEquals(count, all); + } + + @Test + public void givenFilteredDocs_whenExampleCount_thenCountEqualsSize() { + long all = service.findCars() + .stream() + .filter(car -> car.getBrand() + .equals(car1.getBrand())) + .count(); + + long count = service.getCountWithExample(car1); + + assertEquals(count, all); + } + + @Test + public void givenFilteredDocs_whenExampleCriteriaCount_thenCountEqualsSize() { + long all = service.findCars() + .stream() + .filter(car -> car.getBrand() + .equals(car1.getBrand())) + .count(); + + long count = service.getCountWithExampleCriteria(car1); + + assertEquals(count, all); + } +} From 6b8313682e2bcb8c5e238c0605792eb7e1e8738f Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Fri, 22 Jul 2022 04:58:46 +0200 Subject: [PATCH 19/21] BAEL-5693 rm whitespace from a string (#12515) --- .../RemoveWhitespaceUnitTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/removewhitespace/RemoveWhitespaceUnitTest.java diff --git a/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/removewhitespace/RemoveWhitespaceUnitTest.java b/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/removewhitespace/RemoveWhitespaceUnitTest.java new file mode 100644 index 0000000000..f749484ba9 --- /dev/null +++ b/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/removewhitespace/RemoveWhitespaceUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.removewhitespace; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class RemoveWhitespaceUnitTest { + private final String myString = " I am a wonderful String ! "; + + @Test + void givenStringWithWhitespace_whenRemoveAllWhitespace_shouldGetExpectedResult() { + String result = myString.replaceAll("\\s", ""); + assertThat(result).isEqualTo("IamawonderfulString!"); + } + + @Test + void givenStringWithWhitespace_whenRemoveAllWhitespaceUsingStringUtils_shouldGetExpectedResult() { + String result = StringUtils.deleteWhitespace(myString); + assertThat(result).isEqualTo("IamawonderfulString!"); + } + + @Test + void givenStringWithWhitespace_whenReplaceConsecutiveSpacesWithSingleSpace_shouldGetExpectedResult() { + String result = myString.replaceAll("\\s+", " "); + assertThat(result).isEqualTo(" I am a wonderful String ! "); + assertThat(result.trim()).isEqualTo("I am a wonderful String !"); + } + + @Test + void givenStringWithWhitespace_whenNormalizeWithApacheCommons_shouldGetExpectedResult() { + String result = StringUtils.normalizeSpace(myString); + assertThat(result).isEqualTo("I am a wonderful String !"); + } +} From 735f657223734e017187b4f9b4dae7e930624af4 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Fri, 22 Jul 2022 16:06:46 +0100 Subject: [PATCH 20/21] [JAVA-13130] Logging cleanup --- .../src/test/resources/logback-test.xml | 4 ++++ .../exception/detachedentity/HibernateUtil.java | 3 ++- .../src/test/resources/logback-test.xml | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 persistence-modules/hibernate-exceptions/src/test/resources/logback-test.xml diff --git a/libraries-security/src/test/resources/logback-test.xml b/libraries-security/src/test/resources/logback-test.xml index 8d4771e308..fcbdb8b5f7 100644 --- a/libraries-security/src/test/resources/logback-test.xml +++ b/libraries-security/src/test/resources/logback-test.xml @@ -6,6 +6,10 @@ + + + + diff --git a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/detachedentity/HibernateUtil.java b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/detachedentity/HibernateUtil.java index 0420755354..a7b1f496d9 100644 --- a/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/detachedentity/HibernateUtil.java +++ b/persistence-modules/hibernate-exceptions/src/main/java/com/baeldung/hibernate/exception/detachedentity/HibernateUtil.java @@ -24,7 +24,8 @@ public class HibernateUtil { settings.put(Environment.USER, "sa"); settings.put(Environment.PASS, ""); settings.put(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect"); - settings.put(Environment.SHOW_SQL, "true"); + // enable to show Hibernate generated SQL + settings.put(Environment.SHOW_SQL, "false"); settings.put(Environment.FORMAT_SQL, "true"); settings.put(Environment.USE_SQL_COMMENTS, "true"); settings.put(Environment.HBM2DDL_AUTO, "update"); diff --git a/persistence-modules/hibernate-exceptions/src/test/resources/logback-test.xml b/persistence-modules/hibernate-exceptions/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..5d5620b2c1 --- /dev/null +++ b/persistence-modules/hibernate-exceptions/src/test/resources/logback-test.xml @@ -0,0 +1,16 @@ + + + + + [%d{ISO8601}]-[%thread] %-5level %logger - %msg%n + + + + + + + + + + + \ No newline at end of file From a4691406f2b391651ca0826ae5afb2f4501280da Mon Sep 17 00:00:00 2001 From: victorsempere Date: Mon, 25 Jul 2022 05:51:00 +0200 Subject: [PATCH 21/21] BAEL-5328 (#12457) * BAEL-5328 First draft of the code to the article: https://drafts.baeldung.com/wp-admin/post.php?post=136328&action=edit * BAEL-5328 Fixed comment. Used entry point instead of endpoint * BAEL-5328 Added SecurityFilterChain configuration to allow requests to: * "/api/auth/**", "/swagger-ui-custom.html" ,"/swagger-ui.html", "/swagger-ui/**", "/v3/api-docs/**", "/webjars/**", "/swagger-ui/index.html","/api-docs/**" As the spring-boot-starter-security has been started we need to add it to keep this article, simple. Fixed description of the login() method for the 200 httpstatus response * BAEL-5328 Added required attribute in @RequestBody of the login() method * BAEL-5328 Code formatting --- ...efaultGlobalSecuritySchemeApplication.java | 32 ++++++ ...GlobalSecuritySchemeOpenApiController.java | 61 +++++++++++ .../dto/ApplicationExceptionDto.java | 26 +++++ .../dto/LoginDto.java | 103 ++++++++++++++++++ .../dto/PingResponseDto.java | 84 ++++++++++++++ .../dto/TokenDto.java | 57 ++++++++++ 6 files changed, 363 insertions(+) create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/DefaultGlobalSecuritySchemeApplication.java create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/controller/DefaultGlobalSecuritySchemeOpenApiController.java create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/ApplicationExceptionDto.java create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/LoginDto.java create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/PingResponseDto.java create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/TokenDto.java diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/DefaultGlobalSecuritySchemeApplication.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/DefaultGlobalSecuritySchemeApplication.java new file mode 100644 index 0000000000..1ce81a1e83 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/DefaultGlobalSecuritySchemeApplication.java @@ -0,0 +1,32 @@ +package com.baeldung.defaultglobalsecurityscheme; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.web.SecurityFilterChain; + +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn; +import io.swagger.v3.oas.annotations.enums.SecuritySchemeType; +import io.swagger.v3.oas.annotations.info.Info; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.security.SecurityScheme; + +@SpringBootApplication +@OpenAPIDefinition(info = @Info(title = "Apply Default Global SecurityScheme in springdoc-openapi", version = "1.0.0"), security = { @SecurityRequirement(name = "api_key") }) +@SecurityScheme(type = SecuritySchemeType.APIKEY, name = "api_key", in = SecuritySchemeIn.HEADER) +public class DefaultGlobalSecuritySchemeApplication { + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + return http.authorizeHttpRequests(authorizeRequests -> authorizeRequests.antMatchers("/api/auth/**", "/swagger-ui-custom.html", "/swagger-ui.html", "/swagger-ui/**", "/v3/api-docs/**", "/webjars/**", "/swagger-ui/index.html", "/api-docs/**") + .permitAll() + .anyRequest() + .authenticated()) + .build(); + } + + public static void main(String[] args) { + SpringApplication.run(DefaultGlobalSecuritySchemeApplication.class, args); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/controller/DefaultGlobalSecuritySchemeOpenApiController.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/controller/DefaultGlobalSecuritySchemeOpenApiController.java new file mode 100644 index 0000000000..4ad7a2a2c3 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/controller/DefaultGlobalSecuritySchemeOpenApiController.java @@ -0,0 +1,61 @@ +package com.baeldung.defaultglobalsecurityscheme.controller; + +import java.time.OffsetDateTime; +import java.time.ZoneOffset; + +import javax.validation.Valid; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.defaultglobalsecurityscheme.dto.LoginDto; +import com.baeldung.defaultglobalsecurityscheme.dto.ApplicationExceptionDto; +import com.baeldung.defaultglobalsecurityscheme.dto.PingResponseDto; +import com.baeldung.defaultglobalsecurityscheme.dto.TokenDto; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.ExampleObject; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.security.SecurityRequirements; + +@RestController +@RequestMapping("/") +public class DefaultGlobalSecuritySchemeOpenApiController { + @RequestMapping(method = RequestMethod.POST, value = "/login", produces = { "application/json" }, consumes = { "application/json" }) + @Operation(operationId = "login", responses = { + @ApiResponse(responseCode = "200", description = "api_key to be used in the secured-ping entry point", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = TokenDto.class)) }), + @ApiResponse(responseCode = "401", description = "Unauthorized request", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ApplicationExceptionDto.class)) }) }) + @SecurityRequirements() + public ResponseEntity login(@Parameter(name = "LoginDto", description = "Login") @Valid @RequestBody(required = true) LoginDto loginDto) { + TokenDto token = new TokenDto(); + token.setRaw("Generated Token"); + return ResponseEntity.ok(token); + } + + @Operation(operationId = "ping", responses = { + @ApiResponse(responseCode = "200", description = "Ping that needs an api_key attribute in the header", content = { + @Content(mediaType = "application/json", schema = @Schema(implementation = PingResponseDto.class), examples = { @ExampleObject(value = "{ pong: '2022-06-17T18:30:33.465+02:00' }") }) }), + @ApiResponse(responseCode = "401", description = "Unauthorized request", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ApplicationExceptionDto.class)) }), + @ApiResponse(responseCode = "403", description = "Forbidden request", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ApplicationExceptionDto.class)) }) }) + @RequestMapping(method = RequestMethod.GET, value = "/ping", produces = { "application/json" }) + public ResponseEntity ping(@RequestHeader(name = "api_key", required = false) String api_key) { + int year = 2000; + int month = 1; + int dayOfMonth = 1; + int hour = 0; + int minute = 0; + int second = 0; + int nanoSeccond = 0; + ZoneOffset offset = ZoneOffset.UTC; + PingResponseDto response = new PingResponseDto(); + response.setPong(OffsetDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoSeccond, offset)); + return ResponseEntity.ok(response); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/ApplicationExceptionDto.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/ApplicationExceptionDto.java new file mode 100644 index 0000000000..5fb63793c4 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/ApplicationExceptionDto.java @@ -0,0 +1,26 @@ +package com.baeldung.defaultglobalsecurityscheme.dto; + +public class ApplicationExceptionDto { + private long errorCode; + private String description; + + public ApplicationExceptionDto() { + super(); + } + + public long getErrorCode() { + return errorCode; + } + + public void setErrorCode(long errorCode) { + this.errorCode = errorCode; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/LoginDto.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/LoginDto.java new file mode 100644 index 0000000000..cf88cc4d98 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/LoginDto.java @@ -0,0 +1,103 @@ +package com.baeldung.defaultglobalsecurityscheme.dto; + +import java.util.Objects; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeName; + +import io.swagger.v3.oas.annotations.media.Schema; + +/** + * LoginDto + */ + +@JsonTypeName("Login") +public class LoginDto { + + @JsonProperty("user") + private String user; + + @JsonProperty("pass") + private String pass; + + public LoginDto user(String user) { + this.user = user; + return this; + } + + /** + * Get user + * @return user + */ + + @Schema(name = "user", required = true) + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + public LoginDto pass(String pass) { + this.pass = pass; + return this; + } + + /** + * Get pass + * @return pass + */ + + @Schema(name = "pass", required = true) + public String getPass() { + return pass; + } + + public void setPass(String pass) { + this.pass = pass; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + LoginDto login = (LoginDto) o; + return Objects.equals(this.user, login.user) && Objects.equals(this.pass, login.pass); + } + + @Override + public int hashCode() { + return Objects.hash(user, pass); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class LoginDto {\n"); + sb.append(" user: ") + .append(toIndentedString(user)) + .append("\n"); + sb.append(" pass: ") + .append(toIndentedString(pass)) + .append("\n"); + sb.append("}"); + return sb.toString(); + } + + /** + * Convert the given object to string with each line indented by 4 spaces + * (except the first line). + */ + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString() + .replace("\n", "\n "); + } +} diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/PingResponseDto.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/PingResponseDto.java new file mode 100644 index 0000000000..0d367785d8 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/PingResponseDto.java @@ -0,0 +1,84 @@ +package com.baeldung.defaultglobalsecurityscheme.dto; + +import java.time.OffsetDateTime; +import java.util.Objects; + +import javax.validation.Valid; + +import org.springframework.format.annotation.DateTimeFormat; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeName; + +import io.swagger.v3.oas.annotations.media.Schema; + +/** + * SecuredPingResponseDto + */ + +@JsonTypeName("PingResponse") +public class PingResponseDto { + + @JsonProperty("pong") + @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) + private OffsetDateTime pong; + + public PingResponseDto pong(OffsetDateTime pong) { + this.pong = pong; + return this; + } + + /** + * Get pong + * @return pong + */ + @Valid + @Schema(name = "pong", required = false) + public OffsetDateTime getPong() { + return pong; + } + + public void setPong(OffsetDateTime pong) { + this.pong = pong; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + PingResponseDto securedPingResponse = (PingResponseDto) o; + return Objects.equals(this.pong, securedPingResponse.pong); + } + + @Override + public int hashCode() { + return Objects.hash(pong); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class PingResponseDto {\n"); + sb.append(" pong: ") + .append(toIndentedString(pong)) + .append("\n"); + sb.append("}"); + return sb.toString(); + } + + /** + * Convert the given object to string with each line indented by 4 spaces + * (except the first line). + */ + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString() + .replace("\n", "\n "); + } +} diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/TokenDto.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/TokenDto.java new file mode 100644 index 0000000000..d8f7daa09c --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/defaultglobalsecurityscheme/dto/TokenDto.java @@ -0,0 +1,57 @@ +package com.baeldung.defaultglobalsecurityscheme.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeName; + +import io.swagger.v3.oas.annotations.media.Schema; + +/** + * LoginDto + */ + +@JsonTypeName("Token") +public class TokenDto { + + @JsonProperty("raw") + private String raw; + + @Schema(name = "raw", example = "app token") + public String getRaw() { + return raw; + } + + public void setRaw(String raw) { + this.raw = raw; + } + + @Override + public String toString() { + return "TokenDto [raw=" + raw + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((raw == null) ? 0 : raw.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TokenDto other = (TokenDto) obj; + if (raw == null) { + if (other.raw != null) + return false; + } else if (!raw.equals(other.raw)) + return false; + return true; + } + +}