mirror of
https://github.com/magefree/mage.git
synced 2025-12-22 03:22:00 -08:00
Mage.Stats WS module for getting server stats in json format
This commit is contained in:
parent
78c0d76088
commit
71614becc2
43 changed files with 1587 additions and 0 deletions
145
Mage.Stats/src/test/java/com/anygo/ws/json/TestJSONParser.java
Normal file
145
Mage.Stats/src/test/java/com/anygo/ws/json/TestJSONParser.java
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
package com.anygo.ws.json;
|
||||
|
||||
import com.xmage.ws.util.json.JSONParser;
|
||||
import com.xmage.ws.util.json.JSONValidationException;
|
||||
import junit.framework.Assert;
|
||||
import net.minidev.json.JSONArray;
|
||||
import net.minidev.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author noxx
|
||||
*/
|
||||
public class TestJSONParser {
|
||||
|
||||
@Test
|
||||
public void testParse() throws Exception {
|
||||
JSONParser parser = new JSONParser();
|
||||
parser.parseJSON("{}");
|
||||
parser.parseJSON("{\"test\" : 1}");
|
||||
parser.parseJSON("{\"test\" : \"test\"}");
|
||||
parser.parseJSON("{\"list\" : [\"1\", \"2\", \"3\"]}");
|
||||
parser.parseJSON("{test:test}");
|
||||
|
||||
testError(parser, "{");
|
||||
testError(parser, "}");
|
||||
testError(parser, "{{}");
|
||||
testError(parser, "{\"test\" : [}}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryForInt() throws Exception {
|
||||
JSONParser parser = new JSONParser();
|
||||
parser.parseJSON("{\"test\" : 1}");
|
||||
Assert.assertEquals(1, parser.getInt("test"));
|
||||
|
||||
parser = new JSONParser();
|
||||
parser.parseJSON("{test : { internal : {level : 2}}}");
|
||||
Assert.assertEquals(2, parser.getInt("test.internal.level"));
|
||||
Assert.assertFalse("No cache should have been used", parser.isHitCache());
|
||||
|
||||
Assert.assertEquals(2, parser.getInt("test.internal.level"));
|
||||
Assert.assertTrue("Cache should have been used this time!", parser.isHitCache());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryForJSONArray() throws Exception {
|
||||
JSONParser parser = new JSONParser();
|
||||
parser.parseJSON("{\"test\" : [\"1\", \"2\", \"3\"]}");
|
||||
Assert.assertTrue(parser.getJSONArray("test") instanceof JSONArray);
|
||||
Assert.assertEquals("1", parser.getJSONArray("test").get(0));
|
||||
|
||||
parser = new JSONParser();
|
||||
parser.parseJSON("{\"test\" : [1,2,3]}");
|
||||
Assert.assertTrue(parser.getJSONArray("test") instanceof JSONArray);
|
||||
Assert.assertFalse(parser.isHitCache());
|
||||
Assert.assertEquals(2, parser.getJSONArray("test").get(1));
|
||||
Assert.assertTrue(parser.isHitCache());
|
||||
|
||||
Assert.assertTrue(parser.getJSONArray("test") instanceof JSONArray);
|
||||
Assert.assertEquals(2, parser.getJSONArray("test").get(1));
|
||||
Assert.assertTrue(parser.isHitCache());
|
||||
|
||||
parser = new JSONParser();
|
||||
parser.parseJSON("{\"test\" : [{second_level: \"3\"}, {\"third_level\" : 2}]}");
|
||||
Assert.assertTrue(parser.getJSONArray("test") instanceof JSONArray);
|
||||
Assert.assertTrue(parser.getJSONArray("test").get(0) instanceof JSONObject);
|
||||
Assert.assertEquals(2, parser.getInt("test[1].third_level"));
|
||||
Assert.assertEquals("3", parser.getString("test[0].second_level"));
|
||||
|
||||
parser = new JSONParser();
|
||||
parser.parseJSON("{\"test\" : [{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{2:3},{4:5}]}");
|
||||
Assert.assertTrue(parser.getJSONArray("test") instanceof JSONArray);
|
||||
Assert.assertEquals(5, parser.getInt("test[10].4"));
|
||||
}
|
||||
|
||||
@Test
|
||||
//TODO: implement
|
||||
public void testErrors() throws Exception {
|
||||
//JSONParser parser = new JSONParser();
|
||||
//parser.parseJSON("{test : { internal : {level : \"2\"}}}");
|
||||
//parser.getInt("test.internal.level");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtendedCache() throws Exception {
|
||||
JSONParser parser = new JSONParser();
|
||||
parser.parseJSON("{test : { internal : {level : 2}}}");
|
||||
Assert.assertEquals(2, parser.getInt("test.internal.level"));
|
||||
Assert.assertFalse("No cache should have been used", parser.isHitCache());
|
||||
|
||||
Assert.assertTrue(parser.getJSON("test") instanceof JSONObject);
|
||||
Assert.assertFalse("No cache should have been used", parser.isHitCache());
|
||||
Assert.assertTrue(parser.getJSON("test.internal") instanceof JSONObject);
|
||||
Assert.assertFalse("No cache should have been used", parser.isHitCache());
|
||||
|
||||
parser = new JSONParser();
|
||||
parser.parseJSON("{test : { internal : {level : 2}}}");
|
||||
parser.setCachePolicy(JSONParser.CachePolicy.CACHE_ALL_LEVELS);
|
||||
Assert.assertEquals(2, parser.getInt("test.internal.level"));
|
||||
Assert.assertFalse("No cache should have been used", parser.isHitCache());
|
||||
|
||||
Assert.assertTrue(parser.getJSON("test") instanceof JSONObject);
|
||||
Assert.assertTrue("Cache should have been used this time!", parser.isHitCache());
|
||||
Assert.assertTrue(parser.getJSON("test.internal") instanceof JSONObject);
|
||||
Assert.assertTrue("Cache should have been used this time!", parser.isHitCache());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtendedIndexes() throws Exception {
|
||||
JSONParser parser = new JSONParser();
|
||||
parser.parseJSON("{\"test\" : [1,2,3,4,5]}");
|
||||
Assert.assertEquals(1, parser.getInt("test[].$first"));
|
||||
Assert.assertEquals(2, parser.getInt("test[].$second"));
|
||||
Assert.assertEquals(3, parser.getInt("test[].$third"));
|
||||
Assert.assertEquals(4, parser.getInt("test[].$fourth"));
|
||||
Assert.assertEquals(5, parser.getInt("test[].$fifth"));
|
||||
|
||||
parser = new JSONParser();
|
||||
parser.parseJSON("{\"test\" : [{1:1},{2:2},{3:3},{4:4},{5:5}]}");
|
||||
Assert.assertEquals(1, parser.getInt("test[].$first.1"));
|
||||
Assert.assertEquals(2, parser.getInt("test[].$second.2"));
|
||||
Assert.assertEquals(3, parser.getInt("test[].$third.3"));
|
||||
Assert.assertEquals(4, parser.getInt("test[].$fourth.4"));
|
||||
Assert.assertEquals(5, parser.getInt("test[].$fifth.5"));
|
||||
|
||||
parser = new JSONParser();
|
||||
parser.parseJSON("{\"contacts\": {\"phones\": [\n" +
|
||||
" {\"phone\": \"100000\"},\n" +
|
||||
" {\"phone\": \"+7 999 1234567\"}\n" +
|
||||
" ]}}");
|
||||
|
||||
Assert.assertEquals("100000", parser.getString("contacts.phones[].$first.phone"));
|
||||
|
||||
}
|
||||
|
||||
private void testError(JSONParser parser, String jsonToTest) throws Exception {
|
||||
try {
|
||||
parser.parseJSON(jsonToTest);
|
||||
Assert.assertTrue("Should have thrown an exception", false);
|
||||
} catch (JSONValidationException j) {
|
||||
// ok
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package com.anygo.ws.rest;
|
||||
|
||||
import com.xmage.ws.model.DomainErrors;
|
||||
import com.xmage.ws.rest.services.XMageStatsService;
|
||||
import com.xmage.ws.util.json.JSONParser;
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
/**
|
||||
* Testings XMage stats service without need to deploy.
|
||||
*
|
||||
* @author noxx
|
||||
*/
|
||||
public class XMageStatsServiceTest {
|
||||
|
||||
@Test
|
||||
public void testAddNewAndGet() throws Exception {
|
||||
|
||||
XMageStatsService xMageStatsService = new XMageStatsService();
|
||||
|
||||
Response response = xMageStatsService.getAllStats();
|
||||
|
||||
JSONParser parser = new JSONParser();
|
||||
parser.parseJSON((String) response.getEntity());
|
||||
|
||||
Assert.assertEquals(DomainErrors.Errors.STATUS_OK.getCode(), parser.getInt("code"));
|
||||
System.out.println("response = " + response.getEntity().toString());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
32
Mage.Stats/src/test/java/com/anygo/ws/util/FileUtil.java
Normal file
32
Mage.Stats/src/test/java/com/anygo/ws/util/FileUtil.java
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package com.anygo.ws.util;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author noxx
|
||||
*/
|
||||
public class FileUtil {
|
||||
|
||||
private FileUtil() {}
|
||||
|
||||
public static String readFile(String file) throws IOException {
|
||||
InputStream in = FileUtil.class.getResourceAsStream(file);
|
||||
if (in == null) {
|
||||
throw new FileNotFoundException("Couldn't find file " + file);
|
||||
}
|
||||
Reader fr = new InputStreamReader(in, "utf-8");
|
||||
|
||||
BufferedReader reader = new BufferedReader(fr);
|
||||
String line;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
String ls = System.getProperty("line.separator");
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
stringBuilder.append(line);
|
||||
stringBuilder.append(ls);
|
||||
}
|
||||
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue