MetadataStateDescriptionFragmentProvider implements StateDescriptionFragmentProvider { @Override public @Nullable StateDescriptionFragment getStateDescriptionFragment(String itemName, @Nullable Locale locale) { Metadata metadata = metadataRegistry.get(new MetadataKey(STATEDESCRIPTION_METADATA_NAMESPACE, itemName)); if (metadata != null) { try { StateDescriptionFragmentBuilder builder = StateDescriptionFragmentBuilder.create(); if (metadata.getConfiguration().containsKey("pattern")) { builder.withPattern((String) metadata.getConfiguration().get("pattern")); } if (metadata.getConfiguration().containsKey("min")) { builder.withMinimum(getBigDecimal(metadata.getConfiguration().get("min"))); } if (metadata.getConfiguration().containsKey("max")) { builder.withMaximum(getBigDecimal(metadata.getConfiguration().get("max"))); } if (metadata.getConfiguration().containsKey("step")) { builder.withStep(getBigDecimal(metadata.getConfiguration().get("step"))); } if (metadata.getConfiguration().containsKey("readOnly")) { builder.withReadOnly(getBoolean(metadata.getConfiguration().get("readOnly"))); } if (metadata.getConfiguration().containsKey("options")) { List<StateOption> stateOptions = Stream .of(metadata.getConfiguration().get("options").toString().split(",")).map(o -> { return (o.contains("=")) ? new StateOption(o.split("=")[0].trim(), o.split("=")[1].trim()) : new StateOption(o.trim(), null); }).collect(Collectors.toList()); builder.withOptions(stateOptions); } return builder.build(); } catch (Exception e) { logger.warn("Unable to parse the stateDescription from metadata for item {}, ignoring it", itemName); } } return null; } @Activate  MetadataStateDescriptionFragmentProvider(final @Reference MetadataRegistry metadataRegistry,
            Map<String, Object> properties); @Override @Nullable StateDescriptionFragment getStateDescriptionFragment(String itemName, @Nullable Locale locale); @Override Integer getRank(); static final String STATEDESCRIPTION_METADATA_NAMESPACE; }
@Test public void testEmpty() throws Exception { StateDescriptionFragment stateDescriptionFragment = stateDescriptionFragmentProvider .getStateDescriptionFragment(ITEM_NAME, null); assertNull(stateDescriptionFragment); }
@SuppressWarnings("null") @Test public void testFragment() throws Exception { MetadataKey metadataKey = new MetadataKey("stateDescription", ITEM_NAME); Map<String, Object> metadataConfig = new HashMap<>(); metadataConfig.put("pattern", "%.1f %unit%"); metadataConfig.put("min", 18.5); metadataConfig.put("max", "34"); metadataConfig.put("step", 3); metadataConfig.put("readOnly", "true"); metadataConfig.put("options", "OPTION1,OPTION2 , 3 =Option 3 "); Metadata metadata = new Metadata(metadataKey, "N/A", metadataConfig); metadataRegistry.added(managedProvider, metadata); StateDescriptionFragment stateDescriptionFragment = stateDescriptionFragmentProvider .getStateDescriptionFragment(ITEM_NAME, null); assertNotNull(stateDescriptionFragment); assertEquals("%.1f %unit%", stateDescriptionFragment.getPattern()); assertEquals(new BigDecimal(18.5), stateDescriptionFragment.getMinimum()); assertEquals(new BigDecimal(34), stateDescriptionFragment.getMaximum()); assertEquals(new BigDecimal(3), stateDescriptionFragment.getStep()); assertEquals(true, stateDescriptionFragment.isReadOnly()); assertNotNull(stateDescriptionFragment.getOptions()); Iterator<StateOption> it = stateDescriptionFragment.getOptions().iterator(); StateOption stateOption = it.next(); assertEquals("OPTION1", stateOption.getValue()); assertNull(stateOption.getLabel()); stateOption = it.next(); assertEquals("OPTION2", stateOption.getValue()); assertNull(stateOption.getLabel()); stateOption = it.next(); assertEquals("3", stateOption.getValue()); assertEquals("Option 3", stateOption.getLabel()); }