use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.
the class TestUnsignedRead method main.
/**
* Update a field, them read the field without a reader
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
final GNSClientCommands client = new GNSClientCommands();
GuidEntry entry = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_GUID, PASSWORD);
// Update someField with someValue
client.fieldUpdate(entry, someField, someValue);
// Test unsigned read
String response = client.fieldRead(entry.getGuid(), someField, null);
assert (response.equals(someValue));
System.out.println("Test succeeds!");
}
use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.
the class BatchCreateOpsFail method setupClientsAndGuids.
private void setupClientsAndGuids() throws Exception {
clients = new GNSClientCommands[numClients];
for (int i = 0; i < numClients; i++) {
clients[i] = new GNSClientCommands();
}
String gnsInstance = clients[0].getGNSProvider();
accountGuidEntries = new GuidEntry[numAccountGuids];
for (int i = 0; i < numAccountGuids; i++) {
log.log(Level.FINE, "{0} creating account GUID {1}", new Object[] { this, ACCOUNT_GUID_PREFIX + i });
try {
accountGuidEntries[i] = clients[0].accountGuidCreate(ACCOUNT_GUID_PREFIX + i, PASSWORD);
log.log(Level.FINE, "{0} created account ", new Object[] { this, accountGuidEntries[i] });
assert (accountGuidEntries[i].getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i).getGuid()));
} catch (DuplicateNameException e) {
accountGuidEntries[i] = KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i);
log.log(Level.INFO, "{0} found that account {1} already exists", new Object[] { this, accountGuidEntries[i] });
}
// any other exception should be throw up
}
System.out.println("Created or found pre-existing " + numAccountGuids + " account GUIDs: " + Arrays.asList(accountGuidEntries));
if (accountGuidsOnly) {
for (int i = 0; i < accountGuidEntries.length; i++) {
guidEntries[i] = accountGuidEntries[i];
}
return;
}
guidEntries = new GuidEntry[numGuids];
Set<String> subGuids = new HashSet<String>();
for (int i = 0, j = 0; i < numGuids; i++) {
subGuids.add(Config.getGlobalString(TC.TEST_GUID_PREFIX) + i);
if (subGuids.size() == numGuidsPerAccount || i == numGuids - 1) {
// because batch creation seems buggy
if (subGuids.size() == 1) {
String subGuid = subGuids.iterator().next();
try {
GuidEntry created = clients[0].guidCreate(accountGuidEntries[i / numGuidsPerAccount], subGuid);
assert (created.getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, subGuid).getGuid()));
} catch (DuplicateNameException de) {
// ignore, will retrieve it locally below
}
// any other exception should be throw up
} else {
try {
// batch create
clients[0].guidBatchCreate(accountGuidEntries[i / numGuidsPerAccount], subGuids);
} catch (Exception e) {
for (String subGuid : subGuids) {
try {
clients[0].guidCreate(accountGuidEntries[i / numGuidsPerAccount], subGuid);
} catch (DuplicateNameException de) {
// ignore, will retrieve it locally below
}
// any other exception should be throw up
}
}
}
for (String subGuid : subGuids) {
guidEntries[j++] = KeyPairUtils.getGuidEntry(gnsInstance, subGuid);
}
log.log(Level.FINE, "{0} created sub-guid(s) {1}", new Object[] { this, subGuids });
subGuids.clear();
}
}
for (GuidEntry guidEntry : accountGuidEntries) {
assert (guidEntry != null);
}
for (GuidEntry guidEntry : guidEntries) {
assert (guidEntry != null);
}
System.out.println("Created or found " + guidEntries.length + " pre-existing sub-guids " + Arrays.asList(guidEntries));
}
use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.
the class BatchCreateOpsFail method cleanup.
/**
*
* @throws Exception
*/
@After
public void cleanup() throws Exception {
Thread.sleep(2000);
assert (clients != null && clients[0] != null);
System.out.println("About to delete " + guidEntries.length + " sub-guids: " + Arrays.asList(guidEntries));
for (GuidEntry guidEntry : guidEntries) {
try {
log.log(Level.INFO, "{0} about to delete sub-guid {1}", new Object[] { this, guidEntry });
clients[0].guidRemove(guidEntry);
log.log(Level.INFO, "{0} deleted sub-guid {1}", new Object[] { this, guidEntry });
} catch (Exception e) {
log.log(Level.WARNING, "{0} failed to delete sub-guid {1}", new Object[] { this, guidEntry });
e.printStackTrace();
// continue with rest
}
}
System.out.println("About to delete " + accountGuidEntries.length + " account guids: " + Arrays.asList(accountGuidEntries));
for (GuidEntry accGuidEntry : accountGuidEntries) {
try {
clients[0].accountGuidRemove(accGuidEntry);
log.log(Level.FINE, "{0} deleted account guid {1}", new Object[] { this, accGuidEntry });
} catch (Exception e) {
log.log(Level.WARNING, "{0} failed to delete account guid {1}", new Object[] { this, accGuidEntry });
e.printStackTrace();
// continue with rest
}
}
for (GNSClientCommands client : clients) {
client.close();
}
}
use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.
the class SomeReadsEncryptionFails method cleanup.
/**
* Removes all account and sub-guids created during the test.
*
* @throws Exception
*/
@AfterClass
public static void cleanup() throws Exception {
Thread.sleep(2000);
assert (clients != null && clients[0] != null);
if (!accountGuidsOnly) {
System.out.println("About to delete " + guidEntries.length + " sub-guids: " + Arrays.asList(guidEntries));
for (GuidEntry guidEntry : guidEntries) {
try {
log.log(Level.FINE, "About to delete sub-guid {0}", new Object[] { guidEntry });
clients[0].guidRemove(guidEntry);
log.log(Level.FINE, "Deleted sub-guid {0}", new Object[] { guidEntry });
} catch (Exception e) {
log.log(Level.WARNING, "Failed to delete sub-guid {0}", new Object[] { guidEntry });
e.printStackTrace();
// continue with rest
}
}
}
System.out.println("About to delete " + accountGuidEntries.length + " account guids: " + Arrays.asList(accountGuidEntries));
for (GuidEntry accGuidEntry : accountGuidEntries) {
try {
log.log(Level.FINE, "About to delete account guid {0}", new Object[] { accGuidEntry });
clients[0].accountGuidRemove(accGuidEntry);
log.log(Level.FINE, "Deleted account guid {0}", new Object[] { accGuidEntry });
} catch (Exception e) {
log.log(Level.WARNING, "Failed to delete account guid {0}", new Object[] { accGuidEntry });
e.printStackTrace();
// continue with rest
}
}
for (GNSClientCommands client : clients) {
client.close();
}
executor.shutdown();
}
use of edu.umass.cs.gnsclient.client.GNSClientCommands in project GNS by MobilityFirst.
the class ContextAwareGroupGuidExample method main.
/**
*
* @param args
* @throws IOException
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
* @throws ClientException
* @throws InvalidKeyException
* @throws SignatureException
* @throws Exception
*/
public static void main(String[] args) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, ClientException, InvalidKeyException, SignatureException, Exception {
// BOILER PLATE FOR RUNNING AN EXAMPLE
// Start the client
client = new GNSClientCommands(null);
try {
// Create the account guid using your email address and password = "password"
masterGuid = lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, "password");
} catch (Exception e) {
System.out.println("Exception during accountGuid creation: " + e);
System.exit(1);
}
System.out.println("Client connected to GNS.");
// Create 5 guids each of which have the field using our fieldname with a value of 25
for (int cnt = 0; cnt < 5; cnt++) {
GuidEntry guidEntry = client.guidCreate(masterGuid, "valueField-" + cnt);
client.fieldUpdate(guidEntry, fieldName, 25);
}
//FIXME: THIS CODE IS OBSOLETE. THE INTERFACE HAS CHANGED.
// Create a guid for the first group
groupOneGuidEntry = client.guidCreate(masterGuid, "contextAware > 20 Group");
// Set up the group context with a query
String query = "~" + fieldName + " : {$gt: 20}";
// Note that the zero for the interval means that we will never get stale results (not a
// good idea to do in production code! The default is 60 (seconds))
JSONArray result = client.selectSetupGroupQuery(masterGuid, groupOneGuidEntry.getGuid(), query, 0);
// Show the values from the guids that we got back
System.out.println("Members of " + groupOneGuidEntry.getEntityName() + ":");
showFieldValuesInGuids(result, fieldName);
//FIXME: THIS CODE IS OBSOLETE. THE INTERFACE HAS CHANGED.
// Create a second group guid
groupTwoGuidEntry = client.guidCreate(masterGuid, "contextAware = 0 Group");
// Set up a second group with a different query
query = "~" + fieldName + " : 0";
// Note that the zero for the interval means that we will never get stale results (not a
// good idea to do in production code! The default is 60 (seconds))
JSONArray resultTwo = client.selectSetupGroupQuery(masterGuid, groupTwoGuidEntry.getGuid(), query, 0);
// Show the values from the guids that we got back
System.out.println("Members of " + groupTwoGuidEntry.getEntityName() + ": (should be empty)");
showFieldValuesInGuids(resultTwo, fieldName);
// Now we lookup the values of the first group again
JSONArray resultThree = client.selectLookupGroupQuery(groupOneGuidEntry.getGuid());
System.out.println("Members of " + groupOneGuidEntry.getEntityName() + ": (should still be 5 of them)");
showFieldValuesInGuids(resultThree, fieldName);
// THIS IS WHERE IT GETS INTERESTING
// And change the values of all but one of them
System.out.println("Changing 4 of the 5 guids to have a their " + fieldName + " field's value be 0");
for (int i = 0; i < result.length() - 1; i++) {
BasicGuidEntry guidInfo = new BasicGuidEntry(client.lookupGuidRecord(result.getString(i)));
GuidEntry entry = GuidUtils.lookupGuidEntryFromDatabase(client, guidInfo.getEntityName());
System.out.println("Changing value of " + fieldName + " field in " + entry.getEntityName() + " to 0");
client.fieldUpdate(entry, fieldName, 0);
}
// Now we lookup the values of the first group again - showing that there is only one guid in the group
JSONArray resultFour = client.selectLookupGroupQuery(groupOneGuidEntry.getGuid());
System.out.println("Members of " + groupOneGuidEntry.getEntityName() + ": (should only be one of them)");
showFieldValuesInGuids(resultFour, fieldName);
// Now we lookup the values of the second group again - this one now has 4 guids
JSONArray resultFive = client.selectLookupGroupQuery(groupTwoGuidEntry.getGuid());
System.out.println("Members of " + groupTwoGuidEntry.getEntityName() + ": (should be 4 of them)");
showFieldValuesInGuids(resultFive, fieldName);
// So we can run this again without duplicate name/guid errors.
cleanupAllGuids();
System.exit(0);
}
Aggregations