|
| 1 | +package com.baeldung; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.net.HttpURLConnection; |
| 7 | +import java.net.URL; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import com.baeldung.domain.Book; |
| 12 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 13 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 14 | + |
| 15 | +import junit.framework.Test; |
| 16 | +import junit.framework.TestCase; |
| 17 | +import junit.framework.TestSuite; |
| 18 | + |
| 19 | +public class AppTest extends TestCase { |
| 20 | + |
| 21 | + ObjectMapper mapper = new ObjectMapper(); |
| 22 | + |
| 23 | + public AppTest( String testName ) { |
| 24 | + super( testName ); |
| 25 | + } |
| 26 | + |
| 27 | + public static Test suite() { |
| 28 | + return new TestSuite( AppTest.class ); |
| 29 | + } |
| 30 | + |
| 31 | + public void testApp() throws IOException, ClassNotFoundException { |
| 32 | + |
| 33 | + URL url; |
| 34 | + HttpURLConnection conn; |
| 35 | + BufferedReader br; |
| 36 | + String output; |
| 37 | + StringBuffer resp; |
| 38 | + Book book; |
| 39 | + Book temp; |
| 40 | + |
| 41 | + url = new URL("http://localhost:8080/AddBook/Odessy/YannMartel/GreenLeaves"); |
| 42 | + conn = (HttpURLConnection) url.openConnection(); |
| 43 | + conn.setRequestMethod("POST"); |
| 44 | + conn.getContent(); |
| 45 | + br = new BufferedReader(new InputStreamReader( |
| 46 | + (conn.getInputStream()))); |
| 47 | + resp = new StringBuffer(); |
| 48 | + |
| 49 | + while ((output = br.readLine()) != null) { |
| 50 | + resp.append(output); |
| 51 | + } |
| 52 | + |
| 53 | + book = mapper.readValue(resp.toString(), Book.class); |
| 54 | + temp = new Book("Odessy","YannMartel","GreenLeaves"); |
| 55 | + |
| 56 | + assertEquals(book, temp); |
| 57 | + |
| 58 | + url = new URL("http://localhost:8080/AddBook/Twilight/StephenieMeyer/LittleBrown"); |
| 59 | + conn = (HttpURLConnection) url.openConnection(); |
| 60 | + conn.setRequestMethod("POST"); |
| 61 | + br = new BufferedReader(new InputStreamReader( |
| 62 | + (conn.getInputStream()))); |
| 63 | + resp = new StringBuffer(); |
| 64 | + |
| 65 | + while ((output = br.readLine()) != null) { |
| 66 | + resp.append(output); |
| 67 | + } |
| 68 | + |
| 69 | + book = mapper.readValue(resp.toString(), Book.class); |
| 70 | + temp = new Book("Twilight","StephenieMeyer","LittleBrown"); |
| 71 | + |
| 72 | + assertEquals(book, temp); |
| 73 | + |
| 74 | + url = new URL("http://localhost:8080/ListOfBooks"); |
| 75 | + conn = (HttpURLConnection) url.openConnection(); |
| 76 | + conn.setRequestMethod("GET"); |
| 77 | + br = new BufferedReader(new InputStreamReader( |
| 78 | + (conn.getInputStream()))); |
| 79 | + resp = new StringBuffer(); |
| 80 | + |
| 81 | + while ((output = br.readLine()) != null) { |
| 82 | + resp.append(output); |
| 83 | + } |
| 84 | + |
| 85 | + List<Book> books = new ArrayList<Book>(); |
| 86 | + |
| 87 | + books.add(new Book("Odessy","YannMartel","GreenLeaves")); |
| 88 | + books.add(new Book("Twilight","StephenieMeyer","LittleBrown")); |
| 89 | + |
| 90 | + List<Book> listOfBooks = mapper.readValue(resp.toString(), new TypeReference<List<Book>>(){}); |
| 91 | + |
| 92 | + assertEquals(books, listOfBooks); |
| 93 | + |
| 94 | + url = new URL("http://localhost:8080/SearchBook/Twilight"); |
| 95 | + conn = (HttpURLConnection) url.openConnection(); |
| 96 | + conn.setRequestMethod("GET"); |
| 97 | + br = new BufferedReader(new InputStreamReader( |
| 98 | + (conn.getInputStream()))); |
| 99 | + resp = new StringBuffer(); |
| 100 | + |
| 101 | + while ((output = br.readLine()) != null) { |
| 102 | + resp.append(output); |
| 103 | + } |
| 104 | + |
| 105 | + book = mapper.readValue(resp.toString(), Book.class); |
| 106 | + temp = new Book("Twilight","StephenieMeyer","LittleBrown"); |
| 107 | + |
| 108 | + assertEquals(book, temp); |
| 109 | + |
| 110 | + url = new URL("http://localhost:8080/DeleteBook/Twilight"); |
| 111 | + conn = (HttpURLConnection) url.openConnection(); |
| 112 | + conn.setRequestMethod("DELETE"); |
| 113 | + br = new BufferedReader(new InputStreamReader( |
| 114 | + (conn.getInputStream()))); |
| 115 | + resp = new StringBuffer(); |
| 116 | + |
| 117 | + while ((output = br.readLine()) != null) { |
| 118 | + resp.append(output); |
| 119 | + } |
| 120 | + |
| 121 | + book = mapper.readValue(resp.toString(), Book.class); |
| 122 | + temp = new Book("Twilight","StephenieMeyer","LittleBrown"); |
| 123 | + |
| 124 | + assertEquals(book, temp); |
| 125 | + |
| 126 | + url = new URL("http://localhost:8080/ListOfBooks"); |
| 127 | + conn = (HttpURLConnection) url.openConnection(); |
| 128 | + conn.setRequestMethod("GET"); |
| 129 | + br = new BufferedReader(new InputStreamReader( |
| 130 | + (conn.getInputStream()))); |
| 131 | + resp = new StringBuffer(); |
| 132 | + |
| 133 | + while ((output = br.readLine()) != null) { |
| 134 | + resp.append(output); |
| 135 | + } |
| 136 | + |
| 137 | + books = new ArrayList<Book>(); |
| 138 | + |
| 139 | + books.add(new Book("Odessy","YannMartel","GreenLeaves")); |
| 140 | + listOfBooks = mapper.readValue(resp.toString(), new TypeReference<List<Book>>(){}); |
| 141 | + |
| 142 | + assertEquals(books, listOfBooks); |
| 143 | + |
| 144 | + conn.disconnect(); |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments