diff --git a/src/main/java/fi/helsinki/cs/tmc/core/TmcCore.java b/src/main/java/fi/helsinki/cs/tmc/core/TmcCore.java index 10d6175c..54131265 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/TmcCore.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/TmcCore.java @@ -314,7 +314,7 @@ public ListenableFuture> getNewAndUpdatedExercises(Course course) */ public ListenableFuture sendFeedback(Map answers, URI url) throws TmcCoreException { - SendFeedback feedback = new SendFeedback(settings, answers, url.toString()); + SendFeedback feedback = new SendFeedback(settings, answers, url); return threadPool.submit(feedback); } diff --git a/src/main/java/fi/helsinki/cs/tmc/core/commands/GetCourse.java b/src/main/java/fi/helsinki/cs/tmc/core/commands/GetCourse.java index e19de956..58637f9e 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/commands/GetCourse.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/commands/GetCourse.java @@ -19,7 +19,7 @@ public class GetCourse extends Command { private TmcApi tmcApi; - private String url; + private URI url; /** * Constructs a new get course command with {@code settings} for fetching course details for @@ -40,7 +40,7 @@ public GetCourse(TmcSettings settings, URI courseUri) { super(settings); this.tmcApi = new TmcApi(settings); - this.url = courseUri.toString(); + this.url = courseUri; } /** @@ -51,7 +51,7 @@ public Course call() throws TmcCoreException, URISyntaxException { validate(this.settings.getUsername(), "Username must be set!"); validate(this.settings.getPassword(), "Password must be set!"); - String urlWithApiVersion = new UrlHelper(settings).withParams(this.url); + URI urlWithApiVersion = new UrlHelper(settings).withParams(this.url); Optional course; try { course = tmcApi.getCourse(urlWithApiVersion); @@ -61,13 +61,13 @@ public Course call() throws TmcCoreException, URISyntaxException { if (!course.isPresent()) { throw new TmcCoreException( - "Attempted to fetch nonexistent course " + urlWithApiVersion); + "Attempted to fetch nonexistent course " + urlWithApiVersion); } return course.get(); } - private String pollServerForCourseUrl(String courseName) throws TmcCoreException { + private URI pollServerForCourseUrl(String courseName) throws TmcCoreException { List courses = null; try { courses = tmcApi.getCourses(); diff --git a/src/main/java/fi/helsinki/cs/tmc/core/commands/PasteWithComment.java b/src/main/java/fi/helsinki/cs/tmc/core/commands/PasteWithComment.java index 6b5c9f04..a95dbc98 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/commands/PasteWithComment.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/commands/PasteWithComment.java @@ -57,7 +57,7 @@ public URI call() Optional currentCourse = settings.getCurrentCourse(); if (currentCourse.isPresent()) { - return URI.create(submitter.submitPasteWithComment(this.path, this.comment)); + return submitter.submitPasteWithComment(this.path, this.comment); } else { throw new TmcCoreException("Unable to determine course"); } diff --git a/src/main/java/fi/helsinki/cs/tmc/core/commands/RequestCodeReview.java b/src/main/java/fi/helsinki/cs/tmc/core/commands/RequestCodeReview.java index 134d4939..3c04e769 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/commands/RequestCodeReview.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/commands/RequestCodeReview.java @@ -56,7 +56,7 @@ public URI call() Optional currentCourse = settings.getCurrentCourse(); if (currentCourse.isPresent()) { - return URI.create(submitter.submitWithCodeReviewRequest(this.path, this.comment)); + return submitter.submitWithCodeReviewRequest(this.path, this.comment); } else { throw new TmcCoreException("Cannot request a code review. Unable to determine course."); } diff --git a/src/main/java/fi/helsinki/cs/tmc/core/commands/SendFeedback.java b/src/main/java/fi/helsinki/cs/tmc/core/commands/SendFeedback.java index 019cf476..7bef0ac0 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/commands/SendFeedback.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/commands/SendFeedback.java @@ -6,6 +6,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; +import java.net.URI; import java.util.Map; import java.util.Map.Entry; @@ -16,13 +17,13 @@ public class SendFeedback extends Command { private Map answers; - private String url; + private URI url; /** * Constructs a send feedback command with {@code settings} for sending {@code answers} to * {@code url}. */ - public SendFeedback(TmcSettings settings, Map answers, String url) { + public SendFeedback(TmcSettings settings, Map answers, URI url) { super(settings); this.answers = answers; this.url = url; diff --git a/src/main/java/fi/helsinki/cs/tmc/core/commands/Submit.java b/src/main/java/fi/helsinki/cs/tmc/core/commands/Submit.java index f68c0d96..a116200c 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/commands/Submit.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/commands/Submit.java @@ -10,6 +10,7 @@ import fi.helsinki.cs.tmc.langs.domain.NoLanguagePluginFoundException; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import java.text.ParseException; @@ -77,11 +78,11 @@ public SubmissionResult call() assertHasRequiredData(); if (observer != null) { - String returnUrl = submitter.submit(this.path, observer); + URI returnUrl = submitter.submit(this.path, observer); return submissionPoller.getSubmissionResult(returnUrl, observer); } - String returnUrl = submitter.submit(this.path); + URI returnUrl = submitter.submit(this.path); return submissionPoller.getSubmissionResult(returnUrl); } } diff --git a/src/main/java/fi/helsinki/cs/tmc/core/commands/VerifyCredentials.java b/src/main/java/fi/helsinki/cs/tmc/core/commands/VerifyCredentials.java index 833475f8..cb1ef9e1 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/commands/VerifyCredentials.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/commands/VerifyCredentials.java @@ -5,6 +5,7 @@ import fi.helsinki.cs.tmc.core.exceptions.TmcCoreException; import java.io.IOException; +import java.net.URI; /** * A {@link Command} for authenticating the user details saved in {@link TmcSettings}. @@ -49,7 +50,9 @@ public Boolean call() throws TmcCoreException, IOException { int response = communicator - .makeGetRequest(settings.getServerAddress() + TMC_SERVER_ROUTE, auth) + .makeGetRequest( + URI.create(settings.getServerAddress() + TMC_SERVER_ROUTE), + auth) .getStatusCode(); return (response >= HTTP_SUCCESS_RANGE_MIN && response <= HTTP_SUCCESS_RANGE_MAX); diff --git a/src/main/java/fi/helsinki/cs/tmc/core/communication/ExerciseDownloader.java b/src/main/java/fi/helsinki/cs/tmc/core/communication/ExerciseDownloader.java index 29f88561..c7acc4d3 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/communication/ExerciseDownloader.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/communication/ExerciseDownloader.java @@ -14,6 +14,7 @@ import java.io.File; import java.io.IOException; +import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -51,7 +52,7 @@ public ExerciseDownloader(UrlCommunicator urlCommunicator, TmcApi tmcApi) { * @param courseUrl course url * @return info about downloading. */ - public Optional> downloadExercises(String courseUrl) throws IOException { + public Optional> downloadExercises(URI courseUrl) throws IOException { List exercises = tmcApi.getExercises(courseUrl); if (exercises.isEmpty()) { return Optional.absent(); @@ -194,7 +195,7 @@ public double getPercents(int exCount, int exercisesSize) { * @param zipUrl url which will be downloaded * @param path where to download */ - private void downloadExerciseZip(String zipUrl, String path) { + private void downloadExerciseZip(URI zipUrl, String path) { File file = new File(path); urlCommunicator.downloadToFile(zipUrl, file); } diff --git a/src/main/java/fi/helsinki/cs/tmc/core/communication/ExerciseSubmitter.java b/src/main/java/fi/helsinki/cs/tmc/core/communication/ExerciseSubmitter.java index 4a011027..5d51184d 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/communication/ExerciseSubmitter.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/communication/ExerciseSubmitter.java @@ -8,9 +8,6 @@ import fi.helsinki.cs.tmc.core.exceptions.TmcCoreException; import fi.helsinki.cs.tmc.core.zipping.ProjectRootFinder; import fi.helsinki.cs.tmc.core.zipping.RootFinder; -import fi.helsinki.cs.tmc.langs.io.EverythingIsStudentFileStudentFilePolicy; -import fi.helsinki.cs.tmc.langs.io.zip.StudentFileAwareZipper; -import fi.helsinki.cs.tmc.langs.io.zip.Zipper; import fi.helsinki.cs.tmc.langs.util.TaskExecutor; import fi.helsinki.cs.tmc.langs.util.TaskExecutorImpl; @@ -19,6 +16,7 @@ import java.io.File; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Path; import java.nio.file.Paths; @@ -94,10 +92,10 @@ private boolean deadlineGone(Date current, Date deadline) { * Submits folder of exercise to TMC. Finds it from current directory. * * @param currentPath path from which this was called. - * @return String with url from which to get results or null if exercise was not found. + * @return URI from which to get results or null if exercise was not found. * @throws IOException if zip creation fails */ - public String submit(String currentPath) + public URI submit(String currentPath) throws IOException, ParseException, ExpiredException, IllegalArgumentException, TmcCoreException, URISyntaxException, NoLanguagePluginFoundException { Exercise currentExercise = initExercise(currentPath); @@ -109,10 +107,10 @@ public String submit(String currentPath) * * @param currentPath path from which this was called. * @param observer {@link ProgressObserver} that is informed of the submission progress - * @return String with url from which to get results or null if exercise was not found. + * @return URI from which to get results or null if exercise was not found. * @throws IOException if zip creation fails */ - public String submit(String currentPath, ProgressObserver observer) + public URI submit(String currentPath, ProgressObserver observer) throws ParseException, ExpiredException, IllegalArgumentException, IOException, TmcCoreException, URISyntaxException, NoLanguagePluginFoundException { Exercise currentExercise = initExercise(currentPath); @@ -124,10 +122,10 @@ public String submit(String currentPath, ProgressObserver observer) * paste. * * @param currentPath path from which this was called. - * @return String with url from which to get paste URL or null if exercise was not found. + * @return URI from which to get paste URL or null if exercise was not found. * @throws IOException if zip creation fails */ - public String submitPaste(String currentPath) + public URI submitPaste(String currentPath) throws IOException, ParseException, ExpiredException, IllegalArgumentException, TmcCoreException, URISyntaxException, NoLanguagePluginFoundException { Exercise currentExercise = initExercise(currentPath); @@ -139,10 +137,10 @@ public String submitPaste(String currentPath) * Result includes URL of paste. * * @param currentPath path from which this was called. - * @return String with url from which to get paste URL or null if exercise was not found. + * @return URI from which to get paste URL or null if exercise was not found. * @throws IOException if failed to create zip. */ - public String submitPasteWithComment(String currentPath, String comment) + public URI submitPasteWithComment(String currentPath, String comment) throws IOException, ParseException, ExpiredException, IllegalArgumentException, TmcCoreException, URISyntaxException, NoLanguagePluginFoundException { Exercise currentExercise = initExercise(currentPath); @@ -155,7 +153,7 @@ public String submitPasteWithComment(String currentPath, String comment) /** * Requests a code review for a exercise. */ - public String submitWithCodeReviewRequest(Path currentPath, String message) + public URI submitWithCodeReviewRequest(Path currentPath, String message) throws IOException, ParseException, ExpiredException, IllegalArgumentException, TmcCoreException, URISyntaxException, NoLanguagePluginFoundException { Exercise currentExercise = initExercise(currentPath.toString()); @@ -193,18 +191,18 @@ private Exercise searchExercise(String currentPath) return currentExercise.get(); } - private String sendSubmissionToServerWithPaste(byte[] file, String url) throws IOException { + private URI sendSubmissionToServerWithPaste(byte[] file, URI url) throws IOException { HttpResult result = urlCommunicator.makePostWithByteArray( url, file, new HashMap(), new HashMap()); return tmcApi.getPasteUrl(result); } - private String sendZipFile(String currentPath, Exercise currentExercise, boolean paste) + private URI sendZipFile(String currentPath, Exercise currentExercise, boolean paste) throws IOException, URISyntaxException, NoLanguagePluginFoundException { - String returnUrl = urlHelper.withParams(currentExercise.getReturnUrl()); + URI returnUrl = urlHelper.withParams(currentExercise.getReturnUrl()); byte[] zippedExercise = langs.compressProject(Paths.get(currentPath)); - String resultUrl; + URI resultUrl; if (paste) { resultUrl = sendSubmissionToServerWithPaste(zippedExercise, returnUrl); } else { @@ -213,14 +211,14 @@ private String sendZipFile(String currentPath, Exercise currentExercise, boolean return resultUrl; } - private String sendZipFile( + private URI sendZipFile( String currentPath, Exercise currentExercise, ProgressObserver observer, boolean paste) throws IOException, URISyntaxException, NoLanguagePluginFoundException { - String returnUrl = urlHelper.withParams(currentExercise.getReturnUrl()); + URI returnUrl = urlHelper.withParams(currentExercise.getReturnUrl()); observer.progress("zipping exercise"); byte[] zippedExercise = langs.compressProject(Paths.get(currentPath)); observer.progress("submitting exercise"); - String resultUrl; + URI resultUrl; if (paste) { resultUrl = sendSubmissionToServerWithPaste(zippedExercise, returnUrl); } else { @@ -229,12 +227,12 @@ private String sendZipFile( return resultUrl; } - private String sendZipFileWithParams( + private URI sendZipFileWithParams( String currentPath, Exercise currentExercise, boolean paste, Map params) throws IOException, URISyntaxException, NoLanguagePluginFoundException { - String returnUrl = urlHelper.withParams(currentExercise.getReturnUrl()); + URI returnUrl = urlHelper.withParams(currentExercise.getReturnUrl()); byte[] zippedExercise = langs.compressProject(Paths.get(currentPath)); - String resultUrl; + URI resultUrl; if (paste) { resultUrl = sendSubmissionToServerWithPasteAndParams(zippedExercise, returnUrl, params); } else { @@ -247,23 +245,23 @@ private String findExerciseFolderToZip(String currentPath) { return rootFinder.getRootDirectory(Paths.get(currentPath)).get().toString(); } - private String sendSubmissionToServer(byte[] file, String url) throws IOException { + private URI sendSubmissionToServer(byte[] file, URI url) throws IOException { HttpResult result = urlCommunicator.makePostWithByteArray( url, file, new HashMap(), new HashMap()); return tmcApi.getSubmissionUrl(result); } - private String sendSubmissionToServerWithParams( - byte[] file, String url, Map params) throws IOException { + private URI sendSubmissionToServerWithParams( + byte[] file, URI url, Map params) throws IOException { HttpResult result = urlCommunicator.makePostWithByteArray( url, file, new HashMap(), params); return tmcApi.getSubmissionUrl(result); } - private String sendSubmissionToServerWithPasteAndParams( - byte[] file, String url, Map params) throws IOException { + private URI sendSubmissionToServerWithPasteAndParams( + byte[] file, URI url, Map params) throws IOException { HttpResult result = urlCommunicator.makePostWithByteArray( url, file, new HashMap(), params); diff --git a/src/main/java/fi/helsinki/cs/tmc/core/communication/SubmissionPoller.java b/src/main/java/fi/helsinki/cs/tmc/core/communication/SubmissionPoller.java index c3b3a21c..5f1f8a91 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/communication/SubmissionPoller.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/communication/SubmissionPoller.java @@ -12,8 +12,8 @@ import fi.helsinki.cs.tmc.core.domain.ProgressObserver; import java.io.IOException; +import java.net.URI; import java.util.List; -import org.apache.log4j.Logger; public class SubmissionPoller { @@ -57,7 +57,7 @@ public SubmissionPoller(TmcApi tmcApi, int timeout) { * @return SubmissionResult containing details of submission. Null if timed out. * @throws InterruptedException if thread failed to sleep */ - private Optional pollSubmissionUrl(String url) + private Optional pollSubmissionUrl(URI url) throws InterruptedException, IOException { for (int i = 0; i < timeOut; i++) { String json = tmcApi.getRawTextFrom(url); @@ -78,7 +78,7 @@ private Optional pollSubmissionUrl(String url) * @return SubmissionResult containing details of submission. Null if timed out. * @throws InterruptedException if thread failed to sleep */ - private Optional pollSubmissionUrl(String url, ProgressObserver observer) + private Optional pollSubmissionUrl(URI url, ProgressObserver observer) throws InterruptedException, IOException { for (int i = 0; i < timeOut; i++) { String json = tmcApi.getRawTextFrom(url); @@ -104,7 +104,7 @@ public List getFeedbackQuestions() { * * @param url the submission url */ - public SubmissionResult getSubmissionResult(String url) + public SubmissionResult getSubmissionResult(URI url) throws InterruptedException, TmcCoreException, IOException { Optional result = pollSubmissionUrl(url); if (!result.isPresent()) { @@ -120,7 +120,7 @@ public SubmissionResult getSubmissionResult(String url) * @param url the submission url * @param observer {@link ProgressObserver} that is informed of the polling status */ - public SubmissionResult getSubmissionResult(String url, ProgressObserver observer) + public SubmissionResult getSubmissionResult(URI url, ProgressObserver observer) throws InterruptedException, TmcCoreException, IOException { Optional result = pollSubmissionUrl(url, observer); if (!result.isPresent()) { diff --git a/src/main/java/fi/helsinki/cs/tmc/core/communication/TmcApi.java b/src/main/java/fi/helsinki/cs/tmc/core/communication/TmcApi.java index 6811fc38..5734a70c 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/communication/TmcApi.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/communication/TmcApi.java @@ -14,6 +14,7 @@ import com.google.gson.JsonParser; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; @@ -48,7 +49,7 @@ public TmcApi(UrlCommunicator urlCommunicator, TmcSettings settings) { */ public List getCourses(String serverAddress) throws IOException { String coursesAddress = helper.allCoursesAddress(serverAddress); - JsonObject jsonObject = getJsonFrom(coursesAddress); + JsonObject jsonObject = getJsonFrom(URI.create(coursesAddress)); Gson mapper = new Gson(); Course[] courses = mapper.fromJson(jsonObject.getAsJsonArray("courses"), Course[].class); return Arrays.asList(courses); @@ -69,7 +70,7 @@ public List getCourses() throws IOException { * @param url url from which the object data is fetched * @return JSON-object */ - public JsonObject getJsonFrom(String url) throws IOException { + public JsonObject getJsonFrom(URI url) throws IOException { HttpResult httpResult = urlCommunicator.makeGetRequestWithAuthentication(url); if (httpResult == null) { return null; @@ -84,7 +85,7 @@ public JsonObject getJsonFrom(String url) throws IOException { * @param url url from which the data is fetched * @return JSON-object */ - public String getRawTextFrom(String url) throws IOException { + public String getRawTextFrom(URI url) throws IOException { HttpResult httpResult = urlCommunicator.makeGetRequestWithAuthentication(url); if (httpResult == null) { return null; @@ -98,7 +99,7 @@ public String getRawTextFrom(String url) throws IOException { * @param reviewUrl which is found from course-object * @return List of reviews */ - public List getReviews(String reviewUrl) throws IOException, URISyntaxException { + public List getReviews(URI reviewUrl) throws IOException, URISyntaxException { JsonObject jsonObject = getJsonFrom(helper.withParams(reviewUrl)); Gson mapper = new Gson(); Review[] reviews = mapper.fromJson(jsonObject.getAsJsonArray("reviews"), Review[].class); @@ -111,7 +112,7 @@ public List getReviews(String reviewUrl) throws IOException, URISyntaxEx * @param courseUrl url of the course we are interested in * @return String of all exercise names separated by newlines */ - public String getExerciseNames(String courseUrl) throws IOException { + public String getExerciseNames(URI courseUrl) throws IOException { List exercises = getExercises(courseUrl); StringBuilder asString = new StringBuilder(); for (Exercise exercise : exercises) { @@ -159,7 +160,7 @@ public Optional getCourse(int courseId) * @param courseUrl URL path to course JSON * @return an Course object (parsed from JSON) */ - public Optional getCourse(String courseUrl) throws IOException { + public Optional getCourse(URI courseUrl) throws IOException { JsonObject courseJson = getJsonFrom(courseUrl); if (courseJson == null) { return Optional.absent(); @@ -215,7 +216,7 @@ public List getExercises(int id) throws IOException, URISyntaxExceptio * @return List of all exercises as Exercise-objects. If no course is found, empty list will be * returned. */ - public List getExercises(String courseUrl) throws IOException { + public List getExercises(URI courseUrl) throws IOException { Optional courseOptional = getCourse(courseUrl); if (courseOptional.isPresent()) { Course course = courseOptional.get(); @@ -233,7 +234,7 @@ public List getExercises(String courseUrl) throws IOException { * @param url to make request to * @return A SubmissionResult object which contains data of submission. */ - public SubmissionResult getSubmissionResult(String url) throws IOException { + public SubmissionResult getSubmissionResult(URI url) throws IOException { JsonObject submission = getJsonFrom(url); Gson mapper = new Gson(); return mapper.fromJson(submission, SubmissionResult.class); @@ -245,7 +246,7 @@ public SubmissionResult getSubmissionResult(String url) throws IOException { * @param result HTTPResult containing JSON with submission url. * @return url where submission results are located. */ - public String getSubmissionUrl(HttpResult result) { + public URI getSubmissionUrl(HttpResult result) { return getPropertyFromResult(result, "submission_url"); } @@ -255,13 +256,13 @@ public String getSubmissionUrl(HttpResult result) { * @param result HTTPResult containing JSON with paste url. * @return url where paste is located. */ - public String getPasteUrl(HttpResult result) { + public URI getPasteUrl(HttpResult result) { return getPropertyFromResult(result, "paste_url"); } - private String getPropertyFromResult(HttpResult result, String property) { + private URI getPropertyFromResult(HttpResult result, String property) { JsonElement jelement = new JsonParser().parse(result.getData()); JsonObject jobject = jelement.getAsJsonObject(); - return jobject.get(property).getAsString(); + return URI.create(jobject.get(property).getAsString()); } } diff --git a/src/main/java/fi/helsinki/cs/tmc/core/communication/UrlCommunicator.java b/src/main/java/fi/helsinki/cs/tmc/core/communication/UrlCommunicator.java index 8ccc5ba8..2b005b9a 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/communication/UrlCommunicator.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/communication/UrlCommunicator.java @@ -35,6 +35,7 @@ import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; @@ -64,9 +65,9 @@ public UrlCommunicator(TmcSettings settings) { * @throws java.io.IOException if file is invalid. */ public HttpResult makePostWithFile( - ContentBody fileBody, String destinationUrl, Map headers) + ContentBody fileBody, URI destinationUrl, Map headers) throws IOException { - HttpPost httppost = new HttpPost(destinationUrl); + HttpPost httppost = new HttpPost(destinationUrl.toString()); addHeadersTo(httppost, headers); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); @@ -83,15 +84,15 @@ public HttpResult makePostWithFile( * Adds byte-array to entity of post-request and executes it. */ public HttpResult makePostWithByteArray( - String url, byte[] data, Map extraHeaders, Map params) + URI url, byte[] data, Map extraHeaders, Map params) throws IOException { HttpPost rawPost = makeRawPostRequest(url, data, extraHeaders, params); return getResponseResult(rawPost); } private HttpPost makeRawPostRequest( - String url, byte[] data, Map extraHeaders, Map params) { - HttpPost request = new HttpPost(url); + URI url, byte[] data, Map extraHeaders, Map params) { + HttpPost request = new HttpPost(url.toString()); addHeadersTo(request, extraHeaders); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); @@ -117,11 +118,11 @@ private HttpPost makeRawPostRequest( */ public HttpResult makePostWithFileAndParams( FileBody fileBody, - String destinationUrl, + URI destinationUrl, Map headers, Map params) throws IOException { - HttpPost httppost = new HttpPost(destinationUrl); + HttpPost httppost = new HttpPost(destinationUrl.toString()); addHeadersTo(httppost, headers); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); @@ -158,7 +159,7 @@ private MultipartEntityBuilder addParamsToRequest( * @param credentials to add to the request * @return A Result-object with some data and a state of success or fail */ - public HttpResult makeGetRequest(String url, String credentials) throws IOException { + public HttpResult makeGetRequest(URI url, String credentials) throws IOException { HttpGet httpGet = createGet(url, credentials); return getResponseResult(httpGet); } @@ -170,10 +171,10 @@ public HttpResult makeGetRequest(String url, String credentials) throws IOExcept * @param body contains key-value -pairs. * @return Result which contains the result. */ - public HttpResult makePutRequest(String url, Optional> body) + public HttpResult makePutRequest(URI url, Optional> body) throws IOException, URISyntaxException { url = urlHelper.withParams(url); - HttpPut httpPut = new HttpPut(url); + HttpPut httpPut = new HttpPut(url.toString()); addCredentials(httpPut, this.settings.getFormattedUserData()); List params = new ArrayList<>(); @@ -185,8 +186,8 @@ public HttpResult makePutRequest(String url, Optional> body) return getResponseResult(httpPut); } - private HttpGet createGet(String url, String credentials) { - HttpGet request = new HttpGet(url); + private HttpGet createGet(URI url, String credentials) { + HttpGet request = new HttpGet(url.toString()); addCredentials(request, credentials); return request; } @@ -198,7 +199,7 @@ private HttpGet createGet(String url, String credentials) { * @param file file to write the results into * @return true if successful */ - public boolean downloadToFile(String url, File file, String credentials) { + public boolean downloadToFile(URI url, File file, String credentials) { try (FileOutputStream fileOutputStream = new FileOutputStream(file)) { HttpGet httpget = createGet(url, credentials); HttpResponse response = executeRequest(httpget); @@ -213,7 +214,7 @@ public boolean downloadToFile(String url, File file, String credentials) { /** * Calls downloadToFile with username and password as params. */ - public boolean downloadToFile(String url, File file) { + public boolean downloadToFile(URI url, File file) { return downloadToFile(url, file, this.settings.getFormattedUserData()); } @@ -268,7 +269,7 @@ private HttpResult getResponseResult(HttpRequestBase httpRequest) throws IOExcep /** * Makes a POST HTTP request. */ - public HttpResult makePostWithJson(JsonObject req, String feedbackUrl) + public HttpResult makePostWithJson(JsonObject req, URI feedbackUrl) throws IOException, URISyntaxException { feedbackUrl = urlHelper.withParams(feedbackUrl); HttpPost httppost = new HttpPost(feedbackUrl); @@ -280,7 +281,7 @@ public HttpResult makePostWithJson(JsonObject req, String feedbackUrl) return getResponseResult(httppost); } - public HttpResult makeGetRequestWithAuthentication(String url) throws IOException { + public HttpResult makeGetRequestWithAuthentication(URI url) throws IOException { return this.makeGetRequest(url, this.settings.getFormattedUserData()); } } diff --git a/src/main/java/fi/helsinki/cs/tmc/core/communication/UrlHelper.java b/src/main/java/fi/helsinki/cs/tmc/core/communication/UrlHelper.java index 75dbd642..99974e32 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/communication/UrlHelper.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/communication/UrlHelper.java @@ -6,6 +6,7 @@ import fi.helsinki.cs.tmc.core.configuration.TmcSettings; import fi.helsinki.cs.tmc.core.domain.Course; +import java.net.URI; import org.apache.http.client.utils.URIBuilder; @@ -27,11 +28,12 @@ public UrlHelper(TmcSettings settings) { this.settings = settings; } - public String getCourseUrl(int courseId) throws URISyntaxException { - return withParams(settings.getServerAddress() + "/courses/" + courseId + ".json"); + public URI getCourseUrl(int courseId) throws URISyntaxException { + return withParams(URI.create( + settings.getServerAddress() + "/courses/" + courseId + ".json")); } - public String getCourseUrl(Course course) throws URISyntaxException { + public URI getCourseUrl(Course course) throws URISyntaxException { return withParams(course.getDetailsUrl()); } @@ -39,12 +41,11 @@ public String allCoursesAddress(String serverAddress) { return serverAddress + this.coursesExtension; } - public String withParams(String url) throws URISyntaxException { + public URI withParams(URI url) throws URISyntaxException { return new URIBuilder(url) .setParameter(API_VERSION_PARAM, settings.apiVersion()) .setParameter(CLIENT_NAME_PARAM, settings.clientName()) .setParameter(CLIENT_VERSION_PARAM, settings.clientVersion()) - .build() - .toString(); + .build(); } } diff --git a/src/main/java/fi/helsinki/cs/tmc/core/domain/Course.java b/src/main/java/fi/helsinki/cs/tmc/core/domain/Course.java index 3d0b33c3..3f0de63e 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/domain/Course.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/domain/Course.java @@ -14,29 +14,29 @@ public class Course { private List exercises; @SerializedName("details_url") - private String detailsUrl; + private URI detailsUrl; @SerializedName("unlock_url") - private String unlockUrl; + private URI unlockUrl; @SerializedName("comet_url") - private String cometUrl; + private URI cometUrl; - public String getReviewsUrl() { + public URI getReviewsUrl() { return reviewsUrl; } - public void setReviewsUrl(String reviewsUrl) { + public void setReviewsUrl(URI reviewsUrl) { this.reviewsUrl = reviewsUrl; } @SerializedName("spyware_urls") - private List spywareUrls; + private List spywareUrls; @SerializedName("reviews_url") - private String reviewsUrl; + private URI reviewsUrl; - private List unlockables; + private List unlockables; public Course() { this(null); @@ -49,11 +49,11 @@ public Course(String name) { this.spywareUrls = new ArrayList<>(); } - public List getSpywareUrls() { + public List getSpywareUrls() { return spywareUrls; } - public void setSpywareUrls(List spywareUrls) { + public void setSpywareUrls(List spywareUrls) { this.spywareUrls = spywareUrls; } @@ -81,15 +81,11 @@ public void setName(String name) { this.name = name; } - public String getDetailsUrl() { + public URI getDetailsUrl() { return detailsUrl; } - public URI getDetailsUrlAsUri() { - return URI.create(detailsUrl); - } - - public void setDetailsUrl(String detailsUrl) { + public void setDetailsUrl(URI detailsUrl) { this.detailsUrl = detailsUrl; } @@ -114,19 +110,19 @@ public boolean equals(Object obj) { private boolean exercisesLoaded; - public String getUnlockUrl() { + public URI getUnlockUrl() { return unlockUrl; } - public void setUnlockUrl(String unlockUrl) { + public void setUnlockUrl(URI unlockUrl) { this.unlockUrl = unlockUrl; } - public String getCometUrl() { + public URI getCometUrl() { return cometUrl; } - public void setCometUrl(String cometUrl) { + public void setCometUrl(URI cometUrl) { this.cometUrl = cometUrl; } @@ -138,11 +134,11 @@ public void setExercisesLoaded(boolean exercisesLoaded) { this.exercisesLoaded = exercisesLoaded; } - public List getUnlockables() { + public List getUnlockables() { return unlockables; } - public void setUnlockables(List unlockables) { + public void setUnlockables(List unlockables) { this.unlockables = unlockables; } diff --git a/src/main/java/fi/helsinki/cs/tmc/core/domain/Exercise.java b/src/main/java/fi/helsinki/cs/tmc/core/domain/Exercise.java index 28467c2e..0e38665d 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/domain/Exercise.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/domain/Exercise.java @@ -1,3 +1,4 @@ + package fi.helsinki.cs.tmc.core.domain; import com.google.gson.annotations.SerializedName; @@ -6,6 +7,7 @@ import org.slf4j.LoggerFactory; import java.io.Serializable; +import java.net.URI; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -28,21 +30,16 @@ public class Exercise implements Serializable { private String checksum; //: "406f2f0690550c6dea94f319b2b1580c", @SerializedName("return_url") - private String returnUrl; //: "https://example.com/staging/exercises/284/submissions.json", + private URI returnUrl; @SerializedName("zip_url") - private String zipUrl; //": "https://example.com/staging/exercises/284.zip", - - // /** - // * The URL this exercise can be downloaded from. - // */ - // @SerializedName("zip_url") - // private String downloadUrl; + private URI zipUrl; + /** * The URL the solution can be downloaded from (admins only). */ @SerializedName("solution_zip_url") - private String solutionDownloadUrl; + private URI solutionDownloadUrl; private boolean returnable; //": true, @@ -72,7 +69,7 @@ public class Exercise implements Serializable { private boolean runTestsLocallyActionEnabled = true; @SerializedName("exercise_submissions_url") - private String exerciseSubmissionsUrl; + private URI exerciseSubmissionsUrl; public Exercise() {} @@ -149,19 +146,19 @@ public void setChecksum(String checksum) { this.checksum = checksum; } - public String getReturnUrl() { + public URI getReturnUrl() { return returnUrl; } - public void setReturnUrl(String returnUrl) { + public void setReturnUrl(URI returnUrl) { this.returnUrl = returnUrl; } - public String getZipUrl() { + public URI getZipUrl() { return zipUrl; } - public void setZipUrl(String zipUrl) { + public void setZipUrl(URI zipUrl) { this.zipUrl = zipUrl; } @@ -245,11 +242,11 @@ public void setRunTestsLocallyActionEnabled(boolean runTestsLocallyActionEnabled this.runTestsLocallyActionEnabled = runTestsLocallyActionEnabled; } - public String getExerciseSubmissionsUrl() { + public URI getExerciseSubmissionsUrl() { return exerciseSubmissionsUrl; } - public void setExerciseSubmissionsUrl(String exerciseSubmissionsUrl) { + public void setExerciseSubmissionsUrl(URI exerciseSubmissionsUrl) { this.exerciseSubmissionsUrl = exerciseSubmissionsUrl; } @@ -296,16 +293,16 @@ public void setCourseName(String courseName) { this.courseName = courseName; } - public String getDownloadUrl() { + public URI getDownloadUrl() { return this.zipUrl; } - public void setDownloadUrl(String downloadAddress) { + public void setDownloadUrl(URI downloadAddress) { if (downloadAddress == null) { throw new NullPointerException( "downloadAddress was null at Exercise.setDownloadAddress"); } - if (downloadAddress.isEmpty()) { + if (downloadAddress.toString().isEmpty()) { throw new IllegalArgumentException( "downloadAddress cannot be empty at Exercise.setDownloadAddress"); } @@ -313,11 +310,11 @@ public void setDownloadUrl(String downloadAddress) { this.zipUrl = downloadAddress; } - public void setSolutionDownloadUrl(String solutionDownloadUrl) { + public void setSolutionDownloadUrl(URI solutionDownloadUrl) { this.solutionDownloadUrl = solutionDownloadUrl; } - public String getSolutionDownloadUrl() { + public URI getSolutionDownloadUrl() { return solutionDownloadUrl; } diff --git a/src/main/java/fi/helsinki/cs/tmc/core/domain/Review.java b/src/main/java/fi/helsinki/cs/tmc/core/domain/Review.java index 04e6cdad..9c29c19c 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/domain/Review.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/domain/Review.java @@ -7,6 +7,7 @@ import com.google.gson.annotations.SerializedName; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; import java.util.List; @@ -36,10 +37,10 @@ public class Review { @SerializedName("points_not_awarded") private List pointsNotAwarded; - private String url; + private URI url; @SerializedName("update_url") - private String updateUrl; + private URI updateUrl; @SerializedName("created_at") private String createdAt; @@ -111,19 +112,19 @@ public void setPointsNotAwarded(List pointsNotAwarded) { this.pointsNotAwarded = pointsNotAwarded; } - public String getUrl() { + public URI getUrl() { return url; } - public void setUrl(String url) { + public void setUrl(URI url) { this.url = url; } - public String getUpdateUrl() { + public URI getUpdateUrl() { return updateUrl; } - public void setUpdateUrl(String updateUrl) { + public void setUpdateUrl(URI updateUrl) { this.updateUrl = updateUrl; } @@ -164,7 +165,7 @@ private Map addHeaders(boolean read) { return headers; } - private String putUrl() { + private URI putUrl() { return this.updateUrl; } diff --git a/src/main/java/fi/helsinki/cs/tmc/core/spyware/DiffSender.java b/src/main/java/fi/helsinki/cs/tmc/core/spyware/DiffSender.java index 43ccb219..dc70329a 100644 --- a/src/main/java/fi/helsinki/cs/tmc/core/spyware/DiffSender.java +++ b/src/main/java/fi/helsinki/cs/tmc/core/spyware/DiffSender.java @@ -7,6 +7,7 @@ import fi.helsinki.cs.tmc.core.exceptions.TmcCoreException; import java.io.IOException; +import java.net.URI; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -35,9 +36,9 @@ public DiffSender(UrlCommunicator urlCommunicator, TmcSettings settings) { */ public List sendToSpyware(byte[] diffs, Course currentCourse) throws TmcCoreException { - List spywareUrls = currentCourse.getSpywareUrls(); + List spywareUrls = currentCourse.getSpywareUrls(); List results = new ArrayList<>(); - for (String url : spywareUrls) { + for (URI url : spywareUrls) { results.add(sendToUrl(diffs, url)); } return results; @@ -50,7 +51,7 @@ public List sendToSpyware(byte[] diffs, Course currentCourse) * @param url of destination * @return HttpResult from UrlCommunicator */ - public HttpResult sendToUrl(byte[] diffs, String url) { + public HttpResult sendToUrl(byte[] diffs, URI url) { try { return urlCommunicator.makePostWithByteArray( url, diffs, createHeaders(), new HashMap()); diff --git a/src/test/java/fi/helsinki/cs/tmc/core/commands/ListCoursesTest.java b/src/test/java/fi/helsinki/cs/tmc/core/commands/ListCoursesTest.java index 9a443df3..0ff0583b 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/commands/ListCoursesTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/commands/ListCoursesTest.java @@ -1,7 +1,6 @@ package fi.helsinki.cs.tmc.core.commands; import static com.github.tomakehurst.wiremock.client.WireMock.get; -import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; import static org.junit.Assert.assertEquals; @@ -27,9 +26,8 @@ import org.mockito.Mockito; import java.io.IOException; -import java.net.URISyntaxException; +import java.net.URI; import java.util.List; -import java.util.concurrent.ExecutionException; public class ListCoursesTest { @@ -53,7 +51,7 @@ public void setUp() throws IOException { settings.setPassword("password"); HttpResult fakeResult = new HttpResult(ExampleJson.allCoursesExample, 200, true); - Mockito.when(communicator.makeGetRequestWithAuthentication(Mockito.anyString())) + Mockito.when(communicator.makeGetRequestWithAuthentication(URI.create(Mockito.anyString()))) .thenReturn(fakeResult); } diff --git a/src/test/java/fi/helsinki/cs/tmc/core/commands/PasteWithCommentTest.java b/src/test/java/fi/helsinki/cs/tmc/core/commands/PasteWithCommentTest.java index 254965e8..34edab41 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/commands/PasteWithCommentTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/commands/PasteWithCommentTest.java @@ -26,7 +26,7 @@ public class PasteWithCommentTest { private PasteWithComment paste; private ExerciseSubmitter submitterMock; - private String pasteUrl = "http://example.com/paste"; + private URI pasteUrl = URI.create("http://example.com/paste"); private CoreTestSettings settings = new CoreTestSettings(); @Before diff --git a/src/test/java/fi/helsinki/cs/tmc/core/commands/RequestCodeReviewTest.java b/src/test/java/fi/helsinki/cs/tmc/core/commands/RequestCodeReviewTest.java index 55a2a31c..8effb36b 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/commands/RequestCodeReviewTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/commands/RequestCodeReviewTest.java @@ -27,7 +27,7 @@ public class RequestCodeReviewTest { private RequestCodeReview request; private ExerciseSubmitter submitterMock; - private String pasteUrl = "http://example.com/paste"; + private URI pasteUrl = URI.create("http://example.com/paste"); private CoreTestSettings settings = new CoreTestSettings(); @Before diff --git a/src/test/java/fi/helsinki/cs/tmc/core/commands/SendFeedbackTest.java b/src/test/java/fi/helsinki/cs/tmc/core/commands/SendFeedbackTest.java index 4376fd03..76168411 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/commands/SendFeedbackTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/commands/SendFeedbackTest.java @@ -11,6 +11,7 @@ import com.github.tomakehurst.wiremock.client.WireMock; import com.github.tomakehurst.wiremock.junit.WireMockRule; +import java.net.URI; import org.junit.Before; import org.junit.Rule; @@ -32,7 +33,9 @@ public class SendFeedbackTest { public void setUp() { settings = new CoreTestSettings(); serverAddress += wireMock.port(); - command = new SendFeedback(settings, testCaseMap(), serverAddress + "/feedback"); + command = new SendFeedback(settings, + testCaseMap(), + URI.create(serverAddress + "/feedback")); } private Map testCaseMap() { @@ -60,10 +63,13 @@ public void testCallSendsFeedbackToServer() throws Exception { .willReturn(WireMock.aResponse().withStatus(200))); Course currentCourse = new Course(); - currentCourse.setSpywareUrls(Collections.singletonList(serverAddress + "/spyware")); + currentCourse.setSpywareUrls(Collections.singletonList( + URI.create(serverAddress + "/spyware"))); settings.setCurrentCourse(currentCourse); - command = new SendFeedback(settings, testCaseMap(), serverAddress + "/feedback"); + command = new SendFeedback(settings, + testCaseMap(), + URI.create(serverAddress + "/feedback")); command.call(); diff --git a/src/test/java/fi/helsinki/cs/tmc/core/commands/SendSpywareDiffsTest.java b/src/test/java/fi/helsinki/cs/tmc/core/commands/SendSpywareDiffsTest.java index 013b7538..c88e690c 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/commands/SendSpywareDiffsTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/commands/SendSpywareDiffsTest.java @@ -13,6 +13,7 @@ import com.github.tomakehurst.wiremock.client.WireMock; import com.github.tomakehurst.wiremock.junit.WireMockRule; +import java.net.URI; import org.junit.Before; import org.junit.Rule; @@ -74,7 +75,7 @@ public void testCallSendsDataToSpywareServer() throws Exception { .withBody("SPYWARE TULI PERILLE"))); Course currentCourse = new Course(); - currentCourse.setSpywareUrls(Arrays.asList(serverAddress + "/spyware")); + currentCourse.setSpywareUrls(Arrays.asList(URI.create(serverAddress + "/spyware"))); settings.setCurrentCourse(currentCourse); command = new SendSpywareDiffs(settings, sender, bytes); diff --git a/src/test/java/fi/helsinki/cs/tmc/core/commands/SubmitTest.java b/src/test/java/fi/helsinki/cs/tmc/core/commands/SubmitTest.java index 07243b02..7004db74 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/commands/SubmitTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/commands/SubmitTest.java @@ -25,6 +25,7 @@ import fi.helsinki.cs.tmc.core.domain.Course; import fi.helsinki.cs.tmc.core.domain.ProgressObserver; import fi.helsinki.cs.tmc.core.domain.submission.SubmissionResult; +import fi.helsinki.cs.tmc.core.exceptions.ExpiredException; import fi.helsinki.cs.tmc.core.exceptions.TmcCoreException; import fi.helsinki.cs.tmc.core.testhelpers.ExampleJson; @@ -37,9 +38,12 @@ import org.junit.Rule; import org.junit.Test; +import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Path; import java.nio.file.Paths; +import java.text.ParseException; public class SubmitTest { @@ -47,7 +51,7 @@ public class SubmitTest { private Submit submit; private ExerciseSubmitter submitterMock; private CoreTestSettings settings; - private String submissionUrl; + private URI submissionUrl; private ProgressObserver observer; @Rule public WireMockRule wireMock = new WireMockRule(0); @@ -104,14 +108,15 @@ public void setup() throws Exception { observer = mock(ProgressObserver.class); - submissionUrl = new UrlHelper(settings).withParams("/submissions/1781.json"); + submissionUrl = new UrlHelper(settings).withParams(URI.create("/submissions/1781.json")); submitterMock = mock(ExerciseSubmitter.class); pollerMock = mock(SubmissionPoller.class); path = Paths.get("polku", "kurssi", "kansioon", "src").toString(); - when(submitterMock.submit(anyString())).thenReturn(serverAddress + submissionUrl); + when(submitterMock.submit(anyString())) + .thenReturn(URI.create(serverAddress + submissionUrl)); submit = new Submit( settings, submitterMock, new SubmissionPoller(new TmcApi(settings)), path); @@ -133,7 +138,7 @@ public void testThrowsExceptionIfNoPassword() throws Exception { @Test public void testHandlesSuccessfulTestRunResponseCorrectly() throws Exception { wireMock.stubFor( - get(urlEqualTo(submissionUrl)) + get(urlEqualTo(submissionUrl.toString())) .willReturn( WireMock.aResponse() .withStatus(200) @@ -147,7 +152,7 @@ public void testHandlesSuccessfulTestRunResponseCorrectly() throws Exception { @Test public void testHandlesUnsuccessfulTestRunResponseCorrectly() throws Exception { wireMock.stubFor( - get(urlEqualTo(submissionUrl)) + get(urlEqualTo(submissionUrl.toString())) .willReturn( WireMock.aResponse() .withStatus(200) @@ -177,11 +182,15 @@ public void submitWithTmcCore() throws Exception { } @Test - public void testSubmissionWithObserver() throws Exception{ - when(submitterMock.submit(eq(path), eq(observer))).thenReturn("xkcd.com"); + public void testSubmissionWithObserver() + throws URISyntaxException, ExpiredException, + TmcCoreException, ParseException, + IOException, InterruptedException { + + when(submitterMock.submit(eq(path), eq(observer))).thenReturn(URI.create("xkcd.com")); submitWithObserver.call(); verify(submitterMock).submit(eq(path), eq(observer)); - verify(pollerMock).getSubmissionResult(eq("xkcd.com"), eq(observer)); + verify(pollerMock).getSubmissionResult(eq(URI.create("xkcd.com")), eq(observer)); } } diff --git a/src/test/java/fi/helsinki/cs/tmc/core/communication/ExerciseDownloaderTest.java b/src/test/java/fi/helsinki/cs/tmc/core/communication/ExerciseDownloaderTest.java index ebbe8263..a02fdccf 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/communication/ExerciseDownloaderTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/communication/ExerciseDownloaderTest.java @@ -33,6 +33,7 @@ import java.io.File; import java.io.IOException; +import java.net.URI; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -68,20 +69,20 @@ public void setup() { zipDestination = Paths.get("src", "test", "resources", "__files").toString(); modelSolutionExample = new Exercise(); - modelSolutionExample.setSolutionDownloadUrl(serverAddress + "/model"); + modelSolutionExample.setSolutionDownloadUrl(URI.create(serverAddress + "/model")); Exercise e1 = new Exercise(); - e1.setZipUrl(serverAddress + "/ex1.zip"); + e1.setZipUrl(URI.create(serverAddress + "/ex1.zip")); e1.setName("Exercise1"); exercises.add(e1); Exercise e2 = new Exercise(); - e2.setZipUrl(serverAddress + "/ex2.zip"); + e2.setZipUrl(URI.create(serverAddress + "/ex2.zip")); e2.setName("Exercise2"); exercises.add(e2); Exercise e3 = new Exercise(); - e3.setZipUrl(serverAddress + "/ex3.zip"); + e3.setZipUrl(URI.create(serverAddress + "/ex3.zip")); e3.setName("Exercise3"); exercises.add(e3); diff --git a/src/test/java/fi/helsinki/cs/tmc/core/communication/ExerciseSubmitterTest.java b/src/test/java/fi/helsinki/cs/tmc/core/communication/ExerciseSubmitterTest.java index 446b7d1c..2f7be41b 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/communication/ExerciseSubmitterTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/communication/ExerciseSubmitterTest.java @@ -8,7 +8,6 @@ import fi.helsinki.cs.tmc.core.testhelpers.ExampleJson; import fi.helsinki.cs.tmc.core.testhelpers.ProjectRootFinderStub; import fi.helsinki.cs.tmc.core.zipping.ProjectRootFinder; -import fi.helsinki.cs.tmc.langs.io.zip.StudentFileAwareZipper; import fi.helsinki.cs.tmc.langs.util.TaskExecutorImpl; import com.google.common.base.Optional; @@ -23,6 +22,7 @@ import java.io.File; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Path; import java.nio.file.Paths; @@ -140,8 +140,9 @@ public void testSubmitWithOneParam() this.courseSubmitter = new ExerciseSubmitter(rootFinder, langs, urlCommunicator, tmcApi, settings); rootFinder.setReturnValue(testPath); - String submissionPath = "http://example.com/submissions/1781.json?api_version=7"; - String result = courseSubmitter.submit(testPath); + URI submissionPath = URI.create( + "http://example.com/submissions/1781.json?api_version=7"); + URI result = courseSubmitter.submit(testPath); assertEquals(submissionPath, result); } @@ -174,8 +175,8 @@ public void submitWithPasteReturnsPasteUrl() this.courseSubmitter = new ExerciseSubmitter(rootFinder, langs, urlCommunicator, tmcApi, settings); rootFinder.setReturnValue(testPath); - String pastePath = "https://example.com/staging/paste/ynpw7_mZZGk3a9PPrMWOOQ"; - String result = courseSubmitter.submitPaste(testPath); + URI pastePath = URI.create("https://example.com/staging/paste/ynpw7_mZZGk3a9PPrMWOOQ"); + URI result = courseSubmitter.submitPaste(testPath); assertEquals(pastePath, result); } @@ -195,8 +196,8 @@ public void submitWithPasteAndCommentReturnsPasteUrl() this.courseSubmitter = new ExerciseSubmitter(rootFinder, langs, urlCommunicator, tmcApi, settings); rootFinder.setReturnValue(testPath); - String pastePath = "https://example.com/staging/paste/ynpw7_mZZGk3a9PPrMWOOQ"; - String result = courseSubmitter.submitPasteWithComment(testPath, "Commentti"); + URI pastePath = URI.create("https://example.com/staging/paste/ynpw7_mZZGk3a9PPrMWOOQ"); + URI result = courseSubmitter.submitPasteWithComment(testPath, "Commentti"); assertEquals(pastePath, result); } @@ -266,7 +267,10 @@ public void submitWithCodeReviewRequest() rootFinder.setReturnValue(testPath.toString()); courseSubmitter.submitWithCodeReviewRequest(testPath, "Help"); Mockito.verify(urlCommunicator) - .makePostWithByteArray(anyString(), any(byte[].class), anyMap(), capture.capture()); + .makePostWithByteArray(URI.create(anyString()), + any(byte[].class), + anyMap(), + capture.capture()); assertEquals("1", capture.getValue().get("request_review")); assertEquals("Help", capture.getValue().get("message_for_reviewer")); } @@ -290,7 +294,10 @@ public void submitWithCodeReviewRequestWithEmptyMessage() rootFinder.setReturnValue(testPath.toString()); courseSubmitter.submitWithCodeReviewRequest(testPath, ""); Mockito.verify(urlCommunicator) - .makePostWithByteArray(anyString(), any(byte[].class), anyMap(), capture.capture()); + .makePostWithByteArray(URI.create(anyString()), + any(byte[].class), + anyMap(), + capture.capture()); assertEquals("1", capture.getValue().get("request_review")); assertNull(capture.getValue().get("message_for_reviewer")); } @@ -310,8 +317,9 @@ public void testSubmitterUsesProgressObserverIfGiven() throws Exception { this.courseSubmitter = new ExerciseSubmitter(rootFinder, langs, urlCommunicator, tmcApi, settings); rootFinder.setReturnValue(testPath); - String submissionPath = "http://example.com/submissions/1781.json?api_version=7"; - String result = courseSubmitter.submit(testPath, observer); + URI submissionPath = URI.create( + "http://example.com/submissions/1781.json?api_version=7"); + URI result = courseSubmitter.submit(testPath, observer); verify(observer).progress("zipping exercise"); verify(observer).progress("submitting exercise"); assertEquals(submissionPath, result); @@ -320,11 +328,11 @@ public void testSubmitterUsesProgressObserverIfGiven() throws Exception { private void mockUrlCommunicator(String pieceOfUrl, String returnValue) throws IOException { HttpResult fakeResult = new HttpResult(returnValue, 200, true); - Mockito.when( - urlCommunicator.makeGetRequest( - Mockito.contains(pieceOfUrl), Mockito.anyString())) + Mockito.when(urlCommunicator.makeGetRequest( + Mockito.argThat(new UriContains(pieceOfUrl)), Mockito.anyString())) .thenReturn(fakeResult); - Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(Mockito.contains(pieceOfUrl))) + Mockito.when(urlCommunicator.makeGetRequestWithAuthentication( + Mockito.argThat(new UriContains(pieceOfUrl)))) .thenReturn(fakeResult); } @@ -333,14 +341,11 @@ private void mockUrlCommunicatorWithFile(String url, String returnValue) throws HttpResult fakeResult = new HttpResult(returnValue, 200, true); Mockito.when( urlCommunicator.makePostWithByteArray( - Mockito.contains(url), + Mockito.argThat(new UriContains(url)), Mockito.any(byte[].class), Mockito.any(Map.class), Mockito.any(Map.class))) .thenReturn(fakeResult); - /*Mockito.when(urlCommunicator.makePostWithFileAndParams(Mockito.any(FileBody.class), - Mockito.contains(url), Mockito.any(Map.class), Mockito.any(Map.class))) - .thenReturn(fakeResult);*/ } } diff --git a/src/test/java/fi/helsinki/cs/tmc/core/communication/SubmissionPollerTest.java b/src/test/java/fi/helsinki/cs/tmc/core/communication/SubmissionPollerTest.java index bd4a4d2e..73379f50 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/communication/SubmissionPollerTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/communication/SubmissionPollerTest.java @@ -9,6 +9,7 @@ import fi.helsinki.cs.tmc.core.domain.submission.SubmissionResult.Status; import fi.helsinki.cs.tmc.core.testhelpers.ExampleJson; import java.io.IOException; +import java.net.URI; import org.junit.Before; import org.junit.Test; @@ -17,15 +18,14 @@ import org.mockito.Mockito; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class SubmissionPollerTest { private SubmissionPoller submissionPoller; - private String url = - "https://example.com/staging/submissions/1764.json?api_version=7&client=tmc_cli&client_version=1"; + private URI url = URI.create( + "https://example.com/staging/submissions/1764.json?api_version=7&client=tmc_cli&client_version=1"); private CoreTestSettings settings; private TmcApi tmcApi; @@ -40,7 +40,7 @@ public void setup() { @Test public void successfulSubmission() throws Exception { - Mockito.when(tmcApi.getRawTextFrom(Mockito.anyString())) + Mockito.when(tmcApi.getRawTextFrom(URI.create(Mockito.anyString()))) .thenReturn(ExampleJson.successfulSubmission); SubmissionResult output = submissionPoller.getSubmissionResult(url); assertFalse(output == null); @@ -50,7 +50,7 @@ public void successfulSubmission() throws Exception { @Test public void unsuccessfulSubmission() throws Exception { - Mockito.when(tmcApi.getRawTextFrom(Mockito.anyString())) + Mockito.when(tmcApi.getRawTextFrom(URI.create(Mockito.anyString()))) .thenReturn(ExampleJson.failedSubmission); SubmissionResult output = submissionPoller.getSubmissionResult(url); assertFalse(output == null); @@ -75,6 +75,7 @@ private void mockPolling() throws IOException { String placeChange = ExampleJson.processingSubmission.replace("0", "3").replace("1", "8"); String success = ExampleJson.successfulSubmission; - when(tmcApi.getRawTextFrom(anyString())).thenReturn(first, second, placeChange, success); + when(tmcApi.getRawTextFrom(URI.create(anyString()))) + .thenReturn(first, second, placeChange, success); } } diff --git a/src/test/java/fi/helsinki/cs/tmc/core/communication/TmcApiTest.java b/src/test/java/fi/helsinki/cs/tmc/core/communication/TmcApiTest.java index a2b1d669..29816a0b 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/communication/TmcApiTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/communication/TmcApiTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.argThat; import static org.mockito.Matchers.contains; import static org.mockito.Matchers.eq; @@ -29,7 +30,9 @@ import org.powermock.modules.junit4.PowerMockRunner; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; +import org.mockito.ArgumentMatcher; @RunWith(PowerMockRunner.class) @PrepareForTest(UrlCommunicator.class) @@ -52,26 +55,26 @@ public void setup() throws IOException, TmcCoreException { HttpResult fakeResult = new HttpResult(ExampleJson.allCoursesExample, 200, true); - Mockito.when(urlCommunicator.makeGetRequest(anyString(), anyString())) + Mockito.when(urlCommunicator.makeGetRequest(URI.create(anyString()), anyString())) .thenReturn(fakeResult); - Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(anyString())) + Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(URI.create(anyString()))) .thenReturn(fakeResult); } private void mockSubmissionUrl() throws IOException { HttpResult fakeResult = new HttpResult(ExampleJson.successfulSubmission, 200, true); - Mockito.when(urlCommunicator.makeGetRequest(anyString(), anyString())) + Mockito.when(urlCommunicator.makeGetRequest(URI.create(anyString()), anyString())) .thenReturn(fakeResult); - Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(anyString())) + Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(URI.create(anyString()))) .thenReturn(fakeResult); } @Test public void getsExercisesCorrectlyFromCourseJson() throws IOException, TmcCoreException { HttpResult fakeResult = new HttpResult(ExampleJson.courseExample, 200, true); - Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(eq("ankka"))) + Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(eq(URI.create("ankka")))) .thenReturn(fakeResult); - String names = tmcApi.getExerciseNames("ankka"); + String names = tmcApi.getExerciseNames(URI.create("ankka")); assertTrue(names.contains("viikko1-Viikko1_001.Nimi")); assertTrue(names.contains("viikko1-Viikko1_002.HeiMaailma")); @@ -81,9 +84,9 @@ public void getsExercisesCorrectlyFromCourseJson() throws IOException, TmcCoreEx @Test public void getsLastExerciseOfCourseJson() throws IOException, TmcCoreException { HttpResult fakeResult = new HttpResult(ExampleJson.courseExample, 200, true); - Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(eq("ankka"))) + Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(eq(URI.create("ankka")))) .thenReturn(fakeResult); - String names = tmcApi.getExerciseNames("ankka"); + String names = tmcApi.getExerciseNames(URI.create("ankka")); assertTrue(names.contains("viikko11-Viikko11_147.Laskin")); } @@ -91,20 +94,21 @@ public void getsLastExerciseOfCourseJson() throws IOException, TmcCoreException @Test public void parsesSubmissionUrlFromJson() throws IOException { HttpResult fakeResult = new HttpResult(ExampleJson.submitResponse, 200, true); - Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(anyString())) + Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(URI.create(anyString()))) .thenReturn(fakeResult); assertEquals( - "http://example.com/submissions/1781.json?api_version=7", + URI.create("http://example.com/submissions/1781.json?api_version=7"), tmcApi.getSubmissionUrl(fakeResult)); } @Test public void parsesPasteUrlFromJson() throws IOException, TmcCoreException { HttpResult fakeResult = new HttpResult(ExampleJson.pasteResponse, 200, true); - Mockito.when(urlCommunicator.makeGetRequest(Mockito.anyString(), Mockito.anyString())) + Mockito.when(urlCommunicator.makeGetRequest(URI.create(Mockito.anyString()), + Mockito.anyString())) .thenReturn(fakeResult); assertEquals( - "https://example.com/staging/paste/ynpw7_mZZGk3a9PPrMWOOQ", + URI.create("https://example.com/staging/paste/ynpw7_mZZGk3a9PPrMWOOQ"), tmcApi.getPasteUrl(fakeResult)); } @@ -112,14 +116,14 @@ public void parsesPasteUrlFromJson() throws IOException, TmcCoreException { private void mockCourse(String url) throws IOException { HttpResult fakeResult = new HttpResult(ExampleJson.courseExample, 200, true); - Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(eq(url))) + Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(eq(URI.create(url)))) .thenReturn(fakeResult); } @Test public void getsLastExerciseOfCourseJson2() throws IOException, TmcCoreException { mockCourse(realAddress); - String names = tmcApi.getExerciseNames(realAddress); + String names = tmcApi.getExerciseNames(URI.create(realAddress)); assertTrue(names.contains("viikko11-Viikko11_147.Laskin")); } @@ -127,7 +131,7 @@ public void getsLastExerciseOfCourseJson2() throws IOException, TmcCoreException @Test public void canFetchOneCourse() throws IOException, TmcCoreException, URISyntaxException { HttpResult fakeResult = new HttpResult(ExampleJson.courseExample, 200, true); - Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(contains("/courses/3"))) + Mockito.when(urlCommunicator.makeGetRequestWithAuthentication(argThat(new UriContains("/courses/3")))) .thenReturn(fakeResult); Optional course = tmcApi.getCourse(3); @@ -138,8 +142,9 @@ public void canFetchOneCourse() throws IOException, TmcCoreException, URISyntaxE @Test public void canFetchSubmissionData() throws IOException, TmcCoreException { mockSubmissionUrl(); - SubmissionResult result = tmcApi.getSubmissionResult("http://real.address.fi"); + SubmissionResult result = tmcApi.getSubmissionResult(URI.create("http://real.address.fi")); assertNotNull(result); assertEquals("2014-mooc-no-deadline", result.getCourse()); } + } diff --git a/src/test/java/fi/helsinki/cs/tmc/core/communication/UriContains.java b/src/test/java/fi/helsinki/cs/tmc/core/communication/UriContains.java new file mode 100644 index 00000000..d7541104 --- /dev/null +++ b/src/test/java/fi/helsinki/cs/tmc/core/communication/UriContains.java @@ -0,0 +1,23 @@ +package fi.helsinki.cs.tmc.core.communication; + +import java.net.URI; +import org.mockito.ArgumentMatcher; + +/** + * Custom argument matcher for testing URI substring. + */ +public class UriContains extends ArgumentMatcher { + private final String search; + + public UriContains(String search) { + this.search = search; + } + + @Override + public boolean matches(Object argument) { + if (argument == null) { + return search == null; + } + return argument.toString().contains(search); + } +} diff --git a/src/test/java/fi/helsinki/cs/tmc/core/communication/UrlCommunicatorTest.java b/src/test/java/fi/helsinki/cs/tmc/core/communication/UrlCommunicatorTest.java index 8e7da07d..9201ac87 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/communication/UrlCommunicatorTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/communication/UrlCommunicatorTest.java @@ -26,6 +26,7 @@ import java.io.File; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; @@ -74,21 +75,22 @@ public void setUpWireMock() { @Test public void okWithValidParams() throws IOException, TmcCoreException { - HttpResult result = urlCommunicator.makeGetRequest(serverAddress, "test:1234"); + HttpResult result = urlCommunicator.makeGetRequest(URI.create(serverAddress), "test:1234"); assertEquals(200, result.getStatusCode()); } @Test public void badRequestWithoutValidUrl() throws IOException, TmcCoreException { - HttpResult result = - urlCommunicator.makeGetRequest(serverAddress + "/vaaraurl", "test:1234"); + HttpResult result = urlCommunicator.makeGetRequest( + URI.create(serverAddress + "/vaaraurl"), + "test:1234"); assertEquals(400, result.getStatusCode()); } @Test public void notFoundWithoutValidParams() throws IOException, TmcCoreException { HttpResult result = - urlCommunicator.makeGetRequest(serverAddress, "ihanvaaraheaderi:1234"); + urlCommunicator.makeGetRequest(URI.create(serverAddress), "ihanvaaraheaderi:1234"); assertEquals(403, result.getStatusCode()); } @@ -100,7 +102,7 @@ public void httpPostAddsFileToRequest() throws IOException, TmcCoreException { HttpResult result = urlCommunicator.makePostWithFile( new FileBody(testFile), - serverAddress + "/kivaurl", + URI.create(serverAddress + "/kivaurl"), new HashMap()); assertEquals("All tests passed", result.getData()); @@ -116,7 +118,7 @@ public void httpPostAddsCommentToRequest() throws IOException, TmcCoreException HttpResult result = urlCommunicator.makePostWithFileAndParams( new FileBody(testFile), - serverAddress + "/kivaurl", + URI.create(serverAddress + "/kivaurl"), new HashMap(), params); @@ -125,7 +127,7 @@ public void httpPostAddsCommentToRequest() throws IOException, TmcCoreException @Test(expected = IOException.class) public void badGetRequestIsThrown() throws IOException, TmcCoreException { - urlCommunicator.makeGetRequest("asasdasd", "chang:/\\\\eiparas"); + urlCommunicator.makeGetRequest(URI.create("asasdasd"), "chang:/\\\\eiparas"); } @Test @@ -134,8 +136,9 @@ public void makePutRequestSendsPut() throws IOException, TmcCoreException, URISy settings.setPassword("1234"); Map body = new HashMap<>(); body.put("mark_as_read", "1"); - HttpResult makePutRequest = - urlCommunicator.makePutRequest(serverAddress + "/putty", Optional.of(body)); + HttpResult makePutRequest = urlCommunicator.makePutRequest( + URI.create(serverAddress + "/putty"), + Optional.of(body)); assertEquals(200, makePutRequest.getStatusCode()); } @@ -148,7 +151,7 @@ public void makePutRequestHasCorrectHeaders() body.put("mark_as_read", "1"); HttpResult makePutRequest = urlCommunicator.makePutRequest( - serverAddress + "/putty_with_headers", Optional.of(body)); + URI.create(serverAddress + "/putty_with_headers"), Optional.of(body)); assertEquals(200, makePutRequest.getStatusCode()); } } diff --git a/src/test/java/fi/helsinki/cs/tmc/core/domain/CourseTest.java b/src/test/java/fi/helsinki/cs/tmc/core/domain/CourseTest.java index 836c213f..d01c1dcb 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/domain/CourseTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/domain/CourseTest.java @@ -1,5 +1,6 @@ package fi.helsinki.cs.tmc.core.domain; +import java.net.URI; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -28,7 +29,7 @@ public void setUp() { course = new Course(); course.setId(id); course.setName(name); - course.setDetailsUrl("http://mooc.fi/"); + course.setDetailsUrl(URI.create("http://mooc.fi/")); exercises = new ArrayList<>(); ex = new Exercise(); @@ -75,13 +76,13 @@ public void testSetExercises() { @Test public void testGetDetailsUrl() { - assertEquals("http://mooc.fi/", course.getDetailsUrl()); + assertEquals(URI.create("http://mooc.fi/"), course.getDetailsUrl()); } @Test public void testSetDetailsUrl() { - course.setDetailsUrl("http://cs.helsinki.fi"); - assertEquals("http://cs.helsinki.fi", course.getDetailsUrl()); + course.setDetailsUrl(URI.create("http://cs.helsinki.fi")); + assertEquals(URI.create("http://cs.helsinki.fi"), course.getDetailsUrl()); } @Test diff --git a/src/test/java/fi/helsinki/cs/tmc/core/domain/ReviewTest.java b/src/test/java/fi/helsinki/cs/tmc/core/domain/ReviewTest.java index bd9a0914..f19d5fc9 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/domain/ReviewTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/domain/ReviewTest.java @@ -22,6 +22,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; @@ -30,8 +31,9 @@ @PrepareForTest(UrlCommunicator.class) public class ReviewTest { - private static final String updateUrl = "http://test.mooc.duck.fi/courses/47/reviews/8"; - private static final String putUrl = updateUrl + ".json?api_version=7"; + private static final URI updateUrl = + URI.create("http://test.mooc.duck.fi/courses/47/reviews/8"); + private static final URI putUrl = URI.create(updateUrl + ".json?api_version=7"); private Review review; @Before @@ -50,7 +52,7 @@ public void setUpReview() { review.setReviewerName("Samu"); review.setSubmissionId(2); review.setUpdatedAt("09.6.2015"); - review.setUrl("http://localhost:8080/url"); + review.setUrl(URI.create("http://localhost:8080/url")); } @Test @@ -73,7 +75,7 @@ public void markAsReadTest() throws IOException, TmcCoreException, URISyntaxExce private UrlCommunicator mockUrlCommunicator() throws IOException, URISyntaxException { UrlCommunicator urlcom = Mockito.mock(UrlCommunicator.class); - when(urlcom.makePutRequest(Mockito.anyString(), Mockito.any(Optional.class))) + when(urlcom.makePutRequest(URI.create(Mockito.anyString()), Mockito.any(Optional.class))) .thenReturn(new HttpResult("OK", 200, true)); return urlcom; } @@ -128,7 +130,7 @@ public void testGetPointsNotAwarded() { @Test public void testGetUrl() { - assertEquals("http://localhost:8080/url", review.getUrl()); + assertEquals(URI.create("http://localhost:8080/url"), review.getUrl()); } @Test diff --git a/src/test/java/fi/helsinki/cs/tmc/core/spyware/DiffSenderTest.java b/src/test/java/fi/helsinki/cs/tmc/core/spyware/DiffSenderTest.java index 82f32b00..454b2c8c 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/spyware/DiffSenderTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/spyware/DiffSenderTest.java @@ -24,6 +24,7 @@ import java.io.File; import java.io.IOException; +import java.net.URI; import java.util.Arrays; import java.util.List; @@ -34,7 +35,7 @@ public class DiffSenderTest { @Rule public ExpectedException exceptedEx = ExpectedException.none(); - private String spywareUrl; + private URI spywareUrl; private DiffSender sender; private CoreTestSettings settings; @@ -49,7 +50,7 @@ public void setup() throws IOException { settings.setPassword("1234"); serverAddress += wireMockRule.port(); - spywareUrl = serverAddress + "/spyware"; + spywareUrl = URI.create(serverAddress + "/spyware"); sender = new DiffSender(settings); startWiremock(); @@ -73,7 +74,7 @@ public void requestWithInvalidParams() throws IOException, TmcCoreException { DiffSender sender = new DiffSender(settings); - HttpResult res = sender.sendToUrl(byteArray, "vaaraUrl"); + HttpResult res = sender.sendToUrl(byteArray, URI.create("vaaraUrl")); assertEquals(500, res.getStatusCode()); } diff --git a/src/test/java/fi/helsinki/cs/tmc/core/updates/ExerciseUpdaterTest.java b/src/test/java/fi/helsinki/cs/tmc/core/updates/ExerciseUpdaterTest.java index e5e0d840..fb251f43 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/updates/ExerciseUpdaterTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/updates/ExerciseUpdaterTest.java @@ -20,19 +20,12 @@ import fi.helsinki.cs.tmc.core.testhelpers.ExampleJson; import fi.helsinki.cs.tmc.core.testhelpers.builders.ExerciseBuilder; -import com.google.gson.Gson; - -import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; - -import java.io.File; -import java.io.FileWriter; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -50,7 +43,7 @@ public void setUp() throws IOException, TmcCoreException { builder = new ExerciseBuilder(); urlCommunicator = mock(UrlCommunicator.class); - when(urlCommunicator.makeGetRequest(anyString(), any(String.class))) + when(urlCommunicator.makeGetRequest(URI.create(anyString()), any(String.class))) .thenReturn(new HttpResult(ExampleJson.courseExample, 200, true)); } diff --git a/src/test/java/fi/helsinki/cs/tmc/core/updates/ReviewHandlerTest.java b/src/test/java/fi/helsinki/cs/tmc/core/updates/ReviewHandlerTest.java index f415e0aa..813b7c7f 100644 --- a/src/test/java/fi/helsinki/cs/tmc/core/updates/ReviewHandlerTest.java +++ b/src/test/java/fi/helsinki/cs/tmc/core/updates/ReviewHandlerTest.java @@ -19,6 +19,7 @@ import org.mockito.Mockito; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import java.util.List; @@ -32,7 +33,7 @@ public void setUp() throws IOException, URISyntaxException { tmcApi = Mockito.mock(TmcApi.class); handler = new ReviewHandler(tmcApi); - Mockito.when(tmcApi.getReviews(anyString())) + Mockito.when(tmcApi.getReviews(URI.create(anyString()))) .thenReturn( new ReviewListBuilder() .withExercise(3, true) @@ -44,14 +45,14 @@ public void setUp() throws IOException, URISyntaxException { @Test public void fetchReviewReturnsEmptyListIfServerSendsNull() throws IOException, URISyntaxException { - Mockito.when(tmcApi.getReviews(anyString())).thenReturn(null); + Mockito.when(tmcApi.getReviews(URI.create(anyString()))).thenReturn(null); assertNotNull(handler.fetchFromServer(new Course())); assertEquals(0, handler.fetchFromServer(new Course()).size()); } @Test public void reviewsFetchedFromCorrectUrl() throws Exception { - String url = "www.example.com/reviews"; + URI url = URI.create("www.example.com/reviews"); Course course = new Course(); course.setReviewsUrl(url); handler.getNewObjects(course);