WidgetLayoutResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus updateResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<Map<String, Object>> propertyMaps = request.getProperties(); modifyResources(new Command<Void>() { @Override public Void invoke() throws AmbariException { for (Map<String, Object> propertyMap : propertyMaps) { final Long layoutId; try { layoutId = Long.parseLong(propertyMap.get(WIDGETLAYOUT_ID_PROPERTY_ID).toString()); } catch (Exception ex) { throw new AmbariException("WidgetLayout should have numerical id"); } lock.writeLock().lock(); try { final WidgetLayoutEntity entity = widgetLayoutDAO.findById(layoutId); if (entity == null) { throw new ObjectNotFoundException("There is no widget layout with id " + layoutId); } if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID)))) { entity.setLayoutName(propertyMap.get(WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID).toString()); } if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID)))) { entity.setSectionName(propertyMap.get(WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID).toString()); } if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID)))) { entity.setDisplayName(propertyMap.get(WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID).toString()); } if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_SCOPE_PROPERTY_ID)))) { entity.setScope(propertyMap.get(WIDGETLAYOUT_SCOPE_PROPERTY_ID).toString()); } Set widgetsSet = (LinkedHashSet) propertyMap.get(WIDGETLAYOUT_WIDGETS_PROPERTY_ID); for (WidgetLayoutUserWidgetEntity widgetLayoutUserWidgetEntity : entity.getListWidgetLayoutUserWidgetEntity()) { widgetLayoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity() .remove(widgetLayoutUserWidgetEntity); } entity.setListWidgetLayoutUserWidgetEntity(new LinkedList<WidgetLayoutUserWidgetEntity>()); List<WidgetLayoutUserWidgetEntity> widgetLayoutUserWidgetEntityList = new LinkedList<WidgetLayoutUserWidgetEntity>(); int order = 0; for (Object widgetObject : widgetsSet) { HashMap<String, Object> widget = (HashMap) widgetObject; long id = Integer.parseInt(widget.get("id").toString()); WidgetEntity widgetEntity = widgetDAO.findById(id); if (widgetEntity == null) { throw new AmbariException("Widget with id " + widget.get("id").toString() + " does not exists"); } WidgetLayoutUserWidgetEntity widgetLayoutUserWidgetEntity = new WidgetLayoutUserWidgetEntity(); widgetLayoutUserWidgetEntity.setWidget(widgetEntity); widgetLayoutUserWidgetEntity.setWidgetOrder(order++); widgetLayoutUserWidgetEntity.setWidgetLayout(entity); widgetLayoutUserWidgetEntityList.add(widgetLayoutUserWidgetEntity); widgetEntity.getListWidgetLayoutUserWidgetEntity().add(widgetLayoutUserWidgetEntity); entity.getListWidgetLayoutUserWidgetEntity().add(widgetLayoutUserWidgetEntity); } widgetLayoutDAO.mergeWithFlush(entity); } finally { lock.writeLock().unlock(); } } return null; } }); return getRequestStatus(null); } WidgetLayoutResourceProvider(AmbariManagementController managementController); @Override RequestStatus createResources(final Request request); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus updateResources(Request request, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate); static final String WIDGETLAYOUT_ID_PROPERTY_ID; static final String WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID; static final String WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID; static final String WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID; static final String WIDGETLAYOUT_SCOPE_PROPERTY_ID; static final String WIDGETLAYOUT_WIDGETS_PROPERTY_ID; static final String WIDGETLAYOUT_USERNAME_PROPERTY_ID; static final String WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID; @SuppressWarnings("serial")
  static Set<String> propertyIds; @SuppressWarnings("serial")
  static Map<Type, String> keyPropertyIds; }
@Test public void testUpdateResources() throws Exception { AmbariManagementController amc = createMock(AmbariManagementController.class); Clusters clusters = createMock(Clusters.class); Cluster cluster = createMock(Cluster.class); expect(amc.getClusters()).andReturn(clusters).atLeastOnce(); expect(clusters.getCluster((String) anyObject())).andReturn(cluster).atLeastOnce(); expect(cluster.getClusterId()).andReturn(Long.valueOf(1)).atLeastOnce(); Capture<WidgetLayoutEntity> entityCapture = EasyMock.newCapture(); dao.create(capture(entityCapture)); expectLastCall(); WidgetEntity widgetEntity = new WidgetEntity(); widgetEntity.setId(1L); widgetEntity.setListWidgetLayoutUserWidgetEntity(new ArrayList<WidgetLayoutUserWidgetEntity>()); WidgetEntity widgetEntity2 = new WidgetEntity(); widgetEntity2.setId(2L); widgetEntity2.setListWidgetLayoutUserWidgetEntity(new ArrayList<WidgetLayoutUserWidgetEntity>()); expect(widgetDAO.findById(1L)).andReturn(widgetEntity).atLeastOnce(); replay(amc, clusters, cluster, dao, widgetDAO); Map<String, Object> requestProps = new HashMap<String, Object>(); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID, "c1"); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID, "layout_name"); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID, "display_name"); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID, "section_name"); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_PROPERTY_ID, "admin"); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_SCOPE_PROPERTY_ID, "CLUSTER"); Set<Map<String, String>> widgetsInfo = new LinkedHashSet<>(); Map<String, String> widget = new HashMap<String, String>(); widget.put("id","1"); widgetsInfo.add(widget); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_WIDGETS_PROPERTY_ID, widgetsInfo); Request request = PropertyHelper.getCreateRequest( Collections.singleton(requestProps), null); WidgetLayoutResourceProvider provider = createProvider(amc); provider.createResources(request); Assert.assertTrue(entityCapture.hasCaptured()); WidgetLayoutEntity entity = entityCapture.getValue(); Assert.assertNotNull(entity); Predicate predicate = new PredicateBuilder().property( WidgetLayoutResourceProvider.WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID).equals("c1") .and().property(WidgetLayoutResourceProvider.WIDGETLAYOUT_ID_PROPERTY_ID).equals("1") .and().property(WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_PROPERTY_ID).equals("username").toPredicate(); entity.setId(Long.valueOf(1)); String oldLayoutName = entity.getLayoutName(); String oldScope = entity.getScope(); resetToStrict(dao, widgetDAO); expect(dao.findById(1L)).andReturn(entity).anyTimes(); expect(dao.mergeWithFlush((WidgetLayoutEntity) anyObject())).andReturn(entity).anyTimes(); expect(widgetDAO.merge(widgetEntity)).andReturn(widgetEntity).anyTimes(); expect(widgetDAO.findById(2L)).andReturn(widgetEntity2).anyTimes(); replay(dao, widgetDAO); requestProps = new HashMap<String, Object>(); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID, "layout_name_new"); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_SCOPE_PROPERTY_ID, "USER"); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_ID_PROPERTY_ID, "1"); widget.put("id","2"); widgetsInfo.add(widget); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_WIDGETS_PROPERTY_ID, widgetsInfo); request = PropertyHelper.getUpdateRequest(requestProps, null); provider.updateResources(request, predicate); Assert.assertFalse(oldLayoutName.equals(entity.getLayoutName())); Assert.assertFalse(oldScope.equals(entity.getScope())); verify(amc, clusters, cluster, dao, widgetDAO); }