-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADDED: BranchApi with endpoints 'create', 'getDefault', 'updateDefault'.
- Loading branch information
Showing
9 changed files
with
413 additions
and
30 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
60 changes: 60 additions & 0 deletions
60
src/main/java/com/cdancy/bitbucket/rest/domain/branch/Branch.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,60 @@ | ||
/* | ||
* 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.branch; | ||
|
||
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 Branch { | ||
|
||
@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(); | ||
|
||
public abstract boolean isDefault(); | ||
|
||
@Nullable | ||
public abstract List<Error> errors(); | ||
|
||
Branch() { | ||
} | ||
|
||
@SerializedNames({ "id", "displayId", "type", "latestCommit", "latestChangeset", "isDefault", "errors" }) | ||
public static Branch create(String id, String displayId, String type, | ||
String latestCommit, String latestChangeset, boolean isDefault, List<Error> errors) { | ||
return new AutoValue_Branch(id, displayId, type, latestCommit, latestChangeset, isDefault, | ||
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
60 changes: 60 additions & 0 deletions
60
src/main/java/com/cdancy/bitbucket/rest/features/BranchApi.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,60 @@ | ||
/* | ||
* 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.branch.Branch; | ||
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.CreateBranch; | ||
import com.cdancy.bitbucket.rest.options.CreateTag; | ||
import org.jclouds.Fallbacks; | ||
import org.jclouds.rest.annotations.*; | ||
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 BranchApi { | ||
|
||
@Named("branch:create") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Path("/{project}/repos/{repo}/branches") | ||
@Fallback(BitbucketFallbacks.BranchOnError.class) | ||
@POST | ||
Branch create(@PathParam("project") String project, @PathParam("repo") String repo, @BinderParam(BindToJsonPayload.class) CreateBranch createBranch); | ||
|
||
@Named("branch:update-default") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Path("/{project}/repos/{repo}/branches/default") | ||
@Fallback(BitbucketFallbacks.FalseOnError.class) | ||
@Payload("%7B \"id\": \"{id}\" %7D") | ||
@PUT | ||
boolean updateDefault(@PathParam("project") String project, @PathParam("repo") String repo, @PayloadParam("id") String id); | ||
|
||
@Named("branch:get-default") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Path("/{project}/repos/{repo}/branches/default") | ||
@Fallback(BitbucketFallbacks.BranchOnError.class) | ||
@GET | ||
Branch getDefault(@PathParam("project") String project, @PathParam("repo") String repo); | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/cdancy/bitbucket/rest/options/CreateBranch.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 CreateBranch { | ||
|
||
public abstract String name(); | ||
|
||
public abstract String startPoint(); | ||
|
||
// defaults to value of name if null | ||
@Nullable | ||
public abstract String message(); | ||
|
||
CreateBranch() { | ||
} | ||
|
||
@SerializedNames({ "name", "startPoint", "message" }) | ||
public static CreateBranch create(String name, String startPoint, String message) { | ||
return new AutoValue_CreateBranch(name, startPoint, message != null ? message : name); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/test/java/com/cdancy/bitbucket/rest/features/BranchApiLiveTest.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,86 @@ | ||
/* | ||
* 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.branch.Branch; | ||
import com.cdancy.bitbucket.rest.domain.tags.Tag; | ||
import com.cdancy.bitbucket.rest.options.CreateBranch; | ||
import com.cdancy.bitbucket.rest.options.CreateTag; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.*; | ||
|
||
@Test(groups = "live", testName = "BranchApiLiveTest", singleThreaded = true) | ||
public class BranchApiLiveTest extends BaseBitbucketApiLiveTest { | ||
|
||
/* | ||
String projectKey = randomStringLettersOnly(); | ||
String repoKey = randomStringLettersOnly(); | ||
String tagName = randomStringLettersOnly(); | ||
String commitHash = ""; | ||
*/ | ||
|
||
String projectKey = "TEST"; | ||
String repoKey = "dev"; | ||
String branchName = randomStringLettersOnly(); | ||
String commitHash = "d90ca08fa076e2e4c076592fce3832aba80a494f"; | ||
|
||
String defaultBranchId = "refs/heads/master"; | ||
|
||
@BeforeClass | ||
public void init() { | ||
Branch branch = api().getDefault(projectKey, repoKey); | ||
assertNotNull(branch); | ||
assertTrue(branch.errors().size() == 0); | ||
defaultBranchId = branch.id(); | ||
} | ||
|
||
@Test | ||
public void testCreateBranch() { | ||
CreateBranch createBranch = CreateBranch.create(branchName, commitHash, null); | ||
Branch branch = api().create(projectKey, repoKey, createBranch); | ||
assertNotNull(branch); | ||
assertTrue(branch.errors().size() == 0); | ||
assertTrue(branch.id().endsWith(branchName)); | ||
assertTrue(branch.latestCommit().equalsIgnoreCase(commitHash)); | ||
} | ||
|
||
@Test (dependsOnMethods = "testCreateBranch") | ||
public void testUpdateDefaultBranch() { | ||
boolean success = api().updateDefault(projectKey, repoKey, "refs/heads/" + branchName); | ||
assertTrue(success); | ||
} | ||
|
||
@Test (dependsOnMethods = "testUpdateDefaultBranch") | ||
public void testGetNewDefaultBranch() { | ||
Branch branch = api().getDefault(projectKey, repoKey); | ||
assertNotNull(branch); | ||
assertTrue(branch.errors().size() == 0); | ||
assertNotNull(branch.id()); | ||
} | ||
|
||
@AfterClass | ||
public void fin() { | ||
boolean success = api().updateDefault(projectKey, repoKey, defaultBranchId); | ||
assertTrue(success); | ||
} | ||
|
||
private BranchApi api() { return api.branchApi(); } | ||
} |
Oops, something went wrong.