Java Code Examples for com.squareup.okhttp.mockwebserver.MockWebServer#shutdown()

The following examples show how to use com.squareup.okhttp.mockwebserver.MockWebServer#shutdown() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProjectApiMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 2
Source File: DefaultReviewersApiMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 3
Source File: DefaultReviewersApiMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 4
Source File: CommentsApiMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 5
Source File: BranchApiMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 6
Source File: ProjectApiMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 7
Source File: CommitsApiMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 8
Source File: SyncApiMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 9
Source File: CommentsApiMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 10
Source File: BranchApiMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 11
Source File: BranchApiMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 12
Source File: BranchApiMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 13
Source File: BranchApiMockTest.java    From bitbucket-rest with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 14
Source File: SubscriptionTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@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();
}
 
Example 15
Source File: FileApiMockTest.java    From bitbucket-rest with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 16
Source File: BranchApiMockTest.java    From bitbucket-rest with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 17
Source File: SubscriptionsTest.java    From actioncable-client-java with MIT License 5 votes vote down vote up
@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();
}
 
Example 18
Source File: FileApiMockTest.java    From bitbucket-rest with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 19
Source File: FileApiMockTest.java    From bitbucket-rest with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 20
Source File: BaseAWSEC2ApiMockTest.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
@AfterMethod(alwaysRun = true)
public void stop() throws IOException {
   for (MockWebServer server : regionToServers.values()) {
      server.shutdown();
   }
}