Skip to content

Instantly share code, notes, and snippets.

View yusufcakal's full-sized avatar
:octocat:
Leave your code better than you found it

Yusuf Çakal yusufcakal

:octocat:
Leave your code better than you found it
View GitHub Profile
@yusufcakal
yusufcakal / SellerLabel.java
Created August 16, 2020 13:28
SellerLabel
public enum SellerLabel {
NEW_SELLER,
SELLER_1,
SELLER_5,
SELLER_20;
public boolean isSellerLabelNotNewSeller() {
return SellerLabel.NEW_SELLER != this;
}
@yusufcakal
yusufcakal / SellerLabelProcessor.java
Created August 16, 2020 13:25
SellerLabelProcessor
public interface SellerLabelProcessor {
SellerLabel getSellerLabel(int sellCount);
}
@yusufcakal
yusufcakal / SellerCenterService.java
Created August 16, 2020 13:24
SellerCenterService
@Service
public class SellerCenterService {
private final List<SellerLabelProcessor> sellerLabelProcessors;
public SellerCenterService(List<SellerLabelProcessor> sellerLabelProcessors) {
this.sellerLabelProcessors = sellerLabelProcessors;
}
public SellerCenterLabel getSellerCenterLabel(Seller seller) {
@yusufcakal
yusufcakal / pom.xml
Created August 16, 2020 13:15
Junit and Mockito Dependencies
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@yusufcakal
yusufcakal / ParameterizedTests.java
Created August 11, 2020 20:58
Java JUnit5 Parameterized Tests Example
@DisplayName("should convert from page type to notification type")
public class PageTypeNotificationTypeListConverterTest {
@ParameterizedTest
@MethodSource("pageTypeMapped")
public void shouldConvertPageTypeToNotificationType(PageType pageType, List<NotificationType> notificationTypes) {
final PageTypeNotificationTypeListConverter pageTypeNotificationTypeListConverter = new PageTypeNotificationTypeListConverter();
assertEquals(notificationTypes, pageTypeNotificationTypeListConverter.convert(pageType));
}
@yusufcakal
yusufcakal / Clock.java
Last active August 16, 2020 13:22
freeze/unfreeze operations date in java / virtual clock pattern
import java.util.Calendar;
import java.util.Date;
public final class Clock {
private static boolean isFrozen;
private static Date timeSet;
private Clock() {
@yusufcakal
yusufcakal / DatabaseConfiguration.java
Last active June 20, 2020 10:24
Using TransactionalReadOnly annotation to change data source in runtime for Spring
@Configuration
@EnableTransactionManagement
public class DatabaseConfiguration {
@Bean
@Primary
@Profile("!test")
@DependsOn({"primaryDataSource", "replicaDataSource"})
public DataSource dataSource(@Qualifier("primaryDataSource") HikariDataSource primaryDataSource,
@Qualifier("replicaDataSource") HikariDataSource replicaDataSource) {
@yusufcakal
yusufcakal / RequestResponseLoggingInterceptor.java
Created April 9, 2020 15:01
For using display logs that request and response to any http request
@Component
public class RequestResponseLoggingInterceptor implements ClientHttpRequestInterceptor {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
logRequest(request, body);
ClientHttpResponse response = execution.execute(request, body);
logResponse(response);
@yusufcakal
yusufcakal / TextMasker.java
Created January 22, 2020 11:31
It can use text mask to bounds of text certain begin and end indices. Enjoy your data driven test via spock framework :)
public class TextMasker {
public static String mask(String text, int begin, int end) {
if (Objects.isNull(text)) {
return null;
}
if (areIndexesInBoundOfText(text, begin, end)) {
String firstDisplayablePart = text.substring(0, begin);
String secondDisplayablePart = text.substring(end);
@yusufcakal
yusufcakal / Slf4jMDCFilter.java
Last active January 22, 2020 06:56
Gelen her request'e ait özel bir referans numarası ile log'un takip edilmesi
@Component
public class Slf4jMDCFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws java.io.IOException, ServletException {
MDC.put("requestId", "Request ID: " + UUID.randomUUID().toString());
chain.doFilter(request, response);
}
}