Skip to content

Instantly share code, notes, and snippets.

@ufuk
ufuk / logback-spring.xml
Last active January 5, 2021 09:50
Spring Boot & AWS Elastic Beanstalk & AWS Cloudwatch Log Stream friendly logging with newline replacing support to convert root cause's stack trace into single line message. And does these within Spring Boot's default pattern layout.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<appender name="CLOUD_WATCH_FRIENDLY" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m %replace(%rEx{short}){'[\r\n\t]+', '\\n'}%nopex%n</pattern>
</encoder>
@ufuk
ufuk / ExampleConstraint.java
Last active March 20, 2024 13:47
How to initialize an instance of the constraint annotation while writing unit tests for a javax.validation.ConstraintValidator
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
@Documented
@Constraint(validatedBy = ExampleConstraintValidator.class)
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExampleConstraint {
@ufuk
ufuk / SpringDataPageUtils.java
Last active February 8, 2021 13:04
SpringDataPageUtils to convert paged entities to paged model objects
import org.springframework.data.domain.Page;
import org.springframework.data.support.PageableExecutionUtils;
import java.util.function.Function;
import java.util.stream.Collectors;
public final class SpringDataPageUtils {
public static <T, C> Page<C> convertPage(Page<T> page, Function<T, C> converter) {
return PageableExecutionUtils.getPage(
@ufuk
ufuk / HttpServletRequestResponsePrinter.java
Created September 5, 2019 06:54
Utility to generate logging-purpose text for HttpServletRequest/Response
package ...;
import ...JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.WebUtils;
@ufuk
ufuk / BaseMockito2JUnit4Test.java
Last active October 24, 2021 17:52
Performs "verify no more interactions" check automatically for all mock objects (works with Mockito version 2 or higher & JUnit version 4 and 5). For detailed description: https://ufukuzun.wordpress.com/2019/04/09/ne-olup-bittiginden-habersiz-testlere-derman-mockscollector/ (Turkish)
import org.apache.commons.lang3.ArrayUtils;
import org.junit.After;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@RunWith(MockitoJUnitRunner.class)
@ufuk
ufuk / import-csv-file-from-s3-into-aws-redshift.sql
Created January 31, 2019 14:01
Import CSV file from S3 into AWS Redshift
-- Before importing, you need to create table
CREATE TABLE example_table
(
...
);
-- Importing...
COPY example_table
FROM 's3://<BUCKET_NAME>/.../example_table.csv'
CREDENTIALS 'aws_access_key_id=...;aws_secret_access_key=...'
@ufuk
ufuk / bash-echo-time-by-timezone.sh
Created January 31, 2019 13:55
Echo time by timezone in BASH
echo "$(TZ='Europe/Moscow' date +%Y-%m-%d\ %H\:%M)"
# output => 2019-01-31 16:52
@ufuk
ufuk / postgresql-export-tables-to-s3-as-csv-file.sh
Last active July 10, 2023 04:04
BASH script to export some tables from the PostgreSQL database to S3 as CSV files.
#!/bin/bash
export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...
export AWS_DEFAULT_REGION=...
PG_HOST=...
PG_USER=...
PG_PASS='...'
PG_DB=...
@ufuk
ufuk / posgresql-lightspeed-alter-table.sql
Last active November 5, 2018 07:59
Increase performance of alter tables or any other expensive operations by increasing WORK_MEM parameter for current session in PostgreSQL
BEGIN;
LOCK TABLE ... IN SHARE MODE;
SET LOCAL WORK_MEM = '256MB';
ALTER TABLE ...;
COMMIT;
@ufuk
ufuk / TurkishNumberUtils.java
Last active November 13, 2020 07:55
Util for converting whole numbers to Turkish words
public final class TurkishNumberUtils {
private static final String SPACE = " ";
private static final String EMPTY = "";
private static final String[] PERIOD_NAMES = {EMPTY, "bin", "milyon", "milyar", "trilyon", "katrilyon", "kentilyon"};
private static final String[] UNITS_TEXTS = {EMPTY, "bir", "iki", "üç", "dört", "beş", "altı", "yedi", "sekiz", "dokuz"};