-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADDED: TagApi with endpoints 'create' and 'get'.
- Loading branch information
Showing
8 changed files
with
359 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/cdancy/bitbucket/rest/domain/tags/Tag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.cdancy.bitbucket.rest.domain.tags; | ||
|
||
import com.cdancy.bitbucket.rest.error.Error; | ||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.ImmutableList; | ||
import org.jclouds.javax.annotation.Nullable; | ||
import org.jclouds.json.SerializedNames; | ||
|
||
import java.util.List; | ||
|
||
@AutoValue | ||
public abstract class Tag { | ||
|
||
@Nullable | ||
public abstract String id(); | ||
|
||
@Nullable | ||
public abstract String displayId(); | ||
|
||
@Nullable | ||
public abstract String type(); | ||
|
||
@Nullable | ||
public abstract String latestCommit(); | ||
|
||
@Nullable | ||
public abstract String latestChangeset(); | ||
|
||
@Nullable | ||
public abstract String hash(); | ||
|
||
@Nullable | ||
public abstract List<Error> errors(); | ||
|
||
Tag() { | ||
} | ||
|
||
@SerializedNames({ "id", "displayId", "type", "latestCommit", "latestChangeset", "hash", "errors" }) | ||
public static Tag create(String id, String displayId, String type, | ||
String latestCommit, String latestChangeset, String hash, List<Error> errors) { | ||
return new AutoValue_Tag(id, displayId, type, latestCommit, latestChangeset, hash, | ||
errors != null ? ImmutableList.copyOf(errors) : ImmutableList.<Error> of()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/com/cdancy/bitbucket/rest/features/TagApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.cdancy.bitbucket.rest.features; | ||
|
||
import com.cdancy.bitbucket.rest.domain.tags.Tag; | ||
import com.cdancy.bitbucket.rest.fallbacks.BitbucketFallbacks; | ||
import com.cdancy.bitbucket.rest.filters.BitbucketAuthentication; | ||
import com.cdancy.bitbucket.rest.options.CreateTag; | ||
import org.jclouds.Fallbacks; | ||
import org.jclouds.rest.annotations.BinderParam; | ||
import org.jclouds.rest.annotations.Fallback; | ||
import org.jclouds.rest.annotations.RequestFilters; | ||
import org.jclouds.rest.binders.BindToJsonPayload; | ||
|
||
import javax.inject.Named; | ||
import javax.ws.rs.*; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
@Produces(MediaType.APPLICATION_JSON) | ||
@RequestFilters(BitbucketAuthentication.class) | ||
@Path("/rest/api/{jclouds.api-version}/projects") | ||
public interface TagApi { | ||
|
||
@Named("tag:create") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Path("/{project}/repos/{repo}/tags") | ||
@Fallback(BitbucketFallbacks.TagOnError.class) | ||
@POST | ||
Tag create(@PathParam("project") String project, @PathParam("repo") String repo, @BinderParam(BindToJsonPayload.class) CreateTag createTag); | ||
|
||
@Named("tag:get") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Path("/{project}/repos/{repo}/tags/{tag}") | ||
@Fallback(Fallbacks.NullOnNotFoundOr404.class) | ||
@GET | ||
Tag get(@PathParam("project") String project, @PathParam("repo") String repo, @PathParam("tag") String tag); | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/cdancy/bitbucket/rest/options/CreateTag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.cdancy.bitbucket.rest.options; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import org.jclouds.javax.annotation.Nullable; | ||
import org.jclouds.json.SerializedNames; | ||
|
||
@AutoValue | ||
public abstract class CreateTag { | ||
|
||
public abstract String name(); | ||
|
||
public abstract String startPoint(); | ||
|
||
// defaults to value of name if null | ||
@Nullable | ||
public abstract String message(); | ||
|
||
CreateTag() { | ||
} | ||
|
||
@SerializedNames({ "name", "startPoint", "message" }) | ||
public static CreateTag create(String name, String startPoint, String message) { | ||
return new AutoValue_CreateTag(name, startPoint, message != null ? message : name); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/test/java/com/cdancy/bitbucket/rest/features/TagApiLiveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.cdancy.bitbucket.rest.features; | ||
|
||
import com.cdancy.bitbucket.rest.BaseBitbucketApiLiveTest; | ||
|
||
import com.cdancy.bitbucket.rest.domain.tags.Tag; | ||
import com.cdancy.bitbucket.rest.options.CreateTag; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.assertNull; | ||
import static org.testng.Assert.assertNotNull; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
@Test(groups = "live", testName = "TagApiLiveTest", singleThreaded = true) | ||
public class TagApiLiveTest extends BaseBitbucketApiLiveTest { | ||
|
||
/* | ||
String projectKey = randomStringLettersOnly(); | ||
String repoKey = randomStringLettersOnly(); | ||
String tagName = randomStringLettersOnly(); | ||
String commitHash = ""; | ||
*/ | ||
|
||
String projectKey = "TEST"; | ||
String repoKey = "dev"; | ||
String tagName = randomStringLettersOnly(); | ||
String commitHash = "d90ca08fa076e2e4c076592fce3832aba80a494f"; | ||
|
||
@Test | ||
public void testCreateTag() { | ||
CreateTag createTag = CreateTag.create(tagName, commitHash, null); | ||
Tag tag = api().create(projectKey, repoKey, createTag); | ||
assertNotNull(tag); | ||
assertTrue(tag.errors().size() == 0); | ||
assertTrue(tag.id().endsWith(tagName)); | ||
assertTrue(tag.latestCommit().equalsIgnoreCase(commitHash)); | ||
} | ||
|
||
@Test (dependsOnMethods = "testCreateTag") | ||
public void testGetTag() { | ||
Tag tag = api().get(projectKey, repoKey, tagName); | ||
assertNotNull(tag); | ||
assertTrue(tag.errors().size() == 0); | ||
assertTrue(tag.id().endsWith(tagName)); | ||
assertTrue(tag.latestCommit().equalsIgnoreCase(commitHash)); | ||
} | ||
|
||
@Test | ||
public void testGetTagNonExistent() { | ||
Tag tag = api().get(projectKey, repoKey, tagName + "9999"); | ||
assertNull(tag); | ||
} | ||
|
||
private TagApi api() { return api.tagApi(); } | ||
} |
108 changes: 108 additions & 0 deletions
108
src/test/java/com/cdancy/bitbucket/rest/features/TagApiMockTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.cdancy.bitbucket.rest.features; | ||
|
||
import com.cdancy.bitbucket.rest.BitbucketApi; | ||
import com.cdancy.bitbucket.rest.BitbucketApiMetadata; | ||
import com.cdancy.bitbucket.rest.domain.repository.Repository; | ||
import com.cdancy.bitbucket.rest.domain.tags.Tag; | ||
import com.cdancy.bitbucket.rest.internal.BaseBitbucketMockTest; | ||
import com.cdancy.bitbucket.rest.options.CreateRepository; | ||
import com.cdancy.bitbucket.rest.options.CreateTag; | ||
import com.squareup.okhttp.mockwebserver.MockResponse; | ||
import com.squareup.okhttp.mockwebserver.MockWebServer; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.assertNull; | ||
import static org.testng.Assert.assertNotNull; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
/** | ||
* Mock tests for the {@link TagApi} class. | ||
*/ | ||
@Test(groups = "unit", testName = "TagApiMockTest") | ||
public class TagApiMockTest extends BaseBitbucketMockTest { | ||
|
||
public void testCreateTag() throws Exception { | ||
MockWebServer server = mockEtcdJavaWebServer(); | ||
|
||
server.enqueue(new MockResponse().setBody(payloadFromResource("/tag.json")).setResponseCode(200)); | ||
BitbucketApi baseApi = api(server.getUrl("/")); | ||
TagApi api = baseApi.tagApi(); | ||
try { | ||
String projectKey = "PRJ"; | ||
String repoKey = "myrepo"; | ||
String tagName = "release-2.0.0"; | ||
String commitHash = "8d351a10fb428c0c1239530256e21cf24f136e73"; | ||
|
||
CreateTag createTag = CreateTag.create(tagName, commitHash, null); | ||
Tag tag = api.create(projectKey, repoKey, createTag); | ||
assertNotNull(tag); | ||
assertTrue(tag.errors().size() == 0); | ||
assertTrue(tag.id().endsWith(tagName)); | ||
assertTrue(tag.latestCommit().equalsIgnoreCase(commitHash)); | ||
assertSent(server, "POST", "/rest/api/" + BitbucketApiMetadata.API_VERSION + "/projects/" + projectKey + "/repos/" + repoKey + "/tags"); | ||
} finally { | ||
baseApi.close(); | ||
server.shutdown(); | ||
} | ||
} | ||
|
||
public void testGetTag() throws Exception { | ||
MockWebServer server = mockEtcdJavaWebServer(); | ||
|
||
server.enqueue(new MockResponse().setBody(payloadFromResource("/tag.json")).setResponseCode(200)); | ||
BitbucketApi baseApi = api(server.getUrl("/")); | ||
TagApi api = baseApi.tagApi(); | ||
try { | ||
String projectKey = "PRJ"; | ||
String repoKey = "myrepo"; | ||
String tagName = "release-2.0.0"; | ||
String commitHash = "8d351a10fb428c0c1239530256e21cf24f136e73"; | ||
|
||
Tag tag = api.get(projectKey, repoKey, tagName); | ||
assertNotNull(tag); | ||
assertTrue(tag.errors().size() == 0); | ||
assertTrue(tag.id().endsWith(tagName)); | ||
assertTrue(tag.latestCommit().equalsIgnoreCase(commitHash)); | ||
assertSent(server, "GET", "/rest/api/" + BitbucketApiMetadata.API_VERSION + "/projects/" + projectKey + "/repos/" + repoKey + "/tags/" + tagName); | ||
} finally { | ||
baseApi.close(); | ||
server.shutdown(); | ||
} | ||
} | ||
|
||
public void testGetTagNonExistent() throws Exception { | ||
MockWebServer server = mockEtcdJavaWebServer(); | ||
|
||
server.enqueue(new MockResponse().setResponseCode(404)); | ||
BitbucketApi baseApi = api(server.getUrl("/")); | ||
TagApi api = baseApi.tagApi(); | ||
try { | ||
String projectKey = "PRJ"; | ||
String repoKey = "myrepo"; | ||
String tagName = "non-existent-tag"; | ||
|
||
Tag tag = api.get(projectKey, repoKey, tagName); | ||
assertNull(tag); | ||
assertSent(server, "GET", "/rest/api/" + BitbucketApiMetadata.API_VERSION + "/projects/" + projectKey + "/repos/" + repoKey + "/tags/" + tagName); | ||
} finally { | ||
baseApi.close(); | ||
server.shutdown(); | ||
} | ||
} | ||
} |
Oops, something went wrong.