Search in sources :

Example 16 with Employee

use of org.apache.gora.examples.generated.Employee in project gora by apache.

the class DataStoreTestUtil method createBoss.

public static <K> Employee createBoss() throws Exception {
    Employee employee = Employee.newBuilder().build();
    employee.setName(new Utf8("Random boss"));
    employee.setDateOfBirth(System.currentTimeMillis() - 22L * YEAR_IN_MS);
    employee.setSalary(1000000);
    employee.setSsn(new Utf8("202020202020"));
    return employee;
}
Also used : Employee(org.apache.gora.examples.generated.Employee) Utf8(org.apache.avro.util.Utf8)

Example 17 with Employee

use of org.apache.gora.examples.generated.Employee in project gora by apache.

the class DataStoreTestUtil method testGetEmployeeWithFields.

public static void testGetEmployeeWithFields(DataStore<String, Employee> dataStore) throws Exception {
    Employee employee = DataStoreTestUtil.createEmployee();
    WebPage webpage = createWebPage();
    employee.setWebpage(webpage);
    Employee boss = createBoss();
    employee.setBoss(boss);
    String ssn = employee.getSsn().toString();
    dataStore.put(ssn, employee);
    dataStore.flush();
    String[] fields = AvroUtils.getPersistentFieldNames(employee);
    for (Set<String> subset : StringUtils.powerset(fields)) {
        if (subset.isEmpty())
            continue;
        Employee after = dataStore.get(ssn, subset.toArray(new String[subset.size()]));
        Employee expected = Employee.newBuilder().build();
        for (String field : subset) {
            int index = expected.getSchema().getField(field).pos();
            expected.put(index, employee.get(index));
        }
        assertEqualEmployeeObjects(expected, after);
    }
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) Employee(org.apache.gora.examples.generated.Employee)

Example 18 with Employee

use of org.apache.gora.examples.generated.Employee in project gora by apache.

the class TestPersistentSerialization method testSerdeEmployeeOneField.

/**
   * Creates an Employee object but only sets one field as dirty.
   * We then do (de)serialization and check 'before' and 'after'
   * states.
   * @throws Exception
   */
@Test
public void testSerdeEmployeeOneField() throws Exception {
    Employee employee = Employee.newBuilder().build();
    employee.setSsn(new Utf8("11111"));
    TestIOUtils.testSerializeDeserialize(employee);
}
Also used : Employee(org.apache.gora.examples.generated.Employee) Utf8(org.apache.avro.util.Utf8) Test(org.junit.Test)

Example 19 with Employee

use of org.apache.gora.examples.generated.Employee in project gora by apache.

the class DataStoreTestUtil method assertEqualEmployeeObjects.

/**
   * Simple function which iterates through a before (put) and after (get) object
   * in an attempt to verify if the same field's and values have been obtained.
   * Within the original employee object we iterate from 1 instead of 0 due to the 
   * removal of the '__g__' field at position 0 when we put objects into the datastore. 
   * This field is used to identify whether fields within the object, and 
   * consequently the object itself, are/is dirty however this field is not 
   * required when persisting the object.
   * We explicitly get values from each field as this makes it easier to debug 
   * if tests go wrong.
   * @param employee
   * @param after
   */
private static void assertEqualEmployeeObjects(Employee employee, Employee after) {
    //for (int i = 1; i < employee.SCHEMA$.getFields().size(); i++) {
    //  for (int j = 1; j < after.SCHEMA$.getFields().size(); j++) {
    //    assertEquals(employee.SCHEMA$.getFields().get(i), after.SCHEMA$.getFields().get(j));
    //  }
    //}
    //check name field
    CharSequence beforeName = employee.getName();
    CharSequence afterName = after.getName();
    assertEquals(beforeName, afterName);
    //check dateOfBirth field
    Long beforeDOB = employee.getDateOfBirth();
    Long afterDOB = after.getDateOfBirth();
    assertEquals(beforeDOB, afterDOB);
    //check ssn field
    CharSequence beforeSsn = employee.getSsn();
    CharSequence afterSsn = after.getSsn();
    assertEquals(beforeSsn, afterSsn);
    //check salary field
    Integer beforeSalary = employee.getSalary();
    Integer afterSalary = after.getSalary();
    assertEquals(beforeSalary, afterSalary);
    //check boss field
    if (employee.getBoss() != null) {
        if (employee.getBoss() instanceof Utf8) {
            String beforeBoss = employee.getBoss().toString();
            String afterBoss = after.getBoss().toString();
            assertEquals("Boss String field values in UNION should be the same", beforeBoss, afterBoss);
        } else {
            Employee beforeBoss = (Employee) employee.getBoss();
            Employee afterBoss = (Employee) after.getBoss();
            assertEqualEmployeeObjects(beforeBoss, afterBoss);
        }
    }
    //check webpage field
    if (employee.getWebpage() != null) {
        WebPage beforeWebPage = employee.getWebpage();
        WebPage afterWebPage = after.getWebpage();
        assertEqualWebPageObjects(beforeWebPage, afterWebPage);
    }
}
Also used : WebPage(org.apache.gora.examples.generated.WebPage) Employee(org.apache.gora.examples.generated.Employee) Utf8(org.apache.avro.util.Utf8)

Example 20 with Employee

use of org.apache.gora.examples.generated.Employee in project gora by apache.

the class DataStoreTestUtil method testUpdateEmployee.

/**
   * Here we create 5 {@link org.apache.gora.examples.generated.Employee} objects
   * before populating fields with data and flushing them to the datastore.
   * We then update the 1st of the {@link org.apache.gora.examples.generated.Employee}'s
   * with more data and flush this data. Assertions are then made over the updated
   * {@link org.apache.gora.examples.generated.Employee} object.
   * @param dataStore
   * @throws IOException
   * @throws Exception
   */
public static void testUpdateEmployee(DataStore<String, Employee> dataStore) throws Exception {
    dataStore.createSchema();
    long ssn = 1234567890L;
    long now = System.currentTimeMillis();
    for (int i = 0; i < 5; i++) {
        Employee employee = Employee.newBuilder().build();
        employee.setName(new Utf8("John Doe " + i));
        employee.setDateOfBirth(now - 20L * YEAR_IN_MS);
        employee.setSalary(100000);
        employee.setSsn(new Utf8(Long.toString(ssn + i)));
        dataStore.put(employee.getSsn().toString(), employee);
    }
    dataStore.flush();
    for (int i = 0; i < 1; i++) {
        Employee employee = Employee.newBuilder().build();
        employee.setName(new Utf8("John Doe " + (i + 5)));
        employee.setDateOfBirth(now - 18L * YEAR_IN_MS);
        employee.setSalary(120000);
        employee.setSsn(new Utf8(Long.toString(ssn + i)));
        dataStore.put(employee.getSsn().toString(), employee);
    }
    dataStore.flush();
    for (int i = 0; i < 1; i++) {
        String key = Long.toString(ssn + i);
        Employee employee = dataStore.get(key);
        assertEquals(now - 18L * YEAR_IN_MS, employee.getDateOfBirth().longValue());
        assertEquals("John Doe " + (i + 5), employee.getName().toString());
        assertEquals(120000, employee.getSalary().intValue());
    }
}
Also used : Employee(org.apache.gora.examples.generated.Employee) Utf8(org.apache.avro.util.Utf8)

Aggregations

Employee (org.apache.gora.examples.generated.Employee)23 Utf8 (org.apache.avro.util.Utf8)11 Test (org.junit.Test)7 WebPage (org.apache.gora.examples.generated.WebPage)5 Metadata (org.apache.gora.examples.generated.Metadata)2 Configuration (org.apache.hadoop.conf.Configuration)2 HashMap (java.util.HashMap)1 GoraInfinispanTestDriver (org.apache.gora.infinispan.GoraInfinispanTestDriver)1 Before (org.junit.Before)1