Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ARSnova Backend
Manage
Activity
Members
Labels
Plan
Issues
27
Issue boards
Milestones
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Privacy
Imprint
Contact
Snippets
Groups
Projects
Show more breadcrumbs
ARSnova
ARSnova Backend
Commits
e96d2b40
There was an error fetching the commit references. Please try again later.
Commit
e96d2b40
authored
12 years ago
by
Daniel Gerhardt
Browse files
Options
Downloads
Patches
Plain Diff
Added HttpRestApiTest.
Excluded HttpRestApiTest from build process.
parent
9bb068d9
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pom.xml
+1
-0
1 addition, 0 deletions
pom.xml
src/test/java/de/thm/arsnova/HttpRestApiTest.java
+172
-0
172 additions, 0 deletions
src/test/java/de/thm/arsnova/HttpRestApiTest.java
with
173 additions
and
0 deletions
pom.xml
+
1
−
0
View file @
e96d2b40
...
...
@@ -350,6 +350,7 @@
<configuration>
<excludes>
<exclude>
**/Selenium*.java
</exclude>
<exclude>
**/HttpRestApiTest.java
</exclude>
</excludes>
</configuration>
</plugin>
...
...
This diff is collapsed.
Click to expand it.
src/test/java/de/thm/arsnova/HttpRestApiTest.java
0 → 100644
+
172
−
0
View file @
e96d2b40
/*
* Copyright (C) 2012 THM webMedia
*
* This file is part of ARSnova.
*
* ARSnova is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ARSnova is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package
de.thm.arsnova
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java.io.BufferedReader
;
import
java.io.DataOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.util.HashMap
;
import
org.json.JSONArray
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
org.junit.Ignore
;
import
org.junit.Test
;
/**
* Unit test to verify the public RESTlike API.
*
* These tests only check the API communication and structure of responses. They do not verify data.
*/
public
class
HttpRestApiTest
{
public
final
String
host
=
"localhost"
;
public
final
int
port
=
8080
;
public
final
String
pathPrefix
=
"/"
;
private
HttpURLConnection
sendRequest
(
String
path
,
String
method
,
String
accept
,
HashMap
<
String
,
String
>
parameters
,
String
contentType
,
String
body
)
throws
IOException
{
HttpURLConnection
conn
;
try
{
conn
=
(
HttpURLConnection
)
(
new
URL
(
"http"
,
host
,
port
,
pathPrefix
+
path
)).
openConnection
();
System
.
out
.
println
(
conn
.
getURL
().
toExternalForm
());
conn
.
setRequestMethod
(
method
);
conn
.
setRequestProperty
(
"Accept"
,
accept
);
conn
.
setRequestProperty
(
"Host"
,
host
+
":"
+
Integer
.
valueOf
(
port
));
if
(
null
!=
body
)
{
conn
.
setRequestProperty
(
"Content-Type"
,
contentType
);
conn
.
setRequestProperty
(
"Content-Length"
,
Integer
.
toString
(
body
.
getBytes
().
length
));
conn
.
setDoOutput
(
true
);
DataOutputStream
out
=
new
DataOutputStream
(
conn
.
getOutputStream
());
out
.
writeBytes
(
body
);
out
.
flush
();
out
.
close
();
}
}
catch
(
MalformedURLException
e
)
{
conn
=
null
;
e
.
printStackTrace
();
}
return
conn
;
}
private
HttpURLConnection
sendRequest
(
String
path
,
String
method
,
String
accept
,
HashMap
<
String
,
String
>
parameters
)
throws
IOException
{
return
sendRequest
(
path
,
method
,
accept
,
parameters
,
null
,
null
);
}
private
String
transformInputToString
(
InputStream
input
)
throws
IOException
{
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
input
));
StringBuilder
str
=
new
StringBuilder
();
String
line
;
while
(
null
!=
(
line
=
reader
.
readLine
()))
{
str
.
append
(
line
);
}
System
.
out
.
println
(
str
);
return
str
.
toString
();
}
private
JSONObject
transformInputToJsonObject
(
InputStream
input
)
throws
IOException
,
JSONException
{
return
new
JSONObject
(
transformInputToString
(
input
));
}
private
JSONArray
transformInputToJsonArray
(
InputStream
input
)
throws
IOException
,
JSONException
{
return
new
JSONArray
(
transformInputToString
(
input
));
}
@Ignore
(
"Test not implemented"
)
@Test
public
void
testSession
()
throws
Exception
{
}
@Ignore
(
"Test not implemented"
)
@Test
public
void
testQuestionByLecturer
()
throws
Exception
{
}
@Ignore
(
"Test not implemented"
)
@Test
public
void
testQuestionByAudience
()
throws
Exception
{
}
@Ignore
(
"Test not implemented"
)
@Test
public
void
testSocket
()
throws
Exception
{
}
@Test
public
void
testCanteen
()
throws
Exception
{
HttpURLConnection
conn
;
JSONArray
jsonArr
;
String
responseBody
;
/* TODO: make test case more specific */
conn
=
sendRequest
(
"canteen/menu/vote"
,
"GET"
,
"application/json"
,
null
);
assertEquals
(
200
,
conn
.
getResponseCode
());
jsonArr
=
transformInputToJsonArray
(
conn
.
getInputStream
());
assertNotNull
(
jsonArr
);
conn
=
sendRequest
(
"canteen/menu/vote/count"
,
"GET"
,
"text/plain"
,
null
);
assertEquals
(
200
,
conn
.
getResponseCode
());
responseBody
=
transformInputToString
(
conn
.
getInputStream
());
Integer
.
valueOf
(
responseBody
);
/* TODO: implement test for POST /canteen/menu/vote */
}
@Test
public
void
testStatistics
()
throws
Exception
{
HttpURLConnection
conn
;
JSONObject
jsonObj
;
String
responseBody
;
conn
=
sendRequest
(
"statistics"
,
"GET"
,
"application/json"
,
null
);
assertEquals
(
200
,
conn
.
getResponseCode
());
jsonObj
=
transformInputToJsonObject
(
conn
.
getInputStream
());
assertTrue
(
jsonObj
.
has
(
"answers"
));
assertTrue
(
jsonObj
.
has
(
"questions"
));
assertTrue
(
jsonObj
.
has
(
"openSessions"
));
assertTrue
(
jsonObj
.
has
(
"closedSessions"
));
assertTrue
(
jsonObj
.
has
(
"activeUsers"
));
conn
=
sendRequest
(
"statistics/activeusercount"
,
"GET"
,
"text/plain"
,
null
);
assertEquals
(
200
,
conn
.
getResponseCode
());
responseBody
=
transformInputToString
(
conn
.
getInputStream
());
Integer
.
parseInt
(
responseBody
);
conn
=
sendRequest
(
"statistics/sessioncount"
,
"GET"
,
"text/plain"
,
null
);
assertEquals
(
200
,
conn
.
getResponseCode
());
responseBody
=
transformInputToString
(
conn
.
getInputStream
());
Integer
.
parseInt
(
responseBody
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment