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