DataFetchingUtils { public Optional<Delivery> getCommercialDelivery(final RepositoryHandler repoHandler, final String name, final String version) { final Optional<DbProduct> productOptional = repoHandler.getOneByQuery(DbCollections.DB_PRODUCT, makeQuery(name, version), DbProduct.class); if(!productOptional.isPresent()) { return Optional.empty(); } final DbProduct dbProduct = productOptional.get(); final List<Delivery> filtered = dbProduct.getDeliveries() .stream() .filter(d -> d.getCommercialName().equals(name) && d.getCommercialVersion().equals(version) ) .collect(Collectors.toList()); if(filtered.isEmpty()) { return Optional.empty(); } if (filtered.size() > 1 && LOG.isWarnEnabled()) { LOG.warn(String.format("Multiple commercial version entries found for [%s] [%s]", name, version)); } return Optional.of(filtered.get(0)); }  List<DbProduct> getProductWithCommercialDeliveries(final RepositoryHandler repoHandler); Optional<Delivery> getCommercialDelivery(final RepositoryHandler repoHandler,
                                                    final String name,
                                                    final String version); Set<String> getDeliveryDependencies(final RepositoryHandler repoHandler,
                                               final DependencyHandler depHandler,
                                               final Delivery delivery); void initCorporateIDs(final RepositoryHandler repoHandler,
                                 final String orgName); boolean isThirdParty(final DbArtifact artifact);  }
@Test public void getCDNoProduct() { final RepositoryHandler repoHandler = mock(RepositoryHandler.class); when(repoHandler.getOneByQuery(anyString(), anyString(), any(Class.class))).thenReturn(Optional.empty()); final DataFetchingUtils sut = new DataFetchingUtils(); final Optional<Delivery> result = sut.getCommercialDelivery(repoHandler, "a name", "a version"); assertFalse(result.isPresent()); }
@Test public void getCDNoCommercialDelivery() { final DbProduct fakeProduct = mock(DbProduct.class); when(fakeProduct.getDeliveries()).thenReturn(Collections.emptyList()); final RepositoryHandler repoHandler = mock(RepositoryHandler.class); when(repoHandler.getOneByQuery(anyString(), anyString(), any(Class.class))).thenReturn(Optional.of(fakeProduct)); final DataFetchingUtils sut = new DataFetchingUtils(); final Optional<Delivery> result = sut.getCommercialDelivery(repoHandler, "a name", "a version"); assertFalse(result.isPresent()); }
@Test public void getCDTooManyCommercialDeliveries() throws NoSuchFieldException, IllegalAccessException { final DbProduct fakeProduct = mock(DbProduct.class); when(fakeProduct.getDeliveries()).thenReturn(Arrays.asList( DataModelFactory.createDelivery(SAMPLE_NAME, SAMPLE_VERSION, LocalDate.now().toString(), Arrays.asList("one")), DataModelFactory.createDelivery(SAMPLE_NAME, SAMPLE_VERSION, "does-not-matter", Collections.emptyList()) )); final RepositoryHandler repoHandler = mock(RepositoryHandler.class); when(repoHandler.getOneByQuery(anyString(), anyString(), any(Class.class))).thenReturn(Optional.of(fakeProduct)); final DataFetchingUtils sut = new DataFetchingUtils(); final Logger fakeLogger = mock(Logger.class); when(fakeLogger.isWarnEnabled()).thenReturn(true); InjectionUtils.injectField(sut, DataFetchingUtils.class, "LOG", fakeLogger); final Optional<Delivery> result = sut.getCommercialDelivery(repoHandler, SAMPLE_NAME, SAMPLE_VERSION); assertTrue(result.isPresent()); ArgumentCaptor<String> msgCaptor = ArgumentCaptor.forClass(String.class); verify(fakeLogger, times(1)).warn(msgCaptor.capture()); assertEquals(String.format("Multiple commercial version entries found for [%s] [%s]", SAMPLE_NAME, SAMPLE_VERSION), msgCaptor.getValue()); assertEquals(1, result.get().getDependencies().size()); }