@Test public void testCloneAUniquePageTemplate() { final AbstractCMSComponentModel compOne = new SimpleCMSComponentModel(); final AbstractCMSComponentModel compTwo = new SimpleCMSComponentModel(); final ContentSlotModel slot = new ContentSlotModel(); slot.setCmsComponents(Arrays.asList(compOne, compTwo)); slot.setName("slotName"); slot.setUid("slotUid"); final ContentSlotNameModel slotName = prepareContentSlotName("name1", null); final ContentSlotForTemplateModel contentSlotForTemplate = new ContentSlotForTemplateModel(); contentSlotForTemplate.setUid("cs4Template"); contentSlotForTemplate.setContentSlot(slot); final CatalogVersionModel templateCatalogVersion = prepareCatalogVersion("sourceCatalogVersion", "catalog_id"); final PageTemplateModel oneTemplate = preparePageTemplate("uid1", "name1", templateCatalogVersion); BDDMockito.given(oneTemplate.getContentSlots()) .willReturn(Arrays.asList(contentSlotForTemplate)); BDDMockito.given(oneTemplate.getAvailableContentSlots()).willReturn(Arrays.asList(slotName)); final List<PageTemplateModel> sourceTemplates = Arrays.asList(oneTemplate); final List<PageTemplateModel> result = CMSSiteUtils.copyPageTemplatesDeepImpl( sourceTemplates, targetVersion, contentCatalog, modelService, randomProvider); Assert.assertEquals(result.size(), sourceTemplates.size()); Mockito.verify(pageTemplateClone).setUid("uid1"); Mockito.verify(pageTemplateClone).setName("name1 root_content_catalog"); Mockito.verify(pageTemplateClone).setCatalogVersion(targetVersion); // Mockito.verify(pageTemplateClone).setAvailableContentSlots(Mockito.argThat(matcher)); // assure called Mockito.verify(slotTemplateClone).setUid("cs4Template catalog_id sourceCatalogVersion"); Mockito.verify(slotTemplateClone).setCatalogVersion(targetVersion); Mockito.verify(slotTemplateClone).setPageTemplate(pageTemplateClone); Mockito.verify(contentSlotClone).setUid("uid1 slotUid catalog_id sourceCatalogVersion"); Mockito.verify(contentSlotClone).setCatalogVersion(targetVersion); Mockito.verify(componentClone).setUid("AbstractCMSComponent_comp_0"); Mockito.verify(componentClone).setUid("AbstractCMSComponent_comp_1"); Mockito.verify(componentClone, Mockito.times(2)) .setSlots(java.util.Collections.<ContentSlotModel>emptyList()); Mockito.verify(componentClone, Mockito.times(2)).setCatalogVersion(targetVersion); }
/** * internals for {@link #copyPageTemplatesDeep(List, CatalogVersionModel, ContentCatalogModel)} * separated from platform so could be tested separately somehow */ protected static List<PageTemplateModel> copyPageTemplatesDeepImpl( final List<PageTemplateModel> sourceTemplates, final CatalogVersionModel catVersion, final ContentCatalogModel contentCatalog, final ModelService modelService, final RandomProducer random) { final List<PageTemplateModel> clonedTemplates = new ArrayList<PageTemplateModel>(); for (final PageTemplateModel template : sourceTemplates) { final String catalogId = template.getCatalogVersion().getCatalog().getId(); final String catalogVersionId = template.getCatalogVersion().getVersion(); final PageTemplateModel clonedPageTemplate = modelService.clone(template); clonedPageTemplate.setUid(template.getUid()); clonedPageTemplate.setName(getUidWithSuffix(template.getName(), contentCatalog.getName())); clonedPageTemplate.setCatalogVersion(catVersion); final List<ContentSlotNameModel> slotsNameList = new ArrayList<ContentSlotNameModel>(16); for (final ContentSlotNameModel contentSlotNameModel : template.getAvailableContentSlots()) // possible unique problem { final ContentSlotNameModel clonedSlotNameModel = modelService.create(ContentSlotNameModel.class); clonedSlotNameModel.setName(contentSlotNameModel.getName()); clonedSlotNameModel.setTemplate(clonedPageTemplate); slotsNameList.add(clonedSlotNameModel); } clonedPageTemplate.setAvailableContentSlots(slotsNameList); for (final ContentSlotForTemplateModel contentSlotForTemplate : template.getContentSlots()) { final ContentSlotForTemplateModel clonedContentSlotForTemplate = modelService.clone(contentSlotForTemplate); clonedContentSlotForTemplate.setCatalogVersion(catVersion); clonedContentSlotForTemplate.setPageTemplate(clonedPageTemplate); clonedContentSlotForTemplate.setUid( getUidWithSuffix(contentSlotForTemplate.getUid(), catalogId, catalogVersionId)); final ContentSlotModel contentSlotModel = contentSlotForTemplate.getContentSlot(); final ContentSlotModel clonedContentSlotModel = modelService.clone(contentSlotModel); clonedContentSlotModel.setUid( getUidWithSuffix( template.getUid(), contentSlotModel.getUid(), catalogId, catalogVersionId)); clonedContentSlotModel.setCatalogVersion(catVersion); final List<AbstractCMSComponentModel> clonedComponents = new ArrayList<AbstractCMSComponentModel>(16); for (final AbstractCMSComponentModel component : contentSlotModel.getCmsComponents()) { final AbstractCMSComponentModel clonedComponent = modelService.clone(component); clonedComponent.setUid( random.getSequenceUidValue(AbstractCMSComponentModel._TYPECODE, CMSITEM_UID_PREFIX)); clonedComponent.setSlots(Collections.<ContentSlotModel>emptyList()); clonedComponent.setCatalogVersion(catVersion); clonedComponents.add(clonedComponent); } clonedContentSlotModel.setCmsComponents(clonedComponents); clonedContentSlotForTemplate.setContentSlot(clonedContentSlotModel); modelService.save(clonedContentSlotForTemplate); } modelService.save(clonedPageTemplate); clonedTemplates.add(clonedPageTemplate); } return clonedTemplates; }