Search in sources :

Example 61 with GuidEntry

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

the class DatabaseTest method test_17_SubstituteList.

/**
   *
   */
@Test
public void test_17_SubstituteList() {
    String testSubstituteListGuid = "testSubstituteListGUID" + RandomString.randomString(12);
    String field = "people";
    GuidEntry testEntry = null;
    try {
        //Utils.clearTestGuids(client);
        //System.out.println("cleared old GUIDs");
        testEntry = clientCommands.guidCreate(masterGuid, testSubstituteListGuid);
    //System.out.println("created test guid: " + testEntry);
    } catch (IOException | ClientException e) {
        Utils.failWithStackTrace("Exception during init: " + e);
    }
    if (testEntry != null) {
        try {
            clientCommands.fieldAppendOrCreateList(testEntry.getGuid(), field, new JSONArray(Arrays.asList("Frank", "Joe", "Sally", "Rita")), testEntry);
        } catch (IOException | ClientException e) {
            Utils.failWithStackTrace("Exception during create: " + e);
        }
        try {
            HashSet<String> expected = new HashSet<>(Arrays.asList("Frank", "Joe", "Sally", "Rita"));
            HashSet<String> actual = JSONUtils.JSONArrayToHashSet(clientCommands.fieldReadArray(testEntry.getGuid(), field, testEntry));
            Assert.assertEquals(expected, actual);
        } catch (IOException | ClientException | JSONException e) {
            Utils.failWithStackTrace("Exception when we were not expecting it: " + e);
        }
        try {
            clientCommands.fieldSubstitute(testEntry.getGuid(), field, new JSONArray(Arrays.asList("BillyBob", "Hank")), new JSONArray(Arrays.asList("Frank", "Joe")), testEntry);
        } catch (IOException | ClientException e) {
            Utils.failWithStackTrace("Exception during substitute: " + e);
        }
        try {
            HashSet<String> expected = new HashSet<>(Arrays.asList("BillyBob", "Hank", "Sally", "Rita"));
            HashSet<String> actual = JSONUtils.JSONArrayToHashSet(clientCommands.fieldReadArray(testEntry.getGuid(), field, testEntry));
            Assert.assertEquals(expected, actual);
        } catch (IOException | ClientException | JSONException e) {
            Utils.failWithStackTrace("Exception when we were not expecting it: " + e);
        }
        try {
            clientCommands.guidRemove(masterGuid, testEntry.getGuid());
        } catch (ClientException | IOException e) {
            Utils.failWithStackTrace("Exception while deleting guid: " + e);
        }
    }
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) RandomString(edu.umass.cs.gnscommon.utils.RandomString) IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) HashSet(java.util.HashSet) Test(org.junit.Test) DefaultGNSTest(edu.umass.cs.gnsserver.utils.DefaultGNSTest)

Example 62 with GuidEntry

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

the class AclTest method test_130_ACLALLFields.

/**
   *
   */
@Test
public void test_130_ACLALLFields() {
    //testACL();
    String superUserName = "superuser" + RandomString.randomString(12);
    try {
        try {
            clientCommands.lookupGuid(superUserName);
            Utils.failWithStackTrace(superUserName + " entity should not exist");
        } catch (ClientException e) {
        }
        GuidEntry superuserEntry = clientCommands.guidCreate(masterGuid, superUserName);
        // let superuser read any of barney's fields
        clientCommands.aclAdd(AclAccessType.READ_WHITELIST, barneyEntry, GNSProtocol.ENTIRE_RECORD.toString(), superuserEntry.getGuid());
        Assert.assertEquals("413-555-1234", clientCommands.fieldReadArrayFirstElement(barneyEntry.getGuid(), "cell", superuserEntry));
        Assert.assertEquals("100 Main Street", clientCommands.fieldReadArrayFirstElement(barneyEntry.getGuid(), "address", superuserEntry));
        try {
            clientCommands.guidRemove(masterGuid, superuserEntry.getGuid());
        } catch (ClientException | IOException e) {
            Utils.failWithStackTrace("Exception while removing superuserEntry in  ACLALLFields: " + e);
        }
    } catch (ClientException | IOException e) {
        Utils.failWithStackTrace("Exception when we were not expecting it in ACLALLFields: " + 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) Test(org.junit.Test) DefaultGNSTest(edu.umass.cs.gnsserver.utils.DefaultGNSTest)

Example 63 with GuidEntry

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

the class ActiveCodeHelloWorldExample method main.

/**
	 * @param args
	 * @throws IOException 
	 * @throws ClientException 
	 * @throws JSONException 
	 */
public static void main(String[] args) throws IOException, ClientException, JSONException {
    boolean update = true;
    if (System.getProperty("update") != null) {
        update = Boolean.parseBoolean(System.getProperty("update"));
    }
    boolean isRead = true;
    if (System.getProperty("isRead") != null) {
        isRead = Boolean.parseBoolean(System.getProperty("isRead"));
    }
    String name = "benign";
    if (System.getProperty("name") != null) {
        name = System.getProperty("name");
    }
    String codeFile = "scripts/activeCode/noop.js";
    if (System.getProperty("codeFile") != null) {
        codeFile = System.getProperty("codeFile");
    }
    if (args.length > 0) {
        codeFile = args[0];
    }
    // create a client
    final GNSClient client = new GNSClient();
    final String ACCOUNT_GUID_PREFIX = "GNS_ACCOUNT_";
    final String ACCOUNT_GUID = ACCOUNT_GUID_PREFIX + name;
    final String PASSWORD = "";
    edu.umass.cs.gnsclient.client.util.GuidEntry entry = null;
    // create an account	
    try {
        entry = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_GUID, PASSWORD);
        ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("guid")));
        entry.writeObject(output);
        output.flush();
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
        // The guid is already created, try to read from a local file
        try {
            ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("guid")));
            entry = new GuidEntry(input);
            input.close();
        } catch (IOException | EncryptionException ie) {
            ie.printStackTrace();
        }
    }
    String field = "someField";
    String value = "original value";
    String depth_field = "depthField";
    String depth_result = "Depth query succeeds";
    // set up a field
    client.execute(GNSCommand.fieldUpdate(entry, field, value));
    client.execute(GNSCommand.fieldUpdate(entry, depth_field, depth_result));
    // clear code for both read and write action
    client.execute(GNSCommand.activeCodeClear(entry.getGuid(), ActiveCode.READ_ACTION, entry));
    client.execute(GNSCommand.activeCodeClear(entry.getGuid(), ActiveCode.WRITE_ACTION, entry));
    // get the value of the field
    String response = client.execute(GNSCommand.fieldRead(entry, field)).getResultJSONObject().getString(field);
    System.out.println("Before the code is deployed, the value of field(" + field + ") is " + response);
    // read in the code as a string
    final String code = new String(Files.readAllBytes(Paths.get(codeFile)));
    // set up the code for on read operation
    if (isRead) {
        if (update) {
            client.execute(GNSCommand.activeCodeSet(entry.getGuid(), ActiveCode.READ_ACTION, code, entry));
        }
    } else {
        if (update) {
            client.execute(GNSCommand.activeCodeSet(entry.getGuid(), ActiveCode.WRITE_ACTION, code, entry));
        }
    }
    // get the value of the field again
    response = client.execute(GNSCommand.fieldRead(entry, field)).getResultJSONObject().getString(field);
    System.out.println("After the code is deployed, the value of field(" + field + ") is " + response);
    ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("guid")));
    entry.writeObject(output);
    output.flush();
    output.close();
    System.exit(0);
}
Also used : GNSClient(edu.umass.cs.gnsclient.client.GNSClient) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ObjectOutputStream(java.io.ObjectOutputStream) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) IOException(java.io.IOException) JSONException(org.json.JSONException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ObjectInputStream(java.io.ObjectInputStream)

Example 64 with GuidEntry

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

the class CapacityTestForLatencyClient method sequential_latency_test.

/**
	 * @throws InterruptedException
	 * @throws IOException 
	 * @throws EncryptionException 
	 */
public static void sequential_latency_test() throws InterruptedException, IOException, EncryptionException {
    guidIndex = Integer.parseInt(System.getProperty("guidIndex"));
    numGuids = Integer.parseInt(System.getProperty("numGuids"));
    entries = new GuidEntry[numGuids];
    for (int i = 0; i < numGuids; i++) {
        ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("guid" + (i * 1000 + guidIndex))));
        entries[i] = new GuidEntry(input);
        input.close();
    }
    System.out.println("Start running experiment for " + (withSignature ? "signed" : "unsigned") + " " + (isRead ? "read" : "write"));
    executor.execute(new GNSClientTask(clients[0], entries, ((Integer) RATE).doubleValue(), TOTAL));
    while (getRcvd() < TOTAL) {
        System.out.println("Client received " + received + " responses, " + (TOTAL - received) + " left.");
        Thread.sleep(1000);
    }
    System.out.println("Received all responses!");
    executor.shutdown();
}
Also used : File(java.io.File) FileInputStream(java.io.FileInputStream) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ObjectInputStream(java.io.ObjectInputStream)

Example 65 with GuidEntry

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

the class CapacityTestForLatencyClient method setup.

/**
	 * @throws Exception
	 */
public static void setup() throws Exception {
    numClients = 10;
    if (System.getProperty("numClients") != null) {
        numClients = Integer.parseInt(System.getProperty("numClients"));
    }
    System.out.println("There are " + numClients + " clients.");
    someField = "someField";
    if (System.getProperty("field") != null) {
        someField = System.getProperty("field");
    }
    withSignature = false;
    if (System.getProperty("withSigniture") != null) {
        withSignature = Boolean.parseBoolean(System.getProperty("withSigniture"));
    }
    RATE = 10;
    if (System.getProperty("rate") != null) {
        RATE = Integer.parseInt(System.getProperty("rate"));
    }
    TOTAL = RATE * DURATION / 1000;
    EXTRA_WAIT_TIME = 0;
    if (System.getProperty("extraTime") != null) {
        EXTRA_WAIT_TIME = 1000 * Integer.parseInt(System.getProperty("extraTime"));
    }
    isRead = true;
    if (System.getProperty("isRead") != null) {
        isRead = Boolean.parseBoolean(System.getProperty("isRead"));
    }
    if (System.getProperty("numThread") != null) {
        NUM_THREAD = Integer.parseInt(System.getProperty("numThread"));
    }
    if (System.getProperty("sequential") != null) {
        sequential = Boolean.parseBoolean(System.getProperty("sequential"));
    }
    String keyFile = "guid";
    if (System.getProperty("keyFile") != null) {
        keyFile = System.getProperty("keyFile");
    }
    ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File(keyFile)));
    entry = new GuidEntry(input);
    assert (entry != null);
    executor = Executors.newFixedThreadPool(NUM_THREAD);
    clients = new GNSClientCommands[numClients];
    for (int i = 0; i < numClients; i++) {
        clients[i] = new GNSClientCommands();
    }
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) File(java.io.File) FileInputStream(java.io.FileInputStream) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ObjectInputStream(java.io.ObjectInputStream)

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