下面列出了com.squareup.okhttp.mockwebserver.MockWebServer#shutdown ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testGetBranchModelConfigurationOnError() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(payloadFromResource("/branch-model-configuration-error.json")).setResponseCode(404));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {
final BranchModelConfiguration configuration = baseApi.branchApi().getModelConfiguration(projectKey, repoKey);
assertThat(configuration).isNotNull();
assertThat(configuration.errors()).isNotEmpty();
assertThat(configuration.production()).isNull();
assertThat(configuration.development()).isNull();
assertSent(server, localGetMethod, localRestPath + BitbucketApiMetadata.API_VERSION
+ localProjectsPath + projectKey + localReposPath + repoKey + branchModelPath);
} finally {
server.shutdown();
}
}
public void testGetBranchModelConfiguration() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(payloadFromResource("/branch-model-configuration.json")).setResponseCode(200));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {
final BranchModelConfiguration configuration = baseApi.branchApi().getModelConfiguration(projectKey, repoKey);
assertThat(configuration).isNotNull();
assertThat(configuration.errors().isEmpty()).isTrue();
assertThat(configuration.types().size() > 0).isTrue();
assertThat(configuration.development().refId().equals("refs/heads/master")).isTrue();
assertThat(configuration.production()).isNull();
assertSent(server, localGetMethod, localRestPath + BitbucketApiMetadata.API_VERSION
+ localProjectsPath + projectKey + localReposPath + repoKey + branchModelPath);
} finally {
server.shutdown();
}
}
public void testDeleteBranch() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setResponseCode(204));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {
final RequestStatus success = baseApi.branchApi().delete(projectKey, repoKey, "refs/heads/some-branch-name");
assertThat(success).isNotNull();
assertThat(success.value()).isTrue();
assertThat(success.errors()).isEmpty();
assertSent(server, localDeleteMethod, localRestPath + BitbucketApiMetadata.API_VERSION
+ localProjectsPath + projectKey + localReposPath + repoKey + "/branches");
} finally {
server.shutdown();
}
}
public void testUpdateBranchesPermissions() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setResponseCode(204));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {
final List<String> groupPermission = new ArrayList<>();
groupPermission.add("Test12354");
final List<Long> listAccessKey = new ArrayList<>();
listAccessKey.add(123L);
final List<BranchRestriction> listBranchPermission = new ArrayList<>();
listBranchPermission.add(BranchRestriction.createWithId(839L, BranchRestrictionEnumType.FAST_FORWARD_ONLY,
Matcher.create(Matcher.MatcherId.RELEASE, true), new ArrayList<User>(), groupPermission,
listAccessKey));
final RequestStatus success = baseApi.branchApi().createBranchRestriction(projectKey, repoKey, listBranchPermission);
assertThat(success).isNotNull();
assertThat(success.value()).isTrue();
assertThat(success.errors()).isEmpty();
assertSent(server, "POST", branchPermissionsPath
+ localProjectsPath + projectKey + localReposPath + repoKey + "/restrictions");
} finally {
server.shutdown();
}
}
public void testGetComment() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(payloadFromResource("/comments.json")).setResponseCode(200));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {
final Comments pr = baseApi.commentsApi().get(projectKey, repoKey, 101, 1);
assertThat(pr).isNotNull();
assertThat(pr.errors()).isEmpty();
assertThat(pr.text()).isEqualTo(measuredReplyKeyword);
assertThat(pr.links()).isNull();
assertSent(server, getMethod, restApiPath + BitbucketApiMetadata.API_VERSION
+ "/projects/PRJ/repos/my-repo/pull-requests/101/comments/1");
} finally {
server.shutdown();
}
}
public void testCreateProject() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse()
.setBody(payloadFromResource("/project.json"))
.setResponseCode(201));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {
final String projectKey = "HELLO";
final CreateProject createProject = CreateProject.create(projectKey, null, null, null);
final Project project = baseApi.projectApi().create(createProject);
assertThat(project).isNotNull();
assertThat(project.errors()).isEmpty();
assertThat(project.key()).isEqualToIgnoringCase(projectKey);
assertThat(project.name()).isEqualToIgnoringCase(projectKey);
assertThat(project.links()).isNotNull();
assertSent(server, "POST", restBasePath + BitbucketApiMetadata.API_VERSION + localPath);
} finally {
server.shutdown();
}
}
public void testEnabledOnError() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(payloadFromResource("/errors.json")).setResponseCode(400));
try (final BitbucketApi baseApi = api(server.getUrl("/"));) {
final SyncStatus status = baseApi.syncApi().enable(projectKey, repoKey, true);
assertThat(status.available()).isFalse();
assertThat(status.enabled()).isFalse();
assertThat(status.divergedRefs()).isEmpty();
assertThat(status.errors()).isNotEmpty();
assertSent(server, postMethod, restApiPath + BitbucketApiMetadata.API_VERSION + syncPath);
} finally {
server.shutdown();
}
}
public void testGetPullRequestChanges() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse()
.setBody(payloadFromResource("/pull-request-changes.json"))
.setResponseCode(200));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {
final ChangePage changePage = baseApi.commitsApi().listChanges(projectKey, repoKey, commitHash, 12, null);
assertThat(changePage).isNotNull();
assertThat(changePage.errors()).isEmpty();
assertThat(changePage.values()).hasSize(1);
final Map<String, ?> queryParams = ImmutableMap.of(limitKeyword, 12);
assertSent(server, getMethod, restApiPath + BitbucketApiMetadata.API_VERSION
+ "/projects/PRJ/repos/myrepo/commits/abcdef0123abcdef4567abcdef8987abcdef6543/changes", queryParams);
} finally {
server.shutdown();
}
}
public void testGetProjectList() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse()
.setBody(payloadFromResource("/project-page-full.json"))
.setResponseCode(200));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {
final ProjectPage projectPage = baseApi.projectApi().list(null, null, null, null);
assertThat(projectPage).isNotNull();
assertThat(projectPage.errors()).isEmpty();
assertThat(projectPage.size()).isLessThanOrEqualTo(projectPage.limit());
assertThat(projectPage.start()).isEqualTo(0);
assertThat(projectPage.isLastPage()).isTrue();
assertThat(projectPage.values()).hasSize(projectPage.size());
assertThat(projectPage.values()).hasOnlyElementsOfType(Project.class);
assertSent(server, localMethod, restBasePath + BitbucketApiMetadata.API_VERSION + localPath);
} finally {
server.shutdown();
}
}
public void testDeleteBranchModelConfigurationOnError() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(payloadFromResource("/repository-not-exist.json")).setResponseCode(404));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {
final RequestStatus success = baseApi.branchApi().deleteModelConfiguration(projectKey, repoKey);
assertThat(success).isNotNull();
assertThat(success.value()).isFalse();
assertThat(success.errors()).isNotEmpty();
assertSent(server, localDeleteMethod, localRestPath + BitbucketApiMetadata.API_VERSION
+ localProjectsPath + projectKey + localReposPath + repoKey + branchModelPath);
} finally {
server.shutdown();
}
}
public void testGetCommentOnError() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(payloadFromResource("/commit-error.json")).setResponseCode(404));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {
final Comments pr = baseApi.commentsApi().get(projectKey, repoKey, 101, 1);
assertThat(pr).isNotNull();
assertThat(pr.errors()).isNotEmpty();
assertSent(server, getMethod, restApiPath + BitbucketApiMetadata.API_VERSION
+ "/projects/PRJ/repos/my-repo/pull-requests/101/comments/1");
} finally {
server.shutdown();
}
}
public void testListConditions() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(payloadFromResource("/default-reviwers-list.json")).setResponseCode(200));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {
final List<Condition> conditions = baseApi.defaultReviewersApi().listConditions(projectKey, repoKey);
assertThat(conditions).isNotNull();
assertThat(conditions.size()).isEqualTo(3);
assertSent(server, "GET", defaultReviewersPath + BitbucketApiMetadata.API_VERSION
+ projectsPath + projectKey + reposPath + repoKey + "/conditions");
} finally {
server.shutdown();
}
}
public void testUpdateCondition() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(payloadFromResource("/default-reviwers-create.json")).setResponseCode(200));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {
final Long requiredApprover = 1L;
final Matcher matcherSrc = Matcher.create(Matcher.MatcherId.ANY, true);
final Matcher matcherDst = Matcher.create(Matcher.MatcherId.ANY, true);
final List<User> listUser = new ArrayList<>();
listUser.add(User.create(projectKey, testEmail, 1, projectKey, true, projectKey, normalKeyword));
final CreateCondition condition = CreateCondition.create(10L, matcherSrc, matcherDst, listUser, requiredApprover);
final Condition returnCondition = baseApi.defaultReviewersApi().updateCondition(projectKey, repoKey, 10L, condition);
assertThat(returnCondition).isNotNull();
assertThat(returnCondition.errors()).isEmpty();
assertThat(returnCondition.id()).isEqualTo(3L);
assertSent(server, "PUT", defaultReviewersPath + BitbucketApiMetadata.API_VERSION
+ projectsPath + projectKey + reposPath + repoKey + "/condition/10");
} finally {
server.shutdown();
}
}
@Test(timeout = TIMEOUT)
public void performWithDataByDefaultInterface() throws URISyntaxException, InterruptedException, IOException {
final BlockingQueue<String> events = new LinkedBlockingQueue<String>();
final MockWebServer mockWebServer = new MockWebServer();
final MockResponse response = new MockResponse();
response.withWebSocketUpgrade(new DefaultWebSocketListener() {
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
events.offer(payload.readUtf8());
payload.close();
}
});
mockWebServer.enqueue(response);
mockWebServer.start();
final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
final Subscription subscription = consumer.getSubscriptions().create(new Channel("CommentsChannel"));
consumer.connect();
events.take(); // { command: subscribe }
final JsonObject data = new JsonObject();
data.addProperty("foo", "bar");
subscription.perform("follow", data);
final JsonObject expected = new JsonObject();
expected.addProperty("command", "message");
expected.addProperty("identifier", subscription.getIdentifier());
expected.addProperty("data", data.toString());
assertThat(events.take(), is(expected.toString()));
mockWebServer.shutdown();
}
public void testLastModifiedBadRequest() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(errorResponseBody).setResponseCode(400));
try (final BitbucketApi baseApi = api(server.getUrl("/"));) {
final LastModified summary = baseApi.fileApi().lastModified(projectKey, repoKey, null, branch);
assertThat(summary).isNotNull();
assertThat(summary.files().isEmpty()).isTrue();
assertThat(summary.errors().isEmpty()).isFalse();
assertSent(server, getMethod, lastModifiedPath, Collections.singletonMap("at", branch));
} finally {
server.shutdown();
}
}
public void testLastModifiedAtPath() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(lastModifiedResposnseBody).setResponseCode(200));
try (final BitbucketApi baseApi = api(server.getUrl("/"));) {
final LastModified summary = baseApi.fileApi().lastModified(projectKey, repoKey, directoryPath, branch);
assertThat(summary).isNotNull();
assertThat(summary.latestCommit()).isNotNull();
assertThat(summary.files().isEmpty()).isFalse();
assertThat(summary.errors().isEmpty()).isTrue();
assertSent(server, getMethod, lastModifiedPath + "/" + directoryPath, Collections.singletonMap("at", branch));
} finally {
server.shutdown();
}
}
public void testGetBranchInfoOnError() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(payloadFromResource("/branch-list-error.json")).setResponseCode(404));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {
final BranchPage branchPage = baseApi.branchApi().info(projectKey, repoKey, commitId);
assertThat(branchPage).isNotNull();
assertThat(branchPage.errors()).isNotEmpty();
assertSent(server, localGetMethod, localRestPath + BitbucketApiMetadata.API_VERSION
+ localProjectsPath + projectKey + localReposPath + repoKey + localInfoPath + "/" + commitId);
} finally {
server.shutdown();
}
}
public void testUpdateContentBadRequest() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(errorResponseBody).setResponseCode(400));
try (final BitbucketApi baseApi = api(server.getUrl("/"));) {
final Commit commit = baseApi.fileApi().updateContent(projectKey, repoKey, filePath, branch, content, null, null, null);
assertThat(commit).isNotNull();
assertThat(commit.errors().isEmpty()).isFalse();
assertSent(server, putMethod, browsePath + filePath);
} finally {
server.shutdown();
}
}
@Test(timeout = TIMEOUT)
public void removeWhenIdentifierIsUnique() throws IOException, InterruptedException {
final BlockingQueue<String> events = new LinkedBlockingQueue<String>();
final MockWebServer mockWebServer = new MockWebServer();
final MockResponse response = new MockResponse();
response.withWebSocketUpgrade(new DefaultWebSocketListener() {
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
events.offer("onMessage:" + payload.readUtf8());
payload.close();
}
});
mockWebServer.enqueue(response);
final Consumer consumer = new Consumer(mockWebServer.url("/").uri());
final Subscriptions subscriptions = consumer.getSubscriptions();
final Subscription subscription1 = subscriptions.create(new Channel("CommentsChannel"));
final Subscription subscription2 = subscriptions.create(new Channel("NotificationChannel"));
consumer.connect();
events.take(); // WebSocketListener#onMessage (subscribe)
events.take(); // WebSocketListener#onMessage (subscribe)
subscriptions.remove(subscription1);
assertThat(subscriptions.contains(subscription1), is(false));
assertThat(subscriptions.contains(subscription2), is(true));
assertThat(events.take(), is("onMessage:" + Command.unsubscribe(subscription1.getIdentifier()).toJson()));
mockWebServer.shutdown();
}
@AfterMethod(alwaysRun = true)
public void stop() throws IOException {
for (MockWebServer server : regionToServers.values()) {
server.shutdown();
}
}