Skip to content

Instantly share code, notes, and snippets.

@LenarBad
LenarBad / ReadFileIntoListOfStrings.java
Created December 8, 2017 18:23
How to read file into String with Apache Commons IO
public List<String> getFileAsListOfStrings(String fileName) throws IOException {
InputStream input = this.getClass().getResourceAsStream("/" + fileName);
return IOUtils.readLines(input, StandardCharsets.UTF_8);
}
@Component
public class TestData extends FileReader {
@Value("${testData.users.file}")
private String usersFile;
public List<User> getUsers() {
Gson gson = new Gson();
return Arrays.asList(gson.fromJson(getFileAsString(usersFile), User[].class));
}
@LenarBad
LenarBad / Exclude Tomcat from Spring Boot in Maven.xml
Created December 22, 2017 22:21
How to exclude Tomcat from Spring Boot in Maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
@LenarBad
LenarBad / Choose Source for Default Value.groovy
Last active May 18, 2018 14:15
Jenkins dynamic Extended Choice Parameter with service call (Groovy Script)
// host will be passed in the groovy bindings for extended choice plugin.
def getVersion = { host, path ->
try {
def version = new groovy.json.JsonSlurper().parse(new URL("https://${host}${path}")).version
return version
} catch(Exception e) {
return null
}
}
@LenarBad
LenarBad / GET HTTP Request.java
Created February 2, 2018 21:50
How to send GET Request and get Response with Apache Http Components
String url = "https://www.some-web-service/api/endpoint";
HttpGet get = new HttpGet(url);
Header headers[] = { new BasicHeader("Accept", "application/json")};
get.setHeaders(headers);
CloseableHttpClient client = HttpClients.custom().build();
HttpResponse response = client.execute(get);
@LenarBad
LenarBad / Skip TestNG tests based on condition.java
Last active November 20, 2020 08:49
How to skip TestNG tests based on condition using IInvokedMethodListener
@Listeners(value = ConditionalSkipTestAnalyzer.class)
public class ExampleConditionalSkippingTest {
@NonProduction
@Test
public void test1() { }
@ProductionOnly
@Test
public void test2() { }
@LenarBad
LenarBad / HttpClient.java
Created February 26, 2018 16:52
Http Client with Apache Http Component
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
@LenarBad
LenarBad / HttpClientExampleIT.java
Last active March 1, 2018 00:57
Usage in Tests - Http Client with Apache Http Components
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import io.lenar.examples.spring.TestNGWithSpringApplication;
import io.lenar.examples.spring.http.HttpClient;
import io.lenar.examples.spring.model.Book;
import org.apache.commons.io.IOUtils;
@LenarBad
LenarBad / ServiceClient.java
Last active February 27, 2018 21:04
REST Service Client base on created HttpClient. Example for BookService
import java.io.IOException;
import io.lenar.examples.spring.clients.response.BookResponse;
import io.lenar.examples.spring.clients.response.BooksResponse;
import io.lenar.examples.spring.clients.response.Response;
import io.lenar.examples.spring.http.HttpClient;
import io.lenar.examples.spring.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@LenarBad
LenarBad / ResponseClassesForServiceClient.java
Last active February 27, 2018 21:03
Response Classes for service Client
public class Response {
private ResponseStatus status;
public Response(HttpResponse httpResponse) {
this.status = new ResponseStatus(httpResponse);
}
public ResponseStatus getStatus() {
return status;
}