Skip to content

Instantly share code, notes, and snippets.

@dehidehidehi
dehidehidehi / elasticSearchJsonToSearchResponse.java
Created October 23, 2024 08:46
ElasticSearch Json to SearchResponse
public SearchResponse getSearchResponseFromJson(String jsonResponse) throws IOException {
var parser = JsonXContent.jsonXContent.createParser(
getDefaultNamedXContentRegistry(),
LoggingDeprecationHandler.INSTANCE,
jsonResponse);
return SearchResponse.fromXContent(parser);
}
private NamedXContentRegistry getDefaultNamedXContentRegistry() {
List<NamedXContentRegistry.Entry> entries = new ArrayList<>();
@dehidehidehi
dehidehidehi / pom.xml
Created July 3, 2024 12:41
Maven - Autogeneration of OpenAPI specification in Json format after integration test phase
<profiles>
<profile>
<!-- Profil dédié pour la génération du json OpenAPI -->
<id>springdocs</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<springdocs.host>http://localhost</springdocs.host>
<springdocs.port>9191</springdocs.port>
@dehidehidehi
dehidehidehi / SimpleTestContainer.java
Created January 9, 2024 17:21
TestContainers class setup
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;
@dehidehidehi
dehidehidehi / profile.zsh
Created November 30, 2023 23:05
Enable navigating between Tmux panes within zsh using vim-like navigation bindings.
# Unbind ZSH keys occupying VIM-like navigation shortcuts.
bindkey -r '^H' # Unbind Ctrl-h
bindkey -r '^J' # Unbind Ctrl-j
bindkey -r '^K' # Unbind Ctrl-k
bindkey -r '^L' # Unbind Ctrl-l
# Define functions calling Tmux pane navigation.
function tmux-navigate-left() {
tmux select-pane -L
}
@dehidehidehi
dehidehidehi / jshell.sh
Created October 26, 2023 15:44
JShell bootstrap code for loading full application classpath with Maven.
#!/bin/bash
# Compile if needed.
#mvn package -DskipTests
# Remove Maven logs and trim leading and trailing whitespaces with xargs.
cp=$(mvn -q dependency:build-classpath \
-DincludeTypes=jar \
-Dmdep.outputFile=/dev/stderr \
2>&1 >/dev/null | grep -v -E '(DEBUG|INFO|WARNING)' | xargs)
@dehidehidehi
dehidehidehi / Dockerfile
Created October 2, 2023 12:57
Dockerfile with Java and remote debugger attached.
FROM openjdk:20
EXPOSE 9090
EXPOSE 9091
EXPOSE 9092
COPY /target/${project_name}-*.jar /app/app.jar
WORKDIR /app
# Enable remote debugging
/**
* Normalizes a {@link CharSequence} using the {@link Normalizer.Form#NFKC} method;
* It is a Unicode normalization form that replaces characters with their
* compatibility equivalents and then applies canonical composition.
* This method is useful for ensuring that strings containing different
* representations of the same characters are treated as equal.
* <p>
* Example 1:
* Input: "№123"
* Output: "No123"
@dehidehidehi
dehidehidehi / FilterCharacters.java
Created May 15, 2023 13:29
Filters the characters of a given CharSequence based on a provided IntPredicate and a specified Charset.
/**
* Filters the characters of a given CharSequence based on a provided IntPredicate.
* This method is useful for removing unwanted characters from a string, such as non-alphanumeric characters.
*
* @param cs The input CharSequence to be filtered.
* @param characterFilter The IntPredicate to determine which characters should be kept.
* @return A new String containing only the characters that satisfy the characterFilter.
*/
private String filterCharacters(final CharSequence cs, final IntPredicate characterFilter) {
return cs
@dehidehidehi
dehidehidehi / ResourceFileIterator.java
Created May 9, 2023 20:37
Utility for iterating over a folder located in the resources folder in Java.
/**
* Utility for iterating over a folder located in the resources folder.<br>
* Methods using this method are responsible for closing or operate on the stream.
*/
private static Stream<Path> getResourceFolderFiles (String folder) throws IOException, URISyntaxException {
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
final URI uri = Objects.requireNonNull(loader.getResource(folder)).toURI();
final String path = Paths.get(uri).toString();
return Files.walk(Paths.get(path));
}
@dehidehidehi
dehidehidehi / main.yml
Created January 11, 2023 23:28
Github workflow: execute Maven tests with pom.xml cached-dependencies (.github/workflows/main.yml)
name: 'Test PRs'
on:
pull_request:
types: [ opened, synchronize, reopened ]
jobs:
test:
runs-on: ubuntu-latest
steps: