From 01ad4d885555ec887fcd5fd7a79f978b3f38a960 Mon Sep 17 00:00:00 2001 From: marcin witkowski Date: Sun, 16 Oct 2022 20:34:41 +0200 Subject: [PATCH] Classes 2022 --- pom.xml | 13 ++++++ .../java/second/junit/HttpQueryClass.java | 45 +++++++++++++++++++ src/main/java/second/junit/Main.java | 10 +++++ src/main/java/second/junit/ProcessQuery.java | 17 +++++++ src/main/resources/log4j.properties | 2 +- src/main/resources/test | 1 - .../java/second/junit/AdvanceMathTest.java | 3 +- src/test/java/second/junit/MockTest.java | 32 +++++++++++++ 8 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 src/main/java/second/junit/HttpQueryClass.java create mode 100644 src/main/java/second/junit/Main.java create mode 100644 src/main/java/second/junit/ProcessQuery.java delete mode 100644 src/main/resources/test create mode 100644 src/test/java/second/junit/MockTest.java diff --git a/pom.xml b/pom.xml index 8f6a1a6..775d26b 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,19 @@ 4.12 test + + org.apache.httpcomponents + httpclient + 4.5.10 + + + + org.mockito + mockito-core + 4.8.0 + test + + \ No newline at end of file diff --git a/src/main/java/second/junit/HttpQueryClass.java b/src/main/java/second/junit/HttpQueryClass.java new file mode 100644 index 0000000..6f57750 --- /dev/null +++ b/src/main/java/second/junit/HttpQueryClass.java @@ -0,0 +1,45 @@ +package second.junit; + +import org.apache.http.Header; +import org.apache.http.HttpEntity; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; + +public class HttpQueryClass { + + public String query() { + String result = "none"; + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet request = new HttpGet("https://api.gios.gov.pl/pjp-api/rest/station/findAll"); + + // add request headers + request.addHeader("custom-key", "programming"); + + try (CloseableHttpResponse response = httpclient.execute(request)) { + + // Get HttpResponse Status + System.out.println(response.getStatusLine().toString()); + + HttpEntity entity = response.getEntity(); + Header headers = entity.getContentType(); + System.out.println(headers); + + if (entity != null) { + // return it as a String + result = EntityUtils.toString(entity); + System.out.println(result); + } + } catch (ClientProtocolException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + return result; + } +} diff --git a/src/main/java/second/junit/Main.java b/src/main/java/second/junit/Main.java new file mode 100644 index 0000000..cae3e9d --- /dev/null +++ b/src/main/java/second/junit/Main.java @@ -0,0 +1,10 @@ +package second.junit; + +public class Main { + + public static void main(final String... args) { + ProcessQuery processQuery = new ProcessQuery(new HttpQueryClass()); + System.out.println(processQuery.process()); + } + +} diff --git a/src/main/java/second/junit/ProcessQuery.java b/src/main/java/second/junit/ProcessQuery.java new file mode 100644 index 0000000..cb31f22 --- /dev/null +++ b/src/main/java/second/junit/ProcessQuery.java @@ -0,0 +1,17 @@ +package second.junit; + +import java.util.Locale; + +public class ProcessQuery { + + HttpQueryClass queryClass; + + public ProcessQuery(HttpQueryClass queryClass) { + this.queryClass = queryClass; + } + + public String process() { + return queryClass.query().toUpperCase(); + } + +} diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties index 6225f22..d0e7050 100644 --- a/src/main/resources/log4j.properties +++ b/src/main/resources/log4j.properties @@ -1,5 +1,5 @@ # Root logger option -log4j.rootLogger=DEBUG, stdout, file +log4j.rootLogger=INFO, stdout, file # Redirect log messages to console log4j.appender.stdout=org.apache.log4j.ConsoleAppender diff --git a/src/main/resources/test b/src/main/resources/test deleted file mode 100644 index a229f18..0000000 --- a/src/main/resources/test +++ /dev/null @@ -1 +0,0 @@ -test resource file \ No newline at end of file diff --git a/src/test/java/second/junit/AdvanceMathTest.java b/src/test/java/second/junit/AdvanceMathTest.java index a622ab5..a7b3946 100644 --- a/src/test/java/second/junit/AdvanceMathTest.java +++ b/src/test/java/second/junit/AdvanceMathTest.java @@ -1,4 +1,3 @@ - package second.junit; import org.junit.Assert; @@ -20,7 +19,7 @@ public class AdvanceMathTest { @Test public void additionTest() { Integer a = math.addition(1, 4); - assertTrue(a == 5); + Assert.assertTrue(a == 5); } @Test diff --git a/src/test/java/second/junit/MockTest.java b/src/test/java/second/junit/MockTest.java new file mode 100644 index 0000000..7d08161 --- /dev/null +++ b/src/test/java/second/junit/MockTest.java @@ -0,0 +1,32 @@ +package second.junit; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +public class MockTest { + + HttpQueryClass httpQueryClass; + + @Before + public void setUp() { + System.out.println("Run setUp"); + httpQueryClass = Mockito.mock(HttpQueryClass.class); + } + + @Test + public void mockTestExample() { + Mockito.when(httpQueryClass.query()).thenReturn("test"); + + ProcessQuery processQuery = new ProcessQuery(httpQueryClass); + + String result = processQuery.process(); + assertThat(result).isEqualTo("TEST"); + } + +}