@Test
  public void testMyElement() throws Exception {

    IMetaStore metaStore = new MemoryMetaStore();

    // List of named elements...
    //
    List<MyNameElement> nameList = new ArrayList<MyNameElement>();
    for (int i = 0; i < NR_NAME; i++) {
      nameList.add(new MyNameElement("name" + i, "description" + i, "color" + i));
    }
    List<MyFilenameElement> filenameList = new ArrayList<MyFilenameElement>();
    for (int i = 0; i < NR_FILENAME; i++) {
      filenameList.add(new MyFilenameElement("filename" + i, "size" + i, "gender" + i));
    }

    // Construct our test element...
    //
    MyElement me = new MyElement(NAME, ATTR, ANOTHER, PASSWORD, INT, LONG, BOOL, DATE);
    for (int i = 0; i < NR_ATTR; i++) {
      me.getSubAttributes().add(new MyElementAttr("key" + i, "value" + i, "desc" + i));
    }
    me.setNameElement(nameList.get(NR_NAME - 1));
    me.setFilenameElement(filenameList.get(NR_FILENAME - 1));
    List<String> stringList = Arrays.asList("a", "b", "c", "d");
    me.setStringList(stringList);
    MyOtherElement myOtherElement = new MyOtherElement("other", "other attribute");
    me.setMyOtherElement(myOtherElement);

    MetaStoreFactory<MyOtherElement> otherFactory =
        new MetaStoreFactory<MyOtherElement>(MyOtherElement.class, metaStore, "custom");
    MetaStoreFactory<MyElement> factory =
        new MetaStoreFactory<MyElement>(MyElement.class, metaStore, "custom");

    // For loading, specify the name, filename lists or factory that we're referencing...
    //
    factory.addNameList(MyElement.LIST_KEY_MY_NAMES, nameList);
    factory.addFilenameList(MyElement.LIST_KEY_MY_FILENAMES, filenameList);
    factory.addNameFactory(MyElement.FACTORY_OTHER_ELEMENT, otherFactory);

    // Store the class in the meta store
    //
    factory.saveElement(me);

    // Load the class from the meta store
    //
    MyElement verify = factory.loadElement(NAME);

    // Verify list element details...
    //
    IMetaStoreElement element =
        metaStore.getElementByName("custom", factory.getElementType(), NAME);
    assertNotNull(element);

    // Verify the general idea
    //
    assertNotNull(verify);
    assertEquals(ATTR, verify.getMyAttribute());
    assertEquals(ANOTHER, verify.getAnotherAttribute());
    assertEquals(PASSWORD, verify.getPasswordAttribute());
    assertEquals(INT, verify.getIntAttribute());
    assertEquals(LONG, verify.getLongAttribute());
    assertEquals(BOOL, verify.isBoolAttribute());
    assertEquals(DATE, verify.getDateAttribute());
    assertEquals(me.getSubAttributes().size(), verify.getSubAttributes().size());
    assertEquals(me.getNameElement(), verify.getNameElement());
    assertEquals(me.getFilenameElement(), verify.getFilenameElement());

    // verify the details...
    //
    assertTrue(metaStore.namespaceExists("custom"));
    IMetaStoreElementType elementType = factory.getElementType();
    assertNotNull(elementType);
    assertEquals("My element type", elementType.getName());
    assertEquals("This is my element type", elementType.getDescription());

    assertNotNull(element);
    IMetaStoreAttribute child = element.getChild("my_attribute");
    assertNotNull(child);
    assertEquals(ATTR, MetaStoreUtil.getAttributeString(child));
    child = element.getChild("passwordAttribute");
    assertNotNull(child);
    assertNotSame(
        "Password needs to be encoded", PASSWORD, MetaStoreUtil.getAttributeString(child));

    child = element.getChild("anotherAttribute");
    assertNotNull(child);
    assertEquals(ANOTHER, MetaStoreUtil.getAttributeString(child));

    // Verify the child attributes as well...
    // This also verifies that the attributes are in the right order.
    // The list can't be re-ordered after loading.
    //
    for (int i = 0; i < NR_ATTR; i++) {
      MyElementAttr attr = verify.getSubAttributes().get(i);
      assertEquals("key" + i, attr.getKey());
      assertEquals("value" + i, attr.getValue());
      assertEquals("desc" + i, attr.getDescription());
    }

    // Verify the referenced MyOtherElement
    //
    MyOtherElement verifyOtherElement = verify.getMyOtherElement();
    assertNotNull(verifyOtherElement);
    assertEquals(myOtherElement.getName(), verifyOtherElement.getName());
    assertEquals(myOtherElement.getSomeAttribute(), verifyOtherElement.getSomeAttribute());

    // verify that the String list is loaded...
    List<String> verifyList = verify.getStringList();
    assertEquals(stringList.size(), verifyList.size());
    for (int i = 0; i < stringList.size(); i++) {
      assertEquals(stringList.get(i), verifyList.get(i));
    }

    List<String> names = factory.getElementNames();
    assertEquals(1, names.size());
    assertEquals(NAME, names.get(0));

    List<MyElement> list = factory.getElements();
    assertEquals(1, list.size());
    assertEquals(NAME, list.get(0).getName());

    factory.deleteElement(NAME);
    assertEquals(0, factory.getElementNames().size());
    assertEquals(0, factory.getElements().size());
  }
  @Test
  public void testIDMigration() throws Exception {

    String namespace = "custom";
    String stepName = "Step Name";
    String elementName = "migration";
    String hostName = "Host Name";
    String fieldMappings = "Field Mappings";
    String sourceFieldName = "Source Field Name";
    String targetFieldName = "Target Field Name";
    String parameterName = "Parameter Name";

    IMetaStore metaStore = new MemoryMetaStore();

    MetaStoreFactory<MyMigrationElement> factory =
        new MetaStoreFactory<MyMigrationElement>(MyMigrationElement.class, metaStore, namespace);

    if (!metaStore.namespaceExists(namespace)) {
      metaStore.createNamespace(namespace);
    }

    MetaStoreElementType elementTypeAnnotation =
        MyMigrationElement.class.getAnnotation(MetaStoreElementType.class);

    // Make sure the element type exists...
    IMetaStoreElementType elementType =
        metaStore.getElementTypeByName(namespace, elementTypeAnnotation.name());
    if (elementType == null) {
      elementType = metaStore.newElementType(namespace);
      elementType.setName(elementTypeAnnotation.name());
      elementType.setDescription(elementTypeAnnotation.description());
      metaStore.createElementType(namespace, elementType);
    }

    // Create an element with the old keys we want to migrate
    IMetaStoreElement element = metaStore.newElement();
    element.setName(elementName);
    element.setElementType(elementType);

    element.addChild(metaStore.newAttribute("stepName", stepName));
    element.addChild(metaStore.newAttribute("hostname", hostName));
    element.addChild(metaStore.newAttribute("fieldMappings", fieldMappings));
    element.addChild(metaStore.newAttribute("sourceFieldName", sourceFieldName));
    element.addChild(metaStore.newAttribute("targetFieldName", targetFieldName));
    element.addChild(metaStore.newAttribute("parameterName", parameterName));

    metaStore.createElement(namespace, elementType, element);

    MyMigrationElement loadedElement = factory.loadElement(elementName);

    assertNotNull(loadedElement);
    assertEquals(loadedElement.getStepName(), stepName);
    assertEquals(loadedElement.getHostname(), hostName);
    assertEquals(loadedElement.getFieldMappings(), fieldMappings);
    assertEquals(loadedElement.getSourceFieldName(), sourceFieldName);
    assertEquals(loadedElement.getTargetFieldName(), targetFieldName);
    assertEquals(loadedElement.getParameterName(), parameterName);

    // Test the variation of the step name id
    element = metaStore.newElement();
    element.setName(elementName);
    element.setElementType(elementType);

    element.addChild(metaStore.newAttribute("stepname", stepName));

    metaStore.createElement(namespace, elementType, element);

    loadedElement = factory.loadElement(elementName);

    assertNotNull(loadedElement);
    assertEquals(loadedElement.getStepname(), stepName);
  }
示例#3
0
 /**
  * Create the specified namespace if it doesn't exist.
  *
  * @param metaStore
  * @param namespace
  * @throws MetaStoreException
  */
 public static void verifyNamespaceCreated(IMetaStore metaStore, String namespace)
     throws MetaStoreException {
   if (!metaStore.namespaceExists(namespace)) {
     metaStore.createNamespace(namespace);
   }
 }