Notes on Java Concurrency

Atomic Reference https://stackoverflow.com/questions/3964211/when-to-use-atomicreference-in-java https://jenkov.com/tutorials/java-util-concurrent/atomicreference.html

April 1, 2023 · 4 words · Peter Dieleman

Map / Filter / Lambda / Stream snippets

Map inputString.map(x -> { if (!StringUtils.isBlank(x)) { return x; } else { return Optional.empty(); } }).orElse(Optional.empty()); Filter Java < 16 return thing.getAllSubEntities() .stream() .filter(e -> e.isDisabled()) .collect(Collectors.toList()); Java >= 16 return thing.getAllSubEntities() .stream() .filter(e -> e.isDisabled()) .tolist();

January 1, 2023 · 37 words · Peter Dieleman

Reflection

Sources https://dzone.com/articles/think-twice-before-using-reflection https://www.javacodegeeks.com/2015/09/how-to-use-reflection-effectively.html https://www.baeldung.com/java-reflection

October 8, 2022 · 4 words · Peter Dieleman

Notes on Unit testing using Rest Assured & Junit

Unpacking a List of results https://stackoverflow.com/questions/43850101/rest-assured-json-body-assertion-for-set-of-values-in-list-regardless-of-posit https://stackoverflow.com/questions/68794097/how-to-get-list-of-objects-with-restassured-instead-of-array-of-objects https://stackoverflow.com/questions/15531767/rest-assured-generic-list-deserialization https://stackoverflow.com/questions/49324680/how-to-verify-that-array-contains-object-with-rest-assured https://www.james-willett.com/rest-assured-extract-json-response/ https://devqa.io/parse-json-response-rest-assured/ List<Person> persons = given() .when() .get("/person") .then() .extract() .body() // here's the magic .jsonPath().getList(".", Person.class); Further Notes on Logging ALL asserts (even passing ones) How to output of Asserts (expected, actual) to console, for failing as well as succesfull tests? https://stackoverflow.com/questions/3963708/gradle-how-to-display-test-results-in-the-console-in-real-time https://github.com/radarsh/gradle-test-logger-plugin https://mkyong.com/gradle/gradle-display-test-results-in-console/ https://stackoverflow.com/questions/56436541/assertj-log-all-passing-and-failing-assertions https://stackoverflow.com/questions/56436541/assertj-log-all-passing-and-failing-assertions https://github.com/assertj/assertj/issues/1518 https://assertj.github.io/doc/#assertj-core-assertion-description-consumer https://stackoverflow.com/questions/65113051/junit5-assertj-create-log-entry-on-assertion https://stackoverflow.com/questions/28994316/can-you-add-a-custom-message-to-assertj-assertthat https://stackoverflow.com/questions/57334283/is-it-possible-to-log-the-name-and-values-of-fields-being-asserted-along-with-co https://stackoverflow.com/questions/15926005/how-make-junit-print-assertion-results AssertJ Alternatives (maybe these support it?) https://blog.frankel.ch/comparison-assertion-libraries/ Hamcrest Does not change the story....

September 22, 2022 · 91 words · Peter Dieleman

Spring Data: bi-directional relationships

Should we map both sides? https://stackoverflow.com/questions/30464782/how-to-maintain-bi-directional-relationships-with-spring-data-rest-and-jpa/30474303#30474303 It usually simplifies the matter if you try not to use bi-directional relationship whenever possible and rather fall back to a repository to obtain all the entities that make up the backside of the association. https://thorben-janssen.com/best-practices-many-one-one-many-associations-mappings/ https://twitter.com/odrotbohm/status/603247455094841344 https://stackoverflow.com/questions/48754783/do-i-have-to-set-both-sides-for-a-bidirectional-relationship If Yes - Do we have to manually update both sides? https://stackoverflow.com/questions/7546161/update-bidirectional-manytomany-from-both-sides https://stackoverflow.com/questions/20068742/jpa-updating-bidirectional-association https://hellokoding.com/jpa-one-to-many-relationship-mapping-example-with-spring-boot-maven-and-mysql/ CascadeType.ALL is for propagating the CRUD operations on the parent entity to the child entities....

August 10, 2022 · 133 words · Peter Dieleman

Persisting ArrayList

https://stackoverflow.com/questions/25415738/how-to-persist-arraylist-within-spring-entity-class Need: @ElementCollection & @CollectionTable(name="abc") ? https://thorben-janssen.com/hibernate-tips-elementcollection/ Does this still require a separate table? https://stackoverflow.com/questions/26512916/what-column-type-is-required-when-using-elementcollection-with-jpa https://www.postgresql.org/docs/current/arrays.html https://sajithv.medium.com/spring-jpa-data-with-postgres-array-types-a6cc4be421e2 Hibernate & Arrays From: Vlad Mihalcea " Hibernate ORM does not support ARRAY column types" Also see: https://stackoverflow.com/questions/1647583/how-to-map-a-postgresql-array-with-hibernate Semi-canonical solution Use: https://github.com/vladmihalcea/hibernate-types https://vladmihalcea.com/how-to-map-java-and-sql-arrays-with-jpa-and-hibernate/ PostgreSQL data types https://www.postgresql.org/docs/current/arrays.html And there’s this https://stackoverflow.com/questions/69858533/replacement-for-hibernates-deprecated-type-annotation https://stackoverflow.com/questions/4332467/mapping-array-with-hibernate https://stackoverflow.com/questions/62647125/hibernate-envers-does-not-recognize-custom-types-from-hibernate-types-52

August 3, 2022 · 50 words · Peter Dieleman

Disabling CORS in local development

Problem Access to the XMLHttpRequest at ‘http://localhost:8084’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Frontend side Disabling CORS in https://stackoverflow.com/questions/46337471/how-to-allow-cors-in-react-js https://nabil6391.medium.com/avoid-cors-requests-for-a-react-app-2988e0061c1a https://reactjs.org/docs/cross-origin-errors.html <> Backend side https://howtodoinjava.com/spring-boot2/spring-cors-configuration/ Should we be doing this in prod? https://zetcode.com/springboot/cors/ https://www.baeldung.com/spring-cors https://www.baeldung.com/spring-security-cors-preflight If we use Spring Security in our project, we must take an extra step to make sure it plays well with CORS....

August 2, 2022 · 105 words · Peter Dieleman

Covering Cornering Cases

Defining Unique constraints over multiple columns https://www.baeldung.com/jpa-unique-constraints https://stackoverflow.com/questions/3126769/uniqueconstraint-annotation-in-java

July 29, 2022 · 8 words · Peter Dieleman

Commiting .idea folder to repository

How to share config between developers? Q: Can we check in (parts of) the .idea folder as in VS Code? https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 All files under the .idea directory in the project root except the items that store user-specific settings: workspace.xml usage.statistics.xml shelf directory All the .iml module files (can be located in different module directories) -> applies to IntelliJ IDEA https://www.jetbrains.com/help/idea/creating-and-managing-projects.html#share-project-through-vcs SO says https://stackoverflow.com/questions/43198273/which-files-in-idea-folder-should-be-tracked-by-git?r=SearchResults&s=1%7C91.0115 Jetbrains official gitignore https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore But but I just want to set required plugins for the whole team https://www....

July 28, 2022 · 98 words · Peter Dieleman

Binding @RequestParam through a POJO

Long List of Query params / request parameters http://dolszewski.com/spring/how-to-bind-requestparam-to-object/ “You also need to mark the POJO parameter in controller’s method with the @Valid annotation. This way you inform Spring that it should execute validation on the binding step.” Downsides: are not able to map query param to a nice variable name in case the ones in URL are shortened (q instead of query), unless you specify a different getter. Note In many cases, it makes much more sense to use @NotBlack instead @NotNull as it also covers the undesired empty string problem (a string with the length of zero)....

July 18, 2022 · 99 words · Peter Dieleman