StateDescriptionServiceImpl implements StateDescriptionService { @Override public @Nullable StateDescription getStateDescription(String itemName, @Nullable Locale locale) { StateDescriptionFragment stateDescriptionFragment = getMergedStateDescriptionFragments(itemName, locale); return stateDescriptionFragment != null ? stateDescriptionFragment.toStateDescription() : null; }  @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) void addStateDescriptionFragmentProvider(StateDescriptionFragmentProvider provider); void removeStateDescriptionFragmentProvider(StateDescriptionFragmentProvider provider); @Override @Nullable StateDescription getStateDescription(String itemName, @Nullable Locale locale);  }
@Test public void testServiceWithOneStateDescriptionProvider() { StateDescriptionFragment stateDescriptionFragment = StateDescriptionFragmentBuilder.create() .withMinimum(STATE_DESCRIPTION_PROVIDER_DEFAULT_MIN_VALUE) .withMaximum(STATE_DESCRIPTION_PROVIDER_DEFAULT_MAX_VALUE) .withStep(STATE_DESCRIPTION_PROVIDER_DEFAULT_STEP) .withPattern(STATE_DESCRIPTION_PROVIDER_DEFAULT_PATTERN) .withReadOnly(STATE_DESCRIPTION_PROVIDER_DEFAULT_IS_READONLY) .withOptions(STATE_DESCRIPTION_PROVIDER_DEFAULT_OPTIONS).build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment, STATE_DESCRIPTION_PROVIDER_DEFAULT_SERVICE_RANKING); StateDescription stateDescription = item.getStateDescription(); assertThat(stateDescription.getMinimum(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_MIN_VALUE)); assertThat(stateDescription.getMaximum(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_MAX_VALUE)); assertThat(stateDescription.getStep(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_STEP)); assertThat(stateDescription.getPattern(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_PATTERN)); assertThat(stateDescription.isReadOnly(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_IS_READONLY)); assertThat(stateDescription.getOptions(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_OPTIONS)); }
@Test public void testMinValueMaxValueStepAndPatternTwoDescriptionProviders() { StateDescriptionFragment stateDescriptionFragment1 = StateDescriptionFragmentBuilder.create() .withMinimum(new BigDecimal(-1)) .withMaximum(new BigDecimal(-1)) .withStep(new BigDecimal(-1)) .withPattern("pattern1").build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment1, -1); StateDescriptionFragment stateDescriptionFragment2 = StateDescriptionFragmentBuilder.create() .withMinimum(new BigDecimal(-2)) .withMaximum(new BigDecimal(-2)) .withStep(new BigDecimal(-2)) .withPattern("pattern2").build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment2, -2); StateDescription stateDescription = item.getStateDescription(); assertThat(stateDescription.getMinimum(), is(stateDescriptionFragment1.getMinimum())); assertThat(stateDescription.getMaximum(), is(stateDescriptionFragment1.getMaximum())); assertThat(stateDescription.getStep(), is(stateDescriptionFragment1.getStep())); assertThat(stateDescription.getPattern(), is(stateDescriptionFragment1.getPattern())); }
@Test public void testIsReadOnlyWhenTwoDescriptionProvidersHigherRankingIsNotReadOnly() { StateDescriptionFragment stateDescriptionFragment1 = StateDescriptionFragmentBuilder.create().build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment1, -1); StateDescriptionFragment stateDescriptionFragment2 = StateDescriptionFragmentBuilder.create() .withReadOnly(Boolean.TRUE).build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment2, -2); StateDescription stateDescription = item.getStateDescription(); assertThat(stateDescription.isReadOnly(), is(stateDescriptionFragment1.isReadOnly())); }
@Test public void testIsReadOnlyWhenTwoDescriptionProvidersHigherRankingIsReadOnly() { StateDescriptionFragment stateDescriptionFragment1 = StateDescriptionFragmentBuilder.create() .withReadOnly(Boolean.TRUE).build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment1, -1); StateDescriptionFragment stateDescriptionFragment2 = StateDescriptionFragmentBuilder.create().build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment2, -2); StateDescription stateDescription = item.getStateDescription(); assertThat(stateDescription.isReadOnly(), is(stateDescriptionFragment1.isReadOnly())); }
@Test public void testOptionsWhenTwoDescriptionProvidersHigherRankingProvidesOptions() { StateDescriptionFragment stateDescriptionFragment1 = StateDescriptionFragmentBuilder.create() .withOption(new StateOption("value", "label")).build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment1, -1); StateDescriptionFragment stateDescriptionFragment2 = StateDescriptionFragmentBuilder.create().build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment2, -2); StateDescription stateDescription = item.getStateDescription(); assertThat(stateDescription.getOptions(), is(stateDescriptionFragment1.getOptions())); }
@Test public void testOptionsWhenTwoDescriptionProvidersHigherRankingDoesntProvideOptions() { StateDescriptionFragment stateDescriptionFragment1 = StateDescriptionFragmentBuilder.create().build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment1, -1); StateDescriptionFragment stateDescriptionFragment2 = StateDescriptionFragmentBuilder.create() .withOption(new StateOption("value", "label")).build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment2, -2); StateDescription stateDescription = item.getStateDescription(); assertThat(stateDescription.getOptions(), is(stateDescriptionFragment2.getOptions())); }
@Test public void testFragmentsAreMergedInProviderOrder() { final List<StateOption> options = List.of(new StateOption("value", "label")); StateDescriptionFragment stateDescriptionFragment1 = StateDescriptionFragmentBuilder.create() .withMinimum(BigDecimal.ZERO) .withMaximum(BigDecimal.TEN) .withPattern("pattern") .withReadOnly(Boolean.TRUE) .withOptions(options).build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment1, -1); StateDescriptionFragment stateDescriptionFragment2 = StateDescriptionFragmentBuilder.create() .withMinimum(BigDecimal.ONE) .withMaximum(BigDecimal.ONE) .withStep(BigDecimal.ONE) .withPattern("base_pattern") .withOptions(options).build(); registerStateDescriptionFragmentProvider(stateDescriptionFragment2, -2); StateDescription stateDescription = item.getStateDescription(); assertThat(stateDescription.getMinimum(), is(BigDecimal.ZERO)); assertThat(stateDescription.getMaximum(), is(BigDecimal.TEN)); assertThat(stateDescription.getStep(), is(BigDecimal.ONE)); assertThat(stateDescription.getPattern(), is("pattern")); assertThat(stateDescription.isReadOnly(), is(true)); assertThat(stateDescription.getOptions(), is(options)); }