Trinh Tong před 6 roky
rodič
revize
30291dc01b

+ 33
- 0
Client/Client.iml Zobrazit soubor

@@ -5,6 +5,7 @@
5 5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6 6
     <content url="file://$MODULE_DIR$">
7 7
       <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8
+      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
8 9
       <excludeFolder url="file://$MODULE_DIR$/target" />
9 10
     </content>
10 11
     <orderEntry type="inheritedJdk" />
@@ -20,5 +21,37 @@
20 21
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.7" level="project" />
21 22
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-json-org:2.9.7" level="project" />
22 23
     <orderEntry type="library" name="Maven: org.json:json:20171018" level="project" />
24
+    <orderEntry type="library" scope="TEST" name="Maven: com.github.tomakehurst:wiremock:2.19.0" level="project" />
25
+    <orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-server:9.2.24.v20180105" level="project" />
26
+    <orderEntry type="library" scope="TEST" name="Maven: javax.servlet:javax.servlet-api:3.1.0" level="project" />
27
+    <orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-http:9.2.24.v20180105" level="project" />
28
+    <orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-io:9.2.24.v20180105" level="project" />
29
+    <orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-servlet:9.2.24.v20180105" level="project" />
30
+    <orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-security:9.2.24.v20180105" level="project" />
31
+    <orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-servlets:9.2.24.v20180105" level="project" />
32
+    <orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-continuation:9.2.24.v20180105" level="project" />
33
+    <orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-util:9.2.24.v20180105" level="project" />
34
+    <orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-webapp:9.2.24.v20180105" level="project" />
35
+    <orderEntry type="library" scope="TEST" name="Maven: org.eclipse.jetty:jetty-xml:9.2.24.v20180105" level="project" />
36
+    <orderEntry type="library" scope="TEST" name="Maven: com.google.guava:guava:20.0" level="project" />
37
+    <orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.5.1" level="project" />
38
+    <orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-legacy:2.5.1" level="project" />
39
+    <orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.4.0" level="project" />
40
+    <orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.3" level="project" />
41
+    <orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:1.2" level="project" />
42
+    <orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:5.0.4" level="project" />
43
+    <orderEntry type="library" scope="TEST" name="Maven: org.slf4j:slf4j-api:1.7.12" level="project" />
44
+    <orderEntry type="library" scope="TEST" name="Maven: net.sf.jopt-simple:jopt-simple:5.0.3" level="project" />
45
+    <orderEntry type="library" scope="TEST" name="Maven: org.apache.commons:commons-lang3:3.7" level="project" />
46
+    <orderEntry type="library" scope="TEST" name="Maven: com.flipkart.zjsonpatch:zjsonpatch:0.4.4" level="project" />
47
+    <orderEntry type="library" scope="TEST" name="Maven: com.github.jknack:handlebars:4.0.7" level="project" />
48
+    <orderEntry type="library" scope="TEST" name="Maven: org.antlr:antlr4-runtime:4.7.1" level="project" />
49
+    <orderEntry type="library" scope="TEST" name="Maven: com.github.jknack:handlebars-helpers:4.0.7" level="project" />
50
+    <orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.2.0" level="project" />
51
+    <orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.0.0" level="project" />
52
+    <orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.1.0" level="project" />
53
+    <orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.2.0" level="project" />
54
+    <orderEntry type="library" scope="TEST" name="Maven: org.testng:testng:7.0.0-beta1" level="project" />
55
+    <orderEntry type="library" scope="TEST" name="Maven: com.beust:jcommander:1.72" level="project" />
23 56
   </component>
24 57
 </module>

+ 18
- 0
Client/pom.xml Zobrazit soubor

@@ -46,6 +46,24 @@
46 46
             <version>2.9.7</version>
47 47
             <scope>compile</scope>
48 48
         </dependency>
49
+        <dependency>
50
+            <groupId>com.github.tomakehurst</groupId>
51
+            <artifactId>wiremock</artifactId>
52
+            <version>2.19.0</version>
53
+            <scope>test</scope>
54
+        </dependency>
55
+        <dependency>
56
+            <groupId>org.junit.jupiter</groupId>
57
+            <artifactId>junit-jupiter-api</artifactId>
58
+            <version>5.2.0</version>
59
+            <scope>test</scope>
60
+        </dependency>
61
+        <dependency>
62
+            <groupId>org.testng</groupId>
63
+            <artifactId>testng</artifactId>
64
+            <version>7.0.0-beta1</version>
65
+            <scope>test</scope>
66
+        </dependency>
49 67
     </dependencies>
50 68
 
51 69
 

+ 3
- 4
Client/src/main/java/SimpleShell.java Zobrazit soubor

@@ -1,7 +1,6 @@
1
-import ids.Id;
2
-import ids.IdController;
3
-import message.MessageController;
4
-import message.SendController;
1
+import idtest.IdController;
2
+import messagetest.MessageController;
3
+import messagetest.SendController;
5 4
 import shellutilities.YouAreEll;
6 5
 
7 6
 import java.io.BufferedReader;

Client/src/main/java/ids/Id.java → Client/src/main/java/idtest/Id.java Zobrazit soubor

@@ -1,4 +1,4 @@
1
-package ids;
1
+package idtest;
2 2
 
3 3
 public class Id {
4 4
     public String userid, name, github;

Client/src/main/java/ids/IdController.java → Client/src/main/java/idtest/IdController.java Zobrazit soubor

@@ -1,4 +1,4 @@
1
-package ids;
1
+package idtest;
2 2
 
3 3
 import shellutilities.Console;
4 4
 import shellutilities.RequestType;

Client/src/main/java/ids/PrettyPrintId.java → Client/src/main/java/idtest/PrettyPrintId.java Zobrazit soubor

@@ -1,4 +1,4 @@
1
-package ids;
1
+package idtest;
2 2
 
3 3
 import com.fasterxml.jackson.core.type.TypeReference;
4 4
 import com.fasterxml.jackson.databind.ObjectMapper;

Client/src/main/java/message/Message.java → Client/src/main/java/messagetest/Message.java Zobrazit soubor

@@ -1,4 +1,4 @@
1
-package message;
1
+package messagetest;
2 2
 
3 3
 import com.fasterxml.jackson.annotation.JsonIgnore;
4 4
 

Client/src/main/java/message/MessageController.java → Client/src/main/java/messagetest/MessageController.java Zobrazit soubor

@@ -1,4 +1,4 @@
1
-package message;
1
+package messagetest;
2 2
 
3 3
 import shellutilities.Console;
4 4
 import shellutilities.RequestType;

Client/src/main/java/message/PrettyPrintMessage.java → Client/src/main/java/messagetest/PrettyPrintMessage.java Zobrazit soubor

@@ -1,4 +1,4 @@
1
-package message;
1
+package messagetest;
2 2
 
3 3
 import com.fasterxml.jackson.core.type.TypeReference;
4 4
 import com.fasterxml.jackson.databind.ObjectMapper;

Client/src/main/java/message/SendController.java → Client/src/main/java/messagetest/SendController.java Zobrazit soubor

@@ -1,6 +1,6 @@
1
-package message;
1
+package messagetest;
2 2
 
3
-import ids.IdController;
3
+import idtest.IdController;
4 4
 import shellutilities.Console;
5 5
 import shellutilities.RequestType;
6 6
 import shellutilities.YouAreEll;

+ 18
- 16
Client/src/main/java/shellutilities/YouAreEll.java Zobrazit soubor

@@ -14,30 +14,37 @@ import java.io.IOException;
14 14
 import java.io.InputStreamReader;
15 15
 
16 16
 public class YouAreEll {
17
-    private HttpClient client;
18
-    private final String baseUrl = "http://zipcode.rocks:8085";
17
+    public HttpClient client;
18
+    public final String baseUrl;
19 19
 
20 20
     public YouAreEll() {
21
+        this.baseUrl = "http://zipcode.rocks:8085"; // Default URL
21 22
         this.client = HttpClientBuilder.create().build();
22 23
     }
23 24
 
24
-    public static void main(String[] args) throws IOException {
25
+    public YouAreEll(String baseUrl) {
26
+        this.baseUrl = baseUrl;
27
+        this.client = HttpClientBuilder.create().build();
28
+    }
29
+
30
+    public static void main(String[] args) {
25 31
         YouAreEll urlhandler = new YouAreEll();
26 32
         // response.getStatusLine().getStatusCode(); // 200 OK, 404 Not Found, etc.
27 33
     }
28 34
 
29
-    public String MakeURLCall(String mainurl, RequestType req, String jpayload) throws IOException {
35
+    public String MakeURLCall(String mainUrl, RequestType req, String jsonPayload) {
30 36
         switch (req) {
31 37
             case GET:
32
-                return getCall(baseUrl + mainurl);
38
+                System.out.println("Entering get...");
39
+                return getCall(baseUrl + mainUrl);
33 40
 
34 41
             case POST:
35
-                HttpPost postReq = new HttpPost(baseUrl + mainurl);
36
-                return makeCall(postReq, jpayload);
42
+                HttpPost postReq = new HttpPost(baseUrl + mainUrl);
43
+                return makeCall(postReq, jsonPayload);
37 44
 
38 45
             case PUT:
39
-                HttpPut putReq = new HttpPut(baseUrl + mainurl);
40
-                return makeCall(putReq, jpayload);
46
+                HttpPut putReq = new HttpPut(baseUrl + mainUrl);
47
+                return makeCall(putReq, jsonPayload);
41 48
             default:
42 49
                 return "Could not make connection.";
43 50
         }
@@ -54,7 +61,7 @@ public class YouAreEll {
54 61
         }
55 62
     }
56 63
 
57
-    public String getCall(String url) {
64
+    private String getCall(String url) {
58 65
 
59 66
         HttpGet request = new HttpGet(url);
60 67
         try {
@@ -65,7 +72,7 @@ public class YouAreEll {
65 72
         }
66 73
     }
67 74
 
68
-    public String readCalls(HttpResponse response) throws IOException {
75
+    public String readCalls(HttpResponse response) {
69 76
         try {
70 77
             BufferedReader rd = new BufferedReader(
71 78
                     new InputStreamReader(response.getEntity().getContent()));
@@ -83,9 +90,4 @@ public class YouAreEll {
83 90
             return ("An unexpected IO error occurred: " + e.getMessage());
84 91
         }
85 92
     }
86
-
87
-
88 93
 }
89
-
90
-
91
-

+ 51
- 0
Client/src/test/java/idtest/IdControllerTest.java Zobrazit soubor

@@ -0,0 +1,51 @@
1
+package idtest;
2
+
3
+import static com.github.tomakehurst.wiremock.client.WireMock.*;
4
+import static org.testng.Assert.*;
5
+
6
+import com.github.tomakehurst.wiremock.WireMockServer;
7
+import org.apache.http.HttpResponse;
8
+import org.apache.http.client.methods.HttpGet;
9
+import org.testng.annotations.*;
10
+import shellutilities.YouAreEll;
11
+
12
+import java.io.IOException;
13
+
14
+import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
15
+
16
+public class IdControllerTest {
17
+    private WireMockServer wireMockServer;
18
+    private YouAreEll urlClient;
19
+
20
+    @BeforeSuite
21
+    public void beforeAll() {
22
+        wireMockServer = new WireMockServer(wireMockConfig().dynamicPort());
23
+        wireMockServer.start();
24
+        urlClient = new YouAreEll("http://localhost:" + wireMockServer.port());
25
+    }
26
+
27
+    @AfterSuite
28
+    public void afterAll() {
29
+        wireMockServer.shutdown();
30
+    }
31
+
32
+    @AfterTest
33
+    public void beforeEach() {
34
+        wireMockServer.resetAll();
35
+    }
36
+
37
+    @Test
38
+    public void getIdsSuccess() throws IOException {
39
+        String response = new Id("userId", "name", "githubId").toString().replace("\n", "");
40
+
41
+        wireMockServer.stubFor(
42
+            get(urlEqualTo("/ids"))
43
+                .willReturn(aResponse().withStatus(200).withBody(response))
44
+        );
45
+
46
+        HttpResponse result = urlClient.client.execute(new HttpGet(urlClient.baseUrl + "/ids"));
47
+
48
+        assertEquals(result.getStatusLine().getStatusCode(), 200);
49
+        assertEquals(urlClient.readCalls(result), response);
50
+    }
51
+}

+ 4
- 0
Client/src/test/java/messagetest/MessageControllerTest.java Zobrazit soubor

@@ -0,0 +1,4 @@
1
+package messagetest;
2
+
3
+public class MessageControllerTest {
4
+}