Skip to content

Commit

Permalink
Merge pull request #1053 from fellahi-ali/#984
Browse files Browse the repository at this point in the history
#984 BitbucketOrganizationRepos impl & test
  • Loading branch information
amihaiemil authored Mar 11, 2021
2 parents 36dbcb4 + 57d3dc5 commit 53f3029
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -59,7 +60,36 @@ public BitbucketOrganizationRepos(final URI uri,

@Override
public Iterator<Repo> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit 53f3029

Please sign in to comment.