Debian Codegraph @

plugins/gnostic-swift-generator/examples/bookstore/Tests/BookstoreTests/BookstoreTests.swift

raw
1 /*
2 *
3 * Copyright 2017, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33 import XCTest
34 import Foundation
35 @testable import Bookstore
36
37 func Log(_ message : String) {
38 FileHandle.standardError.write((message + "\n").data(using:.utf8)!)
39 }
40
41 let service = "http://localhost:8080"
42
43 class BookstoreTests: XCTestCase {
44
45 func testBasic() {
46 // create a client
47 let b = Bookstore.Client(service:service)
48 Log("// reset the service by deleting all shelves")
49 do {
50 try b.deleteShelves()
51 } catch (let error) {
52 XCTFail("\(error)")
53 }
54 Log("// verify that the service has no shelves")
55 do {
56 let response = try b.listShelves()
57 XCTAssertEqual(response.shelves.count, 0)
58 } catch (let error) {
59 XCTFail("\(error)")
60 }
61 Log("// attempting to get a shelf should return an error")
62 do {
63 let _ = try b.getShelf(shelf:1)
64 XCTFail("server error")
65 } catch {
66 }
67 Log("// attempting to get a book should return an error")
68 do {
69 let _ = try b.getBook(shelf:1, book:2)
70 } catch {
71 }
72 Log("// add a shelf")
73 do {
74 let shelf = Shelf()
75 shelf.theme = "mysteries"
76 let response = try b.createShelf(shelf:shelf)
77 if (response.name != "shelves/1") ||
78 (response.theme != "mysteries") {
79 XCTFail("mismatch")
80 }
81 } catch (let error) {
82 XCTFail("\(error)")
83 }
84 Log("// add another shelf")
85 do {
86 let shelf = Shelf()
87 shelf.theme = "comedies"
88 let response = try b.createShelf(shelf:shelf)
89 if (response.name != "shelves/2") ||
90 (response.theme != "comedies") {
91 XCTFail("mismatch")
92 }
93 } catch (let error) {
94 XCTFail("\(error)")
95 }
96 Log("// get the first shelf that was added")
97 do {
98 let response = try b.getShelf(shelf:1)
99 if (response.name != "shelves/1") ||
100 (response.theme != "mysteries") {
101 XCTFail("mismatch")
102 }
103 } catch (let error) {
104 XCTFail("\(error)")
105 }
106 Log("// list shelves and verify that there are 2")
107 do {
108 let response = try b.listShelves()
109 XCTAssertEqual(response.shelves.count, 2)
110 } catch (let error) {
111 XCTFail("\(error)")
112 }
113 Log("// delete a shelf")
114 do {
115 try b.deleteShelf(shelf:2)
116 } catch (let error) {
117 XCTFail("\(error)")
118 }
119 Log("// list shelves and verify that there is only 1")
120 do {
121 let response = try b.listShelves()
122 XCTAssertEqual(response.shelves.count, 1)
123 } catch (let error) {
124 XCTFail("\(error)")
125 }
126 Log("// list books on a shelf, verify that there are none")
127 do {
128 let response = try b.listBooks(shelf:1)
129 XCTAssertEqual(response.books.count, 0)
130 } catch (let error) {
131 XCTFail("\(error)")
132 }
133 Log("// create a book")
134 do {
135 let book = Book()
136 book.author = "Agatha Christie"
137 book.title = "And Then There Were None"
138 let _ = try b.createBook(shelf:1, book:book)
139 } catch (let error) {
140 XCTFail("\(error)")
141 }
142 Log("// create another book")
143 do {
144 let book = Book()
145 book.author = "Agatha Christie"
146 book.title = "Murder on the Orient Express"
147 let _ = try b.createBook(shelf:1, book:book)
148 } catch (let error) {
149 XCTFail("\(error)")
150 }
151 Log("// get the first book that was added")
152 do {
153 let response = try b.getBook(shelf:1, book:1)
154 if (response.author != "Agatha Christie") ||
155 (response.title != "And Then There Were None") {
156 XCTFail("mismatch")
157 }
158 } catch (let error) {
159 XCTFail("\(error)")
160 }
161 Log("// list the books on a shelf and verify that there are 2")
162 do {
163 let response = try b.listBooks(shelf:1)
164 XCTAssertEqual(response.books.count, 2)
165 } catch (let error) {
166 XCTFail("\(error)")
167 }
168 Log("// delete a book")
169 do {
170 try b.deleteBook(shelf:1, book:2)
171 } catch (let error) {
172 XCTFail("\(error)")
173 }
174 Log("// list the books on a shelf and verify that is only 1")
175 do {
176 let response = try b.listBooks(shelf:1)
177 XCTAssertEqual(response.books.count, 1)
178 } catch (let error) {
179 XCTFail("\(error)")
180 }
181 Log("// verify the handling of a badly-formed request")
182 var path = service
183 path = path + "/shelves"
184 guard let url = URL(string:path) else {
185 XCTFail("Failed to construct URL")
186 return
187 }
188 var request = URLRequest(url:url)
189 request.httpMethod = "POST"
190 request.httpBody = "".data(using:.utf8)
191 let (_, response, _) = fetch(request)
192 // we expect a 400 (Bad Request) code
193 if let response = response {
194 XCTAssertEqual(response.statusCode, 400)
195 } else {
196 // Failed requests are returning nil responses on Linux. For now we'll say that is OK.
197 //XCTFail("Null response for bad request")
198 }
199 }
200 }
201
202 extension BookstoreTests {
203 static var allTests : [(String, (BookstoreTests) -> () throws -> Void)] {
204 return [
205 ("testBasic", testBasic),
206 ]
207 }
208 }