diff --git a/build.gradle b/build.gradle index 5781d2e..a7085e3 100644 --- a/build.gradle +++ b/build.gradle @@ -11,9 +11,9 @@ dependencies { compile "commons-codec:commons-codec:1.9" compile "commons-lang:commons-lang:2.6" compile "commons-logging:commons-logging:1.2" - compile "org.apache.httpcomponents:httpcore:4.3.2" - compile "org.apache.httpcomponents:httpmime:4.3.5" - compile "org.apache.httpcomponents:httpclient:4.3.5" + compile "org.apache.httpcomponents:httpcore:4.4.4" + compile "org.apache.httpcomponents:httpmime:4.5.2" + compile "org.apache.httpcomponents:httpclient:4.5.2" compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3' compile 'com.fasterxml.jackson.core:jackson-core:2.2.3' compile 'com.fasterxml.jackson.core:jackson-annotations:2.2.3' @@ -22,7 +22,7 @@ dependencies { jar { baseName = 'jDeployR' - version = '8.0.0' + version = '8.0.5' } javadoc { diff --git a/examples/tutorial/build.gradle b/examples/tutorial/build.gradle index fd9932e..404d454 100644 --- a/examples/tutorial/build.gradle +++ b/examples/tutorial/build.gradle @@ -4,7 +4,7 @@ apply plugin: 'application' sourceCompatibility = "1.6" targetCompatibility = "1.6" -applicationDefaultJvmArgs = ["-Dendpoint=http://localhost:8000/deployr", +applicationDefaultJvmArgs = ["-Dendpoint=http://localhost:8050/deployr", "-Dusername=testuser", "-Dpassword=ADD_TESTUSER_PASSWORD"] @@ -17,9 +17,9 @@ dependencies { compile "commons-codec:commons-codec:1.9" compile "commons-lang:commons-lang:2.6" compile "commons-logging:commons-logging:1.2" - compile "org.apache.httpcomponents:httpcore:4.3.2" - compile "org.apache.httpcomponents:httpmime:4.3.5" - compile "org.apache.httpcomponents:httpclient:4.3.5" + compile "org.apache.httpcomponents:httpcore:4.4.4" + compile "org.apache.httpcomponents:httpmime:4.5.2" + compile "org.apache.httpcomponents:httpclient:4.5.2" compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3' compile 'com.fasterxml.jackson.core:jackson-core:2.2.3' compile 'com.fasterxml.jackson.core:jackson-annotations:2.2.3' @@ -28,7 +28,7 @@ dependencies { jar { baseName = 'client-tutorial' - version = '8.0.0' + version = '8.0.5' } task execute(type: JavaExec) { diff --git a/src/main/java/com/revo/deployr/client/RClient.java b/src/main/java/com/revo/deployr/client/RClient.java index 6b60541..e8a4006 100644 --- a/src/main/java/com/revo/deployr/client/RClient.java +++ b/src/main/java/com/revo/deployr/client/RClient.java @@ -197,27 +197,6 @@ public URL renderScript(String scriptName, throws RClientException, RDataException; - /** - * Execute a repository-managed shell script. Execution occurs on the - * DeployR server. The response captures the line-by-line console output - * generated by the execution of the shell script on the server. - * Only shell scripts created by an ADMIN user on the DeployR server - * can be executed on this call. All attempts to execute shell scripts - * not created by an ADMIN user will be rejected. Access to shell - * scripts is also governed by standard repository access controls. - *

- * @throws RClientException if RClient fails to complete call. - * @throws RSecurityException if DeployR server security conditions not met on call. - */ - public List executeShell(String shellName, - String shellDirectory, - String shellAuthor, - String shellVersion, - String args) - throws RClientException, - RSecurityException, - RDataException; - /** * Interrupts the current execution on the HTTP blackbox project associated * with the current HTTP session. diff --git a/src/main/java/com/revo/deployr/client/about/RRepositoryFileDetails.java b/src/main/java/com/revo/deployr/client/about/RRepositoryFileDetails.java index be1a58e..e4749a6 100644 --- a/src/main/java/com/revo/deployr/client/about/RRepositoryFileDetails.java +++ b/src/main/java/com/revo/deployr/client/about/RRepositoryFileDetails.java @@ -30,7 +30,7 @@ public RRepositoryFileDetails(String filename, String directory, String restricted, boolean shared, boolean published, List authors, String inputs, String outputs, String tags, RRepositoryFile.Category category, - String md5, Date lastModified) { + String sha256, Date lastModified) { this.filename = filename; this.directory = directory; this.author = author; @@ -48,7 +48,8 @@ public RRepositoryFileDetails(String filename, String directory, this.outputs = outputs; this.tags = tags; this.category = category; - this.md5 = md5; + this.sha256 = sha256; + this.md5 = this.sha256; // @deprecated - as of 8.0.5 server, use sha256 this.lastModified = lastModified; this.url = url; } @@ -142,8 +143,14 @@ public RRepositoryFileDetails(String filename, String directory, */ public final RRepositoryFile.Category category; + /** + * Repository file 256 checksum. + */ + public final String sha256; + /** * Repository file md5 checksum. + * @deprecated As of release 8.0.5 server, replaced by {@link #sha256} */ public final String md5; @@ -156,6 +163,4 @@ public RRepositoryFileDetails(String filename, String directory, * Repository file url. */ public final URL url; - - } diff --git a/src/main/java/com/revo/deployr/client/about/RUserDetails.java b/src/main/java/com/revo/deployr/client/about/RUserDetails.java index 6b37625..4bb0a38 100644 --- a/src/main/java/com/revo/deployr/client/about/RUserDetails.java +++ b/src/main/java/com/revo/deployr/client/about/RUserDetails.java @@ -18,11 +18,16 @@ public class RUserDetails { - public RUserDetails(String username, String displayname, String cookie, RUserLimitDetails limits) { + public RUserDetails(String username, + String displayname, + String cookie, + String csrf, + RUserLimitDetails limits) { this.username = username; this.displayname = displayname; this.cookie = cookie; + this.csrf = csrf; this.limits = limits; } @@ -45,4 +50,9 @@ public RUserDetails(String username, String displayname, String cookie, RUserLim * About limits for currently authenticated user. */ public final RUserLimitDetails limits; + + /** + * The `X_CSRF_TOKEN` value. + */ + public final String csrf; } diff --git a/src/main/java/com/revo/deployr/client/call/AbstractCall.java b/src/main/java/com/revo/deployr/client/call/AbstractCall.java index 35d9ee2..23b3e8b 100644 --- a/src/main/java/com/revo/deployr/client/call/AbstractCall.java +++ b/src/main/java/com/revo/deployr/client/call/AbstractCall.java @@ -51,10 +51,9 @@ public abstract class AbstractCall implements RCall, RCoreResponse { protected HttpClient httpClient; protected String serverUrl; protected Future future; - protected HttpUriRequest httpUriRequest; - - + protected HttpUriRequest httpUriRequest; protected Map httpParams = new HashMap(); + protected Map httpHeaders = new HashMap(); /** * Method, internal use only. @@ -118,6 +117,12 @@ public boolean isCompleted() { return future.isDone(); } + public void addHeader(String name, String value) { + if (value != null) { + httpHeaders.put(name, value); + } + } + /* * Protected implementation making HTTP Request. */ @@ -164,17 +169,18 @@ protected RCoreResult makeRequest(HttpUriRequest httpUriRequest, String API) { RCoreResultImpl pResult = null; try { + // set any custom headers on the request + for (Map.Entry entry : httpHeaders.entrySet()) { + httpUriRequest.addHeader(entry.getKey(), entry.getValue()); + } HttpResponse response = httpClient.execute(this.httpUriRequest); StatusLine statusLine = response.getStatusLine(); HttpEntity responseEntity = response.getEntity(); String markup = EntityUtils.toString(responseEntity); - - pResult = new RCoreResultImpl(); - - pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase()); - + pResult = new RCoreResultImpl(response.getAllHeaders()); + pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase()); } catch (UnsupportedEncodingException ueex) { log.warn("AbstractCall: makeRequest unsupported encoding exception=" + ueex); } catch (IOException ioex) { diff --git a/src/main/java/com/revo/deployr/client/call/project/ProjectDirectoryUploadCall.java b/src/main/java/com/revo/deployr/client/call/project/ProjectDirectoryUploadCall.java index edd6907..7524b30 100644 --- a/src/main/java/com/revo/deployr/client/call/project/ProjectDirectoryUploadCall.java +++ b/src/main/java/com/revo/deployr/client/call/project/ProjectDirectoryUploadCall.java @@ -37,6 +37,7 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.concurrent.Callable; /** @@ -72,7 +73,7 @@ public RCoreResult call() { try { HttpPost httpPost = new HttpPost(serverUrl + API); - super.httpUriRequest = httpPost; + super.httpUriRequest = httpPost; List postParams = new ArrayList(); postParams.add(new BasicNameValuePair("format", "json")); @@ -91,12 +92,17 @@ public RCoreResult call() { httpPost.setEntity(entity); + // set any custom headers on the request + for (Map.Entry entry : httpHeaders.entrySet()) { + httpPost.addHeader(entry.getKey(), entry.getValue()); + } + HttpResponse response = httpClient.execute(httpPost); StatusLine statusLine = response.getStatusLine(); HttpEntity responseEntity = response.getEntity(); String markup = EntityUtils.toString(responseEntity); - - pResult = new RCoreResultImpl(); + + pResult = new RCoreResultImpl(response.getAllHeaders()); pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase()); } catch (UnsupportedEncodingException ueex) { diff --git a/src/main/java/com/revo/deployr/client/call/project/ProjectImportCall.java b/src/main/java/com/revo/deployr/client/call/project/ProjectImportCall.java index 114e252..922b359 100644 --- a/src/main/java/com/revo/deployr/client/call/project/ProjectImportCall.java +++ b/src/main/java/com/revo/deployr/client/call/project/ProjectImportCall.java @@ -36,6 +36,7 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.concurrent.Callable; /** @@ -83,12 +84,17 @@ public RCoreResult call() { httpPost.setEntity(entity); + // set any custom headers on the request + for (Map.Entry entry : httpHeaders.entrySet()) { + httpPost.addHeader(entry.getKey(), entry.getValue()); + } + HttpResponse response = httpClient.execute(httpPost); StatusLine statusLine = response.getStatusLine(); HttpEntity responseEntity = response.getEntity(); String markup = EntityUtils.toString(responseEntity); - - pResult = new RCoreResultImpl(); + + pResult = new RCoreResultImpl(response.getAllHeaders()); pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase()); } catch (UnsupportedEncodingException ueex) { diff --git a/src/main/java/com/revo/deployr/client/call/project/ProjectWorkspaceUploadCall.java b/src/main/java/com/revo/deployr/client/call/project/ProjectWorkspaceUploadCall.java index 4122b17..bcea108 100644 --- a/src/main/java/com/revo/deployr/client/call/project/ProjectWorkspaceUploadCall.java +++ b/src/main/java/com/revo/deployr/client/call/project/ProjectWorkspaceUploadCall.java @@ -36,6 +36,7 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.concurrent.Callable; /** @@ -86,12 +87,17 @@ public RCoreResult call() { httpPost.setEntity(entity); + // set any custom headers on the request + for (Map.Entry entry : httpHeaders.entrySet()) { + httpPost.addHeader(entry.getKey(), entry.getValue()); + } + HttpResponse response = httpClient.execute(httpPost); StatusLine statusLine = response.getStatusLine(); HttpEntity responseEntity = response.getEntity(); String markup = EntityUtils.toString(responseEntity); - pResult = new RCoreResultImpl(); + pResult = new RCoreResultImpl(response.getAllHeaders()); pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase()); } catch (UnsupportedEncodingException ueex) { diff --git a/src/main/java/com/revo/deployr/client/call/repository/RepositoryDirectoryUploadCall.java b/src/main/java/com/revo/deployr/client/call/repository/RepositoryDirectoryUploadCall.java index 176f979..f1cde13 100644 --- a/src/main/java/com/revo/deployr/client/call/repository/RepositoryDirectoryUploadCall.java +++ b/src/main/java/com/revo/deployr/client/call/repository/RepositoryDirectoryUploadCall.java @@ -37,6 +37,7 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.concurrent.Callable; /** @@ -96,12 +97,17 @@ public RCoreResult call() { httpPost.setEntity(entity); + // set any custom headers on the request + for (Map.Entry entry : httpHeaders.entrySet()) { + httpPost.addHeader(entry.getKey(), entry.getValue()); + } + HttpResponse response = httpClient.execute(httpPost); StatusLine statusLine = response.getStatusLine(); HttpEntity responseEntity = response.getEntity(); String markup = EntityUtils.toString(responseEntity); - pResult = new RCoreResultImpl(); + pResult = new RCoreResultImpl(response.getAllHeaders()); pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase()); } catch (UnsupportedEncodingException ueex) { diff --git a/src/main/java/com/revo/deployr/client/call/repository/RepositoryFileUploadCall.java b/src/main/java/com/revo/deployr/client/call/repository/RepositoryFileUploadCall.java index 2af0610..7ec2214 100644 --- a/src/main/java/com/revo/deployr/client/call/repository/RepositoryFileUploadCall.java +++ b/src/main/java/com/revo/deployr/client/call/repository/RepositoryFileUploadCall.java @@ -37,6 +37,7 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.concurrent.Callable; /** @@ -100,12 +101,17 @@ public RCoreResult call() { httpPost.setEntity(entity); + // set any custom headers on the request + for (Map.Entry entry : httpHeaders.entrySet()) { + httpPost.addHeader(entry.getKey(), entry.getValue()); + } + HttpResponse response = httpClient.execute(httpPost); StatusLine statusLine = response.getStatusLine(); HttpEntity responseEntity = response.getEntity(); String markup = EntityUtils.toString(responseEntity); - - pResult = new RCoreResultImpl(); + + pResult = new RCoreResultImpl(response.getAllHeaders()); pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase()); } catch (UnsupportedEncodingException ueex) { diff --git a/src/main/java/com/revo/deployr/client/call/repository/RepositoryShellExecuteCall.java b/src/main/java/com/revo/deployr/client/call/repository/RepositoryShellExecuteCall.java deleted file mode 100644 index 25ef4f0..0000000 --- a/src/main/java/com/revo/deployr/client/call/repository/RepositoryShellExecuteCall.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * RepositoryShellExecuteCall.java - * - * Copyright (C) 2010-2016, Microsoft Corporation - * - * This program is licensed to you under the terms of Version 2.0 of the - * Apache License. This program is distributed WITHOUT - * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, - * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the - * Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0) for more details. - * - */ -package com.revo.deployr.client.call.repository; - -import com.revo.deployr.client.core.RCoreResult; -import com.revo.deployr.client.call.AbstractCall; -import com.revo.deployr.client.core.REndpoints; - -import java.util.concurrent.Callable; - -/** - * Provides support for DeployR API call: /r/repository/shell/execute. - *

- * Simply construct an instance of this call and pass it on the - * executeShell() method of your {@link com.revo.deployr.client.RClient}. - */ -public class RepositoryShellExecuteCall extends AbstractCall - implements Callable { - - private final String API = REndpoints.RREPOSITORYSHELLEXECUTE; - - public RepositoryShellExecuteCall(String shellName, - String shellDirectory, - String shellAuthor, - String shellVersion, - String shellArgs) { - - httpParams.put("filename", shellName); - httpParams.put("directory", shellDirectory); - httpParams.put("author", shellAuthor); - httpParams.put("version", shellVersion); - httpParams.put("args", shellArgs); - httpParams.put("format", "json"); - } - - /** - * Internal use only, to execute call use RClient.executeShell(). - */ - public RCoreResult call() { - - return makePostRequest(API); - } - -} diff --git a/src/main/java/com/revo/deployr/client/core/RCoreResult.java b/src/main/java/com/revo/deployr/client/core/RCoreResult.java index 365a153..720fbb6 100644 --- a/src/main/java/com/revo/deployr/client/core/RCoreResult.java +++ b/src/main/java/com/revo/deployr/client/core/RCoreResult.java @@ -17,11 +17,24 @@ import java.util.List; import java.util.Map; +import org.apache.http.Header; + /** * Represents the result of a completed DeployR API call. */ public interface RCoreResult { + /** + * Returns the HTTP Response Headers from the DeployR API call that + * generated this result. + *

+ *

+ * Available: always. + * + * @returns response headers. + */ + public Header[] getHeaders(); + /** * Returns the call result status of the DeployR API call. *

@@ -43,14 +56,14 @@ public interface RCoreResult { public String getCall(); /** - * Returns the HTTP session cookie associated with the DeployR API call. + * Returns a unique id for the request. *

*

- * Available: on LoginCall. + * Available: always. * * @returns String */ - public String getCookie(); + public String getUid(); /** * Returns identity information on currently logged-in user making the DeployR API call. @@ -132,15 +145,6 @@ public interface RCoreResult { */ public List getRepoDirectories(); - /** - * Returns console output generated by a repository-managed - * shell script execution on the DeployR API call. - *

- * - * @returns List - */ - public List getRepoShellConsoleOutput(); - /** * Returns job on the DeployR API call. *

diff --git a/src/main/java/com/revo/deployr/client/core/impl/RClientImpl.java b/src/main/java/com/revo/deployr/client/core/impl/RClientImpl.java index d6b1354..3aba8bc 100644 --- a/src/main/java/com/revo/deployr/client/core/impl/RClientImpl.java +++ b/src/main/java/com/revo/deployr/client/core/impl/RClientImpl.java @@ -23,7 +23,6 @@ import com.revo.deployr.client.call.user.AutosaveCall; import com.revo.deployr.client.call.project.ProjectCreateCall; import com.revo.deployr.client.call.repository.RepositoryScriptExecuteCall; -import com.revo.deployr.client.call.repository.RepositoryShellExecuteCall; import com.revo.deployr.client.call.repository.RepositoryScriptInterruptCall; import com.revo.deployr.client.RClientException; import com.revo.deployr.client.RDataException; @@ -52,8 +51,10 @@ import com.revo.deployr.client.util.REntityUtil; +import org.apache.http.Header; import org.apache.http.HttpVersion; import org.apache.http.client.HttpClient; +import org.apache.http.client.CookieStore; import org.apache.http.params.HttpParams; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; @@ -70,6 +71,7 @@ import org.apache.http.client.params.CookiePolicy; import org.apache.http.client.params.ClientPNames; import org.apache.http.client.utils.URIBuilder; +import org.apache.http.cookie.Cookie; import javax.net.ssl.SSLContext; import org.apache.http.conn.ssl.*; import org.apache.http.config.*; @@ -97,11 +99,13 @@ public class RClientImpl implements RClient, RClientExecutor { private Log log = LogFactory.getLog(RClientImpl.class); + private final static String XSRF_HEADER = "X-XSRF-TOKEN"; - private HttpClient httpClient; + private DefaultHttpClient httpClient; private ExecutorService eService; private String serverurl; private String httpcookie; + private String csrf; private SSLSocketFactory sslSocketFactory; private RLiveContext liveContext; @@ -217,11 +221,28 @@ public RUser login(RAuthentication rAuthentication, boolean disableautosave) Map identity = rResult.getIdentity(); Map limits = rResult.getLimits(); - this.httpcookie = rResult.getCookie(); - - RUserDetails userDetails = REntityUtil.getUserDetails(identity, limits); + // + // Store cookie from response header, we no longer return this value in + // the DeployR response markup as of `8.0.5` + // + CookieStore cookieStore = httpClient.getCookieStore(); + Cookie cookie = cookieStore.getCookies().get(0); + this.httpcookie = (cookie != null ? cookie.getValue() : null); + + // + // - Store `X-XSRF-TOKEN` from `/r/uer/login` for future authenticated + // requests + // + for (Header header : rResult.getHeaders()) { + if (header.getName().equals(XSRF_HEADER)) { + this.csrf = header.getValue(); + } + } + RUserDetails userDetails = REntityUtil.getUserDetails(identity, limits, csrf); + liveContext = new RLiveContext(this, serverurl, httpcookie); + return new RUserImpl(userDetails, liveContext); } @@ -440,35 +461,6 @@ public URL renderScript(String scriptName, return renderURL; } - public List executeShell(String shellName, - String shellDirectory, - String shellAuthor, - String shellVersion, - String shellArgs) - throws RClientException, - RSecurityException, - RDataException { - - RCall rCall = new RepositoryShellExecuteCall(shellName, - shellDirectory, - shellAuthor, - shellVersion, - shellArgs); - RCoreResult rResult = processCall(rCall); - - List repoShellConsoleOutput = - rResult.getRepoShellConsoleOutput(); - - String error = rResult.getError(); - int errorCode = rResult.getErrorCode(); - boolean success = rResult.isSuccess(); - - log.debug("executeShell: success=" + success + - " error=" + error + " errorCode=" + errorCode); - - return repoShellConsoleOutput; - } - public void interruptScript() throws RClientException, RSecurityException { RCall rCall = new RepositoryScriptInterruptCall(); @@ -634,6 +626,8 @@ public InputStream download(URIBuilder builder) public RCoreResponse execute(RCall call) { AbstractCall abstractCall = (AbstractCall) call; + abstractCall.addHeader(XSRF_HEADER, csrf); + // Provide httpClient and DeployR server url context to RCall. abstractCall.setClient(httpClient, serverurl); Callable callable = (Callable) call; diff --git a/src/main/java/com/revo/deployr/client/core/impl/RCoreResultImpl.java b/src/main/java/com/revo/deployr/client/core/impl/RCoreResultImpl.java index 9db451c..1175064 100644 --- a/src/main/java/com/revo/deployr/client/core/impl/RCoreResultImpl.java +++ b/src/main/java/com/revo/deployr/client/core/impl/RCoreResultImpl.java @@ -23,8 +23,7 @@ import java.util.Iterator; import java.io.IOException; import java.io.UnsupportedEncodingException; - -// import org.codehaus.jackson.map.ObjectMapper; +import org.apache.http.Header; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.logging.Log; @@ -34,9 +33,10 @@ public class RCoreResultImpl implements RCoreResult { private Log log = LogFactory.getLog(RCoreResult.class); + private Header[] headers; private boolean success; private String call; - private String httpcookie; + private String uid; private Map identity; private Map limits; private Map project; @@ -46,7 +46,6 @@ public class RCoreResultImpl implements RCoreResult { private List repoScripts; private Map repoDirectory; private List repoDirectories; - private List repoShellConsoleOutput; private Map job; private List jobs; @@ -68,6 +67,14 @@ public class RCoreResultImpl implements RCoreResult { private String error; private int errorCode; + public RCoreResultImpl(Header[] headers) { + this.headers = headers; + } + + public Header[] getHeaders() { + return headers; + } + public boolean isSuccess() { return success; } @@ -76,9 +83,9 @@ public String getCall() { return call; } - public String getCookie() { - return httpcookie; - } + public String getUid() { + return uid; + } public Map getIdentity() { return identity; @@ -117,10 +124,6 @@ public List getRepoScripts() { return repoScripts; } - public List getRepoShellConsoleOutput() { - return repoShellConsoleOutput; - } - public Map getJob() { return job; } @@ -219,17 +222,17 @@ public void parseMarkup(String markup, String call, int statusCode, String statu Map responseMap = (Map) deployrMap.get("response"); log.debug("RCoreResult: responseMap=" + responseMap); - // Properties: success, call, httpcookie, session, error, hisotry. + // Properties: success, call, uid, session, error, hisotry. success = (Boolean) responseMap.get("success"); call = (String) responseMap.get("call"); - httpcookie = (String) responseMap.get("httpcookie"); - error = (String) responseMap.get("error"); + uid = (String) responseMap.get("uid"); + error = (String) responseMap.get("error"); if(responseMap.get("errorCode") != null) { errorCode = (Integer) responseMap.get("errorCode"); } log.debug("RCoreResult: success=" + success + " call=" + call); - log.debug("RCoreResult: httpcookie=" + httpcookie); + log.debug("RCoreResult: uid=" + uid); log.debug("RCoreResult: error=" + error + " errorCode=" + errorCode); // Property: User Identity. @@ -341,11 +344,6 @@ public void parseMarkup(String markup, String call, int statusCode, String statu repoDirectories.addAll(systemDirectories); } log.debug("RCoreResult: repoDirectories=" + repoDirectories); - - Map shellMap = (Map) repository.get("shell"); - if(shellMap != null) { - repoShellConsoleOutput = (List) shellMap.get("console"); - } } // Property: Job. diff --git a/src/main/java/com/revo/deployr/client/data/RBooleanVector.java b/src/main/java/com/revo/deployr/client/data/RBooleanVector.java index f874a63..fdbc377 100644 --- a/src/main/java/com/revo/deployr/client/data/RBooleanVector.java +++ b/src/main/java/com/revo/deployr/client/data/RBooleanVector.java @@ -22,7 +22,7 @@ public interface RBooleanVector extends RData { /** * Gets the List of Boolean values for this RData. * - * @return List<Boolean< value + * @return List<Boolean> value * @see RData */ public List getValue(); diff --git a/src/main/java/com/revo/deployr/client/util/REntityUtil.java b/src/main/java/com/revo/deployr/client/util/REntityUtil.java index 13c6206..8333235 100644 --- a/src/main/java/com/revo/deployr/client/util/REntityUtil.java +++ b/src/main/java/com/revo/deployr/client/util/REntityUtil.java @@ -30,7 +30,7 @@ public class REntityUtil { - public static RUserDetails getUserDetails(Map identity, Map limits) { + public static RUserDetails getUserDetails(Map identity, Map limits, String csrf) { String username = (String) identity.get("username"); String displayname = (String) identity.get("displayname"); @@ -42,7 +42,7 @@ public static RUserDetails getUserDetails(Map identity, Map packageNames = null; String expStatus = "success"; diff --git a/test/src/test/java/com/revo/deployr/client/api/RUserDefaultRepositoryFileCallsTest.java b/test/src/test/java/com/revo/deployr/client/api/RUserDefaultRepositoryFileCallsTest.java index dad0350..9b9eab9 100644 --- a/test/src/test/java/com/revo/deployr/client/api/RUserDefaultRepositoryFileCallsTest.java +++ b/test/src/test/java/com/revo/deployr/client/api/RUserDefaultRepositoryFileCallsTest.java @@ -1,883 +1,998 @@ -/* - * RUserDefaultRepositoryFileCallsTest.java - * - * Copyright (C) 2010-2016, Microsoft Corporation - * - * This program is licensed to you under the terms of Version 2.0 of the - * Apache License. This program is distributed WITHOUT - * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, - * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the - * Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0) for more details. - * - */ -package com.revo.deployr.client.api; - -import com.revo.deployr.DeployrUtil; -import com.revo.deployr.client.RClient; -import com.revo.deployr.client.RClientException; -import com.revo.deployr.client.RRepositoryFile; -import com.revo.deployr.client.RUser; -import com.revo.deployr.client.auth.basic.RBasicAuthentication; -import com.revo.deployr.client.factory.RClientFactory; -import com.revo.deployr.client.params.RepoUploadOptions; -import org.junit.*; - -import java.io.File; -import java.io.InputStream; -import java.io.FileInputStream; -import java.net.URL; -import java.util.List; - -import static org.junit.Assert.*; - -public class RUserDefaultRepositoryFileCallsTest { - - RClient rClient = null; - RUser rUser = null; - - public RUserDefaultRepositoryFileCallsTest() { - } - - @BeforeClass - public static void setUpClass() throws Exception { - } - - @AfterClass - public static void tearDownClass() throws Exception { - } - - @Before - public void setUp() { - try { - String url = System.getProperty("connection.protocol") + - System.getProperty("connection.endpoint"); - if (url == null) { - fail("setUp: connection.[protocol|endpoint] null."); - } - boolean allowSelfSigned = - Boolean.valueOf(System.getProperty("allow.SelfSignedSSLCert")); - rClient =RClientFactory.createClient(url, allowSelfSigned); - RBasicAuthentication rAuthentication = new RBasicAuthentication("testuser", System.getProperty("password.testuser")); - String expResultName = "testuser"; - rUser = rClient.login(rAuthentication); - assertNotNull(rUser); - - } catch (Exception ex) { - - if (rClient != null) { - rClient.release(); - } - fail("setUp: " + ex); - } - } - - @After - public void tearDown() { - if (rClient != null) { - rClient.release(); - } - } - - /** - * Test of about method, of class RRepositoryFile. - */ - @Test - public void testUserRepositoryAboutFile() { - - // Test variables. - String text = "this is a line of text"; - String actualRepoFileName = ""; - String actualRepoFileDesc = ""; - String expRepoFileName = ""; - String expRepoFileDesc = ""; - RepoUploadOptions options = null; - RRepositoryFile repoFile = null; - InputStream downStream = null; - String urlData = ""; - - // Test error handling. - Exception exception = null; - String exceptionMsg = ""; - Exception cleanupException = null; - String cleanupExceptionMsg = ""; - - // Test. - expRepoFileName = DeployrUtil.getUniqueFileName("txt"); - expRepoFileDesc = "Repository File About"; - options = new RepoUploadOptions(); - options.descr = expRepoFileDesc; - options.filename = expRepoFileName; - - try { - repoFile = rUser.writeFile(text, options); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.writeFile failed: "; - } - - if (exception == null) { - try { - actualRepoFileName = repoFile.about().filename; - actualRepoFileDesc = repoFile.about().descr; - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoFile.about failed: "; - } - } - - if (exception == null) { - try { - downStream = repoFile.download(); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoFile.download failed: "; - } - } - - if (exception == null) { - urlData = DeployrUtil.getDataFromStream(downStream); - } - - // Test cleanup. - try { - if (repoFile != null) { - repoFile.delete(); - } - } catch (Exception ex) { - cleanupException = ex; - cleanupExceptionMsg = "repoFile.delete failed: "; - } - - if (exception == null) { - // Test assertions. - assertEquals(expRepoFileName, actualRepoFileName); - assertEquals(expRepoFileDesc, actualRepoFileDesc); - assertEquals(DeployrUtil.encodeString(text), - DeployrUtil.encodeString(urlData)); - } else { - fail(exceptionMsg + exception.getMessage()); - } - - // Test cleanup errors. - if (cleanupException != null) { - fail(cleanupExceptionMsg + cleanupException.getMessage()); - } - } - - /** - * Test of listFiles method, of class RUserRepositoryFileCalls. - */ - @Test - public void testUserRepositoryListFiles() { - - // Test variables. - String text = "this is a line of text"; - String actualRepoFileName = ""; - String actualRepoFileDesc = ""; - String expRepoFileName = ""; - String expRepoFileDesc = ""; - RepoUploadOptions options = null; - RRepositoryFile repoFile = null; - InputStream downStream = null; - String urlData = ""; - List listFiles = null; - boolean repoFileFound = false; - - // Test error handling. - Exception exception = null; - String exceptionMsg = ""; - Exception cleanupException = null; - String cleanupExceptionMsg = ""; - - // Test. - expRepoFileName = DeployrUtil.getUniqueFileName("txt"); - expRepoFileDesc = "Repository File List"; - options = new RepoUploadOptions(); - options.descr = expRepoFileDesc; - options.filename = expRepoFileName; - - try { - repoFile = rUser.writeFile(text, options); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.writeFile failed: "; - } - - if (exception == null) { - try { - actualRepoFileName = repoFile.about().filename; - actualRepoFileDesc = repoFile.about().descr; - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoFile.about failed: "; - } - } - - if (exception == null) { - try { - downStream = repoFile.download(); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoFile.download failed: "; - } - } - - if (exception == null) { - try { - listFiles = rUser.listFiles(); - // search for filename - for (RRepositoryFile file : listFiles) { - if (file.about().filename.equalsIgnoreCase(expRepoFileName)) { - repoFileFound = true; - break; - } - } - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.listFiles failed: "; - } - } - - if (exception == null) { - urlData = DeployrUtil.getDataFromStream(downStream); - } - - // Test cleanup. - try { - if (repoFile != null) { - repoFile.delete(); - } - } catch (Exception ex) { - cleanupException = ex; - cleanupExceptionMsg = "repoFile.delete failed: "; - } - - if (exception == null) { - // Test assertions. - assertTrue(listFiles.size() > 0); - assertTrue(repoFileFound); - assertEquals(expRepoFileName, actualRepoFileName); - assertEquals(expRepoFileDesc, actualRepoFileDesc); - assertEquals(DeployrUtil.encodeString(text), DeployrUtil.encodeString(urlData)); - } else { - fail(exceptionMsg + exception.getMessage()); - } - - // Test cleanup errors. - if (cleanupException != null) { - fail(cleanupExceptionMsg + cleanupException.getMessage()); - } - } - - /** - * Test RUserRepositoryFileCalls.listFiles(categoryFilter, directoryFilter). - */ - @Test - public void testUserRepositoryListFilesGoodFilters() { - - // Test variables. - List listFilesExampleDirectory = null; - List listFilesExampleScripts = null; - List listFilesExampleBinary = null; - String exampleFraudScoreDirectory = "example-fraud-score"; - int fraudExampleTotalFileCount = 2; - int fraudExampleScriptFileCount = 1; - boolean fraudExampleScriptsAreScripts = false; - int fraudExampleBinaryFileCount = 1; - boolean fraudExampleBinaryAreBinary = false; - - // Test error handling. - Exception exception = null; - String exceptionMsg = ""; - Exception cleanupException = null; - String cleanupExceptionMsg = ""; - - // Test. - try { - listFilesExampleDirectory = - rUser.listFiles((RRepositoryFile.Category) null, - exampleFraudScoreDirectory); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.listFiles(null, directory) failed: "; - } - - if(exception == null) { - - try { - listFilesExampleScripts = - rUser.listFiles(RRepositoryFile.Category.RSCRIPT, - exampleFraudScoreDirectory); - - for(RRepositoryFile scriptFile : listFilesExampleScripts) { - if(scriptFile.about().category != - RRepositoryFile.Category.RSCRIPT) { - fraudExampleScriptsAreScripts = false; - break; - } else { - fraudExampleScriptsAreScripts = true; - } - } - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.listFiles(RSCRIPT, directory) failed: "; - } - } - - if(exception == null) { - - try { - listFilesExampleBinary = - rUser.listFiles(RRepositoryFile.Category.RBINARY, - exampleFraudScoreDirectory); - - for(RRepositoryFile binFile : listFilesExampleBinary) { - if(binFile.about().category != - RRepositoryFile.Category.RBINARY) { - fraudExampleBinaryAreBinary = false; - break; - } else { - fraudExampleBinaryAreBinary = true; - } - } - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.listFiles(RSCRIPT, directory) failed: "; - } - } - - if (exception == null) { - // Test assertions. - assertEquals(fraudExampleTotalFileCount, listFilesExampleDirectory.size()); - assertEquals(fraudExampleScriptFileCount, listFilesExampleScripts.size()); - assertTrue(fraudExampleScriptsAreScripts); - assertEquals(fraudExampleBinaryFileCount, listFilesExampleBinary.size()); - assertTrue(fraudExampleBinaryAreBinary); - } else { - fail(exceptionMsg + exception.getMessage()); - } - - // Test cleanup errors. - if (cleanupException != null) { - fail(cleanupExceptionMsg + cleanupException.getMessage()); - } - } - - /** - * Test RUserRepositoryFileCalls.listFiles(categoryFilter, directoryFilter). - */ - @Test - public void testUserRepositoryListFilesBadFilters() { - - // Test variables. - List listFiles = null; - RClientException clientEx = null; - - // Test error handling. - Exception exception = null; - String exceptionMsg = ""; - Exception cleanupException = null; - String cleanupExceptionMsg = ""; - - // Test. - try { - listFiles = - rUser.listFiles((RRepositoryFile.Category) null, - "dir-not-found"); - } catch (RClientException cex) { - clientEx = cex; - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.listFiles(null, dir-not-found) failed: "; - } - - if (exception == null) { - // Test assertions. - assertNotNull(clientEx); - } else { - fail(exceptionMsg + exception.getMessage()); - } - - // Test cleanup errors. - if (cleanupException != null) { - fail(cleanupExceptionMsg + cleanupException.getMessage()); - } - } - - /** - * Test of listFiles method, of class RUserRepositoryFileCalls. - */ - @Test - public void testUserRepositoryListFilesShowPublicFiles() { - - // Test variables. - String expRepoFileName = "Histogram of Auto Sales.R"; - List listFiles = null; - String expAuthor = "testuser"; - String actualAuthor = ""; - boolean repoFileFound = false; - boolean archived = false; - boolean shared = false; - boolean published = true; - boolean isPublished = false; - RUser rUserManager = null; - RBasicAuthentication rAuthentication = null; - - // Test error handling. - Exception exception = null; - String exceptionMsg = ""; - - // Test. - - // sign-out as testuser - String expResultName = "testuser"; - try { - rClient.logout(rUser); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rClient.logout failed: "; - } - - // sign-in as admin. Try 5 times - if (exception == null) { - rAuthentication = new RBasicAuthentication("admin", System.getProperty("password.admin")); - for (int i = 0; i < 5; i++) { - try { - Thread.sleep(1000); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "Thread.sleep failed: "; - } - if (exception == null) { - try { - rUserManager = rClient.login(rAuthentication); - } catch (Exception ex) { - if (i < 5) { - continue; - } - exception = ex; - exceptionMsg = "rClient.login failed: "; - } - } - } - } - - if (exception == null) { - try { - listFiles = rUserManager.listFiles(archived, shared, published); - // search for filename - for (RRepositoryFile file : listFiles) { - if (file.about().filename.equalsIgnoreCase(expRepoFileName)) { - repoFileFound = true; - actualAuthor = file.about().author; - isPublished = file.about().published; - break; - } - } - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.listFiles failed: "; - } - } - - if (exception == null) { - // Test assertions. - assertTrue(repoFileFound); - assertTrue(isPublished); - assertEquals(expAuthor, actualAuthor); - } else { - fail(exceptionMsg + exception.getMessage()); - } - } - - /** - * Test of uploadFile method, of class RUserRepositoryFileCalls. - */ - @Test - public void testUserRepositoryUploadFile() { - - // Test variables. - String actualRepoFileName = ""; - String actualRepoFileDesc = ""; - String expRepoFileName = ""; - String expRepoFileDesc = ""; - File file = new File("/etc/hosts"); - RepoUploadOptions options = null; - RRepositoryFile repoFile = null; - InputStream downStream = null; - String urlData = ""; - - // Test error handling. - Exception exception = null; - String exceptionMsg = ""; - Exception cleanupException = null; - String cleanupExceptionMsg = ""; - - //Test. - expRepoFileName = DeployrUtil.getUniqueFileName("txt"); - expRepoFileDesc = "hosts file"; - options = new RepoUploadOptions(); - options.descr = expRepoFileDesc; - options.filename = expRepoFileName; - - try { - repoFile = rUser.uploadFile(new FileInputStream(file), options); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.uploadFile failed: "; - } - - if (exception == null) { - try { - actualRepoFileName = repoFile.about().filename; - actualRepoFileDesc = repoFile.about().descr; - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoFile.about failed: "; - } - } - - if (exception == null) { - try { - downStream = repoFile.download(); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoFile.download failed: "; - } - } - - if (exception == null) { - urlData = DeployrUtil.getDataFromStream(downStream); - } - - // Test cleanup. - try { - if (repoFile != null) { - repoFile.delete(); - } - } catch (Exception ex) { - cleanupException = ex; - cleanupExceptionMsg = "repoFile.delete failed: "; - } - - if (exception == null) { - // Test assertions. - assertEquals(expRepoFileName, actualRepoFileName); - assertEquals(expRepoFileDesc, actualRepoFileDesc); - assertNotNull(urlData); - } else { - fail(exceptionMsg + exception.getMessage()); - } - - // Test cleanup errors. - if (cleanupException != null) { - fail(cleanupExceptionMsg + cleanupException.getMessage()); - } - } - - /** - * Test of writeFile method, of class RUserRepositoryFileCalls. - */ - @Test - public void testUserRepositoryWriteFile() { - - // Test variables. - String text = "this is a line of text"; - String actualRepoFileName = ""; - String actualRepoFileDesc = ""; - String expRepoFileName = ""; - String expRepoFileDesc = ""; - RepoUploadOptions options = null; - RRepositoryFile repoFile = null; - InputStream downStream = null; - String urlData = ""; - - // Test error handling. - Exception exception = null; - String exceptionMsg = ""; - Exception cleanupException = null; - String cleanupExceptionMsg = ""; - - // Test. - expRepoFileName = DeployrUtil.getUniqueFileName("txt"); - expRepoFileDesc = "Repository Write File"; - options = new RepoUploadOptions(); - options.descr = expRepoFileDesc; - options.filename = expRepoFileName; - - try { - repoFile = rUser.writeFile(text, options); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.writeFile failed: "; - } - - if (exception == null) { - try { - actualRepoFileName = repoFile.about().filename; - actualRepoFileDesc = repoFile.about().descr; - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoFile.about failed: "; - } - } - - if (exception == null) { - try { - downStream = repoFile.download(); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoFile.download failed: "; - } - } - - if (exception == null) { - urlData = DeployrUtil.getDataFromStream(downStream); - } - - // Test cleanup. - try { - if (repoFile != null) { - repoFile.delete(); - } - } catch (Exception ex) { - cleanupException = ex; - cleanupExceptionMsg = "repoFile.delete failed: "; - } - - if (exception == null) { - // Test assertions. - assertEquals(expRepoFileName, actualRepoFileName); - assertEquals(expRepoFileDesc, actualRepoFileDesc); - assertEquals(DeployrUtil.encodeString(text), DeployrUtil.encodeString(urlData)); - } else { - fail(exceptionMsg + exception.getMessage()); - } - - // Test cleanup errors. - if (cleanupException != null) { - fail(cleanupExceptionMsg + cleanupException.getMessage()); - } - } - - /** - * Test of writeFile method, of class RUserRepositoryFileCalls. - */ - @Test - public void testUserRepositoryDownloadFile() { - - // Test variables. - String text = "this is a line of text"; - String actualRepoFileName = ""; - String actualRepoFileDesc = ""; - String expRepoFileName = ""; - String expRepoFileDesc = ""; - RepoUploadOptions options = null; - RRepositoryFile repoFile = null; - InputStream downStream = null; - String urlData = ""; - - // Test error handling. - Exception exception = null; - String exceptionMsg = ""; - Exception cleanupException = null; - String cleanupExceptionMsg = ""; - - // Test. - expRepoFileName = DeployrUtil.getUniqueFileName("txt"); - expRepoFileDesc = "User Repository Download File"; - options = new RepoUploadOptions(); - options.descr = expRepoFileDesc; - options.filename = expRepoFileName; - - try { - repoFile = rUser.writeFile(text, options); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.writeFile failed: "; - } - - if (exception == null) { - try { - actualRepoFileName = repoFile.about().filename; - actualRepoFileDesc = repoFile.about().descr; - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoFile.about failed: "; - } - } - - if (exception == null) { - try { - downStream = repoFile.download(); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoFile.download failed: "; - } - } - - if (exception == null) { - urlData = DeployrUtil.getDataFromStream(downStream); - } - - // Test cleanup. - try { - if (repoFile != null) { - repoFile.delete(); - } - } catch (Exception ex) { - cleanupException = ex; - cleanupExceptionMsg = "repoFile.delete failed: "; - } - - if (exception == null) { - // Test assertions. - assertEquals(expRepoFileName, actualRepoFileName); - assertEquals(expRepoFileDesc, actualRepoFileDesc); - assertEquals(DeployrUtil.encodeString(text), DeployrUtil.encodeString(urlData)); - } else { - fail(exceptionMsg + exception.getMessage()); - } - - // Test cleanup errors. - if (cleanupException != null) { - fail(cleanupExceptionMsg + cleanupException.getMessage()); - } - } - - /** - * Test of transferFile method, of class RUserRepositoryFileCalls. - */ - @Test - public void testUserRepositoryTransferFile() { - - // Test variables. - String text = "this is a line of text"; - String actualRepoFileName = ""; - String actualRepoFileDesc = ""; - String actualTransFileName = ""; - String actualTransFileDesc = ""; - String expRepoFileName = ""; - String expRepoFileDesc = ""; - String expTransFileName = ""; - String expTransFileDesc = ""; - RepoUploadOptions options = null; - RRepositoryFile repoFile = null; - String urlData = ""; - String urlTransData = ""; - URL repoURL = null; - URL repoTransURL = null; - RRepositoryFile repoTransFile = null; - long repoFileSize = 0; - long repoTransFileSize = 0; - - // Test error handling. - Exception exception = null; - String exceptionMsg = ""; - Exception cleanupException = null; - String cleanupExceptionMsg = ""; - - // Test. - expRepoFileName = DeployrUtil.getUniqueFileName("txt"); - expRepoFileDesc = "User Repository Download File"; - options = new RepoUploadOptions(); - options.descr = expRepoFileDesc; - options.filename = expRepoFileName; - - try { - repoFile = rUser.writeFile(text, options); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.writeFile failed: "; - } - - if (exception == null) { - try { - actualRepoFileName = repoFile.about().filename; - actualRepoFileDesc = repoFile.about().descr; - repoFileSize = repoFile.about().size; - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoFile.about failed: "; - } - } - - if (exception == null) { - try { - repoURL = repoFile.about().url; - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoFile.download failed: "; - } - } - - if (exception == null) { - expTransFileName = DeployrUtil.getUniqueFileName("txt"); - expTransFileDesc = "User Repository Transfer File"; - options = new RepoUploadOptions(); - options.descr = expTransFileDesc; - options.filename = expTransFileName; - try { - repoTransFile = rUser.transferFile(repoURL, options); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.transferFile failed: "; - } - } - - if (exception == null) { - try { - actualTransFileName = repoTransFile.about().filename; - actualTransFileDesc = repoTransFile.about().descr; - repoTransFileSize = repoTransFile.about().size; - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoTransFile.about failed: "; - } - } - - if (exception == null) { - try { - repoTransURL = repoTransFile.about().url; - } catch (Exception ex) { - exception = ex; - exceptionMsg = "repoTransFile.download failed: "; - } - } - - // Test cleanup. - try { - if (repoFile != null) { - repoFile.delete(); - } - } catch (Exception ex) { - cleanupException = ex; - cleanupExceptionMsg = "repoFile.delete failed: "; - } - - try { - if (repoTransFile != null) { - repoTransFile.delete(); - } - } catch (Exception ex) { - cleanupException = ex; - cleanupExceptionMsg = "repoTransFile.delete failed: "; - } - - if (exception == null) { - // Test assertions. - assertEquals(expRepoFileName, actualRepoFileName); - assertEquals(expRepoFileDesc, actualRepoFileDesc); - assertEquals(repoTransFileSize, repoFileSize); - assertEquals(DeployrUtil.encodeString(urlTransData), - DeployrUtil.encodeString(urlData)); - } else { - fail(exceptionMsg + exception.getMessage()); - } - - // Test cleanup errors. - if (cleanupException != null) { - fail(cleanupExceptionMsg + cleanupException.getMessage()); - } - } -} +/* + * RUserDefaultRepositoryFileCallsTest.java + * + * Copyright (C) 2010-2016, Microsoft Corporation + * + * This program is licensed to you under the terms of Version 2.0 of the + * Apache License. This program is distributed WITHOUT + * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, + * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the + * Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0) for more details. + * + */ +package com.revo.deployr.client.api; + +import com.revo.deployr.DeployrUtil; +import com.revo.deployr.client.RClient; +import com.revo.deployr.client.RClientException; +import com.revo.deployr.client.RRepositoryFile; +import com.revo.deployr.client.RUser; +import com.revo.deployr.client.auth.basic.RBasicAuthentication; +import com.revo.deployr.client.factory.RClientFactory; +import com.revo.deployr.client.params.RepoUploadOptions; +import org.junit.*; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.InputStream; +import java.io.IOException; +import java.net.URL; +import java.util.List; + +import static org.junit.Assert.*; + +public class RUserDefaultRepositoryFileCallsTest { + + RClient rClient = null; + RUser rUser = null; + + public RUserDefaultRepositoryFileCallsTest() { + } + + @BeforeClass + public static void setUpClass() throws Exception { + } + + @AfterClass + public static void tearDownClass() throws Exception { + } + + @Before + public void setUp() { + try { + String url = System.getProperty("connection.protocol") + + System.getProperty("connection.endpoint"); + if (url == null) { + fail("setUp: connection.[protocol|endpoint] null."); + } + boolean allowSelfSigned = + Boolean.valueOf(System.getProperty("allow.SelfSignedSSLCert")); + rClient =RClientFactory.createClient(url, allowSelfSigned); + RBasicAuthentication rAuthentication = new RBasicAuthentication("testuser", System.getProperty("password.testuser")); + String expResultName = "testuser"; + rUser = rClient.login(rAuthentication); + assertNotNull(rUser); + + } catch (Exception ex) { + + if (rClient != null) { + rClient.release(); + } + fail("setUp: " + ex); + } + } + + @After + public void tearDown() { + if (rClient != null) { + rClient.release(); + } + } + + /** + * Test of about method, of class RRepositoryFile. + */ + @Test + public void testUserRepositoryAboutFile() { + + // Test variables. + String text = "this is a line of text"; + String actualRepoFileName = ""; + String actualRepoFileDesc = ""; + String actualRepoFileType = ""; + String expRepoFileName = ""; + String expRepoFileDesc = ""; + String expRepoFileType; + RepoUploadOptions options = null; + RRepositoryFile repoFile = null; + InputStream downStream = null; + String urlData = ""; + + // Test error handling. + Exception exception = null; + String exceptionMsg = ""; + Exception cleanupException = null; + String cleanupExceptionMsg = ""; + + // Test. + expRepoFileName = DeployrUtil.getUniqueFileName("txt"); + expRepoFileDesc = "Repository File About"; + expRepoFileType = "text/plain"; + options = new RepoUploadOptions(); + options.descr = expRepoFileDesc; + options.filename = expRepoFileName; + + try { + repoFile = rUser.writeFile(text, options); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.writeFile failed: "; + } + + if (exception == null) { + try { + actualRepoFileName = repoFile.about().filename; + actualRepoFileDesc = repoFile.about().descr; + actualRepoFileType = repoFile.about().type; + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.about failed: "; + } + } + + if (exception == null) { + try { + downStream = repoFile.download(); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.download failed: "; + } + } + + if (exception == null) { + urlData = DeployrUtil.getDataFromStream(downStream); + } + + // Test cleanup. + try { + if (repoFile != null) { + repoFile.delete(); + } + } catch (Exception ex) { + cleanupException = ex; + cleanupExceptionMsg = "repoFile.delete failed: "; + } + + if (exception == null) { + // Test assertions. + assertEquals(expRepoFileName, actualRepoFileName); + assertEquals(expRepoFileDesc, actualRepoFileDesc); + assertEquals(expRepoFileType, actualRepoFileType); + assertEquals(DeployrUtil.encodeString(text), + DeployrUtil.encodeString(urlData)); + } else { + fail(exceptionMsg + exception.getMessage()); + } + + // Test cleanup errors. + if (cleanupException != null) { + fail(cleanupExceptionMsg + cleanupException.getMessage()); + } + } + + /** + * Test of listFiles method, of class RUserRepositoryFileCalls. + */ + @Test + public void testUserRepositoryListFiles() { + + // Test variables. + String text = "this is a line of text"; + String actualRepoFileName = ""; + String actualRepoFileDesc = ""; + String expRepoFileName = ""; + String expRepoFileDesc = ""; + RepoUploadOptions options = null; + RRepositoryFile repoFile = null; + InputStream downStream = null; + String urlData = ""; + List listFiles = null; + boolean repoFileFound = false; + + // Test error handling. + Exception exception = null; + String exceptionMsg = ""; + Exception cleanupException = null; + String cleanupExceptionMsg = ""; + + // Test. + expRepoFileName = DeployrUtil.getUniqueFileName("txt"); + expRepoFileDesc = "Repository File List"; + options = new RepoUploadOptions(); + options.descr = expRepoFileDesc; + options.filename = expRepoFileName; + + try { + repoFile = rUser.writeFile(text, options); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.writeFile failed: "; + } + + if (exception == null) { + try { + actualRepoFileName = repoFile.about().filename; + actualRepoFileDesc = repoFile.about().descr; + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.about failed: "; + } + } + + if (exception == null) { + try { + downStream = repoFile.download(); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.download failed: "; + } + } + + if (exception == null) { + try { + listFiles = rUser.listFiles(); + // search for filename + for (RRepositoryFile file : listFiles) { + if (file.about().filename.equalsIgnoreCase(expRepoFileName)) { + repoFileFound = true; + break; + } + } + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.listFiles failed: "; + } + } + + if (exception == null) { + urlData = DeployrUtil.getDataFromStream(downStream); + } + + // Test cleanup. + try { + if (repoFile != null) { + repoFile.delete(); + } + } catch (Exception ex) { + cleanupException = ex; + cleanupExceptionMsg = "repoFile.delete failed: "; + } + + if (exception == null) { + // Test assertions. + assertTrue(listFiles.size() > 0); + assertTrue(repoFileFound); + assertEquals(expRepoFileName, actualRepoFileName); + assertEquals(expRepoFileDesc, actualRepoFileDesc); + assertEquals(DeployrUtil.encodeString(text), DeployrUtil.encodeString(urlData)); + } else { + fail(exceptionMsg + exception.getMessage()); + } + + // Test cleanup errors. + if (cleanupException != null) { + fail(cleanupExceptionMsg + cleanupException.getMessage()); + } + } + + /** + * Test RUserRepositoryFileCalls.listFiles(categoryFilter, directoryFilter). + */ + @Test + public void testUserRepositoryListFilesGoodFilters() { + + // Test variables. + List listFilesExampleDirectory = null; + List listFilesExampleScripts = null; + List listFilesExampleBinary = null; + String exampleFraudScoreDirectory = "example-fraud-score"; + int fraudExampleTotalFileCount = 2; + int fraudExampleScriptFileCount = 1; + boolean fraudExampleScriptsAreScripts = false; + int fraudExampleBinaryFileCount = 1; + boolean fraudExampleBinaryAreBinary = false; + + // Test error handling. + Exception exception = null; + String exceptionMsg = ""; + Exception cleanupException = null; + String cleanupExceptionMsg = ""; + + // Test. + try { + listFilesExampleDirectory = + rUser.listFiles((RRepositoryFile.Category) null, + exampleFraudScoreDirectory); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.listFiles(null, directory) failed: "; + } + + if(exception == null) { + + try { + listFilesExampleScripts = + rUser.listFiles(RRepositoryFile.Category.RSCRIPT, + exampleFraudScoreDirectory); + + for(RRepositoryFile scriptFile : listFilesExampleScripts) { + if(scriptFile.about().category != + RRepositoryFile.Category.RSCRIPT) { + fraudExampleScriptsAreScripts = false; + break; + } else { + fraudExampleScriptsAreScripts = true; + } + } + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.listFiles(RSCRIPT, directory) failed: "; + } + } + + if(exception == null) { + + try { + listFilesExampleBinary = + rUser.listFiles(RRepositoryFile.Category.RBINARY, + exampleFraudScoreDirectory); + + for(RRepositoryFile binFile : listFilesExampleBinary) { + if(binFile.about().category != + RRepositoryFile.Category.RBINARY) { + fraudExampleBinaryAreBinary = false; + break; + } else { + fraudExampleBinaryAreBinary = true; + } + } + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.listFiles(RSCRIPT, directory) failed: "; + } + } + + if (exception == null) { + // Test assertions. + assertEquals(fraudExampleTotalFileCount, listFilesExampleDirectory.size()); + assertEquals(fraudExampleScriptFileCount, listFilesExampleScripts.size()); + assertTrue(fraudExampleScriptsAreScripts); + assertEquals(fraudExampleBinaryFileCount, listFilesExampleBinary.size()); + assertTrue(fraudExampleBinaryAreBinary); + } else { + fail(exceptionMsg + exception.getMessage()); + } + + // Test cleanup errors. + if (cleanupException != null) { + fail(cleanupExceptionMsg + cleanupException.getMessage()); + } + } + + /** + * Test RUserRepositoryFileCalls.listFiles(categoryFilter, directoryFilter). + */ + @Test + public void testUserRepositoryListFilesBadFilters() { + + // Test variables. + List listFiles = null; + RClientException clientEx = null; + + // Test error handling. + Exception exception = null; + String exceptionMsg = ""; + Exception cleanupException = null; + String cleanupExceptionMsg = ""; + + // Test. + try { + listFiles = + rUser.listFiles((RRepositoryFile.Category) null, + "dir-not-found"); + } catch (RClientException cex) { + clientEx = cex; + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.listFiles(null, dir-not-found) failed: "; + } + + if (exception == null) { + // Test assertions. + assertNotNull(clientEx); + } else { + fail(exceptionMsg + exception.getMessage()); + } + + // Test cleanup errors. + if (cleanupException != null) { + fail(cleanupExceptionMsg + cleanupException.getMessage()); + } + } + + /** + * Test of listFiles method, of class RUserRepositoryFileCalls. + */ + @Test + public void testUserRepositoryListFilesShowPublicFiles() { + + // Test variables. + String expRepoFileName = "Histogram of Auto Sales.R"; + List listFiles = null; + String expAuthor = "testuser"; + String actualAuthor = ""; + boolean repoFileFound = false; + boolean archived = false; + boolean shared = false; + boolean published = true; + boolean isPublished = false; + RUser rUserManager = null; + RBasicAuthentication rAuthentication = null; + + // Test error handling. + Exception exception = null; + String exceptionMsg = ""; + + // Test. + + // sign-out as testuser + String expResultName = "testuser"; + try { + rClient.logout(rUser); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rClient.logout failed: "; + } + + // sign-in as admin. Try 5 times + if (exception == null) { + rAuthentication = new RBasicAuthentication("admin", System.getProperty("password.admin")); + for (int i = 0; i < 5; i++) { + try { + Thread.sleep(1000); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "Thread.sleep failed: "; + } + if (exception == null) { + try { + rUserManager = rClient.login(rAuthentication); + } catch (Exception ex) { + if (i < 5) { + continue; + } + exception = ex; + exceptionMsg = "rClient.login failed: "; + } + } + } + } + + if (exception == null) { + try { + listFiles = rUserManager.listFiles(archived, shared, published); + // search for filename + for (RRepositoryFile file : listFiles) { + if (file.about().filename.equalsIgnoreCase(expRepoFileName)) { + repoFileFound = true; + actualAuthor = file.about().author; + isPublished = file.about().published; + break; + } + } + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.listFiles failed: "; + } + } + + if (exception == null) { + // Test assertions. + assertTrue(repoFileFound); + assertTrue(isPublished); + assertEquals(expAuthor, actualAuthor); + } else { + fail(exceptionMsg + exception.getMessage()); + } + } + + /** + * Test of uploadFile method, of class RUserRepositoryFileCalls. + */ + @Test + public void testUserRepositoryUploadFile() { + + // Test variables. + String actualRepoFileName = ""; + String actualRepoFileDesc = ""; + String expRepoFileName = ""; + String expRepoFileDesc = ""; + File file = new File("/etc/hosts"); + RepoUploadOptions options = null; + RRepositoryFile repoFile = null; + InputStream downStream = null; + String urlData = ""; + + // Test error handling. + Exception exception = null; + String exceptionMsg = ""; + Exception cleanupException = null; + String cleanupExceptionMsg = ""; + + //Test. + expRepoFileName = DeployrUtil.getUniqueFileName("txt"); + expRepoFileDesc = "hosts file"; + options = new RepoUploadOptions(); + options.descr = expRepoFileDesc; + options.filename = expRepoFileName; + + try { + repoFile = rUser.uploadFile(new FileInputStream(file), options); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.uploadFile failed: "; + } + + if (exception == null) { + try { + actualRepoFileName = repoFile.about().filename; + actualRepoFileDesc = repoFile.about().descr; + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.about failed: "; + } + } + + if (exception == null) { + try { + downStream = repoFile.download(); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.download failed: "; + } + } + + if (exception == null) { + urlData = DeployrUtil.getDataFromStream(downStream); + } + + // Test cleanup. + try { + if (repoFile != null) { + repoFile.delete(); + } + } catch (Exception ex) { + cleanupException = ex; + cleanupExceptionMsg = "repoFile.delete failed: "; + } + + if (exception == null) { + // Test assertions. + assertEquals(expRepoFileName, actualRepoFileName); + assertEquals(expRepoFileDesc, actualRepoFileDesc); + assertNotNull(urlData); + } else { + fail(exceptionMsg + exception.getMessage()); + } + + // Test cleanup errors. + if (cleanupException != null) { + fail(cleanupExceptionMsg + cleanupException.getMessage()); + } + } + + /** + * Test of uploadFile method with a large file, of class RUserRepositoryFileCalls. + */ + @Test + public void testUserRepositoryUploadLargeFile() throws IOException { + + // Test variables. + String actualRepoFileName = ""; + String actualRepoFileDesc = ""; + String expRepoFileName = ""; + String expRepoFileDesc = ""; + + File file = GetRandomFile(10 * 1024 * 1024 + 5); + long expectedLength = file.length(); + RepoUploadOptions options = null; + RRepositoryFile repoFile = null; + InputStream downStream = null; + String urlData = ""; + + // Test error handling. + Exception exception = null; + String exceptionMsg = ""; + Exception cleanupException = null; + String cleanupExceptionMsg = ""; + + //Test. + expRepoFileName = DeployrUtil.getUniqueFileName("txt"); + expRepoFileDesc = "some large file"; + options = new RepoUploadOptions(); + options.descr = expRepoFileDesc; + options.filename = expRepoFileName; + + try { + repoFile = rUser.uploadFile(new FileInputStream(file), options); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.uploadFile failed: "; + } + + if (exception == null) { + try { + actualRepoFileName = repoFile.about().filename; + actualRepoFileDesc = repoFile.about().descr; + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.about failed: "; + } + } + + if (exception == null) { + try { + downStream = repoFile.download(); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.download failed: "; + } + } + + if (exception == null) { + urlData = DeployrUtil.getDataFromStream(downStream); + } + + // Test cleanup. + try { + if(file != null) { + file.delete(); + } + + if (repoFile != null) { + repoFile.delete(); + } + } catch (Exception ex) { + cleanupException = ex; + cleanupExceptionMsg = "repoFile.delete failed: "; + } + + if (exception == null) { + // Test assertions. + assertEquals(expRepoFileName, actualRepoFileName); + assertEquals(expRepoFileDesc, actualRepoFileDesc); + assertNotNull(urlData); + assertEquals(expectedLength, urlData.length()); + } else { + fail(exceptionMsg + exception.getMessage()); + } + + // Test cleanup errors. + if (cleanupException != null) { + fail(cleanupExceptionMsg + cleanupException.getMessage()); + } + } + + /** + * Test of writeFile method, of class RUserRepositoryFileCalls. + */ + @Test + public void testUserRepositoryWriteFile() { + + // Test variables. + String text = "this is a line of text"; + String actualRepoFileName = ""; + String actualRepoFileDesc = ""; + String expRepoFileName = ""; + String expRepoFileDesc = ""; + RepoUploadOptions options = null; + RRepositoryFile repoFile = null; + InputStream downStream = null; + String urlData = ""; + + // Test error handling. + Exception exception = null; + String exceptionMsg = ""; + Exception cleanupException = null; + String cleanupExceptionMsg = ""; + + // Test. + expRepoFileName = DeployrUtil.getUniqueFileName("txt"); + expRepoFileDesc = "Repository Write File"; + options = new RepoUploadOptions(); + options.descr = expRepoFileDesc; + options.filename = expRepoFileName; + + try { + repoFile = rUser.writeFile(text, options); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.writeFile failed: "; + } + + if (exception == null) { + try { + actualRepoFileName = repoFile.about().filename; + actualRepoFileDesc = repoFile.about().descr; + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.about failed: "; + } + } + + if (exception == null) { + try { + downStream = repoFile.download(); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.download failed: "; + } + } + + if (exception == null) { + urlData = DeployrUtil.getDataFromStream(downStream); + } + + // Test cleanup. + try { + if (repoFile != null) { + repoFile.delete(); + } + } catch (Exception ex) { + cleanupException = ex; + cleanupExceptionMsg = "repoFile.delete failed: "; + } + + if (exception == null) { + // Test assertions. + assertEquals(expRepoFileName, actualRepoFileName); + assertEquals(expRepoFileDesc, actualRepoFileDesc); + assertEquals(DeployrUtil.encodeString(text), DeployrUtil.encodeString(urlData)); + } else { + fail(exceptionMsg + exception.getMessage()); + } + + // Test cleanup errors. + if (cleanupException != null) { + fail(cleanupExceptionMsg + cleanupException.getMessage()); + } + } + + /** + * Test of writeFile method, of class RUserRepositoryFileCalls. + */ + @Test + public void testUserRepositoryDownloadFile() { + + // Test variables. + String text = "this is a line of text"; + String actualRepoFileName = ""; + String actualRepoFileDesc = ""; + String expRepoFileName = ""; + String expRepoFileDesc = ""; + RepoUploadOptions options = null; + RRepositoryFile repoFile = null; + InputStream downStream = null; + String urlData = ""; + + // Test error handling. + Exception exception = null; + String exceptionMsg = ""; + Exception cleanupException = null; + String cleanupExceptionMsg = ""; + + // Test. + expRepoFileName = DeployrUtil.getUniqueFileName("txt"); + expRepoFileDesc = "User Repository Download File"; + options = new RepoUploadOptions(); + options.descr = expRepoFileDesc; + options.filename = expRepoFileName; + + try { + repoFile = rUser.writeFile(text, options); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.writeFile failed: "; + } + + if (exception == null) { + try { + actualRepoFileName = repoFile.about().filename; + actualRepoFileDesc = repoFile.about().descr; + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.about failed: "; + } + } + + if (exception == null) { + try { + downStream = repoFile.download(); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.download failed: "; + } + } + + if (exception == null) { + urlData = DeployrUtil.getDataFromStream(downStream); + } + + // Test cleanup. + try { + if (repoFile != null) { + repoFile.delete(); + } + } catch (Exception ex) { + cleanupException = ex; + cleanupExceptionMsg = "repoFile.delete failed: "; + } + + if (exception == null) { + // Test assertions. + assertEquals(expRepoFileName, actualRepoFileName); + assertEquals(expRepoFileDesc, actualRepoFileDesc); + assertEquals(DeployrUtil.encodeString(text), DeployrUtil.encodeString(urlData)); + } else { + fail(exceptionMsg + exception.getMessage()); + } + + // Test cleanup errors. + if (cleanupException != null) { + fail(cleanupExceptionMsg + cleanupException.getMessage()); + } + } + + /** + * Test of transferFile method, of class RUserRepositoryFileCalls. + */ + @Test + public void testUserRepositoryTransferFile() { + + // Test variables. + String text = "this is a line of text"; + String actualRepoFileName = ""; + String actualRepoFileDesc = ""; + String actualTransFileName = ""; + String actualTransFileDesc = ""; + String expRepoFileName = ""; + String expRepoFileDesc = ""; + String expTransFileName = ""; + String expTransFileDesc = ""; + RepoUploadOptions options = null; + RRepositoryFile repoFile = null; + String urlData = ""; + String urlTransData = ""; + URL repoURL = null; + URL repoTransURL = null; + RRepositoryFile repoTransFile = null; + long repoFileSize = 0; + long repoTransFileSize = 0; + + // Test error handling. + Exception exception = null; + String exceptionMsg = ""; + Exception cleanupException = null; + String cleanupExceptionMsg = ""; + + // Test. + expRepoFileName = DeployrUtil.getUniqueFileName("txt"); + expRepoFileDesc = "User Repository Download File"; + options = new RepoUploadOptions(); + options.descr = expRepoFileDesc; + options.filename = expRepoFileName; + + try { + repoFile = rUser.writeFile(text, options); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.writeFile failed: "; + } + + if (exception == null) { + try { + actualRepoFileName = repoFile.about().filename; + actualRepoFileDesc = repoFile.about().descr; + repoFileSize = repoFile.about().size; + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.about failed: "; + } + } + + if (exception == null) { + try { + repoURL = repoFile.about().url; + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoFile.download failed: "; + } + } + + if (exception == null) { + expTransFileName = DeployrUtil.getUniqueFileName("txt"); + expTransFileDesc = "User Repository Transfer File"; + options = new RepoUploadOptions(); + options.descr = expTransFileDesc; + options.filename = expTransFileName; + try { + repoTransFile = rUser.transferFile(repoURL, options); + } catch (Exception ex) { + exception = ex; + exceptionMsg = "rUser.transferFile failed: "; + } + } + + if (exception == null) { + try { + actualTransFileName = repoTransFile.about().filename; + actualTransFileDesc = repoTransFile.about().descr; + repoTransFileSize = repoTransFile.about().size; + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoTransFile.about failed: "; + } + } + + if (exception == null) { + try { + repoTransURL = repoTransFile.about().url; + } catch (Exception ex) { + exception = ex; + exceptionMsg = "repoTransFile.download failed: "; + } + } + + // Test cleanup. + try { + if (repoFile != null) { + repoFile.delete(); + } + } catch (Exception ex) { + cleanupException = ex; + cleanupExceptionMsg = "repoFile.delete failed: "; + } + + try { + if (repoTransFile != null) { + repoTransFile.delete(); + } + } catch (Exception ex) { + cleanupException = ex; + cleanupExceptionMsg = "repoTransFile.delete failed: "; + } + + if (exception == null) { + // Test assertions. + assertEquals(expRepoFileName, actualRepoFileName); + assertEquals(expRepoFileDesc, actualRepoFileDesc); + assertEquals(repoTransFileSize, repoFileSize); + assertEquals(DeployrUtil.encodeString(urlTransData), + DeployrUtil.encodeString(urlData)); + } else { + fail(exceptionMsg + exception.getMessage()); + } + + // Test cleanup errors. + if (cleanupException != null) { + fail(cleanupExceptionMsg + cleanupException.getMessage()); + } + } + + private File GetRandomFile(int length) throws IOException { + int remainingLength = length; + + File file = File.createTempFile("test", ".tmp"); + BufferedWriter bw = new BufferedWriter(new FileWriter(file)); + + while(remainingLength > 0) { + bw.write("A"); + remainingLength--; + } + + bw.close(); + return file; + } +} diff --git a/test/src/test/java/com/revo/deployr/client/api/RUserExternalRepositoryShellCallsTest.java b/test/src/test/java/com/revo/deployr/client/api/RUserExternalRepositoryShellCallsTest.java deleted file mode 100644 index 2c9fc1d..0000000 --- a/test/src/test/java/com/revo/deployr/client/api/RUserExternalRepositoryShellCallsTest.java +++ /dev/null @@ -1,366 +0,0 @@ -/* - * RUserExternalRepositoryShellCallsTest.java - * - * Copyright (C) 2010-2016, Microsoft Corporation - * - * This program is licensed to you under the terms of Version 2.0 of the - * Apache License. This program is distributed WITHOUT - * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, - * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the - * Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0) for more details. - * - */ -package com.revo.deployr.client.api; - -import com.revo.deployr.DeployrUtil; -import com.revo.deployr.client.RClient; -import com.revo.deployr.client.RUser; -import com.revo.deployr.client.RScriptExecution; -import com.revo.deployr.client.RClientException; -import com.revo.deployr.client.auth.basic.RBasicAuthentication; -import com.revo.deployr.client.RRepositoryFile; -import com.revo.deployr.client.factory.RClientFactory; -import org.junit.*; - -import java.util.List; - -import static org.junit.Assert.*; - -public class RUserExternalRepositoryShellCallsTest { - RClient rClient = null; - RUser rUser = null; - - public RUserExternalRepositoryShellCallsTest() { - } - - @BeforeClass - public static void setUpClass() throws Exception { - } - - @AfterClass - public static void tearDownClass() throws Exception { - } - - @Before - public void setUp() { - try { - String url = System.getProperty("connection.protocol") + - System.getProperty("connection.endpoint"); - if (url == null) { - fail("setUp: connection.[protocol|endpoint] null."); - } - boolean allowSelfSigned = - Boolean.valueOf(System.getProperty("allow.SelfSignedSSLCert")); - rClient =RClientFactory.createClient(url, allowSelfSigned); - RBasicAuthentication rAuthentication = new RBasicAuthentication("testuser", System.getProperty("password.testuser")); - String expResultName = "testuser"; - rUser = rClient.login(rAuthentication); - assertEquals(expResultName, rUser.about().username); - - } catch (Exception ex) { - fail("setUp: " + ex); - } - } - - @After - public void tearDown() { - if (rClient != null) { - rClient.release(); - } - } - - /** - * Test RClient.executeShell using external repository - * shell script. - */ - @Test - public void testClientExternalRepositoryExecuteShell() { - - // Test variables. - RRepositoryFile shell = null; - List consoleOutput = null; - - // Test error handling. - Exception exception = null; - String exceptionMsg = ""; - Exception cleanupException = null; - String cleanupExceptionMsg = ""; - - // Test. - try { - /* - * The echo.[sh,bat] shell script lives by default in - * the following directory on disk within the external - * repository: - * external/repository/public/admin which - * maps to the following directory value: - * external:public:admin - */ - shell = rUser.fetchFile("echo.sh", - "external:public:admin", - "admin", - null); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.fetchFile failed: "; - } - - if(exception == null) { - - try { - /* - * Test DeployR Linux/OSX Server. - */ - consoleOutput = - rClient.executeShell(shell.about().filename, - shell.about().directory, - shell.about().author, - null, - "Hello World!"); - } catch(Exception ex) { - try { - /* - * Test DeployR Windows Server. - */ - consoleOutput = - rClient.executeShell("echo.bat", - shell.about().directory, - shell.about().author, - null, - "Hello World!"); - - } catch(Exception batex) { - exception = ex; - exceptionMsg = "rClient.executeShell failed: "; - } - } - - } - - // Test cleanup. - if (exception == null) { - // Test assertions. - assertNotNull(shell); - assertNotNull(consoleOutput); - assertEquals(consoleOutput.size(), 2); - } else { - fail(exceptionMsg + exception.getMessage()); - } - - // Test cleanup errors. - if (cleanupException != null) { - fail(cleanupExceptionMsg + cleanupException.getMessage()); - } - } - - /** - * Test RClient.executeShell using external repository - * shell script. - */ - @Test - public void testClientExternalRepositoryExecuteShellBadFilename() { - - // Test variables. - RRepositoryFile shell = null; - List consoleOutput = null; - RClientException clientEx = null; - - // Test error handling. - Exception exception = null; - String exceptionMsg = ""; - Exception cleanupException = null; - String cleanupExceptionMsg = ""; - - // Test. - try { - /* - * The echo.sh shell script lives by default in the - * following directory on disk within the external - * repository: - * external/repository/public/admin which - * maps to the following directory value: - * external:public:admin - */ - shell = rUser.fetchFile("echo.sh", - "external:public:admin", - "admin", - null); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.fetchFile failed: "; - } - - if(exception == null) { - - try { - consoleOutput = - rClient.executeShell("does-not-exist.sh", - shell.about().directory, - shell.about().author, - null, - "Hello World!"); - } catch(RClientException ex) { - clientEx = ex; - } catch(Exception ex) { - exception = ex; - exceptionMsg = "rClient.executeShell unexpected ex, failed: "; - } - - } - - // Test cleanup. - if (exception == null) { - // Test assertions. - assertNotNull(shell); - assertNotNull(clientEx); - } else { - fail(exceptionMsg + exception.getMessage()); - } - - // Test cleanup errors. - if (cleanupException != null) { - fail(cleanupExceptionMsg + cleanupException.getMessage()); - } - } - - /** - * Test RClient.executeShell using external repository - * shell script. - */ - @Test - public void testClientExternalRepositoryExecuteShellBadDirectory() { - - // Test variables. - RRepositoryFile shell = null; - List consoleOutput = null; - RClientException clientEx = null; - - // Test error handling. - Exception exception = null; - String exceptionMsg = ""; - Exception cleanupException = null; - String cleanupExceptionMsg = ""; - - // Test. - try { - /* - * The echo.sh shell script lives by default in the - * following directory on disk within the external - * repository: - * external/repository/public/admin which - * maps to the following directory value: - * external:public:admin - */ - shell = rUser.fetchFile("echo.sh", - "external:public:admin", - "admin", - null); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.fetchFile failed: "; - } - - if(exception == null) { - - try { - consoleOutput = - rClient.executeShell(shell.about().filename, - "external:public:dir-not-found", - shell.about().author, - null, - "Hello World!"); - } catch(RClientException ex) { - clientEx = ex; - } catch(Exception ex) { - exception = ex; - exceptionMsg = "rClient.executeShell unexpected ex, failed: "; - } - - } - - // Test cleanup. - if (exception == null) { - // Test assertions. - assertNotNull(shell); - assertNotNull(clientEx); - } else { - fail(exceptionMsg + exception.getMessage()); - } - - // Test cleanup errors. - if (cleanupException != null) { - fail(cleanupExceptionMsg + cleanupException.getMessage()); - } - } - - /** - * Test RClient.executeShell using external repository - * shell script. - */ - @Test - public void testClientExternalRepositoryExecuteShellBadAuthor() { - - // Test variables. - RRepositoryFile shell = null; - List consoleOutput = null; - RClientException clientEx = null; - - // Test error handling. - Exception exception = null; - String exceptionMsg = ""; - Exception cleanupException = null; - String cleanupExceptionMsg = ""; - - // Test. - try { - /* - * The echo.sh script lives by default in the - * following directory on disk within the external - * repository: - * external/repository/public/admin which - * maps to the following directory value: - * external:public:admin - */ - shell = rUser.fetchFile("echo.sh", - "external:public:admin", - "admin", - null); - } catch (Exception ex) { - exception = ex; - exceptionMsg = "rUser.fetchFile failed: "; - } - - if(exception == null) { - - try { - consoleOutput = - rClient.executeShell(shell.about().filename, - shell.about().directory, - "author-not-found", - null, - "Hello World!"); - } catch(RClientException ex) { - clientEx = ex; - } catch(Exception ex) { - exception = ex; - exceptionMsg = "rClient.executeShell unexpected ex, failed: "; - } - - } - - // Test cleanup. - if (exception == null) { - // Test assertions. - assertNotNull(shell); - assertNotNull(clientEx); - } else { - fail(exceptionMsg + exception.getMessage()); - } - - // Test cleanup errors. - if (cleanupException != null) { - fail(cleanupExceptionMsg + cleanupException.getMessage()); - } - } - -}