|
| 1 | +package com.offbytwo.jenkins.model; |
| 2 | + |
| 3 | +import com.offbytwo.jenkins.BaseUnitTest; |
| 4 | +import com.offbytwo.jenkins.client.JenkinsHttpClient; |
| 5 | +import com.offbytwo.jenkins.helper.BuildConsoleStreamListener; |
| 6 | +import org.apache.http.HttpResponse; |
| 7 | +import org.apache.http.NameValuePair; |
| 8 | +import org.apache.http.concurrent.BasicFuture; |
| 9 | +import org.apache.http.entity.StringEntity; |
| 10 | +import org.apache.http.message.BasicHeader; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.concurrent.CompletableFuture; |
| 16 | +import java.util.concurrent.Future; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.fail; |
| 20 | +import static org.mockito.BDDMockito.given; |
| 21 | +import static org.mockito.Matchers.anyBoolean; |
| 22 | +import static org.mockito.Matchers.anyListOf; |
| 23 | +import static org.mockito.Matchers.anyString; |
| 24 | +import static org.mockito.Mockito.mock; |
| 25 | + |
| 26 | +/** |
| 27 | + */ |
| 28 | +public class BuildWithDetailsTest extends BaseUnitTest { |
| 29 | + |
| 30 | + private JenkinsHttpClient client; |
| 31 | + private BuildWithDetails buildWithDetails; |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setUp() { |
| 35 | + client = mock(JenkinsHttpClient.class); |
| 36 | + buildWithDetails = givenBuild(); |
| 37 | + } |
| 38 | + |
| 39 | + private BuildWithDetails givenBuild() { |
| 40 | + BuildWithDetails buildWithDetails = new BuildWithDetails(); |
| 41 | + buildWithDetails.setClient(client); |
| 42 | + return buildWithDetails; |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void getBuildLogWithBuffer() { |
| 47 | + try { |
| 48 | + HttpResponse response = mock(HttpResponse.class); |
| 49 | + String text = "test test test"; |
| 50 | + int textLength = text.length(); |
| 51 | + given(response.getEntity()).willReturn(new StringEntity(text)); |
| 52 | + BasicHeader moreDataHeader = new BasicHeader(BuildWithDetails.MORE_DATA_HEADER, "false"); |
| 53 | + BasicHeader textSizeHeader = new BasicHeader(BuildWithDetails.TEXT_SIZE_HEADER, Integer.toString(textLength)); |
| 54 | + given(response.getFirstHeader(BuildWithDetails.MORE_DATA_HEADER)).willReturn(moreDataHeader); |
| 55 | + given(response.getFirstHeader(BuildWithDetails.TEXT_SIZE_HEADER)).willReturn(textSizeHeader); |
| 56 | + given(client.post_form_with_result(anyString(),anyListOf(NameValuePair.class),anyBoolean())).willReturn(response); |
| 57 | + ConsoleLog consoleOutputText = buildWithDetails.getConsoleOutputText(500); |
| 58 | + assertThat(consoleOutputText.getConsoleLog()).isEqualTo(text); |
| 59 | + assertThat(consoleOutputText.getCurrentBufferSize()).isEqualTo(textLength); |
| 60 | + assertThat(consoleOutputText.getHasMoreData()).isFalse(); |
| 61 | + } catch (IOException e) { |
| 62 | + fail("Should not return exception",e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + @Test |
| 68 | + public void poolBuildLog() throws InterruptedException { |
| 69 | + try { |
| 70 | + HttpResponse response = mock(HttpResponse.class); |
| 71 | + final String text = "test test test"; |
| 72 | + int textLength = text.length(); |
| 73 | + given(response.getEntity()).willReturn(new StringEntity(text)); |
| 74 | + BasicHeader moreDataHeader = new BasicHeader(BuildWithDetails.MORE_DATA_HEADER, "false"); |
| 75 | + BasicHeader textSizeHeader = new BasicHeader(BuildWithDetails.TEXT_SIZE_HEADER, Integer.toString(textLength)); |
| 76 | + given(response.getFirstHeader(BuildWithDetails.MORE_DATA_HEADER)).willReturn(moreDataHeader); |
| 77 | + given(response.getFirstHeader(BuildWithDetails.TEXT_SIZE_HEADER)).willReturn(textSizeHeader); |
| 78 | + given(client.post_form_with_result(anyString(),anyListOf(NameValuePair.class),anyBoolean())).willReturn(response); |
| 79 | + buildWithDetails.streamConsoleOutput(new BuildConsoleStreamListener() { |
| 80 | + @Override |
| 81 | + public void onData(String newLogChunk) { |
| 82 | + assertThat(newLogChunk).isEqualTo(text); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void onError(Exception error) { |
| 87 | + fail("No error expected",error); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void finished() { |
| 92 | + } |
| 93 | + },1,2); |
| 94 | + |
| 95 | + |
| 96 | + } catch (IOException e) { |
| 97 | + fail("Should not return exception",e); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments