@Test public final void shouldUpdateItemIfExists() { MenuItemDefinition menuItemDefinition = MenuItemDefinition.create(PLUGIN_IDENTIFIER, "itemName", "categoryName", ROLE_VISIBLE, true).forView(PLUGIN_IDENTIFIER, "viewName"); Entity itemEntity = mock(Entity.class); given(menuCrudService.getItem(menuItemDefinition)).willReturn(itemEntity); Entity categoryEntity = mockCategory(PLUGIN_IDENTIFIER, "categoryName", ROLE_VISIBLE, Collections.<Entity> emptyList()); given(menuCrudService.getCategory(menuItemDefinition)).willReturn(categoryEntity); Entity viewEntity = mockViewEntity(PLUGIN_IDENTIFIER, "viewName"); given(menuCrudService.getView(menuItemDefinition)).willReturn(viewEntity); menuService.createItem(menuItemDefinition); verify(menuCrudService).save(entityCaptor.capture()); Entity savedItem = entityCaptor.getValue(); verify(savedItem).setField(MenuItemFields.VIEW, viewEntity); verify(savedItem).setField(MenuItemFields.CATEGORY, categoryEntity); verify(savedItem).setField(MenuItemFields.SUCCESSION, 1); verify(savedItem).setField(MenuItemFields.AUTH_ROLE, ROLE_VISIBLE); }
@Override @Transactional public void createItem(final MenuItemDefinition itemDefinition) { Entity menuItem = menuCrudService.getItem(itemDefinition); Entity menuCategory = menuCrudService.getCategory(itemDefinition); Entity menuView = menuCrudService.getView(itemDefinition); Preconditions.checkState( menuCategory != null, String.format("Cannot find menu category %s for item %s.%s", itemDefinition.getCategoryName(), itemDefinition.getPluginIdentifier(), itemDefinition.getName())); Preconditions.checkState(menuView != null, String.format("Cannot find menu view %s.%s for item %s.%s", itemDefinition.getViewPluginIdentifier(), itemDefinition.getViewName(), itemDefinition.getPluginIdentifier(), itemDefinition.getName())); if (menuItem == null) { menuItem = menuCrudService.createEntity(QcadooViewConstants.MODEL_ITEM); menuItem.setField(MenuItemFields.PLUGIN_IDENTIFIER, itemDefinition.getPluginIdentifier()); menuItem.setField(MenuItemFields.NAME, itemDefinition.getName()); menuItem.setField(MenuItemFields.ACTIVE, itemDefinition.isActive()); } if (menuItem == null || !menuView.equals(menuItem.getField(MenuItemFields.VIEW))) { menuItem.setField(MenuItemFields.VIEW, menuView); menuItem.setField(MenuItemFields.CATEGORY, menuCategory); menuItem.setField(MenuItemFields.SUCCESSION, menuCategory.getHasManyField(MenuCategoryFields.ITEMS).size() + 1); menuItem.setField(MenuItemFields.AUTH_ROLE, itemDefinition.getAuthRoleIdentifier()); menuCrudService.save(menuItem); } }
MenuServiceImpl implements InternalMenuService { @Override @Transactional public void createItem(final MenuItemDefinition itemDefinition) { Entity menuItem = menuCrudService.getItem(itemDefinition); Entity menuCategory = menuCrudService.getCategory(itemDefinition); Entity menuView = menuCrudService.getView(itemDefinition); Preconditions.checkState( menuCategory != null, String.format("Cannot find menu category %s for item %s.%s", itemDefinition.getCategoryName(), itemDefinition.getPluginIdentifier(), itemDefinition.getName())); Preconditions.checkState(menuView != null, String.format("Cannot find menu view %s.%s for item %s.%s", itemDefinition.getViewPluginIdentifier(), itemDefinition.getViewName(), itemDefinition.getPluginIdentifier(), itemDefinition.getName())); if (menuItem == null) { menuItem = menuCrudService.createEntity(QcadooViewConstants.MODEL_ITEM); menuItem.setField(MenuItemFields.PLUGIN_IDENTIFIER, itemDefinition.getPluginIdentifier()); menuItem.setField(MenuItemFields.NAME, itemDefinition.getName()); menuItem.setField(MenuItemFields.ACTIVE, itemDefinition.isActive()); } if (menuItem == null || !menuView.equals(menuItem.getField(MenuItemFields.VIEW))) { menuItem.setField(MenuItemFields.VIEW, menuView); menuItem.setField(MenuItemFields.CATEGORY, menuCategory); menuItem.setField(MenuItemFields.SUCCESSION, menuCategory.getHasManyField(MenuCategoryFields.ITEMS).size() + 1); menuItem.setField(MenuItemFields.AUTH_ROLE, itemDefinition.getAuthRoleIdentifier()); menuCrudService.save(menuItem); } } }
MenuServiceImpl implements InternalMenuService { @Override @Transactional public void createItem(final MenuItemDefinition itemDefinition) { Entity menuItem = menuCrudService.getItem(itemDefinition); Entity menuCategory = menuCrudService.getCategory(itemDefinition); Entity menuView = menuCrudService.getView(itemDefinition); Preconditions.checkState( menuCategory != null, String.format("Cannot find menu category %s for item %s.%s", itemDefinition.getCategoryName(), itemDefinition.getPluginIdentifier(), itemDefinition.getName())); Preconditions.checkState(menuView != null, String.format("Cannot find menu view %s.%s for item %s.%s", itemDefinition.getViewPluginIdentifier(), itemDefinition.getViewName(), itemDefinition.getPluginIdentifier(), itemDefinition.getName())); if (menuItem == null) { menuItem = menuCrudService.createEntity(QcadooViewConstants.MODEL_ITEM); menuItem.setField(MenuItemFields.PLUGIN_IDENTIFIER, itemDefinition.getPluginIdentifier()); menuItem.setField(MenuItemFields.NAME, itemDefinition.getName()); menuItem.setField(MenuItemFields.ACTIVE, itemDefinition.isActive()); } if (menuItem == null || !menuView.equals(menuItem.getField(MenuItemFields.VIEW))) { menuItem.setField(MenuItemFields.VIEW, menuView); menuItem.setField(MenuItemFields.CATEGORY, menuCategory); menuItem.setField(MenuItemFields.SUCCESSION, menuCategory.getHasManyField(MenuCategoryFields.ITEMS).size() + 1); menuItem.setField(MenuItemFields.AUTH_ROLE, itemDefinition.getAuthRoleIdentifier()); menuCrudService.save(menuItem); } }  }
MenuServiceImpl implements InternalMenuService { @Override @Transactional public void createItem(final MenuItemDefinition itemDefinition) { Entity menuItem = menuCrudService.getItem(itemDefinition); Entity menuCategory = menuCrudService.getCategory(itemDefinition); Entity menuView = menuCrudService.getView(itemDefinition); Preconditions.checkState( menuCategory != null, String.format("Cannot find menu category %s for item %s.%s", itemDefinition.getCategoryName(), itemDefinition.getPluginIdentifier(), itemDefinition.getName())); Preconditions.checkState(menuView != null, String.format("Cannot find menu view %s.%s for item %s.%s", itemDefinition.getViewPluginIdentifier(), itemDefinition.getViewName(), itemDefinition.getPluginIdentifier(), itemDefinition.getName())); if (menuItem == null) { menuItem = menuCrudService.createEntity(QcadooViewConstants.MODEL_ITEM); menuItem.setField(MenuItemFields.PLUGIN_IDENTIFIER, itemDefinition.getPluginIdentifier()); menuItem.setField(MenuItemFields.NAME, itemDefinition.getName()); menuItem.setField(MenuItemFields.ACTIVE, itemDefinition.isActive()); } if (menuItem == null || !menuView.equals(menuItem.getField(MenuItemFields.VIEW))) { menuItem.setField(MenuItemFields.VIEW, menuView); menuItem.setField(MenuItemFields.CATEGORY, menuCategory); menuItem.setField(MenuItemFields.SUCCESSION, menuCategory.getHasManyField(MenuCategoryFields.ITEMS).size() + 1); menuItem.setField(MenuItemFields.AUTH_ROLE, itemDefinition.getAuthRoleIdentifier()); menuCrudService.save(menuItem); } }  @Override @Transactional(readOnly = true) @Monitorable MenuDefinition getMenu(final Locale locale); @Override @Transactional void addView(final MenuItemDefinition itemDefinition); @Override @Transactional void removeView(final MenuItemDefinition itemDefinition); @Override @Transactional void createCategory(final MenuCategoryDefinition menuCategoryDefinition); @Override @Transactional void removeCategory(final MenuCategoryDefinition menuCategoryDefinition); @Override @Transactional void createItem(final MenuItemDefinition itemDefinition); @Override @Transactional void removeItem(final MenuItemDefinition itemDefinition); }
MenuServiceImpl implements InternalMenuService { @Override @Transactional public void createItem(final MenuItemDefinition itemDefinition) { Entity menuItem = menuCrudService.getItem(itemDefinition); Entity menuCategory = menuCrudService.getCategory(itemDefinition); Entity menuView = menuCrudService.getView(itemDefinition); Preconditions.checkState( menuCategory != null, String.format("Cannot find menu category %s for item %s.%s", itemDefinition.getCategoryName(), itemDefinition.getPluginIdentifier(), itemDefinition.getName())); Preconditions.checkState(menuView != null, String.format("Cannot find menu view %s.%s for item %s.%s", itemDefinition.getViewPluginIdentifier(), itemDefinition.getViewName(), itemDefinition.getPluginIdentifier(), itemDefinition.getName())); if (menuItem == null) { menuItem = menuCrudService.createEntity(QcadooViewConstants.MODEL_ITEM); menuItem.setField(MenuItemFields.PLUGIN_IDENTIFIER, itemDefinition.getPluginIdentifier()); menuItem.setField(MenuItemFields.NAME, itemDefinition.getName()); menuItem.setField(MenuItemFields.ACTIVE, itemDefinition.isActive()); } if (menuItem == null || !menuView.equals(menuItem.getField(MenuItemFields.VIEW))) { menuItem.setField(MenuItemFields.VIEW, menuView); menuItem.setField(MenuItemFields.CATEGORY, menuCategory); menuItem.setField(MenuItemFields.SUCCESSION, menuCategory.getHasManyField(MenuCategoryFields.ITEMS).size() + 1); menuItem.setField(MenuItemFields.AUTH_ROLE, itemDefinition.getAuthRoleIdentifier()); menuCrudService.save(menuItem); } }  @Override @Transactional(readOnly = true) @Monitorable MenuDefinition getMenu(final Locale locale); @Override @Transactional void addView(final MenuItemDefinition itemDefinition); @Override @Transactional void removeView(final MenuItemDefinition itemDefinition); @Override @Transactional void createCategory(final MenuCategoryDefinition menuCategoryDefinition); @Override @Transactional void removeCategory(final MenuCategoryDefinition menuCategoryDefinition); @Override @Transactional void createItem(final MenuItemDefinition itemDefinition); @Override @Transactional void removeItem(final MenuItemDefinition itemDefinition);  }