11package org .gitlab4j .api ;
22
33import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertFalse ;
45import static org .junit .Assert .assertNotNull ;
56import static org .junit .Assert .assertTrue ;
67import static org .junit .Assume .assumeTrue ;
@@ -54,6 +55,7 @@ public class TestRepositoryApi {
5455 }
5556
5657 private static final String TEST_BRANCH_NAME = "feature/test_branch" ;
58+ private static final String TEST_PROTECT_BRANCH_NAME = "feature/protect_branch" ;
5759 private static final String TEST_FILEPATH = "test-file.txt" ;
5860 private static GitLabApi gitLabApi ;
5961
@@ -100,8 +102,12 @@ public static void teardown() throws GitLabApiException {
100102 } catch (GitLabApiException ignore ) {
101103 }
102104
103- gitLabApi .getRepositoryApi ().deleteBranch (project .getId (), TEST_BRANCH_NAME );
104-
105+ try {
106+ gitLabApi .getRepositoryApi ().deleteBranch (project .getId (), TEST_BRANCH_NAME );
107+ } catch (GitLabApiException ignore ) {
108+ }
109+
110+ gitLabApi .getRepositoryApi ().deleteBranch (project .getId (), TEST_PROTECT_BRANCH_NAME );
105111
106112 } catch (GitLabApiException ignore ) {
107113 }
@@ -212,7 +218,7 @@ public void testCompare() throws GitLabApiException {
212218 compareResults = gitLabApi .getRepositoryApi ().compare (TEST_NAMESPACE + "/" + TEST_PROJECT_NAME , commits .get (numCommits - 1 ).getId (), commits .get (numCommits - 2 ).getId ());
213219 assertNotNull (compareResults );
214220 }
215-
221+
216222 @ Test
217223 public void testCreateFileAndDeleteFile () throws GitLabApiException {
218224
@@ -227,4 +233,22 @@ public void testCreateFileAndDeleteFile() throws GitLabApiException {
227233
228234 gitLabApi .getRepositoryFileApi ().deleteFile (TEST_FILEPATH , project .getId (), TEST_BRANCH_NAME , "Testing deleteFile()." );
229235 }
236+
237+ @ Test
238+ public void testProtectBranch () throws GitLabApiException {
239+
240+ Project project = gitLabApi .getProjectApi ().getProject (TEST_NAMESPACE , TEST_PROJECT_NAME );
241+ assertNotNull (project );
242+
243+ Branch branch = gitLabApi .getRepositoryApi ().createBranch (project .getId (), TEST_PROTECT_BRANCH_NAME , "master" );
244+ assertNotNull (branch );
245+
246+ Branch protectedBranch = gitLabApi .getRepositoryApi ().protectBranch (project .getId (), TEST_PROTECT_BRANCH_NAME );
247+ assertNotNull (protectedBranch );
248+ assertTrue (protectedBranch .getProtected ());
249+
250+ Branch unprotectedBranch = gitLabApi .getRepositoryApi ().unprotectBranch (project .getId (), TEST_PROTECT_BRANCH_NAME );
251+ assertNotNull (unprotectedBranch );
252+ assertFalse (unprotectedBranch .getProtected ());
253+ }
230254}
0 commit comments