Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.provagroup.provaserver.wamp.legit;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertNotNull;
- import static org.junit.Assert.assertTrue;
- import static org.junit.Assert.fail;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.UUID;
- import java.util.logging.Logger;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import com.provagroup.provaserver.wamp.WampApiFieldKeys;
- import com.provagroup.wamp.client.LegitWampClient;
- /**
- * @author rbense
- *
- */
- public class LegitTest {
- private static Logger log = Logger.getLogger(LegitTest.class.getName());
- public static final long WAIT_TIME = 100000; // DEBUG - increase this value or the client code will finish in 100s
- private static final boolean push = true;
- private static int production = 0;
- // private static int production = 0; // local test
- // private static final int production = 1; // p4 testing
- // private static final int production = 2; // production
- private static final String host = production == 0 ? "rb.provagroup.com" : production == 1 ? "p4.provagroup.com" : "cloud.provagroup.com";
- private static final String port = production == 0 ? "9443" : production == 1 ? "10443" : "9443";
- private static final String URI = "wss://" + host + ":" + port + "/provaserver/wamp";
- private static final String realmlegit = "provagroup.provaserver.legitconsumerapp";
- private static String user;
- private static String pw;
- private static LegitWampClient client;
- @BeforeClass
- public static void setUserPw() {
- user = System.getProperty("user");
- pw = System.getProperty("pw");
- // user = "jmedeiros@provagroup.com";
- // pw = "6bccb1d24c";
- if (user == null) {
- throw new IllegalStateException("User is null, please set the argument user=");
- }
- if (pw == null) {
- throw new IllegalStateException("Password (pw) is null, please set the argument pw=");
- }
- }
- @Test
- public void testLogin() {
- try {
- out("\n\tUser: " + user + "\n\tPush: " + push + " URI: " + URI);
- LegitWampClient c = getClient(realmlegit);
- assertNotNull(c);
- c.login();
- } catch (Exception e) {
- fail("Failed to login \n\tUser: " + user + "\n\tURI: " + URI);
- }
- }
- @Test
- public void testLoginUser() {
- try {
- LegitWampClient c = getClient(realmlegit);
- Map<String, Object> luser = c.getLoginUser();
- assertNotNull(luser);
- assertEquals(user, luser.get(WampApiFieldKeys.U_EMAIL));
- out(luser);
- } catch (Exception e) {
- e.printStackTrace();
- fail("Failed to return login \n\tUser: " + user + "\n\tURI: " + URI);
- }
- }
- @Test
- public void testUserRegistration() {
- String uu = null;
- try {
- LegitWampClient c = getClient(realmlegit);
- assertNotNull(c);
- Map<String, Object> args = new HashMap<String, Object>();
- String email = UUID.randomUUID().toString().substring(0, 10) + "@provagroup.com";
- String password = UUID.randomUUID().toString().substring(0, 15);
- args.put(WampApiFieldKeys.U_EMAIL, email );
- args.put(WampApiFieldKeys.U_PWNEW, password);
- args.put(WampApiFieldKeys.U_FIRST, "Joe" );
- args.put(WampApiFieldKeys.U_LAST, "Test" );
- args.put(WampApiFieldKeys.U_MIDDLE, "M" );
- args.put(WampApiFieldKeys.U_TITLE, "Mr");
- Map<String, Object> ret = c.registerUser(args);
- assertNotNull(ret);
- out (String.format("User Created:\nEmail: %s\nName:%s%s%s%s%s",
- ret.get(WampApiFieldKeys.U_EMAIL),
- getName(ret.get(WampApiFieldKeys.U_TITLE)),
- getName(ret.get(WampApiFieldKeys.U_FIRST)),
- getName(ret.get(WampApiFieldKeys.U_MIDDLE)),
- getName(ret.get(WampApiFieldKeys.U_LAST)),
- getName(ret.get(WampApiFieldKeys.U_SUFFIX))));
- assertEquals(ret.get(WampApiFieldKeys.U_EMAIL), ret.get(WampApiFieldKeys.U_EMAIL));
- assertEquals(ret.get(WampApiFieldKeys.U_FIRST), ret.get(WampApiFieldKeys.U_FIRST));
- assertEquals(ret.get(WampApiFieldKeys.U_LAST), ret.get(WampApiFieldKeys.U_LAST));
- assertEquals(ret.get(WampApiFieldKeys.U_MIDDLE), ret.get(WampApiFieldKeys.U_MIDDLE));
- assertEquals(ret.get(WampApiFieldKeys.U_TITLE), ret.get(WampApiFieldKeys.U_TITLE));
- assertEquals(ret.get(WampApiFieldKeys.U_SUFFIX), ret.get(WampApiFieldKeys.U_SUFFIX));
- uu = email;
- c = getClient(realmlegit, uu, password);
- assertNotNull(c);
- Map<String, Object> u = c.getLoginUser();
- assertNotNull(u);
- assertEquals(ret.get(WampApiFieldKeys.U_EMAIL), u.get(WampApiFieldKeys.U_EMAIL));
- assertEquals(ret.get(WampApiFieldKeys.U_FIRST), u.get(WampApiFieldKeys.U_FIRST));
- assertEquals(ret.get(WampApiFieldKeys.U_LAST), u.get(WampApiFieldKeys.U_LAST));
- assertEquals(ret.get(WampApiFieldKeys.U_MIDDLE), u.get(WampApiFieldKeys.U_MIDDLE));
- assertEquals(ret.get(WampApiFieldKeys.U_TITLE), u.get(WampApiFieldKeys.U_TITLE));
- assertEquals(ret.get(WampApiFieldKeys.U_SUFFIX), u.get(WampApiFieldKeys.U_SUFFIX));
- out (String.format("User Created:\nEmail: %s\nName:%s%s%s%s%s",
- u.get(WampApiFieldKeys.U_EMAIL),
- getName(u.get(WampApiFieldKeys.U_TITLE)),
- getName(u.get(WampApiFieldKeys.U_FIRST)),
- getName(u.get(WampApiFieldKeys.U_MIDDLE)),
- getName(u.get(WampApiFieldKeys.U_LAST)),
- getName(u.get(WampApiFieldKeys.U_SUFFIX))));
- } catch (Exception e) {
- e.printStackTrace();
- fail("Failed to login \n\tUser: " + uu + "\n\tURI: " + URI);
- }
- }
- private String getName(Object o) {
- return (String) (o == null ? "" : " " + o);
- }
- @Test
- public void testGetMyAssets() {
- try {
- LegitWampClient c = getClient(realmlegit);
- assertNotNull(c);
- List<Object> ret = c.getMyCollection();
- assertNotNull(ret);
- assertTrue(ret.size() > 0);
- assertNotNull(ret.get(0));
- } catch (Exception e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
- }
- @Test
- public void testUpdatePassword() {
- try {
- LegitWampClient c = getClient(realmlegit);
- assertNotNull(c);
- Map<String, Object> u = c.getLoginUser();
- String newpw = UUID.randomUUID().toString().substring(5, 15);
- c.updatePassword((String) u.get(WampApiFieldKeys.U_ID), pw, newpw, newpw);
- client = null;
- c = getClient(realmlegit);
- try {
- c.login();
- fail("Logged in with existing password");
- } catch (Exception expected) {
- }
- client = null;
- c = getClient(realmlegit, user, newpw);
- c.login();
- c.updatePassword((String) u.get(WampApiFieldKeys.U_ID), newpw, pw, pw);
- try {
- client = null;
- c = getClient(realmlegit, user, newpw);
- c.login();
- fail("Logged in with new password");
- } catch (Exception expected) {
- }
- client = null;
- c = getClient(realmlegit);
- c.login();
- } catch (Exception e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
- }
- @Test
- public void testGetAssetBySerialNo() {
- try {
- LegitWampClient c = getClient(realmlegit);
- assertNotNull(c);
- String serno = "Z4vuNK2vMI"; // test serial no.
- Map<String, Object> ret = c.getAsset(serno);
- assertNotNull(ret);
- assertTrue("Failed to return asset", ret.size() > 0);
- assertEquals(serno, ret.get(WampApiFieldKeys.ASSET_SERIAL_NO));
- } catch (Exception e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
- }
- @Test
- public void testRegisterItem() {
- try {
- LegitWampClient c = getClient(realmlegit);
- assertNotNull(c);
- String serno = "Z4vuNK2vMI"; // test serial no.
- String certserno = "Z4vuNK2vdk"; // test certificate serial no
- Map<String, Object> ret = c.registerAsset(serno, certserno);
- assertNotNull(ret);
- assertTrue("Failed to return asset", ret.size() > 0);
- assertEquals(serno, ret.get(WampApiFieldKeys.ASSET_SERIAL_NO));
- assertEquals(certserno, ret.get(WampApiFieldKeys.CERT_SERIAL_NO));
- } catch (Exception e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
- }
- @Test
- public void testForgotPassword() {
- try {
- LegitWampClient c = getClient(realmlegit);
- assertNotNull(c);
- String ret = c.forgotPassword("sutest1@provagroup.com");
- assertNotNull(ret);
- out("ForgotPassword response: " + ret);
- } catch (Exception e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
- }
- @Test
- public void testUpdateUserInfo() {
- try {
- LegitWampClient c = getClient(realmlegit);
- assertNotNull(c);
- Map<String, Object> u = c.getLoginUser();
- String lname = (String) u.get(WampApiFieldKeys.U_LAST);
- String newname = lname != null && lname.compareTo("Smith") == 0 ? "Jones" : "Smith";
- u.put(WampApiFieldKeys.U_LAST, newname);
- Map<String, Object> u2 = c.updateUser(u);
- assertEquals(newname, u2.get(WampApiFieldKeys.U_LAST));
- } catch (Exception e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
- }
- final LegitWampClient getClient(String realm) {
- return getClient(realm, user, pw);
- }
- final LegitWampClient getClient(String realm, String user, String pw) {
- if (client == null || realm.compareTo(client.getRealm()) != 0 || user.compareTo(client.getClientUser()) != 0) {
- client = LegitWampClient.getClient(URI, realm, user, pw);
- }
- return client;
- }
- // private static Object processEmail(Object object) {
- // return production == 2 ? processDefaultEmail(object) : getNonProdEmail(object);
- // }
- //
- // private static Object processDefaultEmail(Object object) {
- // String e = (String) object;
- // return "tristarshow@provagroup.com".compareTo(e) == 0 ? "michael.creel@anythingdish.com" : e;
- // }
- //
- // private static Object getNonProdEmail(Object object) {
- // String e = (String) object;
- // int idx = e.indexOf('@');
- // if (idx > 0) {
- // e = e.substring(0, idx);
- // }
- // return e + "@provagroup.com";
- // }
- // private static boolean appendIfNotNull(Map<String, Object> json, String key, String val) {
- // final String trimmed;
- // if (val != null && (trimmed = val.trim()).length() > 0) {
- // json.put(key, trimmed);
- // return true;
- // }
- // return false;
- // }
- //
- // private static void append(Map<String, Object> json, String name, Object o) {
- // if (o != null) {
- // json.put(name, o.toString());
- // }
- // }
- private static void out(Object e) {
- // System.out.println(e);
- // System.out.flush();
- log.info(e.toString());
- }
- // private static Long getLong(Object object) {
- // Number num = (Number) object;
- // return num == null ? null : num.longValue();
- // }
- //
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement