WidgetLayoutResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus createResources(final Request request) throws SystemException, UnsupportedPropertyException, ResourceAlreadyExistsException, NoSuchParentResourceException { Set<Resource> associatedResources = new HashSet<Resource>(); for (final Map<String, Object> properties : request.getProperties()) { WidgetLayoutEntity widgetLayoutEntity = createResources(new Command<WidgetLayoutEntity>() { @Override public WidgetLayoutEntity invoke() throws AmbariException { final String[] requiredProperties = { WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID, WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID, WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID, WIDGETLAYOUT_SCOPE_PROPERTY_ID, WIDGETLAYOUT_WIDGETS_PROPERTY_ID, WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID, WIDGETLAYOUT_USERNAME_PROPERTY_ID }; for (String propertyName : requiredProperties) { if (properties.get(propertyName) == null) { throw new AmbariException("Property " + propertyName + " should be provided"); } } final WidgetLayoutEntity entity = new WidgetLayoutEntity(); String userName = getUserName(properties); Set widgetsSet = (LinkedHashSet) properties.get(WIDGETLAYOUT_WIDGETS_PROPERTY_ID); String clusterName = properties.get(WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID).toString(); entity.setLayoutName(properties.get(WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID).toString()); entity.setClusterId(getManagementController().getClusters().getCluster(clusterName).getClusterId()); entity.setSectionName(properties.get(WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID).toString()); entity.setScope(properties.get(WIDGETLAYOUT_SCOPE_PROPERTY_ID).toString()); entity.setUserName(userName); entity.setDisplayName(properties.get(WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID).toString()); 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.setListWidgetLayoutUserWidgetEntity(widgetLayoutUserWidgetEntityList); widgetLayoutDAO.create(entity); notifyCreate(Type.WidgetLayout, request); return entity; } }); Resource resource = new ResourceImpl(Type.WidgetLayout); resource.setProperty(WIDGETLAYOUT_ID_PROPERTY_ID, widgetLayoutEntity.getId()); associatedResources.add(resource); } return getRequestStatus(null, associatedResources); } 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 testCreateResources() 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)).anyTimes(); Capture<WidgetLayoutEntity> entityCapture = EasyMock.newCapture(); dao.create(capture(entityCapture)); expectLastCall(); WidgetEntity widgetEntity = new WidgetEntity(); widgetEntity.setId(1l); widgetEntity.setListWidgetLayoutUserWidgetEntity(new ArrayList<WidgetLayoutUserWidgetEntity>()); expect(widgetDAO.findById(anyLong())).andReturn(widgetEntity).anyTimes(); replay(amc, clusters, cluster, dao, widgetDAO); WidgetLayoutResourceProvider provider = createProvider(amc); 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); RequestStatus requestStatus = provider.createResources(request); Assert.assertTrue(entityCapture.hasCaptured()); WidgetLayoutEntity entity = entityCapture.getValue(); Assert.assertNotNull(entity); Assert.assertEquals(1, requestStatus.getAssociatedResources().size()); Assert.assertEquals(Long.valueOf(1), entity.getClusterId()); Assert.assertEquals("CLUSTER", entity.getScope()); Assert.assertEquals("layout_name", entity.getLayoutName()); Assert.assertEquals("display_name", entity.getDisplayName()); Assert.assertEquals("section_name", entity.getSectionName()); Assert.assertEquals("admin", entity.getUserName()); Assert.assertNotNull(entity.getListWidgetLayoutUserWidgetEntity()); Assert.assertNotNull(entity.getListWidgetLayoutUserWidgetEntity().get(0)); Assert.assertNotNull(entity.getListWidgetLayoutUserWidgetEntity().get(0). getWidget().getListWidgetLayoutUserWidgetEntity()); verify(amc, clusters, cluster, dao, widgetDAO); }