Skip to content

Commit 94fd47d

Browse files
Egima profilepivovarit
authored andcommitted
Added test suite, removed bad names (eugenp#732)
* made changes to java reflection * removed redundant method makeSound in Animal abstract class * added project for play-framework article * added project for regex * changed regex project from own model to core-java * added project for routing in play * made changes to regex project * refactored code for REST API with Play project * refactored student store indexing to zero base * added unit tests, removed bad names
1 parent e7d5f61 commit 94fd47d

2 files changed

Lines changed: 157 additions & 55 deletions

File tree

Lines changed: 157 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,172 @@
1-
import java.util.ArrayList;
2-
import java.util.HashMap;
3-
import java.util.List;
4-
import java.util.Map;
51

6-
import com.fasterxml.jackson.databind.JsonNode;
7-
import org.junit.*;
2+
import static org.junit.Assert.assertEquals;
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
85

9-
import play.mvc.*;
10-
import play.test.*;
11-
import play.data.DynamicForm;
12-
import play.data.validation.ValidationError;
13-
import play.data.validation.Constraints.RequiredValidator;
14-
import play.i18n.Lang;
15-
import play.libs.F;
16-
import play.libs.F.*;
17-
import play.twirl.api.Content;
6+
import java.io.BufferedReader;
7+
import java.io.DataOutputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
import java.net.HttpURLConnection;
12+
import java.net.MalformedURLException;
13+
import java.net.URL;
14+
import java.util.Arrays;
1815

16+
import org.json.JSONArray;
17+
import org.json.JSONObject;
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
import model.Student;
21+
import play.test.*;
1922
import static play.test.Helpers.*;
20-
import static org.junit.Assert.*;
2123

24+
public class ApplicationTest{
25+
private static final String BASE_URL = "http://localhost:9000";
2226

23-
/**
24-
*
25-
* Simple (JUnit) tests that can call all parts of a play app.
26-
* If you are interested in mocking a whole application, see the wiki for more details.
27-
*
28-
*/
29-
public class ApplicationTest {
27+
@Test
28+
public void testInServer() throws Exception {
29+
TestServer server = testServer(3333);
30+
running(server, () -> {
31+
try {
32+
WSClient ws = play.libs.ws.WS.newClient(3333);
33+
CompletionStage<WSResponse> completionStage = ws.url("/").get();
34+
WSResponse response = completionStage.toCompletableFuture().get();
35+
ws.close();
36+
assertEquals(OK, response.getStatus());
37+
} catch (Exception e) {
38+
logger.error(e.getMessage(), e);
39+
}
40+
});
41+
}
42+
@Test
43+
public void whenCreatesRecord_thenCorrect() {
44+
Student student = new Student("jody", "west", 50);
45+
JSONObject obj = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student)));
46+
assertTrue(obj.getBoolean("isSuccessfull"));
47+
JSONObject body = obj.getJSONObject("body");
48+
assertEquals(student.getAge(), body.getInt("age"));
49+
assertEquals(student.getFirstName(), body.getString("firstName"));
50+
assertEquals(student.getLastName(), body.getString("lastName"));
51+
}
3052

3153
@Test
32-
public void simpleCheck() {
33-
int a = 1 + 1;
34-
assertEquals(2, a);
54+
public void whenDeletesCreatedRecord_thenCorrect() {
55+
Student student = new Student("Usain", "Bolt", 25);
56+
JSONObject ob1 = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student))).getJSONObject("body");
57+
int id = ob1.getInt("id");
58+
JSONObject obj1 = new JSONObject(makeRequest(BASE_URL + "/" + id, "POST", new JSONObject()));
59+
assertTrue(obj1.getBoolean("isSuccessfull"));
60+
makeRequest(BASE_URL + "/" + id, "DELETE", null);
61+
JSONObject obj2 = new JSONObject(makeRequest(BASE_URL + "/" + id, "POST", new JSONObject()));
62+
assertFalse(obj2.getBoolean("isSuccessfull"));
3563
}
3664

3765
@Test
38-
public void renderTemplate() {
39-
Content html = views.html.index.render("Your new application is ready.");
40-
assertEquals("text/html", html.contentType());
41-
assertTrue(html.body().contains("Your new application is ready."));
66+
public void whenUpdatesCreatedRecord_thenCorrect() {
67+
Student student = new Student("john", "doe", 50);
68+
JSONObject body1 = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student))).getJSONObject("body");
69+
assertEquals(student.getAge(), body1.getInt("age"));
70+
int newAge = 60;
71+
body1.put("age", newAge);
72+
JSONObject body2 = new JSONObject(makeRequest(BASE_URL, "PUT", body1)).getJSONObject("body");
73+
assertFalse(student.getAge() == body2.getInt("age"));
74+
assertTrue(newAge == body2.getInt("age"));
75+
}
76+
77+
@Test
78+
public void whenGetsAllRecords_thenCorrect() {
79+
Student student1 = new Student("jane", "daisy", 50);
80+
Student student2 = new Student("john", "daniel", 60);
81+
Student student3 = new Student("don", "mason", 55);
82+
Student student4 = new Student("scarlet", "ohara", 90);
83+
84+
makeRequest(BASE_URL, "POST", new JSONObject(student1));
85+
makeRequest(BASE_URL, "POST", new JSONObject(student2));
86+
makeRequest(BASE_URL, "POST", new JSONObject(student3));
87+
makeRequest(BASE_URL, "POST", new JSONObject(student4));
88+
89+
JSONObject objects = new JSONObject(makeRequest(BASE_URL, "GET", null));
90+
assertTrue(objects.getBoolean("isSuccessfull"));
91+
JSONArray array = objects.getJSONArray("body");
92+
assertTrue(array.length() >= 4);
93+
}
94+
95+
public static String makeRequest(String myUrl, String httpMethod, JSONObject parameters) {
96+
97+
URL url = null;
98+
try {
99+
url = new URL(myUrl);
100+
} catch (MalformedURLException e) {
101+
e.printStackTrace();
102+
}
103+
HttpURLConnection conn = null;
104+
try {
105+
106+
conn = (HttpURLConnection) url.openConnection();
107+
} catch (IOException e) {
108+
e.printStackTrace();
109+
}
110+
conn.setDoInput(true);
111+
112+
conn.setReadTimeout(10000);
113+
114+
conn.setRequestProperty("Content-Type", "application/json");
115+
DataOutputStream dos = null;
116+
int respCode = 0;
117+
String inputString = null;
118+
try {
119+
conn.setRequestMethod(httpMethod);
120+
121+
if (Arrays.asList("POST", "PUT").contains(httpMethod)) {
122+
String params = parameters.toString();
123+
124+
conn.setDoOutput(true);
125+
126+
dos = new DataOutputStream(conn.getOutputStream());
127+
dos.writeBytes(params);
128+
dos.flush();
129+
dos.close();
130+
}
131+
respCode = conn.getResponseCode();
132+
if (respCode != 200 && respCode != 201) {
133+
String error = inputStreamToString(conn.getErrorStream());
134+
return error;
135+
}
136+
inputString = inputStreamToString(conn.getInputStream());
137+
138+
} catch (IOException e) {
139+
140+
e.printStackTrace();
141+
}
142+
return inputString;
42143
}
43144

145+
public static String inputStreamToString(InputStream is) {
146+
BufferedReader br = null;
147+
StringBuilder sb = new StringBuilder();
148+
149+
String line;
150+
try {
151+
152+
br = new BufferedReader(new InputStreamReader(is));
153+
while ((line = br.readLine()) != null) {
154+
sb.append(line);
155+
}
44156

157+
} catch (IOException e) {
158+
e.printStackTrace();
159+
} finally {
160+
if (br != null) {
161+
try {
162+
br.close();
163+
} catch (IOException e) {
164+
e.printStackTrace();
165+
}
166+
}
167+
}
168+
169+
return sb.toString();
170+
171+
}
45172
}

‎play-framework/student-api/test/IntegrationTest.java‎

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)