Seth před 6 roky
rodič
revize
bbae74a939

+ 13
- 3
Client/Client.iml Zobrazit soubor

@@ -5,14 +5,24 @@
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/main/resources" type="java-resource" />
9
-      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
8
+      <sourceFolder url="file://$MODULE_DIR$/src/test" isTestSource="true" />
10 9
       <excludeFolder url="file://$MODULE_DIR$/target" />
11 10
     </content>
12 11
     <orderEntry type="inheritedJdk" />
13 12
     <orderEntry type="sourceFolder" forTests="false" />
13
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.3.6" level="project" />
14
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.3.3" level="project" />
15
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.3" level="project" />
16
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.6" level="project" />
17
+    <orderEntry type="library" name="Maven: com.mashape.unirest:unirest-java:1.4.9" level="project" />
18
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.7" level="project" />
19
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpasyncclient:4.0.2" level="project" />
20
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore-nio:4.3.2" level="project" />
21
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.3.6" level="project" />
22
+    <orderEntry type="library" name="Maven: org.json:json:20140107" level="project" />
23
+    <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
24
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
14 25
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
15 26
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
16
-    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />
17 27
   </component>
18 28
 </module>

+ 37
- 1
Client/pom.xml Zobrazit soubor

@@ -10,6 +10,42 @@
10 10
     <modelVersion>4.0.0</modelVersion>
11 11
 
12 12
     <artifactId>Client</artifactId>
13
-
13
+<dependencies><dependency>
14
+    <groupId>org.apache.httpcomponents</groupId>
15
+    <artifactId>httpclient</artifactId>
16
+    <version>4.3.6</version>
17
+</dependency>
18
+    <dependency>
19
+        <groupId>com.mashape.unirest</groupId>
20
+        <artifactId>unirest-java</artifactId>
21
+        <version>1.4.9</version>
22
+    </dependency>
23
+    <dependency>
24
+        <groupId>com.fasterxml.jackson.core</groupId>
25
+        <artifactId>jackson-core</artifactId>
26
+        <version>2.9.7</version>
27
+    </dependency>
28
+    <dependency>
29
+        <groupId>org.apache.httpcomponents</groupId>
30
+        <artifactId>httpasyncclient</artifactId>
31
+        <version>4.0.2</version>
32
+    </dependency>
33
+    <dependency>
34
+        <groupId>org.apache.httpcomponents</groupId>
35
+        <artifactId>httpmime</artifactId>
36
+        <version>4.3.6</version>
37
+    </dependency>
38
+    <dependency>
39
+        <groupId>org.json</groupId>
40
+        <artifactId>json</artifactId>
41
+        <version>20140107</version>
42
+    </dependency>
43
+    <dependency>
44
+        <groupId>junit</groupId>
45
+        <artifactId>junit</artifactId>
46
+        <version>RELEASE</version>
47
+        <scope>test</scope>
48
+    </dependency>
49
+</dependencies>
14 50
 
15 51
 </project>

+ 20
- 0
Client/src/main/java/Console.java Zobrazit soubor

@@ -0,0 +1,20 @@
1
+import java.util.Scanner;
2
+
3
+public class Console {
4
+Scanner input = new Scanner(System.in);
5
+
6
+    public String jsonFormatter(String name, String id) {
7
+
8
+        return "{\n" +
9
+                "\t\"userid\" : \"-\", \n" +
10
+                "\t\"name\" : " + name + ",\n" +
11
+                "\t\"github\" : " + id + "\n}";
12
+    }
13
+
14
+    public String nameAndId(String output) {
15
+        System.out.println(output);
16
+        String name = input.next();
17
+        String id = input.next();
18
+        return jsonFormatter(name, id);
19
+    }
20
+}

+ 24
- 8
Client/src/main/java/SimpleShell.java Zobrazit soubor

@@ -1,3 +1,8 @@
1
+import com.fasterxml.jackson.databind.ObjectMapper;
2
+import com.mashape.unirest.http.JsonNode;
3
+import com.mashape.unirest.http.exceptions.UnirestException;
4
+import org.json.JSONArray;
5
+
1 6
 import java.io.BufferedReader;
2 7
 import java.io.IOException;
3 8
 import java.io.InputStream;
@@ -8,11 +13,16 @@ import java.util.List;
8 13
 public class SimpleShell {
9 14
 
10 15
 
11
-    public static void prettyPrint(String output) {
12
-        // yep, make an effort to format things nicely, eh?
13
-        System.out.println(output);
16
+    public static void prettyPrint(JSONArray jsonNode) {
17
+        try {
18
+            ObjectMapper mapper = new ObjectMapper();
19
+            Object json = mapper.readValue(jsonNode.toString(), Object.class);
20
+            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
21
+        } catch (Exception e) {
22
+            System.out.println("Sorry, pretty print didn't work");
23
+        }
14 24
     }
15
-    public static void main(String[] args) throws java.io.IOException {
25
+    public static void main(String[] args) throws java.io.IOException, UnirestException {
16 26
 
17 27
         YouAreEll webber = new YouAreEll();
18 28
         String commandLine;
@@ -24,6 +34,7 @@ public class SimpleShell {
24 34
         int index = 0;
25 35
         //we break out with <ctrl c>
26 36
         while (true) {
37
+            Console con = new Console();
27 38
             //read what the user enters
28 39
             System.out.println("cmd? ");
29 40
             commandLine = console.readLine();
@@ -60,15 +71,20 @@ public class SimpleShell {
60 71
 
61 72
                 // ids
62 73
                 if (list.contains("ids")) {
63
-                    String results = webber.get_ids();
64
-                    SimpleShell.prettyPrint(results);
74
+                    webber.get_ids();
75
+                    //SimpleShell.prettyPrint(results);
65 76
                     continue;
66 77
                 }
67 78
 
68 79
                 // messages
69 80
                 if (list.contains("messages")) {
70
-                    String results = webber.get_messages();
71
-                    SimpleShell.prettyPrint(results);
81
+                    webber.get_messages();
82
+                   // SimpleShell.prettyPrint(results);
83
+                    continue;
84
+                }
85
+
86
+                if (list.contains("postid")) {
87
+                    webber.postId(con.nameAndId("Enter name and id"));
72 88
                     continue;
73 89
                 }
74 90
                 // you need to add a bunch more.

+ 46
- 6
Client/src/main/java/YouAreEll.java Zobrazit soubor

@@ -1,20 +1,60 @@
1
+import com.fasterxml.jackson.core.JsonProcessingException;
2
+import com.fasterxml.jackson.databind.ObjectMapper;
3
+import com.mashape.unirest.http.HttpResponse;
4
+import com.mashape.unirest.http.JsonNode;
5
+import com.mashape.unirest.http.Unirest;
6
+import com.mashape.unirest.http.exceptions.UnirestException;
7
+
8
+
1 9
 public class YouAreEll {
10
+    private String URL = "http://zipcode.rocks:8085/";
11
+    Console console = new Console();
2 12
 
3 13
     YouAreEll() {
4 14
     }
5 15
 
16
+
6 17
     public static void main(String[] args) {
7 18
         YouAreEll urlhandler = new YouAreEll();
8
-        System.out.println(urlhandler.MakeURLCall("/ids", "GET", ""));
9
-        System.out.println(urlhandler.MakeURLCall("/messages", "GET", ""));
19
+        System.out.println(urlhandler.MakeURLCall("http://zipcode.rocks:8085/ids", "GET", ""));
20
+        System.out.println(urlhandler.MakeURLCall("http://zipcode.rocks:8085/messages", "GET", ""));
21
+    }
22
+
23
+    public HttpResponse<JsonNode> get_ids() throws UnirestException {
24
+        HttpResponse<JsonNode> request = null;
25
+        try {
26
+            request = Unirest.get(URL + "/ids")
27
+                    .header("accept", "application/json")
28
+                    .asJson();
29
+            SimpleShell.prettyPrint(request.getBody().getArray());
30
+        } catch (
31
+                UnirestException e) {
32
+            System.out.println("Server Error");
33
+        }
34
+        return request;
10 35
     }
11 36
 
12
-    public String get_ids() {
13
-        return MakeURLCall("/ids", "GET", "");
37
+    public void get_messages() {
38
+        try { HttpResponse<JsonNode> request = Unirest.get(URL + "/messages")
39
+                .header("accept", "application/json")
40
+                .asJson();
41
+            SimpleShell.prettyPrint(request.getBody().getArray());
42
+        } catch (UnirestException e) {
43
+            System.out.println("Server Error");
44
+        }
14 45
     }
15 46
 
16
-    public String get_messages() {
17
-        return MakeURLCall("/messages", "GET", "");
47
+    public HttpResponse<JsonNode> postId(String payload) throws UnirestException {
48
+       HttpResponse<JsonNode> jSonResponse = null;
49
+        try{
50
+            jSonResponse = Unirest.post(URL + "/ids")
51
+                    .header("accept", "application/json")
52
+                    .body(payload)
53
+                    .asJson();
54
+        } catch (UnirestException e) {
55
+            e.printStackTrace();
56
+        }
57
+        return jSonResponse;
18 58
     }
19 59
 
20 60
     public String MakeURLCall(String mainurl, String method, String jpayload) {

+ 37
- 0
Client/src/test/YouAreEllTest.java Zobrazit soubor

@@ -0,0 +1,37 @@
1
+import com.mashape.unirest.http.HttpResponse;
2
+import com.mashape.unirest.http.JsonNode;
3
+import com.mashape.unirest.http.exceptions.UnirestException;
4
+import org.junit.Assert;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+public class YouAreEllTest {
9
+    Console console;
10
+    YouAreEll test;
11
+    String payload;
12
+
13
+
14
+
15
+
16
+    @Before
17
+    public void setup() {
18
+        test =  new YouAreEll();
19
+        console = new Console();
20
+        payload = console.jsonFormatter("Bob", "Gob");
21
+    }
22
+
23
+    @Test
24
+    public void postIdTest() throws UnirestException {
25
+        HttpResponse<JsonNode> tester = test.postId(payload);
26
+        Assert.assertEquals(200,tester.getStatus());
27
+    }
28
+
29
+    @Test
30
+    public void postGetId() throws UnirestException {
31
+        HttpResponse<JsonNode> requester = test.get_ids();
32
+        Assert.assertEquals(200, requester.getStatus());
33
+    }
34
+
35
+
36
+
37
+}

+ 9
- 0
YouAreEll.iml Zobrazit soubor

@@ -11,5 +11,14 @@
11 11
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
12 12
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
13 13
     <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />
14
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.3.6" level="project" />
15
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.3.3" level="project" />
16
+    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.3" level="project" />
17
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.6" level="project" />
18
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpasyncclient:4.0.2" level="project" />
19
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore-nio:4.3.2" level="project" />
20
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpmime:4.3.6" level="project" />
21
+    <orderEntry type="library" name="Maven: org.json:json:20140107" level="project" />
22
+    <orderEntry type="library" name="Maven: com.mashape.unirest:unirest-java:1.4.9" level="project" />
14 23
   </component>
15 24
 </module>

+ 25
- 0
pom.xml Zobrazit soubor

@@ -18,5 +18,30 @@
18 18
             <artifactId>jackson-databind</artifactId>
19 19
             <version>2.8.6</version>
20 20
         </dependency>
21
+        <dependency>
22
+            <groupId>org.apache.httpcomponents</groupId>
23
+            <artifactId>httpclient</artifactId>
24
+            <version>4.3.6</version>
25
+        </dependency>
26
+        <dependency>
27
+            <groupId>org.apache.httpcomponents</groupId>
28
+            <artifactId>httpasyncclient</artifactId>
29
+            <version>4.0.2</version>
30
+        </dependency>
31
+        <dependency>
32
+            <groupId>org.apache.httpcomponents</groupId>
33
+            <artifactId>httpmime</artifactId>
34
+            <version>4.3.6</version>
35
+        </dependency>
36
+        <dependency>
37
+            <groupId>org.json</groupId>
38
+            <artifactId>json</artifactId>
39
+            <version>20140107</version>
40
+        </dependency>
41
+        <dependency>
42
+            <groupId>com.mashape.unirest</groupId>
43
+            <artifactId>unirest-java</artifactId>
44
+            <version>1.4.9</version>
45
+        </dependency>
21 46
     </dependencies>
22 47
 </project>