/v3.2/nimbits-sdk/test/DataPointIntegrationTest.java

http://nimbits-server.googlecode.com/ · Java · 400 lines · 263 code · 84 blank · 53 comment · 15 complexity · 89784131d2480028464db73a3ddeaf6b MD5 · raw file

  1. /*
  2. * Copyright (c) 2011. Tonic Solutions, LLC. All Rights Reservered. This Code is distributed under GPL V3 without any warrenty.
  3. */
  4. import com.nimbits.client.model.Const;
  5. import com.nimbits.client.model.category.Category;
  6. import com.nimbits.client.model.category.CategoryName;
  7. import com.nimbits.client.model.common.CommonFactoryLocator;
  8. import com.nimbits.client.model.point.Point;
  9. import com.nimbits.client.model.point.PointModel;
  10. import com.nimbits.client.model.value.Value;
  11. import junit.framework.Assert;
  12. import junit.framework.TestCase;
  13. import org.junit.After;
  14. import org.junit.Before;
  15. import org.junit.Test;
  16. import java.io.IOException;
  17. import java.net.URLEncoder;
  18. import java.text.DecimalFormat;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.UUID;
  22. /**
  23. * Created by Benjamin Sautner
  24. * User: benjamin
  25. * Date: 3/14/11
  26. * Time: 7:28 PM
  27. */
  28. public class DataPointIntegrationTest extends TestCase {
  29. private CategoryName cat;
  30. @Before
  31. public void setUp() throws Exception {
  32. cat = CommonFactoryLocator.getInstance().createCategoryName(UUID.randomUUID().toString());
  33. Category c = Common.client().addCategory(cat);
  34. Thread.sleep(1000);
  35. Assert.assertTrue(c.getId() > 0);
  36. }
  37. @After
  38. public void tearDown() throws Exception {
  39. System.out.println("tear down waiting for tasks to finish");
  40. Thread.sleep(5000);
  41. // Common.client().deleteCategory(cat);
  42. System.out.println("tear down complete");
  43. }
  44. @Test
  45. public void testNoCompression() throws Exception {
  46. Point p = new PointModel();
  47. p.setName(CommonFactoryLocator.getInstance().createPointName("test" + UUID.randomUUID().toString()));
  48. p.setExpire(1);
  49. p.setCompression(0);
  50. Point r = Common.client().addPoint(p, cat);
  51. double x = testCompression(p);
  52. Common.client().deletePoint(p.getName());
  53. Assert.assertEquals(345.0, x);
  54. // gClient.DeletePoint(p.getValue());
  55. }
  56. @Test
  57. public void testCompressionSeparatePostsNoDate() throws Exception {
  58. Point p = new PointModel();
  59. p.setName(CommonFactoryLocator.getInstance().createPointName("test" + UUID.randomUUID().toString()));
  60. p.setCompression(0.1);
  61. Common.client().addPoint(p, cat);
  62. double rx = 0.0;
  63. try {
  64. for (int i = 0; i < 40; i++) {
  65. StringBuilder b = new StringBuilder();
  66. rx += 0.1;
  67. System.out.println("Recording " + rx);
  68. b.append("&p1=").append(URLEncoder.encode(p.getName().getValue(), Const.CONST_ENCODING)).append("&v1=").append(rx);
  69. // System.out.println( b.toString());
  70. System.out.println(Common.client().recordBatch(b.toString()));
  71. Thread.sleep(3000);
  72. }
  73. Thread.sleep(5000);
  74. List<Value> v = Common.client().getSeries(p.getName(), 10);
  75. double retVal = 0.0;
  76. for (Value x : v) {
  77. retVal += x.getValue();
  78. System.out.println(x.getValue() + " " + x.getTimestamp());
  79. }
  80. DecimalFormat twoDForm = new DecimalFormat("#.##");
  81. retVal = Double.valueOf(twoDForm.format(retVal));
  82. Common.client().deletePoint(p.getName());
  83. Assert.assertEquals(31.0, retVal);
  84. } catch (IOException e) {
  85. } catch (InterruptedException e) {
  86. }
  87. }
  88. @Test
  89. public void testCompressionSeperateAlternatingValuesPostsNoDate() throws Exception {
  90. Point p = new PointModel();
  91. p.setName(CommonFactoryLocator.getInstance().createPointName("test" + UUID.randomUUID().toString()));
  92. p.setCompression(0.1);
  93. Common.client().addPoint(p, cat);
  94. double rx = 0.0;
  95. try {
  96. Thread.sleep(5000);
  97. for (int i = 0; i < 40; i++) {
  98. StringBuilder b = new StringBuilder();
  99. if (rx == 0.0) {
  100. rx = 1.0;
  101. } else {
  102. rx = 0.0;
  103. }
  104. System.out.println("Recording " + rx);
  105. b.append("&p2=").append(URLEncoder.encode(p.getName().getValue(), Const.CONST_ENCODING)).append("&v2=").append(rx);
  106. // System.out.println( b.toString());
  107. System.out.println(Common.client().recordBatch(b.toString()));
  108. Thread.sleep(2000);
  109. }
  110. Thread.sleep(2000);
  111. List<Value> v = Common.client().getSeries(p.getName(), 40);
  112. double retVal = 0.0;
  113. for (Value x : v) {
  114. retVal += x.getValue();
  115. System.out.println(x.getValue() + " " + x.getTimestamp());
  116. }
  117. DecimalFormat twoDForm = new DecimalFormat("#.##");
  118. retVal = Double.valueOf(twoDForm.format(retVal));
  119. Common.client().deletePoint(p.getName());
  120. Assert.assertEquals(20.0, retVal);
  121. } catch (IOException e) {
  122. // TODO Auto-generated catch block
  123. e.printStackTrace();
  124. } catch (InterruptedException e) {
  125. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  126. }
  127. }
  128. @Test
  129. public void testCompression() {
  130. Point p = new PointModel();
  131. p.setName(CommonFactoryLocator.getInstance().createPointName("test" + UUID.randomUUID().toString()));
  132. p.setCompression(2.0);
  133. Common.client().addPoint(p, cat);
  134. double x = testCompression(p);
  135. Assert.assertEquals(255.0, x);
  136. Common.client().deletePoint(p.getName());
  137. }
  138. @Test
  139. public void testChangeCompression() {
  140. Point p = new PointModel();
  141. p.setName(CommonFactoryLocator.getInstance().createPointName("test" + UUID.randomUUID().toString()));
  142. p.setCompression(0.0);
  143. Common.client().addPoint(p, cat);
  144. double x = testCompression(p);
  145. // long id;
  146. Assert.assertEquals(345.0, x);
  147. Point px = Common.client().getPoint(p.getName());
  148. // id = px.getId();
  149. Assert.assertNotNull(px);
  150. px.setCompression(2.0);
  151. Common.client().updatePoint(px);
  152. Point px2 = Common.client().getPoint(p.getName());
  153. Assert.assertEquals(2.0, px2.getCompression());
  154. Assert.assertEquals(px.getId(), px2.getId());
  155. try {
  156. Thread.sleep(5000);
  157. } catch (InterruptedException e) {
  158. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  159. }
  160. double x2 = testCompression(p);
  161. Common.client().deletePoint(p.getName());
  162. Assert.assertEquals(255.0, x2);
  163. // gClient.DeletePoint(p.getValue());
  164. }
  165. @Test
  166. public void TestZeroCompressionWithBatch() {
  167. Point p = new PointModel();
  168. p.setName(CommonFactoryLocator.getInstance().createPointName("test" + UUID.randomUUID().toString()));
  169. p.setCompression(0.0);
  170. Common.client().addPoint(p, cat);
  171. System.out.println("Starting batch compression integration test compression = " + p.getCompression());
  172. StringBuilder b = new StringBuilder();
  173. try {
  174. for (int i = 0; i < 40; i++) {
  175. b.append("&p").append(i).append("=").append(URLEncoder.encode(p.getName().getValue(), Const.CONST_ENCODING)).append("&v").append(i).append("=").append(i).append("&t").append(i).append("=").append(new Date().getTime());
  176. Thread.sleep(1000);
  177. }
  178. System.out.println(b.toString());
  179. System.out.println(Common.client().recordBatch(b.toString()));
  180. double retVal = 0.0;
  181. Thread.sleep(5000);
  182. List<Value> v = Common.client().getSeries(p.getName(), 10);
  183. for (Value x : v) {
  184. retVal += x.getValue();
  185. System.out.println(x.getValue() + " " + x.getTimestamp());
  186. }
  187. Assert.assertEquals(345.0, retVal);
  188. Common.client().deletePoint(p.getName());
  189. } catch (IOException e) {
  190. // TODO Auto-generated catch block
  191. e.printStackTrace();
  192. } catch (InterruptedException e) {
  193. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  194. }
  195. // String ba = "&email=" + email + "&secret=" + secret +
  196. // "&p1=" + URLEncoder.encode("b1", Const.CONST_ENCODING)+ "&v1=" + r.nextDouble() +
  197. // "&p2=" + URLEncoder.encode("b2", Const.CONST_ENCODING)+ "&v2=" + r.nextDouble() +
  198. // "&p3=" + URLEncoder.encode("b3", Const.CONST_ENCODING)+ "&v3=" + r.nextDouble() +
  199. // "&p4=" + URLEncoder.encode("b4", Const.CONST_ENCODING)+ "&v4=" + r.nextDouble() +
  200. // "&p5=" + URLEncoder.encode("b5", Const.CONST_ENCODING)+ "&v5=" + r.nextDouble() +
  201. // "&p6=" + URLEncoder.encode("b6", Const.CONST_ENCODING)+ "&v6=" + r.nextDouble() +
  202. // "&p7=" + URLEncoder.encode("b7", Const.CONST_ENCODING)+ "&v7=" + r.nextDouble();
  203. //
  204. //
  205. }
  206. @Test
  207. public void TestCompressionWithBatch() {
  208. Point p = new PointModel();
  209. p.setName(CommonFactoryLocator.getInstance().createPointName("test" + UUID.randomUUID().toString()));
  210. p.setCompression(2.0);
  211. Common.client().addPoint(p, cat);
  212. StringBuilder b = new StringBuilder();
  213. try {
  214. for (int i = 0; i < 40; i++) {
  215. b.append("&p").append(i).append("=").append(URLEncoder.encode(p.getName().getValue(), Const.CONST_ENCODING)).append("&v").append(i).append("=").append(i).append("&t").append(i).append("=").append(new Date().getTime());
  216. Thread.sleep(1000);
  217. }
  218. // System.out.println(b.toString());
  219. System.out.println(Common.client().recordBatch(b.toString()));
  220. double retVal = 0.0;
  221. Thread.sleep(5000);
  222. List<Value> v = Common.client().getSeries(p.getName(), 10);
  223. for (Value x : v) {
  224. retVal += x.getValue();
  225. System.out.println(x.getValue() + " " + x.getTimestamp());
  226. }
  227. Assert.assertEquals(255.0, retVal);
  228. Common.client().deletePoint(p.getName());
  229. } catch (IOException e) {
  230. // TODO Auto-generated catch block
  231. e.printStackTrace();
  232. } catch (InterruptedException e) {
  233. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  234. }
  235. }
  236. // @Test
  237. // public void loadRandomDataIntoB1UsingKey() throws InterruptedException, IOException {
  238. // NimbitsUser nx = new NimbitsUser(email, key);
  239. //
  240. // NimbitsClient c = NimbitsClientFactory.getInstance(nx, hostURL);
  241. // Random r = new Random();
  242. //
  243. //
  244. // for (int i = 0; i < 10; i++)
  245. // {
  246. // Value v = new Value();
  247. // v.setValue(r.nextDouble()*10);
  248. // v.setTimestamp(new Date());
  249. // System.out.println(c.recordValue("b1",v));
  250. //
  251. //
  252. // Thread.sleep(1000);
  253. //
  254. //
  255. // }
  256. // }
  257. @Test
  258. public void TestCompressionWithBatchWithMissingPoints() {
  259. Point p = new PointModel();
  260. p.setName(CommonFactoryLocator.getInstance().createPointName("test" + UUID.randomUUID().toString()));
  261. p.setCompression(2.0);
  262. Common.client().addPoint(p, cat);
  263. StringBuilder b = new StringBuilder();
  264. try {
  265. for (int i = 0; i < 40; i++) {
  266. b.append("&p");
  267. b.append(i);
  268. b.append("=");
  269. b.append(URLEncoder.encode(p.getName().getValue(), Const.CONST_ENCODING));
  270. b.append("&v");
  271. b.append(i);
  272. b.append("=");
  273. b.append(i);
  274. }
  275. // b.append("&p41=32423fsdfsdf&v41=324fsdsd");
  276. System.out.println(b.toString());
  277. System.out.println(Common.client().recordBatch(b.toString()));
  278. double retVal = 0.0;
  279. Thread.sleep(5000);
  280. List<Value> v = Common.client().getSeries(p.getName(), 10);
  281. for (Value x : v) {
  282. retVal += x.getValue();
  283. System.out.println(x.getValue() + " " + x.getTimestamp());
  284. }
  285. Assert.assertEquals(216.0, retVal);
  286. Common.client().deletePoint(p.getName());
  287. } catch (IOException e) {
  288. // TODO Auto-generated catch block
  289. e.printStackTrace();
  290. } catch (InterruptedException e) {
  291. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  292. }
  293. }
  294. private double testCompression(Point p) {
  295. System.out.println("Starting compression integration test compression = " + p.getCompression() + " " + p.getId());
  296. double retVal = 0.0;
  297. try {
  298. for (int i = 0; i < 40; i++) {
  299. Thread.sleep(1000);
  300. Value v = Common.client().recordValue(p.getName(), i, new Date());
  301. System.out.println(v.getValue() + " " + v.getTimestamp() + " " + v.getPoint());
  302. }
  303. Thread.sleep(5000);
  304. List<Value> v = Common.client().getSeries(p.getName(), 10);
  305. for (Value x : v) {
  306. retVal += x.getValue();
  307. }
  308. } catch (IOException e) {
  309. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  310. } catch (InterruptedException e) {
  311. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  312. }
  313. System.out.println("End compression integration test " + retVal);
  314. return retVal;
  315. }
  316. }