-
Notifications
You must be signed in to change notification settings - Fork 0
/
FibonacciTester.java
141 lines (120 loc) · 4.28 KB
/
FibonacciTester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.Fibonacci.test;
//
// Run through some basic test cases
// From IDE, do RunAs->JavaApplication
// Eventually, these would be upgraded to junit tests
//
import java.util.List;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.Fibonacci.FibonacciValue;
public class FibonacciTester {
private Client client;
private String SERIES_REST_SERVICE_URL = "http://localhost:8080/Fibonacci/rest/FibonacciService/series";
private String BASE_REST_SERVICE_URL = "http://localhost:8080/Fibonacci/rest/FibonacciService/base";
private static final String PASS = "pass";
private static final String FAIL = "fail";
private void init(){
this.client = ClientBuilder.newClient();
}
public static void main(String[] args){
FibonacciTester tester = new FibonacciTester();
//initialize the tester
tester.init();
//test get all fibonacci series Web Service Methods (good and bad)
tester.testGetFibonacciSeries();
//test set/set fibonacci base value Web Service Method (good and bad)
tester.testSetGetFibonacciBase();
}
//Test: Get valid and invalid fibonacci series depths
private void testGetFibonacciSeries(){
GenericType<List<FibonacciValue>> list = new GenericType<List<FibonacciValue>>() {};
List<FibonacciValue> series = client
.target(SERIES_REST_SERVICE_URL)
.path("/{value}")
.resolveTemplate("value", 3)
.request(MediaType.APPLICATION_XML)
.get(list);
String result = FAIL;
if (series.size() == 3) {
result = PASS;
}
System.out.println("Test case name: testGetFibonacciSeries (positive test1), Result: " + result );
// Expect webexception for invalid input
result = FAIL;
try {
series = client
.target(SERIES_REST_SERVICE_URL)
.path("/{value}")
.resolveTemplate("value", -1)
.request(MediaType.APPLICATION_XML)
.get(list);
} catch (WebApplicationException ex) {
result = PASS;
}
System.out.println("Test case name: testGetFibonacciSeries (negative test1), Result: " + result );
// Expect webexception for invalid input
result = FAIL;
try {
series = client
.target(SERIES_REST_SERVICE_URL)
.path("/{value}")
.resolveTemplate("value", 1025)
.request(MediaType.APPLICATION_XML)
.get(list);
} catch (WebApplicationException ex) {
result = PASS;
}
System.out.println("Test case name: testGetFibonacciSeries (negative test2), Result: " + result );
}
private void testSetGetFibonacciBase(){
FibonacciValue base = client
.target(BASE_REST_SERVICE_URL)
.request(MediaType.APPLICATION_XML)
.get(FibonacciValue.class);
String result = FAIL;
if (base.getValue() == 0 || base.getValue() == 1) {
result = PASS;
}
System.out.println("Test case name: testSetGetFibonacciBase (positive test1), Result: " + result );
// Now set the base and verify it was correct
Form form = new Form();
form.param("value", "0");
Response callResult = client
.target(BASE_REST_SERVICE_URL)
.request(MediaType.APPLICATION_XML)
.post(Entity.entity(form,
MediaType.APPLICATION_FORM_URLENCODED_TYPE),
Response.class);
// Now, we expect the value returned to be 0
base = client
.target(BASE_REST_SERVICE_URL)
.request(MediaType.APPLICATION_XML)
.get(FibonacciValue.class);
result = FAIL;
if (base.getValue() == 0) {
result = PASS;
}
System.out.println("Test case name: testSetGetFibonacciBase (positive test2), Result: " + result );
// Now set the base and verify it was correct
Form newform = new Form();
newform.param("value", "5");
callResult = client
.target(BASE_REST_SERVICE_URL)
.request(MediaType.APPLICATION_XML)
.post(Entity.entity(newform,
MediaType.APPLICATION_FORM_URLENCODED_TYPE),
Response.class);
result = FAIL;
if (callResult.getStatus() == Response.Status.BAD_REQUEST.getStatusCode()) {
result = PASS;
}
System.out.println("Test case name: testSetGetFibonacciBase (negative test1), Result: " + result );
}
}