James Moger
2011-10-27 8e40cd53b6b1579e383bd5e993cb3c35ce4583c4
commit | author | age
831469 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.tests;
17
18 import java.io.IOException;
19 import java.util.Date;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.concurrent.Executors;
23
24 import junit.framework.TestCase;
25
26 import com.gitblit.Constants.AccessRestrictionType;
4aafd4 27 import com.gitblit.Constants.FederationProposalResult;
831469 28 import com.gitblit.Constants.FederationRequest;
JM 29 import com.gitblit.Constants.FederationToken;
30 import com.gitblit.GitBlitServer;
dd9ae7 31 import com.gitblit.models.FederationProposal;
831469 32 import com.gitblit.models.RepositoryModel;
JM 33 import com.gitblit.utils.FederationUtils;
93f0b1 34 import com.gitblit.utils.JsonUtils;
831469 35
JM 36 public class FederationTests extends TestCase {
37
38     int port = 8180;
39
40     int shutdownPort = 8181;
41
42     @Override
43     protected void setUp() throws Exception {
44         // Start a Gitblit instance
45         Executors.newSingleThreadExecutor().execute(new Runnable() {
46             public void run() {
47                 GitBlitServer.main("--httpPort", "" + port, "--httpsPort", "0", "--shutdownPort",
48                         "" + shutdownPort, "--repositoriesFolder",
49                         "\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",
50                         "distrib/users.properties");
51             }
52         });
53
54         // Wait a few seconds for it to be running
55         Thread.sleep(2500);
56     }
57
58     @Override
59     protected void tearDown() throws Exception {
60         // Stop Gitblit
61         GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);
62
63         // Wait a few seconds for it to be running
64         Thread.sleep(2500);
65     }
66
67     public void testProposal() throws Exception {
68         // create dummy repository data
69         Map<String, RepositoryModel> repositories = new HashMap<String, RepositoryModel>();
70         for (int i = 0; i < 5; i++) {
71             RepositoryModel model = new RepositoryModel();
72             model.accessRestriction = AccessRestrictionType.VIEW;
73             model.description = "cloneable repository " + i;
74             model.lastChange = new Date();
75             model.owner = "adminuser";
76             model.name = "repo" + i + ".git";
77             model.size = "5 MB";
78             model.hasCommits = true;
79             repositories.put(model.name, model);
80         }
81
dd9ae7 82         FederationProposal proposal = new FederationProposal("http://testurl", FederationToken.ALL,
JM 83                 "testtoken", repositories);
84
831469 85         // propose federation
4aafd4 86         assertEquals("proposal refused",
JM 87                 FederationUtils.propose("http://localhost:" + port, proposal),
88                 FederationProposalResult.NO_PROPOSALS);
831469 89     }
JM 90
91     public void testPullRepositories() throws Exception {
92         try {
31abc2 93             String url = FederationUtils.asLink("http://localhost:" + port, "testtoken",
JM 94                     FederationRequest.PULL_REPOSITORIES);
95             String json = JsonUtils.retrieveJsonString(url, null, null);
831469 96         } catch (IOException e) {
JM 97             if (!e.getMessage().contains("403")) {
98                 throw e;
99             }
100         }
101     }
102 }