diff --git a/self-core-impl/src/main/java/com/selfxdsd/core/BitbucketOrganizationRepos.java b/self-core-impl/src/main/java/com/selfxdsd/core/BitbucketOrganizationRepos.java index 95189995..eb8deb70 100644 --- a/self-core-impl/src/main/java/com/selfxdsd/core/BitbucketOrganizationRepos.java +++ b/self-core-impl/src/main/java/com/selfxdsd/core/BitbucketOrganizationRepos.java @@ -5,6 +5,8 @@ import com.selfxdsd.api.User; import com.selfxdsd.api.storage.Storage; +import javax.json.JsonValue; +import java.net.HttpURLConnection; import java.net.URI; import java.util.Iterator; import java.util.Spliterator; @@ -16,7 +18,6 @@ * @author Nikita Monokov (nmonokov@gmail.com) * @version $Id$ * @since 0.0.64 - * @todo #978:60min Continue implementing this class and writing tests for it. */ public final class BitbucketOrganizationRepos implements Repos { @@ -59,7 +60,36 @@ public BitbucketOrganizationRepos(final URI uri, @Override public Iterator iterator() { - throw new UnsupportedOperationException("Not implemented yet"); + final Resource resource = resources.get(this.reposUri); + final int statusCode = resource.statusCode(); + if (statusCode == HttpURLConnection.HTTP_OK) { + return resource.asJsonObject() + .getJsonArray("values") + .stream() + .map(this::buildRepo) + .iterator(); + } else { + throw new IllegalStateException("Unable to fetch Bitbucket " + + "organization Repos for current User. Expected 200 OK, " + + "but got: " + statusCode); + } + } + + /** + * Builds a repo from provided JSON data. + * + * @param repoData Repo as JSON. + * @return Repo. + */ + private Repo buildRepo(final JsonValue repoData) { + final String repoUri = this.reposUri.toString() + "/" + + repoData.asJsonObject().getString("slug"); + return new BitbucketRepo( + this.resources, + URI.create(repoUri), + this.owner, + this.storage + ); } @Override diff --git a/self-core-impl/src/test/java/com/selfxdsd/core/BitbucketOrganizationReposTestCase.java b/self-core-impl/src/test/java/com/selfxdsd/core/BitbucketOrganizationReposTestCase.java new file mode 100644 index 00000000..71450da6 --- /dev/null +++ b/self-core-impl/src/test/java/com/selfxdsd/core/BitbucketOrganizationReposTestCase.java @@ -0,0 +1,88 @@ +package com.selfxdsd.core; + +import com.selfxdsd.api.Repos; +import com.selfxdsd.api.User; +import com.selfxdsd.api.storage.Storage; +import com.selfxdsd.core.mock.MockJsonResources; +import com.selfxdsd.core.mock.MockJsonResources.MockResource; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; +import org.mockito.Mockito; + +import javax.json.Json; +import javax.json.JsonValue; +import java.net.HttpURLConnection; +import java.net.URI; + +/** + * Unit tests for {@link BitbucketOrganizationRepos}. + * + * @author Ali FELLAHI (fellahi.ali@gmail.com) + * @version $Id$ + * @since 0.0.68 + */ +public final class BitbucketOrganizationReposTestCase { + + /** + * Can fetches organization repos. + */ + @Test + public void fetchOk() { + final JsonResources resources = new MockJsonResources( + req -> { + MatcherAssert.assertThat( + req.getMethod(), + Matchers.is("GET") + ); + MatcherAssert.assertThat( + req.getUri().toString(), + Matchers.equalTo( + "https://bitbucket.org/api/2.0/repositories/alilosoft" + ) + ); + return new MockResource( + HttpURLConnection.HTTP_OK, + Json.createObjectBuilder().add( + "values", + Json.createArrayBuilder() + .add(Json.createObjectBuilder() + .add("slug", "repo1") + .build()) + .add(Json.createObjectBuilder() + .add("slug", "repo2") + .build()) + .add(Json.createObjectBuilder() + .add("slug", "repo3") + .build()) + .build() + ).build() + ); + } + ); + final Repos repos = new BitbucketOrganizationRepos( + URI.create("https://bitbucket.org/api/2.0/repositories/alilosoft"), + Mockito.mock(User.class), + resources, + Mockito.mock(Storage.class) + ); + MatcherAssert.assertThat(repos, Matchers.iterableWithSize(3)); + } + + /** + * Throw if organization repos are not fetched. + */ + @Test(expected = IllegalStateException.class) + public void fetchNotOk() { + final JsonResources resources = new MockJsonResources( + req -> new MockResource(404, JsonValue.NULL) + ); + new BitbucketOrganizationRepos( + URI.create("bad/uri"), + Mockito.mock(User.class), + resources, + Mockito.mock(Storage.class) + ).iterator(); + } + +}