Skip to content

Commit

Permalink
Reorg request mocking for CommunityProjectBranchesLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
GreyTeardrop committed Apr 8, 2024
1 parent 425db80 commit 2c15a21
Showing 1 changed file with 48 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.github.mc1arke.sonarqube.plugin.scanner;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand All @@ -27,6 +28,7 @@
import org.sonar.scanner.scan.branch.BranchInfo;
import org.sonar.scanner.scan.branch.BranchType;
import org.sonar.scanner.scan.branch.ProjectBranches;
import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.client.HttpException;
import org.sonarqube.ws.client.WsResponse;

Expand All @@ -35,14 +37,17 @@
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;

/**
Expand All @@ -51,21 +56,28 @@
public class CommunityRepositoryBranchesLoaderTest {

private final ScannerWsClient scannerWsClient = mock(ScannerWsClient.class);
private final Map<String, WsResponse> scannerWsResponses = new HashMap<>();
private final ExpectedException expectedException = ExpectedException.none();

@Rule
public ExpectedException expectedException() {
return expectedException;
}

@Before
public void setUp() {
when(scannerWsClient.call(any())).thenAnswer(invocation -> {
GetRequest request = invocation.getArgument(0);
return scannerWsResponses.get(request.getPath());
});
}

@Test
public void testEmptyBranchesOnEmptyServerResponse() {
WsResponse mockResponse = mock(WsResponse.class);
when(scannerWsClient.call(any())).thenReturn(mockResponse);

StringReader stringReader = new StringReader(
GsonHelper.create().toJson(new CommunityProjectBranchesLoader.BranchesResponse(new ArrayList<>())));
when(mockResponse.contentReader()).thenReturn(stringReader);
mockResponse(
"/api/project_branches/list?project=projectKey",
new CommunityProjectBranchesLoader.BranchesResponse(new ArrayList<>())
);

CommunityProjectBranchesLoader testCase = new CommunityProjectBranchesLoader(scannerWsClient);
ProjectBranches response = testCase.load("projectKey");
Expand All @@ -74,17 +86,14 @@ public void testEmptyBranchesOnEmptyServerResponse() {

@Test
public void testAllBranchesFromNonEmptyServerResponse() {
WsResponse mockResponse = mock(WsResponse.class);
when(scannerWsClient.call(any())).thenReturn(mockResponse);

List<BranchInfo> infos = new ArrayList<>();
for (int i = 0; i < 10; i++) {
infos.add(new BranchInfo("key" + i, BranchType.BRANCH, i == 1, "target" + i));
}

StringReader stringReader = new StringReader(
GsonHelper.create().toJson(new CommunityProjectBranchesLoader.BranchesResponse(infos)));
when(mockResponse.contentReader()).thenReturn(stringReader);
mockResponse(
"/api/project_branches/list?project=key",
new CommunityProjectBranchesLoader.BranchesResponse(infos)
);

CommunityProjectBranchesLoader testCase = new CommunityProjectBranchesLoader(scannerWsClient);
ProjectBranches response = testCase.load("key");
Expand All @@ -101,39 +110,34 @@ public void testAllBranchesFromNonEmptyServerResponse() {

@Test
public void testMessageExceptionOnIOException() {
WsResponse mockResponse = mock(WsResponse.class);
when(scannerWsClient.call(any())).thenReturn(mockResponse);

Reader mockReader = new BufferedReader(new StringReader(
mockResponseWithReader(
"/api/project_branches/list?project=project",
new BufferedReader(new StringReader(
GsonHelper.create().toJson(new CommunityProjectBranchesLoader.BranchesResponse(new ArrayList<>())))) {
public void close() throws IOException {
throw new IOException("Dummy IO Exception");
public void close() throws IOException {
throw new IOException("Dummy IO Exception");
}
}
};
when(mockResponse.contentReader()).thenReturn(mockReader);
);

expectedException.expectMessage("Could not load branches from server");
expectedException.expect(MessageException.class);

CommunityProjectBranchesLoader testCase = new CommunityProjectBranchesLoader(scannerWsClient);
testCase.load("project");


}


@Test
public void testErrorOnNon404HttpResponse() {
WsResponse mockResponse = mock(WsResponse.class);
when(scannerWsClient.call(any())).thenReturn(mockResponse);

Reader mockReader = new BufferedReader(new StringReader(
mockResponseWithReader(
"/api/project_branches/list?project=project",
new BufferedReader(new StringReader(
GsonHelper.create().toJson(new CommunityProjectBranchesLoader.BranchesResponse(new ArrayList<>())))) {
public void close() {
throw new HttpException("url", 12, "content");
public void close() throws IOException {
throw new HttpException("url", 12, "content");
}
}
};
when(mockResponse.contentReader()).thenReturn(mockReader);
);

expectedException.expectMessage("Could not load branches from server");
expectedException.expect(MessageException.class);
Expand All @@ -142,12 +146,23 @@ public void close() {
testCase.load("project");
}


@Test
public void testEmptyListOn404HttpResponse() {
reset(scannerWsClient);
when(scannerWsClient.call(any())).thenThrow(new HttpException("url", 404, "content"));

CommunityProjectBranchesLoader testCase = new CommunityProjectBranchesLoader(scannerWsClient);
assertTrue(testCase.load("project").isEmpty());
}

private void mockResponse(String requestPath, Object response) {
mockResponseWithReader(requestPath, new StringReader(GsonHelper.create().toJson(response)));
}

private void mockResponseWithReader(String requestPath, Reader responseReader) {
WsResponse mockResponse = mock(WsResponse.class);
when(mockResponse.contentReader()).thenReturn(responseReader);
scannerWsResponses.put(requestPath, mockResponse);
}

}

0 comments on commit 2c15a21

Please sign in to comment.