@Test
  public void testUpdateContent() {
    final String ownerId = "owner";
    final String productId = "productId";
    final String contentId = "10";

    Owner owner = mock(Owner.class);
    Product product = mock(Product.class);
    Content content = mock(Content.class);

    when(product.getId()).thenReturn(productId);
    when(product.getOwner()).thenReturn(owner);
    when(content.getId()).thenReturn(contentId);
    when(content.getOwner()).thenReturn(owner);

    when(oc.lookupByKey(eq(ownerId))).thenReturn(owner);
    when(cc.lookupById(eq(owner), eq(contentId))).thenReturn(content);
    when(cc.createOrUpdate(eq(content))).thenReturn(content);
    when(productCurator.getProductsWithContent(eq(owner), eq(Arrays.asList(contentId))))
        .thenReturn(Arrays.asList(product));

    ocr.updateContent(ownerId, contentId, content);

    verify(cc).lookupById(eq(owner), eq(contentId));
    verify(cc).createOrUpdate(eq(content));
    verify(productCurator).getProductsWithContent(eq(owner), eq(Arrays.asList(contentId)));
    verify(poolManager).regenerateCertificatesOf(eq(owner), eq(productId), eq(true));
  }
  @Test
  @Parameters(method = "largeValueSetSizes")
  public void testBulkSQLUpdateWithLargeValueSets(int count, int skip) {
    Owner owner = this.createOwner();

    for (int i = 1; i <= count; ++i) {
      this.createContent("c" + i, "content-" + i, owner);
    }

    Map<Object, Object> values = new LinkedHashMap<Object, Object>();

    for (int i = 1; i <= count; ++i) {
      // We want every odd value to be unaffected, but we still want a fake update entry
      // for the query
      values.put("content-" + (i % 2 == skip ? i : "X" + i), "update-" + i);
    }

    int result = this.testContentCurator.bulkSQLUpdate(Content.DB_TABLE, "name", values, null);
    assertEquals(count, result);

    testContentCurator.clear();

    for (int i = 1; i <= count; ++i) {
      Content content = this.ownerContentCurator.getContentById(owner, "c" + i);

      if (i % 2 == skip) {
        assertEquals("update-" + i, content.getName());
      } else {
        assertEquals("content-" + i, content.getName());
      }
    }
  }
Beispiel #3
0
  private void mockContentImport(Owner owner, Content... contents) {
    Map<String, Content> contentMap = new HashMap<String, Content>();

    for (Content content : contents) {
      contentMap.put(content.getId(), content);
    }

    this.mockContentImport(owner, contentMap);
  }
  @Test
  public void create() {
    envContent = envContentCurator.lookupByEnvironmentAndContent(e, c.getId());
    assertNotNull(envContent);

    e = envCurator.find(e.getId());
    assertEquals(1, e.getEnvironmentContent().size());

    assertEquals(1, envContentCurator.lookupByContent(owner, c.getId()).size());
  }
  @Test
  public void createContent() {
    Owner owner = mock(Owner.class);
    Content content = mock(Content.class);

    when(content.getId()).thenReturn("10");
    when(oc.lookupByKey(eq("owner"))).thenReturn(owner);
    when(cc.lookupById(eq(owner), eq("10"))).thenReturn(content);
    when(cc.merge(eq(content))).thenReturn(content);

    assertEquals(content, ocr.createContent("owner", content));
  }
  @Test(expected = NotFoundException.class)
  public void deleteContentNull() {
    Owner owner = mock(Owner.class);
    Content content = mock(Content.class);

    when(content.getId()).thenReturn("10");
    when(oc.lookupByKey(eq("owner"))).thenReturn(owner);
    when(cc.lookupById(eq(owner), eq("10"))).thenReturn(null);

    ocr.remove("owner", "10");
    verify(cc, never()).delete(eq(content));
  }
  @Test
  public void createContentNull() {
    Owner owner = mock(Owner.class);
    Content content = mock(Content.class);

    when(content.getId()).thenReturn("10");
    when(oc.lookupByKey(eq("owner"))).thenReturn(owner);
    when(cc.lookupById(eq(owner), eq("10"))).thenReturn(null);

    ocr.createContent("owner", content);
    verify(cc, atLeastOnce()).create(content);
  }
 private Product createModifyingProduct(String modifiedProductId) {
   Product modifierProd = TestUtil.createProduct();
   String randomString = "" + TestUtil.randomInt();
   Content modContent =
       new Content(randomString, randomString, randomString, "type", "somebody", "", "");
   Set<String> modifiedProdIds = new HashSet<String>();
   modifiedProdIds.add(modifiedProductId);
   modContent.setModifiedProductIds(modifiedProdIds);
   modifierProd.addContent(modContent);
   contentCurator.create(modContent);
   productCurator.create(modifierProd);
   return modifierProd;
 }
  @Test
  public void testSaveOrUpdateProductNoDuplicateProdContent() {
    Product p = createTestProduct();
    Content content =
        new Content(
            this.owner,
            "best-content",
            "best-content",
            "best-content",
            "yum",
            "us",
            "here",
            "here",
            "test-arch");

    p.addContent(content);
    contentCurator.create(content);
    productCurator.createOrUpdate(p);

    // Technically the same product:
    Product p2 = createTestProduct();

    // The content isn't quite the same. We just care about matching
    // product ids with content ids
    Content contentUpdate =
        new Content(
            this.owner,
            "best-content",
            "best-content",
            "best-content",
            "yum",
            "us",
            "here",
            "differnet",
            "test-arch");

    contentUpdate.setUuid(content.getUuid());

    p2.addContent(contentUpdate);
    productCurator.createOrUpdate(p2);

    Product result = productCurator.find(p.getUuid());
    assertEquals(1, result.getProductContent().size());
  }
  @Test
  public void deleteContent() {
    Owner owner = mock(Owner.class);
    Content content = mock(Content.class);

    when(content.getId()).thenReturn("10");
    when(content.getOwner()).thenReturn(owner);
    when(oc.lookupByKey(eq("owner"))).thenReturn(owner);
    when(cc.lookupById(eq(owner), eq("10"))).thenReturn(content);

    EnvironmentContent ec = new EnvironmentContent(mock(Environment.class), content, true);
    List<EnvironmentContent> envContents = Arrays.asList(ec);
    when(envContentCurator.lookupByContent(owner, content.getId())).thenReturn(envContents);

    ocr.remove("owner", "10");

    verify(cc, atLeastOnce()).delete(eq(content));
    verify(envContentCurator, atLeastOnce()).delete(eq(ec));
  }
  @Test
  public void testBulkSQLUpdate() throws Exception {
    Owner owner = this.createOwner();

    Content c1 = this.createContent("c1", "content 1", owner);
    Content c2 = this.createContent("c2", "content 2", owner);
    Content c3 = this.createContent("c3", "content 3", owner);

    Map<Object, Object> values = new HashMap<Object, Object>();
    values.put("content 1", "update 1");
    values.put("content 2", "update 2");
    values.put("content ?", "should not exist");

    int result = this.testContentCurator.bulkSQLUpdate(Content.DB_TABLE, "name", values, null);

    // Note:
    // This looks like it should be 2, and technically that's what's happening here, but with
    // the way the bulk updater works, even the non-matching columns are getting updated to
    // themselves.
    assertEquals(3, result);

    testContentCurator.refresh(c1, c2, c3);

    assertEquals("update 1", c1.getName());
    assertEquals("update 2", c2.getName());
    assertEquals("content 3", c3.getName());
  }
  @Test
  public void testBulkSQLUpdateWithMultipleCriteria() {
    Owner owner = this.createOwner();

    Content c1 = this.createContent("c1", "content 1", owner);
    Content c2 = this.createContent("c2", "content 2", owner);
    Content c3 = this.createContent("c3", "content 3", owner);

    Map<Object, Object> values = new HashMap<Object, Object>();
    values.put("content 1", "update 1");
    values.put("content 2", "update 2");
    values.put("content ?", "should not exist");

    Map<String, Object> criteria = new HashMap<String, Object>();
    criteria.put("name", values.keySet());
    criteria.put("content_id", "c2");

    int result = this.testContentCurator.bulkSQLUpdate(Content.DB_TABLE, "name", values, criteria);

    // Unlike the base test where the result count is 3, this filters by only the values we
    // intend to update, so it should be 1.
    assertEquals(1, result);

    testContentCurator.refresh(c1, c2, c3);

    assertEquals("content 1", c1.getName());
    assertEquals("update 2", c2.getName());
    assertEquals("content 3", c3.getName());
  }
  @Test
  @Parameters(method = "largeValueSetAndCriteriaSizes")
  public void testBulkSQLUpdateWithLargeValueSetAndCriteriaList(
      int valueCount, int criteriaListSize) {
    Owner owner = this.createOwner();

    Map<Object, Object> values = new HashMap<Object, Object>();

    for (int i = 1; i <= valueCount; ++i) {
      this.createContent("c" + i, "content-" + i, owner);

      // We want every odd value to be unaffected, but we still want a fake update entry
      // for the query
      values.put("content-" + (i % 2 == 0 ? i : "X" + i), "update-" + i);
    }

    Map<String, Object> criteria = new HashMap<String, Object>();
    List<String> valueList = new LinkedList<String>();
    criteria.put("name", valueList);

    for (int i = 1; i <= criteriaListSize; ++i) {
      valueList.add("content-" + (i % 2 == 0 ? i : "X" + i));
    }

    int result = this.testContentCurator.bulkSQLUpdate(Content.DB_TABLE, "name", values, criteria);
    assertEquals(valueCount / 2, result);

    testContentCurator.clear();

    for (int i = 1; i <= valueCount; ++i) {
      Content content = this.ownerContentCurator.getContentById(owner, "c" + i);

      if (i % 2 == 0) {
        assertEquals("update-" + i, content.getName());
      } else {
        assertEquals("content-" + i, content.getName());
      }
    }
  }
  @Test
  public void testGetProductIdFromContentUuid() {
    Product p = createTestProduct();
    Content content =
        new Content(
            this.owner,
            "best-content",
            "best-content",
            "best-content",
            "yum",
            "us",
            "here",
            "here",
            "test-arch");
    p.addContent(content);
    contentCurator.create(content);
    productCurator.create(p);

    List<String> contentUuids = new LinkedList<String>();
    contentUuids.add(content.getUuid());
    List<Product> products = productCurator.getProductsWithContent(contentUuids);
    assertEquals(1, products.size());
    assertEquals(p, products.get(0));
  }
  @Test
  public void testBulkSQLUpdateWithEmptyValues() throws Exception {
    Owner owner = this.createOwner();

    Content c1 = this.createContent("c1", "content 1", owner);
    Content c2 = this.createContent("c2", "content 2", owner);
    Content c3 = this.createContent("c3", "content 3", owner);

    Map<Object, Object> values = new HashMap<Object, Object>();

    int result = this.testContentCurator.bulkSQLUpdate(Content.DB_TABLE, "name", values, null);

    assertEquals(0, result);

    testContentCurator.refresh(c1, c2, c3);

    assertEquals("content 1", c1.getName());
    assertEquals("content 2", c2.getName());
    assertEquals("content 3", c3.getName());
  }