Example #1
0
  @Test
  public void testComponentSpec() {
    em.getTransaction().begin();
    long time = System.currentTimeMillis();
    final Map<String, String> initialViews = Collections.singletonMap("v1", "v1value");
    Tag t1 = new Tag();
    t1.setTagId("tag1");
    Tag t2 = new Tag();
    t2.setTagId("tag2");

    final List<Tag> tags = Arrays.asList(t1, t2);
    for (Tag t : tags) {
      em.persist(t);
    }
    ComponentSpec cs =
        createComponentSpec(
            Long.toString(time), "chris", "cs", "type1", "chris", tags, initialViews);
    em.persist(cs);

    ComponentSpec cs1 =
        createComponentSpec(
            Long.toString(time + 1),
            "chris",
            "cs1",
            "type2",
            "chris",
            Collections.<Tag>emptyList(),
            initialViews);
    em.persist(cs1);
    cs.getReferencedComponents().add(cs1);
    em.getTransaction().commit();
    em.clear();

    cs = em.find(ComponentSpec.class, Long.toString(time));
    checkComponentSpec(cs, "chris", "type1", "chris", "cs", tags, initialViews);
    Assert.assertEquals(cs.getReferencedComponents().size(), 1);
    Assert.assertEquals(
        cs.getReferencedComponents().iterator().next().getComponentId(), Long.toString(time + 1));
    checkComponentSpec(
        cs.getReferencedComponents().iterator().next(),
        "chris",
        "type2",
        "chris",
        "cs1",
        Collections.<Tag>emptyList(),
        initialViews);

    em.getTransaction().begin();
    ComponentSpec cs2 =
        createComponentSpec(
            Long.toString(time + 2),
            "chris",
            "cs2",
            "type3",
            "chris",
            Collections.<Tag>emptyList(),
            Collections.<String, String>emptyMap());
    cs = em.find(ComponentSpec.class, Long.toString(time));
    cs.getReferencedComponents().add(0, cs2);
    em.getTransaction().commit();
    em.clear();

    // check cascade persist and relationship ordering
    Assert.assertNotNull(em.find(ComponentSpec.class, Long.toString(time + 2)));
    cs = em.find(ComponentSpec.class, Long.toString(time));
    Assert.assertEquals(cs.getReferencedComponents().size(), 2);
    Iterator<ComponentSpec> it = cs.getReferencedComponents().iterator();
    Assert.assertEquals(it.next().getComponentId(), Long.toString(time + 2));
    Assert.assertEquals(it.next().getComponentId(), Long.toString(time + 1));
  }