Skip to content

Commit

Permalink
Fix JENKINS-11859
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Dec 20, 2011
1 parent 7535d6d commit 6543a8d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 62 deletions.
Expand Up @@ -154,10 +154,8 @@ private boolean checkForScheduling(URLTriggerLog log) throws URLTriggerException
String url = getURLValue(entry, executionNode, log);
log.info(String.format("Invoking the url: \n %s", url));
ClientResponse clientResponse = client.resource(url).get(ClientResponse.class);

URLTriggerService urlTriggerService = URLTriggerService.getInstance();
if (urlTriggerService.isSchedulingForURLEntry(clientResponse, entry, log)) {
urlTriggerService.refreshContent(clientResponse, entry);
if (urlTriggerService.isSchedulingAndGetRefresh(clientResponse, entry, log)) {
return true;
}
}
Expand Down Expand Up @@ -272,7 +270,6 @@ private File getLogFile() {
@Override
public void start(BuildableItem project, boolean newInstance) {
super.start(project, newInstance);

URLTriggerService service = URLTriggerService.getInstance();
try {
for (URLTriggerEntry entry : entries) {
Expand Down
Expand Up @@ -22,10 +22,10 @@ public static URLTriggerService getInstance() {
return INSTANCE;
}

public void refreshContent(ClientResponse clientResponse, URLTriggerEntry entry) throws URLTriggerException {
initContent(clientResponse, entry);
}

// public void refreshContent(ClientResponse clientResponse, URLTriggerEntry entry) throws URLTriggerException {
// initContent(clientResponse, entry);
// }
//
public void initContent(ClientResponse clientResponse, URLTriggerEntry entry) throws URLTriggerException {

if (clientResponse == null) {
Expand All @@ -46,58 +46,93 @@ public void initContent(ClientResponse clientResponse, URLTriggerEntry entry) th
if (entry.isInspectingContent()) {
for (final URLTriggerContentType type : entry.getContentTypes()) {
String stringContent = clientResponse.getEntity(String.class);
// String stringContent = null;
// try {
// InputStream entityInputStream = clientResponse.getEntityInputStream();
// if (entityInputStream != null) {
// stringContent = IOUtils.toString(entityInputStream);
// }
// } catch (IOException ioe) {
// throw new URLTriggerException(ioe);
// }
if (stringContent == null) {
throw new URLTriggerException("The URL content is empty.");
}
type.initForContent(stringContent);
}
}

}

public boolean isSchedulingForURLEntry(ClientResponse clientResponse, URLTriggerEntry entry, URLTriggerLog log) throws URLTriggerException {
//Get the url
String url = entry.getUrl();

//Check the status if needed
public boolean isSchedulingAndGetRefresh(ClientResponse clientResponse, URLTriggerEntry entry, URLTriggerLog log) throws URLTriggerException {
//Check scheduling
boolean job2Schedule = false;
if (entry.isCheckStatus()) {
int status = clientResponse.getStatus();
if (status == entry.getStatusCode()) {
log.info(String.format("The returned status matches the expected status: \n %s", url));
return true;
}
job2Schedule = checkStatus(entry, log, clientResponse.getStatus());
}

//Check the last modified date if needed
if (entry.isCheckLastModificationDate()) {
Date lastModificationDate = clientResponse.getLastModified();
if (lastModificationDate != null) {
long newLastModifiedDate = lastModificationDate.getTime();
long entryLastModificationDate = entry.getLastModificationDate();
if (entryLastModificationDate == 0L) {
entry.setLastModificationDate(newLastModifiedDate);
return false;
}
if (entryLastModificationDate != newLastModifiedDate) {
entry.setLastModificationDate(newLastModifiedDate);
log.info("The last modification date has changed.");
return true;
}
job2Schedule = job2Schedule || checkLastModificationDate(entry, log, lastModificationDate);
refreshLatModificationDate(entry, lastModificationDate);
}
if (entry.isInspectingContent()) {
String content = clientResponse.getEntity(String.class);
job2Schedule = job2Schedule || checkContent(entry, log, content);
refreshContent(entry, content);
}

return job2Schedule;
}

private void refreshLatModificationDate(URLTriggerEntry entry, Date lastModificationDate) {
if (lastModificationDate != null) {
entry.setLastModificationDate(lastModificationDate.getTime());
} else {
entry.setLastModificationDate(0);
}
}

private void refreshContent(URLTriggerEntry entry, String content) throws URLTriggerException {

for (final URLTriggerContentType type : entry.getContentTypes()) {
//Refresh the content
type.initForContent(content);
}
}

private boolean checkStatus(URLTriggerEntry entry, URLTriggerLog log, int status) throws URLTriggerException {
if (status == entry.getStatusCode()) {
log.info(String.format("The returned status matches the expected status: \n %s", entry.getUrl()));
return true;
}
return false;
}

private boolean checkLastModificationDate(URLTriggerEntry entry, URLTriggerLog log, Date clientLastModificationDate) throws URLTriggerException {

boolean isTriggering = false;
if (clientLastModificationDate != null) {
long newLastModifiedDateTime = clientLastModificationDate.getTime();
long previousLastModificationDateTime = entry.getLastModificationDate();
if (previousLastModificationDateTime != 0 && previousLastModificationDateTime != newLastModifiedDateTime) {
log.info("The last modification date has changed.");
isTriggering = true;
}
}
return isTriggering;
}


private boolean checkContent(URLTriggerEntry entry, URLTriggerLog log, String content) throws URLTriggerException {

if (content == null) {
return false;
}

//Check the url content
if (entry.isInspectingContent()) {
log.info("Inspecting the content");
for (final URLTriggerContentType type : entry.getContentTypes()) {
String xmlString = clientResponse.getEntity(String.class);
if (xmlString == null) {
throw new URLTriggerException("The URL content is empty.");
}
boolean isTriggered = type.isTriggeringBuildForContent(xmlString, log);
if (isTriggered) {
return true;
}
log.info("Inspecting the content");
for (final URLTriggerContentType type : entry.getContentTypes()) {
boolean isTriggering = type.isTriggeringBuildForContent(content, log);
if (isTriggering) {
return true;
}
}

Expand Down
Expand Up @@ -30,7 +30,8 @@ THE SOFTWARE.

<f:repeatable var="subAction" items="${it.subActionTitle}" noAddButton="true">
<br/>
<b>Inspecting ${subAction.value} content for URL ${subAction.key}</b>
Inspecting ${subAction.value} content for URL ${subAction.key}
<br/>
<br/>
</f:repeatable>

Expand Down
Expand Up @@ -99,14 +99,14 @@ public void isSchedulingForURLEntry_checkStatus() throws URLTriggerException {
when(urlEntryMock.getStatusCode()).thenReturn(status);
when(clientResponseMock.getStatus()).thenReturn(status);

boolean result = urlTriggerService.isSchedulingForURLEntry(clientResponseMock, urlEntryMock, mock(URLTriggerLog.class));
boolean result = urlTriggerService.isSchedulingAndGetRefresh(clientResponseMock, urlEntryMock, mock(URLTriggerLog.class));
Assert.assertTrue(result);

verify(clientResponseMock, times(1)).getStatus();
verify(urlEntryMock, times(1)).isCheckStatus();
verify(urlEntryMock, times(1)).getStatusCode();
verify(urlEntryMock, never()).isInspectingContent();
verify(urlEntryMock, never()).isCheckLastModificationDate();
verify(urlEntryMock, times(1)).isCheckLastModificationDate();
verify(urlEntryMock, times(1)).isInspectingContent();
}


Expand All @@ -128,7 +128,7 @@ private void verifyIsSchedulingForURLEntryCheckLastModificationDate(URLTriggerEn
verify(urlEntryMock, times(1)).isCheckLastModificationDate();
verify(urlEntryMock, times(1)).getLastModificationDate();
verify(urlEntryMock, times(1)).setLastModificationDate(responseDate.getTime());
verify(urlEntryMock, never()).isInspectingContent();
verify(urlEntryMock, times(1)).isInspectingContent();
verify(urlEntryMock, never()).getContentTypes();
}

Expand All @@ -139,7 +139,7 @@ public void isSchedulingForURLEntry_checkSLastModificationDate_1() throws URLTri
Date responseDate = Calendar.getInstance().getTime();

setWhenIsSchedulingForURLEntryCheckLastModificationDate(urlEntryMock, 0L, responseDate);
boolean result = urlTriggerService.isSchedulingForURLEntry(clientResponseMock, urlEntryMock, mock(URLTriggerLog.class));
boolean result = urlTriggerService.isSchedulingAndGetRefresh(clientResponseMock, urlEntryMock, mock(URLTriggerLog.class));
Assert.assertFalse(result);
verifyIsSchedulingForURLEntryCheckLastModificationDate(urlEntryMock, responseDate);
}
Expand All @@ -151,7 +151,7 @@ public void isSchedulingForURLEntry_checkSLastModificationDate_2() throws URLTri
Date responseDate = Calendar.getInstance().getTime();

setWhenIsSchedulingForURLEntryCheckLastModificationDate(urlEntryMock, 1L, responseDate);
boolean result = urlTriggerService.isSchedulingForURLEntry(clientResponseMock, urlEntryMock, mock(URLTriggerLog.class));
boolean result = urlTriggerService.isSchedulingAndGetRefresh(clientResponseMock, urlEntryMock, mock(URLTriggerLog.class));
Assert.assertTrue(result);
verifyIsSchedulingForURLEntryCheckLastModificationDate(urlEntryMock, responseDate);
}
Expand All @@ -165,7 +165,7 @@ public void isSchedulingForURLEntry_checkContent_1() throws URLTriggerException
when(urlEntryMock.isInspectingContent()).thenReturn(true);
when(urlEntryMock.getContentTypes()).thenReturn(new URLTriggerContentType[]{});

boolean result = urlTriggerService.isSchedulingForURLEntry(clientResponseMock, urlEntryMock, mock(URLTriggerLog.class));
boolean result = urlTriggerService.isSchedulingAndGetRefresh(clientResponseMock, urlEntryMock, mock(URLTriggerLog.class));
Assert.assertFalse(result);

verify(urlEntryMock, times(1)).isCheckStatus();
Expand All @@ -177,7 +177,7 @@ public void isSchedulingForURLEntry_checkContent_1() throws URLTriggerException

verify(clientResponseMock, never()).getStatus();
verify(clientResponseMock, never()).getLastModified();
verify(clientResponseMock, never()).getEntity(String.class);
verify(clientResponseMock, times(1)).getEntity(String.class);
}

@Test
Expand All @@ -191,12 +191,8 @@ public void isSchedulingForURLEntry_checkContent_2() throws URLTriggerException
when(urlEntryMock.getContentTypes()).thenReturn(new URLTriggerContentType[]{contentTypeMock});
when(clientResponseMock.getEntity(Matchers.any(Class.class))).thenReturn(null);

try {
urlTriggerService.isSchedulingForURLEntry(clientResponseMock, urlEntryMock, mock(URLTriggerLog.class));
Assert.assertTrue(false);
} catch (URLTriggerException urle) {
Assert.assertTrue(true);
}
urlTriggerService.isSchedulingAndGetRefresh(clientResponseMock, urlEntryMock, mock(URLTriggerLog.class));
Assert.assertFalse(false);

verify(urlEntryMock, times(1)).isCheckStatus();
verify(urlEntryMock, never()).getStatusCode();
Expand All @@ -223,15 +219,15 @@ private void checkContent(boolean isTriggeringBuildContent, boolean expectedResu
when(clientResponseMock.getEntity(Matchers.any(Class.class))).thenReturn("S");
when(contentTypeMock.isTriggeringBuildForContent(Matchers.any(String.class), Matchers.any(URLTriggerLog.class))).thenReturn(isTriggeringBuildContent);

boolean result = urlTriggerService.isSchedulingForURLEntry(clientResponseMock, urlEntryMock, mock(URLTriggerLog.class));
boolean result = urlTriggerService.isSchedulingAndGetRefresh(clientResponseMock, urlEntryMock, mock(URLTriggerLog.class));
Assert.assertEquals(expectedResult, result);

verify(urlEntryMock, times(1)).isCheckStatus();
verify(urlEntryMock, never()).getStatusCode();
verify(urlEntryMock, times(1)).isCheckLastModificationDate();
verify(urlEntryMock, never()).getLastModificationDate();
verify(urlEntryMock, times(1)).isInspectingContent();
verify(urlEntryMock, times(1)).getContentTypes();
verify(urlEntryMock, times(2)).getContentTypes();

verify(clientResponseMock, never()).getStatus();
verify(clientResponseMock, never()).getLastModified();
Expand Down

0 comments on commit 6543a8d

Please sign in to comment.