MetadataCommandDescriptionProvider implements CommandDescriptionProvider { @Override public @Nullable CommandDescription getCommandDescription(String itemName, @Nullable Locale locale) { Metadata metadata = metadataRegistry.get(new MetadataKey(COMMANDDESCRIPTION_METADATA_NAMESPACE, itemName)); if (metadata != null) { try { CommandDescriptionImpl commandDescription = new CommandDescriptionImpl(); if (metadata.getConfiguration().containsKey("options")) { Stream.of(metadata.getConfiguration().get("options").toString().split(",")).forEach(o -> { if (o.contains("=")) { commandDescription.addCommandOption( new CommandOption(o.split("=")[0].trim(), o.split("=")[1].trim())); } else { commandDescription.addCommandOption(new CommandOption(o.trim(), null)); } }); return commandDescription; } } catch (Exception e) { logger.warn("Unable to parse the commandDescription from metadata for item {}, ignoring it", itemName); return null; } } return null; } @Activate  MetadataCommandDescriptionProvider(final @Reference MetadataRegistry metadataRegistry,
            Map<String, Object> properties); @Override @Nullable CommandDescription getCommandDescription(String itemName, @Nullable Locale locale); static final String COMMANDDESCRIPTION_METADATA_NAMESPACE; }
@Test public void testEmpty() throws Exception { CommandDescription commandDescription = commandDescriptionProvider.getCommandDescription(ITEM_NAME, null); assertNull(commandDescription); }
@Test public void testEmptyConfig() throws Exception { MetadataKey metadataKey = new MetadataKey("commandDescription", ITEM_NAME); Metadata metadata = new Metadata(metadataKey, "N/A", null); metadataRegistry.added(managedProvider, metadata); CommandDescription commandDescription = commandDescriptionProvider.getCommandDescription(ITEM_NAME, null); assertNull(commandDescription); }
@Test public void testOptions() throws Exception { MetadataKey metadataKey = new MetadataKey("commandDescription", ITEM_NAME); Map<String, Object> metadataConfig = new HashMap<>(); metadataConfig.put("options", "OPTION1,OPTION2 , 3 =Option 3 "); Metadata metadata = new Metadata(metadataKey, "N/A", metadataConfig); metadataRegistry.added(managedProvider, metadata); CommandDescription commandDescription = commandDescriptionProvider.getCommandDescription(ITEM_NAME, null); assertNotNull(commandDescription); assertNotNull(commandDescription.getCommandOptions()); assertEquals(3, commandDescription.getCommandOptions().size()); Iterator<CommandOption> it = commandDescription.getCommandOptions().iterator(); CommandOption commandOption = it.next(); assertEquals("OPTION1", commandOption.getCommand()); assertNull(commandOption.getLabel()); commandOption = it.next(); assertEquals("OPTION2", commandOption.getCommand()); assertNull(commandOption.getLabel()); commandOption = it.next(); assertEquals("3", commandOption.getCommand()); assertEquals("Option 3", commandOption.getLabel()); }