FeatureValidator { void validateOptionIdsForUpdate(OptionCollection existing, OptionCollection update) { if (update.getItemsCount() == 0) { return; } final Set<String> firstSet = existing.getItemsList().stream().map(FeatureOption::getId).collect(Collectors.toSet()); final Set<String> secondSet = update.getItemsList().stream().map(FeatureOption::getId).collect(Collectors.toSet()); if (!firstSet.equals(secondSet)) { throw new ServiceException( Problem.clientProblem("option_ids_mismatch", "option ids must be the same", 422)); } }    }
@Test public void validateFeatureThrowingBoolMismatchedOptionIds() { OptionCollection.Builder existing = OptionCollection.newBuilder() .setOption(OptionType.bool) .addItems(FeatureOption.newBuilder().setValue("true").setKey("true").setId("1")) .addItems(FeatureOption.newBuilder().setValue("false").setKey("false").setId("2")); OptionCollection.Builder update = OptionCollection.newBuilder() .setOption(OptionType.bool) .addItems(FeatureOption.newBuilder().setValue("true").setKey("true").setId("1")) .addItems(FeatureOption.newBuilder().setValue("false").setKey("false").setId("3")); try { new FeatureValidator().validateOptionIdsForUpdate(existing.build(), update.build()); fail(); } catch (ServiceException e) { assertEquals(422, e.problem().status()); assertSame("option_ids_mismatch", e.problem().title()); } }
@Test public void validateFeatureThrowingStringMismatchedOptionIds() { OptionCollection.Builder existing = OptionCollection.newBuilder() .setOption(OptionType.string) .addItems(FeatureOption.newBuilder().setValue("red").setKey("opt-red").setWeight(3000).setId("r")) .addItems(FeatureOption.newBuilder().setValue("green").setKey("opt-green").setWeight(2000).setId("g")) .addItems(FeatureOption.newBuilder().setValue("blue").setKey("opt-blue").setWeight(5000).setId("b")); OptionCollection.Builder update = OptionCollection.newBuilder() .setOption(OptionType.string) .addItems(FeatureOption.newBuilder().setValue("red").setKey("opt-red").setWeight(3000).setId("r")) .addItems(FeatureOption.newBuilder().setValue("green").setKey("opt-green").setWeight(2000).setId("g")) .addItems(FeatureOption.newBuilder().setValue("blue").setKey("opt-blue").setWeight(5000).setId("b")) .addItems(FeatureOption.newBuilder().setValue("yello").setKey("opt-yello").setWeight(5000).setId("y")) ; try { new FeatureValidator().validateOptionIdsForUpdate(existing.build(), update.build()); fail(); } catch (ServiceException e) { assertEquals(422, e.problem().status()); assertSame("option_ids_mismatch", e.problem().title()); } }
@Test public void validateFeatureThrowingStringNoUpdateOptionsOk() { OptionCollection.Builder existing = OptionCollection.newBuilder() .setOption(OptionType.string) .addItems(FeatureOption.newBuilder().setValue("red").setKey("opt-red").setWeight(3000).setId("r")) .addItems(FeatureOption.newBuilder().setValue("green").setKey("opt-green").setWeight(2000).setId("g")) .addItems(FeatureOption.newBuilder().setValue("blue").setKey("opt-blue").setWeight(5000).setId("b")); OptionCollection.Builder update = OptionCollection.newBuilder(); try { new FeatureValidator().validateOptionIdsForUpdate(existing.build(), update.build()); } catch (ServiceException e) { fail("an update without options should validate"); } }
@Test public void validateFeatureThrowingBoolNoUpdateOptionsOk() { OptionCollection.Builder existing = OptionCollection.newBuilder() .setOption(OptionType.bool) .addItems(FeatureOption.newBuilder().setValue("true").setKey("true")) .addItems(FeatureOption.newBuilder().setValue("false").setKey("false")); OptionCollection.Builder update = OptionCollection.newBuilder(); try { new FeatureValidator().validateOptionIdsForUpdate(existing.build(), update.build()); } catch (ServiceException e) { fail("an update without options should validate"); } }