Search in sources :

Example 36 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class SingleRemoveGuidTest method test_01_RemoveGuidUsingAccount.

/**
   *
   */
@Test
public void test_01_RemoveGuidUsingAccount() {
    String testGuidName = "testGUID" + RandomString.randomString(12);
    GuidEntry testGuid = null;
    try {
        testGuid = clientCommands.guidCreate(masterGuid, testGuidName);
    } catch (ClientException | IOException e) {
        Utils.failWithStackTrace("Exception while creating testGuid: " + e);
    }
    if (testGuid != null) {
        try {
            clientCommands.guidRemove(masterGuid, testGuid.getGuid());
        } catch (ClientException | IOException e) {
            Utils.failWithStackTrace("Exception while removing testGuid: " + e);
        }
        try {
            clientCommands.lookupGuidRecord(testGuid.getGuid());
            Utils.failWithStackTrace("Lookup testGuid should have throw an exception.");
        } catch (ClientException e) {
        } catch (IOException e) {
            Utils.failWithStackTrace("Exception while doing Lookup testGuid: " + e);
        }
    }
}
Also used : RandomString(edu.umass.cs.gnscommon.utils.RandomString) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) DefaultGNSTest(edu.umass.cs.gnsserver.utils.DefaultGNSTest) Test(org.junit.Test)

Example 37 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class SecureCommandTest method test_15_SecureRead.

/**
   * Remove the account.
   */
@Test
public void test_15_SecureRead() {
    GuidEntry masterGuid = GuidUtils.getGUIDKeys(globalAccountName);
    try {
        client.execute(GNSCommand.guidCreate(masterGuid, "whatever"));
        GuidEntry testGuid = GuidUtils.lookupGuidEntryFromDatabase(client, "whatever");
        client.execute(GNSCommand.fieldUpdate(testGuid, "fred", "value"));
        JSONObject actual = client.execute(GNSCommand.readSecure(testGuid.getGuid())).getResultJSONObject();
        JSONAssert.assertEquals(new JSONObject().put("fred", "value"), actual, JSONCompareMode.STRICT);
        client.execute(GNSCommand.guidRemove(masterGuid, testGuid.getGuid()));
    } catch (ClientException | IOException | JSONException e) {
        Utils.failWithStackTrace("Exception while removing account record: ", e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) Test(org.junit.Test) DefaultGNSTest(edu.umass.cs.gnsserver.utils.DefaultGNSTest)

Example 38 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class SelectRecordsExample method createUserRecords.

/**
   * Create some example user records.
   */
private static void createUserRecords() {
    Random random = new Random();
    for (int i = 0; i < 30; i++) {
        try {
            client.execute(GNSCommand.createGUID(accountGuidEntry, "user-" + i));
            GuidEntry userGuid = GuidUtils.getGUIDKeys("user-" + i);
            System.out.print("+");
            // Randomly place the in the region
            double lon = LEFT_LON + (RIGHT_LON - LEFT_LON) * random.nextDouble();
            double lat = BOTTOM_LAT + (TOP_LAT - BOTTOM_LAT) * random.nextDouble();
            client.execute(GNSCommand.setLocation(userGuid, lon, lat));
            // Add some additional random attributes
            client.execute(GNSCommand.fieldUpdate(userGuid, "age", random.nextInt(40) + 20));
            client.execute(GNSCommand.fieldUpdate(userGuid, "preference", random.nextInt(2) == 0 ? "Long" : "Short"));
        } catch (ClientException e) {
            // Catch the normal case where the records already exist
            if (ResponseCode.CONFLICTING_GUID_EXCEPTION.equals(e.getCode())) {
                System.out.print(".");
            } else {
                System.out.println("Problem creating user: " + e.getMessage());
            }
        } catch (IOException e) {
            System.out.println("Problem creating user: " + e.getMessage());
        }
    }
    System.out.println();
}
Also used : Random(java.util.Random) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 39 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class Select method parse.

@Override
public void parse(String commandText) throws Exception {
    try {
        GNSClientCommands gnsClient = module.getGnsClient();
        StringTokenizer st = new StringTokenizer(commandText.trim());
        GuidEntry guidEntry;
        switch(st.countTokens()) {
            case 2:
                guidEntry = null;
                break;
            case 3:
                String alias = st.nextToken();
                guidEntry = KeyPairUtils.getGuidEntry(module.getGnsInstance(), alias);
                if (guidEntry == null) {
                    console.printString("Unknown alias " + alias);
                    console.printNewline();
                    return;
                }
                break;
            default:
                wrongArguments();
                return;
        }
        String field = st.nextToken();
        String value = st.nextToken();
        JSONArray result;
        if (guidEntry != null) {
            result = gnsClient.select(guidEntry, field, value);
        } else {
            result = gnsClient.select(field, value);
        }
        console.printString(result.toString());
        console.printNewline();
    } catch (IOException | ClientException e) {
        console.printString("Failed to access GNS ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) JSONArray(org.json.JSONArray) IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 40 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class SetDefaultGuid method parse.

@Override
public void parse(String commandText) throws Exception {
    StringTokenizer st = new StringTokenizer(commandText.trim());
    if (st.countTokens() != 1) {
        wrongArguments();
        return;
    }
    String aliasName = st.nextToken();
    GNSClientCommands gnsClient = module.getGnsClient();
    try {
        try {
            gnsClient.lookupGuid(aliasName);
        } catch (ClientException e) {
            console.printString("Alias " + aliasName + " is not registered in the GNS\n");
            return;
        }
        if (!module.isSilent()) {
            console.printString("Looking up alias " + aliasName + " GUID and certificates...\n");
        }
        GuidEntry myGuid = KeyPairUtils.getGuidEntry(module.getGnsInstance(), aliasName);
        if (myGuid == null) {
            console.printString("You do not have the private key for alias " + aliasName);
            console.printNewline();
            return;
        }
        // Success, let's update the console prompt with the new alias name
        // module.setCurrentGuid(myGuid);
        module.setDefaultGuidAndCheckForVerified(myGuid);
        if (!module.isSilent()) {
            console.printString("Default GUID set to " + aliasName + "\n");
        }
    } catch (IOException e) {
        console.printString("Failed to access the GNS ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Aggregations

GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)134 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)87 IOException (java.io.IOException)85 Test (org.junit.Test)69 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)61 RandomString (edu.umass.cs.gnscommon.utils.RandomString)54 BasicGuidEntry (edu.umass.cs.gnsclient.client.util.BasicGuidEntry)46 JSONException (org.json.JSONException)45 JSONArray (org.json.JSONArray)36 GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)28 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)18 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)15 DuplicateNameException (edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException)14 StringTokenizer (java.util.StringTokenizer)13 JSONObject (org.json.JSONObject)12 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)11 FileNotFoundException (java.io.FileNotFoundException)11 HashSet (java.util.HashSet)10 FileInputStream (java.io.FileInputStream)8 Repeat (edu.umass.cs.utils.Repeat)7