### Question:
EPPolicyTemplateDaoFacadeImpl implements EPPolicyTemplateDaoFacade { @Override public ListenableFuture<Collection<EndpointPolicyTemplateBySgt>> readBy(@Nonnull final EpPolicyTemplateValueKey templateLookupKey) { final Collection<EndpointPolicyTemplateBySgt> templatesFromDao = epPolicyTemplateDao.readBy(templateLookupKey); final ListenableFuture<Collection<EndpointPolicyTemplateBySgt>> result; if (!templatesFromDao.isEmpty()) { result = Futures.immediateFuture(templatesFromDao); } else { result = sgtGenerator.generateNextSgt(epPolicyTemplateCachedDao) .map(sgt -> buildEpPolicyTemplate(templateLookupKey, sgt)) .map(this::storeTemplate) .orElse(Futures.immediateFuture(Collections.emptyList())); } return result; } EPPolicyTemplateDaoFacadeImpl(final DataBroker dataBroker, final EPPolicyTemplateDaoImpl epPolicyTemplateDao,
                                         final SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> epPolicyTemplateCachedDao,
                                         final SgtGeneratorImpl sgtGenerator); @Override void setTemplateProvider(final EPPolicyTemplateProvider templateProvider); @Override ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> read(@Nonnull final Sgt key); @Override ListenableFuture<Collection<EndpointPolicyTemplateBySgt>> readBy(@Nonnull final EpPolicyTemplateValueKey templateLookupKey);  }### Answer:
@Test public void testReadBy() throws Exception { Mockito.when(sgtGeneratorImpl.generateNextSgt(delegateCachedDao)) .thenReturn(java.util.Optional.empty()) .thenReturn(java.util.Optional.of(new Sgt(42))); final EpPolicyTemplateValueKey lookupKey = new EpPolicyTemplateValueKey(TENANT_ID, Collections.singletonList(EPG_ID), Collections.emptyList()); final ListenableFuture<Collection<EndpointPolicyTemplateBySgt>> templateFail = facade.readBy(lookupKey); Assert.assertTrue(templateFail.isDone()); Assert.assertTrue(templateFail.get().isEmpty()); final ListenableFuture<Collection<EndpointPolicyTemplateBySgt>> templateSuccess = facade.readBy(lookupKey); Assert.assertTrue(templateSuccess.isDone()); final Collection<EndpointPolicyTemplateBySgt> templateBag = templateSuccess.get(); Assert.assertFalse(templateBag.isEmpty()); first = Iterables.getFirst(templateBag, null); Assert.assertNotNull(templateBag); Assert.assertEquals(TemplateGenerated.class, first.getOrigin()); Assert.assertEquals(Collections.singletonList(EPG_ID), Iterables.getFirst(templateBag, null).getEndpointGroups()); Assert.assertTrue(Iterables.getFirst(templateBag, null).getConditions().isEmpty()); Assert.assertEquals(42, Iterables.getFirst(templateBag, null).getSgt().getValue().intValue()); }