Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.95 KB | None | 0 0
  1. package com.provagroup.provaserver.wamp.legit;
  2.  
  3. import static org.junit.Assert.assertEquals;
  4. import static org.junit.Assert.assertNotNull;
  5. import static org.junit.Assert.assertTrue;
  6. import static org.junit.Assert.fail;
  7.  
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.UUID;
  12. import java.util.logging.Logger;
  13.  
  14. import org.junit.BeforeClass;
  15. import org.junit.Test;
  16.  
  17. import com.provagroup.provaserver.wamp.WampApiFieldKeys;
  18. import com.provagroup.wamp.client.LegitWampClient;
  19.  
  20. /**
  21. * @author rbense
  22. *
  23. */
  24. public class LegitTest {
  25. private static Logger log = Logger.getLogger(LegitTest.class.getName());
  26. public static final long WAIT_TIME = 100000; // DEBUG - increase this value or the client code will finish in 100s
  27.  
  28. private static final boolean push = true;
  29. private static int production = 0;
  30. // private static int production = 0; // local test
  31. // private static final int production = 1; // p4 testing
  32. // private static final int production = 2; // production
  33.  
  34. private static final String host = production == 0 ? "rb.provagroup.com" : production == 1 ? "p4.provagroup.com" : "cloud.provagroup.com";
  35. private static final String port = production == 0 ? "9443" : production == 1 ? "10443" : "9443";
  36. private static final String URI = "wss://" + host + ":" + port + "/provaserver/wamp";
  37. private static final String realmlegit = "provagroup.provaserver.legitconsumerapp";
  38.  
  39. private static String user;
  40. private static String pw;
  41. private static LegitWampClient client;
  42.  
  43. @BeforeClass
  44. public static void setUserPw() {
  45. user = System.getProperty("user");
  46. pw = System.getProperty("pw");
  47. // user = "jmedeiros@provagroup.com";
  48. // pw = "6bccb1d24c";
  49. if (user == null) {
  50. throw new IllegalStateException("User is null, please set the argument user=");
  51. }
  52. if (pw == null) {
  53. throw new IllegalStateException("Password (pw) is null, please set the argument pw=");
  54. }
  55. }
  56.  
  57. @Test
  58. public void testLogin() {
  59. try {
  60. out("\n\tUser: " + user + "\n\tPush: " + push + " URI: " + URI);
  61. LegitWampClient c = getClient(realmlegit);
  62. assertNotNull(c);
  63. c.login();
  64. } catch (Exception e) {
  65. fail("Failed to login \n\tUser: " + user + "\n\tURI: " + URI);
  66. }
  67. }
  68.  
  69.  
  70. @Test
  71. public void testLoginUser() {
  72. try {
  73. LegitWampClient c = getClient(realmlegit);
  74. Map<String, Object> luser = c.getLoginUser();
  75. assertNotNull(luser);
  76. assertEquals(user, luser.get(WampApiFieldKeys.U_EMAIL));
  77. out(luser);
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. fail("Failed to return login \n\tUser: " + user + "\n\tURI: " + URI);
  81. }
  82. }
  83.  
  84. @Test
  85. public void testUserRegistration() {
  86. String uu = null;
  87. try {
  88. LegitWampClient c = getClient(realmlegit);
  89. assertNotNull(c);
  90.  
  91. Map<String, Object> args = new HashMap<String, Object>();
  92. String email = UUID.randomUUID().toString().substring(0, 10) + "@provagroup.com";
  93. String password = UUID.randomUUID().toString().substring(0, 15);
  94.  
  95. args.put(WampApiFieldKeys.U_EMAIL, email );
  96. args.put(WampApiFieldKeys.U_PWNEW, password);
  97. args.put(WampApiFieldKeys.U_FIRST, "Joe" );
  98. args.put(WampApiFieldKeys.U_LAST, "Test" );
  99. args.put(WampApiFieldKeys.U_MIDDLE, "M" );
  100. args.put(WampApiFieldKeys.U_TITLE, "Mr");
  101.  
  102. Map<String, Object> ret = c.registerUser(args);
  103. assertNotNull(ret);
  104. out (String.format("User Created:\nEmail: %s\nName:%s%s%s%s%s",
  105. ret.get(WampApiFieldKeys.U_EMAIL),
  106. getName(ret.get(WampApiFieldKeys.U_TITLE)),
  107. getName(ret.get(WampApiFieldKeys.U_FIRST)),
  108. getName(ret.get(WampApiFieldKeys.U_MIDDLE)),
  109. getName(ret.get(WampApiFieldKeys.U_LAST)),
  110. getName(ret.get(WampApiFieldKeys.U_SUFFIX))));
  111.  
  112. assertEquals(ret.get(WampApiFieldKeys.U_EMAIL), ret.get(WampApiFieldKeys.U_EMAIL));
  113. assertEquals(ret.get(WampApiFieldKeys.U_FIRST), ret.get(WampApiFieldKeys.U_FIRST));
  114. assertEquals(ret.get(WampApiFieldKeys.U_LAST), ret.get(WampApiFieldKeys.U_LAST));
  115. assertEquals(ret.get(WampApiFieldKeys.U_MIDDLE), ret.get(WampApiFieldKeys.U_MIDDLE));
  116. assertEquals(ret.get(WampApiFieldKeys.U_TITLE), ret.get(WampApiFieldKeys.U_TITLE));
  117. assertEquals(ret.get(WampApiFieldKeys.U_SUFFIX), ret.get(WampApiFieldKeys.U_SUFFIX));
  118.  
  119. uu = email;
  120. c = getClient(realmlegit, uu, password);
  121. assertNotNull(c);
  122. Map<String, Object> u = c.getLoginUser();
  123. assertNotNull(u);
  124.  
  125. assertEquals(ret.get(WampApiFieldKeys.U_EMAIL), u.get(WampApiFieldKeys.U_EMAIL));
  126. assertEquals(ret.get(WampApiFieldKeys.U_FIRST), u.get(WampApiFieldKeys.U_FIRST));
  127. assertEquals(ret.get(WampApiFieldKeys.U_LAST), u.get(WampApiFieldKeys.U_LAST));
  128. assertEquals(ret.get(WampApiFieldKeys.U_MIDDLE), u.get(WampApiFieldKeys.U_MIDDLE));
  129. assertEquals(ret.get(WampApiFieldKeys.U_TITLE), u.get(WampApiFieldKeys.U_TITLE));
  130. assertEquals(ret.get(WampApiFieldKeys.U_SUFFIX), u.get(WampApiFieldKeys.U_SUFFIX));
  131.  
  132. out (String.format("User Created:\nEmail: %s\nName:%s%s%s%s%s",
  133. u.get(WampApiFieldKeys.U_EMAIL),
  134. getName(u.get(WampApiFieldKeys.U_TITLE)),
  135. getName(u.get(WampApiFieldKeys.U_FIRST)),
  136. getName(u.get(WampApiFieldKeys.U_MIDDLE)),
  137. getName(u.get(WampApiFieldKeys.U_LAST)),
  138. getName(u.get(WampApiFieldKeys.U_SUFFIX))));
  139.  
  140. } catch (Exception e) {
  141. e.printStackTrace();
  142. fail("Failed to login \n\tUser: " + uu + "\n\tURI: " + URI);
  143. }
  144. }
  145.  
  146.  
  147. private String getName(Object o) {
  148. return (String) (o == null ? "" : " " + o);
  149. }
  150.  
  151. @Test
  152. public void testGetMyAssets() {
  153. try {
  154. LegitWampClient c = getClient(realmlegit);
  155. assertNotNull(c);
  156. List<Object> ret = c.getMyCollection();
  157. assertNotNull(ret);
  158. assertTrue(ret.size() > 0);
  159. assertNotNull(ret.get(0));
  160. } catch (Exception e) {
  161. e.printStackTrace();
  162. fail(e.getMessage());
  163. }
  164. }
  165.  
  166. @Test
  167. public void testUpdatePassword() {
  168. try {
  169. LegitWampClient c = getClient(realmlegit);
  170. assertNotNull(c);
  171. Map<String, Object> u = c.getLoginUser();
  172. String newpw = UUID.randomUUID().toString().substring(5, 15);
  173. c.updatePassword((String) u.get(WampApiFieldKeys.U_ID), pw, newpw, newpw);
  174. client = null;
  175. c = getClient(realmlegit);
  176. try {
  177. c.login();
  178. fail("Logged in with existing password");
  179. } catch (Exception expected) {
  180. }
  181. client = null;
  182. c = getClient(realmlegit, user, newpw);
  183. c.login();
  184. c.updatePassword((String) u.get(WampApiFieldKeys.U_ID), newpw, pw, pw);
  185. try {
  186. client = null;
  187. c = getClient(realmlegit, user, newpw);
  188. c.login();
  189. fail("Logged in with new password");
  190. } catch (Exception expected) {
  191. }
  192.  
  193. client = null;
  194. c = getClient(realmlegit);
  195. c.login();
  196.  
  197. } catch (Exception e) {
  198. e.printStackTrace();
  199. fail(e.getMessage());
  200. }
  201. }
  202.  
  203. @Test
  204. public void testGetAssetBySerialNo() {
  205. try {
  206. LegitWampClient c = getClient(realmlegit);
  207. assertNotNull(c);
  208. String serno = "Z4vuNK2vMI"; // test serial no.
  209. Map<String, Object> ret = c.getAsset(serno);
  210. assertNotNull(ret);
  211. assertTrue("Failed to return asset", ret.size() > 0);
  212. assertEquals(serno, ret.get(WampApiFieldKeys.ASSET_SERIAL_NO));
  213. } catch (Exception e) {
  214. e.printStackTrace();
  215. fail(e.getMessage());
  216. }
  217. }
  218.  
  219. @Test
  220. public void testRegisterItem() {
  221. try {
  222. LegitWampClient c = getClient(realmlegit);
  223. assertNotNull(c);
  224. String serno = "Z4vuNK2vMI"; // test serial no.
  225. String certserno = "Z4vuNK2vdk"; // test certificate serial no
  226. Map<String, Object> ret = c.registerAsset(serno, certserno);
  227. assertNotNull(ret);
  228. assertTrue("Failed to return asset", ret.size() > 0);
  229. assertEquals(serno, ret.get(WampApiFieldKeys.ASSET_SERIAL_NO));
  230. assertEquals(certserno, ret.get(WampApiFieldKeys.CERT_SERIAL_NO));
  231.  
  232. } catch (Exception e) {
  233. e.printStackTrace();
  234. fail(e.getMessage());
  235. }
  236. }
  237.  
  238. @Test
  239. public void testForgotPassword() {
  240. try {
  241.  
  242. LegitWampClient c = getClient(realmlegit);
  243. assertNotNull(c);
  244. String ret = c.forgotPassword("sutest1@provagroup.com");
  245. assertNotNull(ret);
  246. out("ForgotPassword response: " + ret);
  247. } catch (Exception e) {
  248. e.printStackTrace();
  249. fail(e.getMessage());
  250. }
  251. }
  252.  
  253. @Test
  254. public void testUpdateUserInfo() {
  255. try {
  256. LegitWampClient c = getClient(realmlegit);
  257. assertNotNull(c);
  258. Map<String, Object> u = c.getLoginUser();
  259. String lname = (String) u.get(WampApiFieldKeys.U_LAST);
  260. String newname = lname != null && lname.compareTo("Smith") == 0 ? "Jones" : "Smith";
  261. u.put(WampApiFieldKeys.U_LAST, newname);
  262. Map<String, Object> u2 = c.updateUser(u);
  263. assertEquals(newname, u2.get(WampApiFieldKeys.U_LAST));
  264. } catch (Exception e) {
  265. e.printStackTrace();
  266. fail(e.getMessage());
  267. }
  268. }
  269.  
  270. final LegitWampClient getClient(String realm) {
  271. return getClient(realm, user, pw);
  272. }
  273. final LegitWampClient getClient(String realm, String user, String pw) {
  274. if (client == null || realm.compareTo(client.getRealm()) != 0 || user.compareTo(client.getClientUser()) != 0) {
  275. client = LegitWampClient.getClient(URI, realm, user, pw);
  276. }
  277. return client;
  278. }
  279.  
  280.  
  281.  
  282. // private static Object processEmail(Object object) {
  283. // return production == 2 ? processDefaultEmail(object) : getNonProdEmail(object);
  284. // }
  285. //
  286. // private static Object processDefaultEmail(Object object) {
  287. // String e = (String) object;
  288. // return "tristarshow@provagroup.com".compareTo(e) == 0 ? "michael.creel@anythingdish.com" : e;
  289. // }
  290. //
  291. // private static Object getNonProdEmail(Object object) {
  292. // String e = (String) object;
  293. // int idx = e.indexOf('@');
  294. // if (idx > 0) {
  295. // e = e.substring(0, idx);
  296. // }
  297. // return e + "@provagroup.com";
  298. // }
  299.  
  300. // private static boolean appendIfNotNull(Map<String, Object> json, String key, String val) {
  301. // final String trimmed;
  302. // if (val != null && (trimmed = val.trim()).length() > 0) {
  303. // json.put(key, trimmed);
  304. // return true;
  305. // }
  306. // return false;
  307. // }
  308. //
  309. // private static void append(Map<String, Object> json, String name, Object o) {
  310. // if (o != null) {
  311. // json.put(name, o.toString());
  312. // }
  313. // }
  314.  
  315. private static void out(Object e) {
  316. // System.out.println(e);
  317. // System.out.flush();
  318. log.info(e.toString());
  319. }
  320.  
  321. // private static Long getLong(Object object) {
  322. // Number num = (Number) object;
  323. // return num == null ? null : num.longValue();
  324. // }
  325. //
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement