### Question:
ValidationServiceImpl implements ValidationService { @Override public ValidateRulesResult validateRules(SwitchId switchId, List<FlowEntry> presentRules, List<FlowEntry> expectedDefaultRules) { log.debug("Validating rules on switch {}", switchId); Set<Long> expectedCookies = getExpectedFlowRules(switchId); return makeRulesResponse(expectedCookies, presentRules, expectedDefaultRules, switchId); } ValidationServiceImpl(PersistenceManager persistenceManager, SwitchManagerTopologyConfig topologyConfig); @Override ValidateRulesResult validateRules(SwitchId switchId, List<FlowEntry> presentRules,
                                             List<FlowEntry> expectedDefaultRules); @Override ValidateGroupsResult validateGroups(SwitchId switchId, List<GroupEntry> presentGroups); @Override ValidateMetersResult validateMeters(SwitchId switchId, List<MeterEntry> presentMeters,
                                               List<MeterEntry> expectedDefaultMeters);  }### Answer:
@Test public void validateRulesEmpty() throws SwitchNotFoundException { ValidationService validationService = new ValidationServiceImpl(persistenceManager().build(), topologyConfig); ValidateRulesResult response = validationService.validateRules(SWITCH_ID_A, emptyList(), emptyList()); assertTrue(response.getMissingRules().isEmpty()); assertTrue(response.getProperRules().isEmpty()); assertTrue(response.getExcessRules().isEmpty()); }
@Test public void validateRulesSimpleSegmentCookies() throws SwitchNotFoundException { ValidationService validationService = new ValidationServiceImpl(persistenceManager().withSegmentsCookies(2L, 3L).build(), topologyConfig); List<FlowEntry> flowEntries = Lists.newArrayList(FlowEntry.builder().cookie(1L).build(), FlowEntry.builder().cookie(2L).build()); ValidateRulesResult response = validationService.validateRules(SWITCH_ID_A, flowEntries, emptyList()); assertEquals(ImmutableList.of(3L), response.getMissingRules()); assertEquals(ImmutableList.of(2L), response.getProperRules()); assertEquals(ImmutableList.of(1L), response.getExcessRules()); }
@Test public void validateRulesSegmentAndIngressCookies() throws SwitchNotFoundException { ValidationService validationService = new ValidationServiceImpl(persistenceManager().withSegmentsCookies(2L).withIngressCookies(1L).build(), topologyConfig); List<FlowEntry> flowEntries = Lists.newArrayList(FlowEntry.builder().cookie(1L).build(), FlowEntry.builder().cookie(2L).build()); ValidateRulesResult response = validationService.validateRules(SWITCH_ID_A, flowEntries, emptyList()); assertTrue(response.getMissingRules().isEmpty()); assertEquals(ImmutableSet.of(1L, 2L), new HashSet<>(response.getProperRules())); assertTrue(response.getExcessRules().isEmpty()); }
@Test public void validateRulesSegmentAndIngressCookiesWithServer42Rules() { SwitchProperties switchProperties = SwitchProperties.builder() .server42FlowRtt(true) .build(); ValidationService validationService = new ValidationServiceImpl(persistenceManager() .withIngressCookies(1L) .withSwitchProperties(switchProperties) .build(), topologyConfig); List<FlowEntry> flowEntries = Lists.newArrayList(FlowEntry.builder().cookie(0xC0000000000001L).build(), FlowEntry.builder().cookie(1L).build()); ValidateRulesResult response = validationService.validateRules(SWITCH_ID_A, flowEntries, emptyList()); assertTrue(response.getMissingRules().isEmpty()); assertEquals(ImmutableSet.of(0xC0000000000001L, 1L), new HashSet<>(response.getProperRules())); assertTrue(response.getExcessRules().isEmpty()); }