Posts

Showing posts from April, 2019

GemfireConfig

package com.mridul.software.automation.spring.configuration; import com.gemstone.gemfire.cache.Region; import com.gemstone.gemfire.cache.client.ClientCache; import com.gemstone.gemfire.cache.client.ClientCacheFactory; import com.gemstone.gemfire.cache.client.ClientRegionShortcut; import com.gemstone.gemfire.internal.concurrent.ConcurrentHashSet; import com.mridul.software.automation.spring.context.properties.gemfire.GemfireCustomProperties; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.gemfire.GemfireTemplate; import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories; import javax.annotation.PreDestroy; import java.util.Map; import java.util.

FileSystemUtils

package com.mridul.software.automation.spring.context.utils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Component; import java.io.File; @Slf4j @Component public class FileSystemUtils { public void deleteFileIfExistsAndReCreate(final String fileName) { try { deleteFileIfExists(fileName); FileUtils.touch(new File(fileName)); } catch (final Exception e) { log.debug("File {} could not be created due to {}", fileName, e); } } public void deleteFileIfExists(final String fileName) { try { File file = new File(fileName); if (file.exists()) { org.springframework.util.FileSystemUtils.deleteRecursively(file); log.debug("Delete file : {}", fileName); } } catch (final Exception e) { log.debug("File {} could not be created due to {}", fileName, e);

Report

package com.mridul.software.automation.spring.context.properties.extent; import lombok.Data; import org.apache.commons.lang3.StringUtils; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.validation.annotation.Validated; import javax.validation.constraints.NotNull; @Configuration @ConfigurationProperties(prefix = "extent.report") @Validated @Data public class Report { @NotNull private String fileName; @NotNull private Screenshot screenshot; @Data public static class Screenshot { @NotNull private String filePath; @NotNull private Test test; @Data public static class Test { @NotNull private Enable enable; //this parameter is set in extent test listener private static final ThreadLocal<String> METHOD_NAME =

ExtentLoggingClientHttpRequestInterceptor

package com.mridul.software.automation.rest.interceptors; import com.aventstack.extentreports.ExtentTest; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import org.springframework.util.StreamUtils; import java.io.IOException; import java.nio.charset.Charset; import java.util.Objects; @Slf4j public class ExtentLoggingClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { private static final ThreadLocal<ExtentTest> TEST_THREAD_LOCAL = new ThreadLocal<>(); public void setExtentTest(ExtentTest test) { TEST_THREAD_LOCAL.set(test); }

AbstractJson

package com.mridul.software.automation.rest.domain; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import lombok.extern.slf4j.Slf4j; @Slf4j public abstract class AbstractJson { private static final ObjectMapper MAPPER = new ObjectMapper() .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); private static final ObjectWriter WRITER = MAPPER.writerWithDefaultPrettyPrinter(); private String toJson() { String json = null; try { json = WRITER.writeValueAsString(this); } catch (final Exception e) { throw new RuntimeException("Not able to serialize to json : " + e); } return json; } public final <T extends AbstractJson> T fromJson(String json, Class<T> clazz) { T t = nul

ExtentLoggingRestTemplateCustomizer

package com.mridul.software.automation.rest.customizers; import com.mridul.software.automation.rest.interceptors.ExtentLoggingClientHttpRequestInterceptor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.client.RestTemplateCustomizer; import org.springframework.web.client.RestTemplate; @Slf4j public class ExtentLoggingRestTemplateCustomizer implements RestTemplateCustomizer { @Autowired private ExtentLoggingClientHttpRequestInterceptor interceptor; @Override public void customize(RestTemplate restTemplate) { restTemplate.getInterceptors().add(interceptor); } }

TakeScreenshotAspect

package com.mridul.software.automation.aspects.webdriver; import com.google.common.base.Preconditions; import com.mridul.software.automation.spring.context.AppPropertiesCtx; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import java.io.File; import java.util.LinkedHashSet; import java.util.Set; @Aspect @Slf4j public class TakeScreenshotAspect { @Autowired private AppPropertiesCtx config; private final static ThreadLocal<Set<String>> FILENAME_THREAD_LOCAL = ThreadLocal.withInitial(LinkedHashSet::new); @Pointcut("execution(** org.openqa.selenium.TakesScreenshot.getScreenshotAs(..))") public void interceptTakeScreenshot() { } @AfterReturning(value = "interceptT

ExtentTestEventListener

package com.mridul.software.automation.listeners; import com.aventstack.extentreports.ExtentTest; import com.mridul.software.automation.tests.TestEvent; import org.springframework.context.ApplicationListener; import static com.google.common.base.Preconditions.checkNotNull; public class ExtentTestEventListener implements ApplicationListener<TestEvent> { private static final ThreadLocal<ExtentTest> TEST_THREAD_LOCAL = new ThreadLocal<>(); public void setExtentTest(ExtentTest test) { TEST_THREAD_LOCAL.set(test); } @Override public void onApplicationEvent(TestEvent event) { ExtentTest test = TEST_THREAD_LOCAL.get(); checkNotNull(event, "test event is not received!"); checkNotNull(test, "extent test not initialized to receive events!"); switch (event.getTestEventType()) { case INFO: test.info(event.getMessage()); break; case WARNING:

ExtentTestExecutionListener

package com.mridul.software.automation.listeners; import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentTest; import com.mridul.software.automation.aspects.webdriver.TakeScreenshotAspect; import com.mridul.software.automation.rest.interceptors.ExtentLoggingClientHttpRequestInterceptor; import com.mridul.software.automation.spring.context.AppPropertiesCtx; import com.mridul.software.automation.spring.context.UtilsCtx; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Component; import org.springframework.test.context.TestContext; import org.springframework.test.context.support.AbstractTestExecutionListener; import org.testng.ITestContext; import org.testng.ITestResult; import org.testng.Reporter; import java.io.File; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Base64; import java.util.List; imp

AbstractTestNgBaseTest

package com.swacorp.qmo.opssuite; import com.swacorp.qmo.opssuite.listeners.ExtentTestExecutionListener; import com.swacorp.qmo.opssuite.tests.TestAgent; import lombok.extern.slf4j.Slf4j; import org.assertj.core.api.SoftAssertions; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.springframework.web.client.RestTemplate; @SpringBootTest(classes = ApplicationConfig.class) @TestExecutionListeners(value = {ExtentTestExecutionListener.class}, mergeMode = TestExecutionListeners.MergeMode.REPLACE_DEFAULTS) @Slf4j public abstract class AbstractTestNgBaseTest extends AbstractTestNGSpringContextTests { private TestAgent testAgent; private SoftAssertions softAssertions; private RestTemplate restTemplate; public TestAgent getTestAgent() { return explicitlyWireBean(TestA

Regex username validator specific to use case

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Sample data : * "C:\Program Files\Java\jdk1.8.0_192\bin\java.exe" * Joe$Schmoe002 * Bob$Smith * Bob$$Smith * JaneAusten * 001Smith * $Smith * joe$Schmoe002 * Joe$Schmoe0023 * Joe$ Schmoe002 * Joe$schmoe0023 * J$SJoe$Schmoe002 * Bob$Smith * Bob$$Smith * JaneAusten * 001Smith * $Smith * joe$Schmoe002 * Joe$Schmoe0023 * Joe$ Schmoe002 * Joe$schmoe0023 * ^D * J$S * Username : Joe$Schmoe002 is true * Username : Bob$Smith is true * Username : Bob$$Smith is false * Username : JaneAusten is false * Username : 001Smith is false * Username : $Smith is false * Username : joe$Schmoe002 is

Frequency counter of words in a String

import java.util.*; /** * Solution is in java 8 * press CTRL-D on new line in Unix or macOS to terminate, CTRL-Z on new line in Windows * * Sample Result: * "C:\Program Files\Java\jdk1.8.0_192\bin\java.exe" * Mightymighty * Hellohello * ^D * Original Text : Mightymighty * Parsed Text :g2h2i2m2t2y2 * Original Text : Hellohello * Parsed Text :e2h2l4o2 * * Process finished with exit code 0 * */ public class StringFrequencyCounter { public static void main(String[] args) { StringFrequencyCounter counter = new StringFrequencyCounter(); List<String> candidates = counter.validateInputAndGetValue(); candidates.forEach(e -> { System.out.format("Original Text : %s%n", e); Map<String, Integer> tokenFrequencyMap = counter.parsedString(e); System.out.print("Parsed Text :"); tokenFrequencyMap.forEach((k, v) -> System.out

Convert Integers Dollar Amounts to English Words

/** * Solution is in java * <p> * "C:\Program Files\Java\jdk1.8.0_192\bin\java.exe" * Input : 987654 * NineHundredEightySevenThousandSixHundredFiftyFourDollars * Input : 111111111 * OneHundredElevenMillionOneHundredElevenThousandOneHundredElevenDollars * Input : 911 * NineHundredElevenDollars * <p> * Process finished with exit code 0 */ import java.util.*; import java.util.concurrent.ThreadLocalRandom; public class DollarsToEnglishConverter { private static final Map<Integer, String> DIGIT_TO_STRING = new HashMap<>(); static { DIGIT_TO_STRING.put(1, "One"); DIGIT_TO_STRING.put(2, "Two"); DIGIT_TO_STRING.put(3, "Three"); DIGIT_TO_STRING.put(4, "Four"); DIGIT_TO_STRING.put(5, "Five"); DIGIT_TO_STRING.put(6, "Six"); DIGIT_TO_STRING.put(7, "Seven"); DIGIT_TO_STRING