class MavenEmbedder {

    public MavenProject readProject( final InputStream mavenProjectStream ) throws ProjectBuildingException, MavenEmbedderException {
        ModelSource modelSource = new ModelSource() {
            @Override
            public InputStream getInputStream() {
                return mavenProjectStream;
            }

            @Override
            public String getLocation() {
                return "";
            }
        };

        ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
        try {
            org.eclipse.aether.artifact.Artifact lastArtifact = null;
            do {
                Thread.currentThread().setContextClassLoader(componentProvider.getSystemClassLoader());
                ProjectBuilder projectBuilder = componentProvider.lookup(ProjectBuilder.class);
                // BZ-1007894: Check if added dependencies are resolvable.
                ProjectBuildingResult result = projectBuilder.build(modelSource, getProjectBuildingRequest());
                if (result != null && result.getDependencyResolutionResult() != null && !result.getDependencyResolutionResult().getCollectionErrors().isEmpty()) {
                    // A dependency resolution error has been produced. It can contains some error. Throw the first one to the client, so the user will fix every one sequentially.
                    final Exception depedencyResolutionException = result.getDependencyResolutionResult().getCollectionErrors().get(0);
                    if (depedencyResolutionException instanceof ArtifactDescriptorException) {
                        final org.eclipse.aether.artifact.Artifact artifact = ((ArtifactDescriptorException) depedencyResolutionException).getResult().getArtifact();
                        if (!artifact.equals(lastArtifact)) {
                            tryRemoveLocalArtifact(artifact);
                            lastArtifact = artifact;
                            continue;
                        }
                    }
                    if (depedencyResolutionException != null) {
                        throw new MavenEmbedderException(depedencyResolutionException.getMessage(), depedencyResolutionException);
                    }
                }
                return (result == null || result.getProject() == null ) ? null : result.getProject();
            } while (true);
        } catch ( ComponentLookupException e ) {
            throw new MavenEmbedderException( e.getMessage(), e );
        } finally {
            Thread.currentThread().setContextClassLoader( originalCl );
            try {
                mavenProjectStream.close();
            } catch ( IOException e ) {
            }
        }
    }

}

class MavenEmbedderTest {

    private final String EMPTY_SETTINGS;
    final ComponentProvider componentProviderMocked;

    @Test
    public void testInvalidLocalDependency() throws MavenEmbedderException, ProjectBuildingException, ComponentLookupException, SettingsBuildingException, InvalidRepositoryException {

        final MavenExecutionRequestPopulator mavenExecutionRequestPopulator =  mock(MavenExecutionRequestPopulator.class);
        final Logger logger =  mock(Logger.class);
        final RepositorySystem repositorySystem = mock(RepositorySystem.class);
        final ArtifactRepository artifactRepository = mock(ArtifactRepository.class);

        final SettingsBuilder settingsBuilder =  mock(SettingsBuilder.class);
        final SettingsBuildingResult settingsBuildingResult = mock(SettingsBuildingResult.class);
        final ProjectBuilder projectBuilderMock =  mock(ProjectBuilder.class);
        final DependencyResolutionResult drr = mock(DependencyResolutionResult.class);
        final ProjectBuildingResult projectBuildingResult =  mock(ProjectBuildingResult.class);
        final ArtifactDescriptorException exception = mock(ArtifactDescriptorException.class);
        final ArtifactDescriptorRequest request = new ArtifactDescriptorRequest();

        final ArtifactDescriptorResult result = new ArtifactDescriptorResult(request);
        final Artifact artifactResult = mock(Artifact .class);

        final List<Exception> list = singletonList(exception);

        request.setArtifact(artifactResult);
        result.setArtifact(artifactResult);

        doReturn(settingsBuilder).when(componentProviderMocked).lookup(SettingsBuilder.class);
        doReturn(settingsBuildingResult).when(settingsBuilder).build(any(SettingsBuildingRequest.class));
        when(settingsBuildingResult.getEffectiveSettings()).thenReturn(mock(Settings.class));
        when(componentProviderMocked.getSystemClassLoader()).thenReturn(getClass().getClassLoader());


        doReturn(artifactRepository).when(repositorySystem).createLocalRepository(any(File.class));

        doReturn(new File("").toPath().toString()).when(artifactRepository).getBasedir();

        doReturn(repositorySystem).when(componentProviderMocked).lookup(RepositorySystem.class);

        doReturn(projectBuilderMock).when(componentProviderMocked).lookup(ProjectBuilder.class);

        doReturn(logger).when(componentProviderMocked).lookup(Logger.class);
        doReturn(mavenExecutionRequestPopulator).when(componentProviderMocked).lookup(MavenExecutionRequestPopulator.class);

        doReturn(projectBuildingResult).when(projectBuilderMock).build(any(ModelSource.class), any(ProjectBuildingRequest.class));
        when(projectBuildingResult.getDependencyResolutionResult()).thenReturn(drr);
        when(projectBuildingResult.getDependencyResolutionResult()).thenReturn(drr);
        when(drr.getCollectionErrors()).thenReturn(list);
        when(exception.getResult()).thenReturn(result);

        boolean[] didExecuteTryRemoveLocalArtifact = {false};
        final MavenRequest mavenRequest = createMavenRequest(null);
        final MavenEmbedder embedder = new MavenEmbedderMock2( mavenRequest, null ) {
            void tryRemoveLocalArtifact(Artifact artifact) {
                didExecuteTryRemoveLocalArtifact[0] = true;
                assertEquals(artifact, artifactResult);
            }
        };

        assertThatThrownBy(() -> embedder.readProject(mock(InputStream.class)))
                .isInstanceOf(MavenEmbedderException.class);

        assertTrue(didExecuteTryRemoveLocalArtifact[0]);
    }

}