@Test public void testUpdateProduct() { Product original = createTestProduct(); productCurator.create(original); // Will have same ID, but we'll modify other data: Product modified = createTestProduct(); String newName = "new name"; modified.setName(newName); // Hack up the attributes, keep a1, skip a2, modify a3, add a4: Set<ProductAttribute> newAttributes = new HashSet<ProductAttribute>(); newAttributes.add(modified.getAttribute("a1")); ProductAttribute a3 = modified.getAttribute("a3"); a3.setValue("a3-modified"); a3.setProduct(modified); newAttributes.add(a3); ProductAttribute a4 = new ProductAttribute("a4", "a4"); a4.setProduct(modified); newAttributes.add(a4); modified.setAttributes(newAttributes); int initialAttrCount = attributeCurator.listAll().size(); productCurator.createOrUpdate(modified); Product lookedUp = productCurator.lookupById(owner, original.getId()); assertEquals(newName, lookedUp.getName()); assertEquals(3, lookedUp.getAttributes().size()); assertEquals("a1", lookedUp.getAttributeValue("a1")); assertEquals("a3-modified", lookedUp.getAttributeValue("a3")); assertEquals("a4", lookedUp.getAttributeValue("a4")); // TODO: test content merging // Old attributes should get cleaned up: assertEquals(initialAttrCount, attributeCurator.listAll().size()); }
@Test public void testJsonListOfHashes() throws Exception { List<Map<String, String>> data = new LinkedList<Map<String, String>>(); Map<String, String> contentSet1 = new HashMap<String, String>(); contentSet1.put("name", "cs1"); contentSet1.put("url", "url"); Map<String, String> contentSet2 = new HashMap<String, String>(); contentSet2.put("name", "cs2"); contentSet2.put("url", "url2"); data.add(contentSet1); data.add(contentSet2); ObjectMapper mapper = new ObjectMapper(); String jsonData = mapper.writeValueAsString(data); Product prod = new Product("cptest-label", "My Product", owner); ProductAttribute a = new ProductAttribute("content_sets", jsonData); prod.addAttribute(a); productCurator.create(prod); attributeCurator.create(a); Product lookedUp = productCurator.find(prod.getUuid()); assertEquals(jsonData, lookedUp.getAttribute("content_sets").getValue()); data = mapper.readValue( lookedUp.getAttribute("content_sets").getValue(), new TypeReference<List<Map<String, String>>>() {}); Map<String, String> cs1 = data.get(0); assertEquals("cs1", cs1.get("name")); Map<String, String> cs2 = data.get(1); assertEquals("cs2", cs2.get("name")); }
@Test public void testWithSimpleJsonAttribute() throws Exception { Map<String, String> data = new HashMap<String, String>(); data.put("a", "1"); data.put("b", "2"); ObjectMapper mapper = new ObjectMapper(); String jsonData = mapper.writeValueAsString(data); Product prod = new Product("cptest-label", "My Product", owner); ProductAttribute a = new ProductAttribute("content_sets", jsonData); prod.addAttribute(a); productCurator.create(prod); attributeCurator.create(a); Product lookedUp = productCurator.find(prod.getUuid()); assertEquals(jsonData, lookedUp.getAttribute("content_sets").getValue()); data = mapper.readValue( lookedUp.getAttribute("content_sets").getValue(), new TypeReference<Map<String, String>>() {}); assertEquals("1", data.get("a")); assertEquals("2", data.get("b")); }