### Question:
ResourceMonitor extends DynamicMBeanProvider { void updatePartitionWeightStats(Map<String, Integer> partitionWeightMap) { synchronized (_dynamicCapacityMetricsMap) { if (_dynamicCapacityMetricsMap.keySet().equals(partitionWeightMap.keySet())) { for (Map.Entry<String, Integer> entry : partitionWeightMap.entrySet()) { _dynamicCapacityMetricsMap.get(entry.getKey()).updateValue((long) entry.getValue()); } return; } _dynamicCapacityMetricsMap.clear(); for (Map.Entry<String, Integer> entry : partitionWeightMap.entrySet()) { String capacityKey = entry.getKey(); _dynamicCapacityMetricsMap.put(capacityKey, new SimpleDynamicMetric<>(capacityKey + GAUGE_METRIC_SUFFIX, (long) entry.getValue())); } } updateAttributesInfo(buildAttributeList(), "Resource monitor for resource: " + getResourceName()); } @SuppressWarnings("unchecked")  ResourceMonitor(String clusterName, String resourceName, ObjectName objectName); @Override DynamicMBeanProvider register(); @Override String getSensorName(); long getPartitionGauge(); long getErrorPartitionGauge(); long getMissingTopStatePartitionGauge(); long getDifferenceWithIdealStateGauge(); long getSuccessfulTopStateHandoffDurationCounter(); long getSucceededTopStateHandoffCounter(); long getMaxSinglePartitionTopStateHandoffDurationGauge(); HistogramDynamicMetric getPartitionTopStateHandoffDurationGauge(); HistogramDynamicMetric getPartitionTopStateNonGracefulHandoffDurationGauge(); HistogramDynamicMetric getPartitionTopStateHandoffHelixLatencyGauge(); long getFailedTopStateHandoffCounter(); long getTotalMessageReceived(); @Deprecated synchronized void increaseMessageCount(long messageReceived); synchronized void increaseMessageCountWithCounter(long messageReceived); String getResourceName(); String getBeanName(); void updateResourceState(ExternalView externalView, IdealState idealState,
      StateModelDefinition stateModelDef); void updatePendingStateTransitionMessages(int messageCount); void updateStateHandoffStats(MonitorState monitorState, long totalDuration,
      long helixLatency, boolean isGraceful, boolean succeeded); void updateRebalancerStats(long numPendingRecoveryRebalancePartitions,
      long numPendingLoadRebalancePartitions, long numRecoveryRebalanceThrottledPartitions,
      long numLoadRebalanceThrottledPartitions); void setRebalanceState(RebalanceStatus state); long getExternalViewPartitionGauge(); long getMissingMinActiveReplicaPartitionGauge(); long getMissingReplicaPartitionGauge(); long getPendingRecoveryRebalancePartitionGauge(); long getPendingLoadRebalancePartitionGauge(); long getRecoveryRebalanceThrottledPartitionGauge(); long getLoadRebalanceThrottledPartitionGauge(); long getNumPendingStateTransitionGauge(); String getRebalanceState(); void resetMaxTopStateHandoffGauge();  }### Answer:
@Test public void testUpdatePartitionWeightStats() throws JMException, IOException { final MBeanServerConnection mBeanServer = ManagementFactory.getPlatformMBeanServer(); final String clusterName = TestHelper.getTestMethodName(); final String resource = "testDB"; final ObjectName resourceObjectName = new ObjectName("testDomain:key=value"); final ResourceMonitor monitor = new ResourceMonitor(clusterName, resource, resourceObjectName); monitor.register(); try { Map<String, Map<String, Integer>> partitionWeightMap = ImmutableMap.of(resource, ImmutableMap.of("capacity1", 20, "capacity2", 40)); partitionWeightMap.values().forEach(monitor::updatePartitionWeightStats); verifyPartitionWeightMetrics(mBeanServer, resourceObjectName, partitionWeightMap); partitionWeightMap = ImmutableMap.of(resource, ImmutableMap.of("capacity1", 20, "capacity3", 60)); partitionWeightMap.values().forEach(monitor::updatePartitionWeightStats); verifyPartitionWeightMetrics(mBeanServer, resourceObjectName, partitionWeightMap); String removedAttribute = "capacity2Gauge"; try { mBeanServer.getAttribute(resourceObjectName, removedAttribute); Assert.fail("AttributeNotFoundException should be thrown because attribute [capacity2Gauge]" + " is removed."); } catch (AttributeNotFoundException expected) { } } finally { monitor.unregister(); Assert.assertFalse(mBeanServer.isRegistered(resourceObjectName), "Failed to unregister resource monitor."); } }