Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Add Update Center Service Test #105

Merged
merged 9 commits into from Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -83,6 +83,12 @@
<artifactId>custom-war-packager-lib</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>1.58</version>
<scope>test</scope>
</dependency>
</dependencies>

<licenses>
Expand Down
Expand Up @@ -33,29 +33,33 @@ public JSONObject downloadUpdateCenterJSON() throws IOException {
*/
LOGGER.info("Update flag is " + updateFlag);
LOGGER.info("Update Center Path " + updateCenterPath);

if(updateFlag == 0) {
downloadJSON(UPDATE_CENTER_URL);
updateFlag = 1;
} else {
responseString = readFileAsString(updateCenterPath);
}
LOGGER.info("Returning Response" + responseString);
return util.convertPayloadToJSON(responseString);
}

public JSONObject downloadJSON (final String updateCenterURL) throws IOException {
final File updateCenterFile = File.createTempFile("update-center", ".json");
updateCenterPath = updateCenterFile.getPath();
LOGGER.info("Creating a new file" + updateCenterFile.getPath());
LOGGER.info("Executing Request at " + UPDATE_CENTER_URL);
LOGGER.info("Executing Request at " + updateCenterURL);
try (CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(new HttpGet(UPDATE_CENTER_URL)))
CloseableHttpResponse response = httpClient.execute(new HttpGet(UPDATE_CENTER_URL)))
{
responseString = EntityUtils.toString(response.getEntity());
responseString = EntityUtils.toString(response.getEntity());
}
final byte[] buf = responseString.getBytes(StandardCharsets.UTF_8);

Files.write(updateCenterFile.toPath(), buf);
updateFlag = 1;


} else {
responseString = readFileAsString(updateCenterPath);
}
LOGGER.info("Returning Response");
return util.convertPayloadToJSON(responseString);
}
LOGGER.info("Returning Response");
// Mark the file for deletion once the JVM shuts down
updateCenterFile.deleteOnExit();
sladyn98 marked this conversation as resolved.
Show resolved Hide resolved
return util.convertPayloadToJSON(responseString);
}

private static String readFileAsString(final String fileName) throws IOException {
String data;
Expand Down
@@ -0,0 +1,45 @@
package com.org.jenkins.custom.jenkins.distribution.service.services;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.org.jenkins.custom.jenkins.distribution.service.util.Util;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class UpdateCenterServiceTest {

/**
* A rule which provides a mock server.
*/
@Rule
public WireMockRule wireMockRule = new WireMockRule();

private Util util = new Util();

@Test
public void checkUpdateCenterResponse() {
try {
// Get update center mock body
String updateCenterBody = util.readStringFromFile("updateCenter.json");
wireMockRule.stubFor(get(urlPathMatching("/getUpdateCenter"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(updateCenterBody)));
JSONObject updateCenterJSON = new UpdateCenterService().downloadJSON("http://localhost:8080/getUpdateCenter");
// Check if the returned JSON is not null
assertNotNull(updateCenterJSON);
martinda marked this conversation as resolved.
Show resolved Hide resolved
Assert.assertEquals(updateCenterJSON.toString(), updateCenterBody);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
}