From 3bc461d0653cc35784ecaa31b2e089c624bd593b Mon Sep 17 00:00:00 2001 From: xardas Date: Sat, 2 Jan 2016 22:32:09 +0300 Subject: [PATCH 01/35] add netty client. Big refactor allowing selection of used client. add client builder changed all requests to accept universal response --- xplenty.jar-core/pom.xml | 6 + .../main/java/com/xplenty/api/Xplenty.java | 2 +- .../main/java/com/xplenty/api/XplentyAPI.java | 147 ++++----- .../main/java/com/xplenty/api/http/Http.java | 88 ++++++ .../java/com/xplenty/api/http/HttpClient.java | 31 ++ .../xplenty/api/http/HttpClientBuilder.java | 78 +++++ .../JerseyClient.java} | 107 +++---- .../xplenty/api/http/JsonMapperFactory.java | 29 ++ .../com/xplenty/api/http/JsonResponse.java | 58 ++++ .../java/com/xplenty/api/http/Response.java | 88 ++++++ .../com/xplenty/api/http/SSLUntruster.java | 55 ++++ .../com/xplenty/api/http/SyncNettyClient.java | 288 ++++++++++++++++++ .../api/request/AbstractDeleteRequest.java | 10 +- .../api/request/AbstractInfoRequest.java | 10 +- .../request/AbstractManipulationRequest.java | 10 +- .../xplenty/api/request/CloneSchedule.java | 10 +- .../xplenty/api/request/CreateCluster.java | 4 +- .../xplenty/api/request/CreateSchedule.java | 2 +- .../com/xplenty/api/request/ListClusters.java | 14 +- .../com/xplenty/api/request/ListJobs.java | 22 +- .../com/xplenty/api/request/ListPackages.java | 20 +- .../xplenty/api/request/ListSchedules.java | 20 +- .../java/com/xplenty/api/request/Request.java | 6 +- .../java/com/xplenty/api/request/RunJob.java | 4 +- .../xplenty/api/request/UpdateCluster.java | 4 +- .../xplenty/api/request/UpdateSchedule.java | 2 +- .../request/watching/AddClusterWatcher.java | 14 +- .../api/request/watching/AddJobWatcher.java | 12 +- .../api/request/watching/ListWatchers.java | 12 +- .../api/request/watching/WatchingStop.java | 15 +- .../main/java/com/xplenty/api/util/Http.java | 45 --- .../api/WatchersTestAgainstMockServer.java | 7 +- .../java/com/xplenty/api/XplentyAPITest.java | 126 ++++++-- .../api/request/CloneScheduleTest.java | 24 +- .../xplenty/api/request/ClusterInfoTest.java | 32 +- .../api/request/CreateClusterTest.java | 45 ++- .../api/request/CreateScheduleTest.java | 24 +- .../api/request/DeleteScheduleTest.java | 24 +- .../com/xplenty/api/request/JobInfoTest.java | 24 +- .../xplenty/api/request/ListClustersTest.java | 36 +-- .../com/xplenty/api/request/ListJobsTest.java | 38 +-- .../api/request/ListSchedulesTest.java | 28 +- .../com/xplenty/api/request/RunJobTest.java | 36 +-- .../xplenty/api/request/ScheduleInfoTest.java | 24 +- .../com/xplenty/api/request/StopJobTest.java | 24 +- .../api/request/TerminateClusterTest.java | 24 +- .../api/request/UpdateClusterTest.java | 45 ++- .../api/request/UpdateScheduleTest.java | 25 +- 48 files changed, 1220 insertions(+), 579 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClientBuilder.java rename xplenty.jar-core/src/main/java/com/xplenty/api/{XplentyWebConnectivity.java => http/JerseyClient.java} (65%) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonMapperFactory.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonResponse.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/http/SSLUntruster.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java delete mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/util/Http.java diff --git a/xplenty.jar-core/pom.xml b/xplenty.jar-core/pom.xml index 9d55a8c..61629c0 100644 --- a/xplenty.jar-core/pom.xml +++ b/xplenty.jar-core/pom.xml @@ -55,6 +55,12 @@ jackson-annotations 2.1.1 + + + io.netty + netty + 3.10.5.Final + junit diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index b13c0d9..60e94f3 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -167,7 +167,7 @@ public String format(String... values) { } public static enum Version { - V1(1); + V1(1), V2(2); private final int value; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index 220edea..6c7222e 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -6,6 +6,8 @@ import com.xplenty.api.Xplenty.ClusterType; import com.xplenty.api.Xplenty.Version; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.HttpClient; +import com.xplenty.api.http.HttpClientBuilder; import com.xplenty.api.model.*; import com.xplenty.api.model.Package; import com.xplenty.api.request.*; @@ -13,13 +15,13 @@ import com.xplenty.api.request.watching.AddJobWatcher; import com.xplenty.api.request.watching.ListWatchers; import com.xplenty.api.request.watching.WatchingStop; -import com.xplenty.api.util.Http; +import com.xplenty.api.http.Http; import java.util.List; import java.util.Map; import java.util.Properties; /** - * A convenience class for making HTTP requests to the Xplenty API for a given user. An underlying {@link XplentyWebConnectivity} is created + * A convenience class for making HTTP requests to the Xplenty API for a given user. An underlying {@link com.xplenty.api.http.HttpClient} is created * for each instance of XplentyAPI. *

* Example usage: @@ -31,18 +33,19 @@ * * {@link RuntimeException} will be thrown for any request failures. * - * @author Yuriy Kovalek + * @author Yuriy Kovalek and Xardas */ -public class XplentyAPI extends XplentyWebConnectivity { +public class XplentyAPI { + private final HttpClient client; /** - * Constructs a XplentyAPI with a {@link XplentyWebConnectivity} based on API key, account name, + * Constructs a XplentyAPI with a {@link com.xplenty.api.http.HttpClient} based on API key, account name, * and internal configuration. * @param accountName account name used for Xplenty sign-up * @param apiKey User's API key found at https://www.xplenty.com/settings/edit */ public XplentyAPI(String accountName, String apiKey) { - super(accountName, apiKey); + client = new HttpClientBuilder().withAccount(accountName).withApiKey(apiKey).build(); } /** @@ -52,51 +55,21 @@ public XplentyAPI(String accountName, String apiKey) { * @param apiKey User's API key found at https://www.xplenty.com/settings/edit * @param logHttpCommunication enables logging of requests and responses */ - public XplentyAPI(String accountName, String apiKey, boolean logHttpCommunication) { - super(accountName, apiKey, logHttpCommunication); - } - /** - * Constructs a XplentyAPI with a {@link XplentyWebConnectivity} based on API key, account name, - * and manual configuration. - * @param accountName account name used for Xplenty sign-up - * @param apiKey User's API key found at https://www.xplenty.com/settings/edit - * @param host Base API host - * @param proto API protocol (plain or encrypted) + * Constructs a XplentyAPI with a {@link com.xplenty.api.http.HttpClient} based on the given http client builder + * @param clientBuilder configured Http client builder. At least account name and API Key must be set! */ - public XplentyAPI(String accountName, String apiKey, String host, Http.Protocol proto){ - this(accountName, apiKey, host, proto, false); + public XplentyAPI(HttpClientBuilder clientBuilder) { + client = clientBuilder.build(); } /** - * Constructs a XplentyAPI with a {@link XplentyWebConnectivity} based on API key, account name, - * and manual configuration. - * @param accountName account name used for Xplenty sign-up - * @param apiKey User's API key found at https://www.xplenty.com/settings/edit - * @param host Base API host - * @param proto API protocol (plain or encrypted) - * @param logHttpCommunication enables logging of requests and responses + * Constructs a XplentyAPI with the {@link com.xplenty.api.http.HttpClient} given + * @param client Configured http client */ - public XplentyAPI(String accountName, String apiKey, String host, Http.Protocol proto, boolean logHttpCommunication){ - this(accountName, apiKey, logHttpCommunication); - this.setHost(host); - this.setProtocol(proto); + public XplentyAPI(HttpClient client){ + this.client = client; } - - public XplentyAPI withVersion(Version ver) { - this.setVersion(ver); - return this; - } - - public XplentyAPI withHost(String host) { - this.setHost(host); - return this; - } - - public XplentyAPI withProtocol(Http.Protocol proto) { - this.setProtocol(proto); - return this; - } /** * Get package information @@ -104,7 +77,7 @@ public XplentyAPI withProtocol(Http.Protocol proto) { * @return package object */ public Package getPackageInfo(long packageId) { - return this.execute(new PackageInfo(packageId)); + return client.execute(new PackageInfo(packageId)); } /** @@ -113,7 +86,7 @@ public Package getPackageInfo(long packageId) { * @return list of packages */ public List listPackages(Properties props) { - return this.execute(new ListPackages(props)); + return client.execute(new ListPackages(props)); } /** @@ -156,7 +129,7 @@ public List listSchedules(int offset, int limit) { * @return list of schedules */ public List listSchedules(Properties props) { - return this.execute(new ListSchedules(props)); + return client.execute(new ListSchedules(props)); } /** @@ -187,7 +160,7 @@ public List listClusters(int offset, int limit) { * @return list of clusters */ public List listClusters(Properties props) { - return this.execute(new ListClusters(props)); + return client.execute(new ListClusters(props)); } /** * Information about a particular cluster @@ -195,7 +168,7 @@ public List listClusters(Properties props) { * @return */ public Cluster clusterInformation(long clusterId) { - return this.execute(new ClusterInfo(clusterId)).withParentApiInstance(this); + return client.execute(new ClusterInfo(clusterId)).withParentApiInstance(this); } /** @@ -209,14 +182,14 @@ public Cluster clusterInformation(long clusterId) { * @return */ public Cluster createCluster(int nodes, ClusterType type, String name, String description, Boolean terminateOnIdle, Long timeToIdle) { - return this.execute(new CreateCluster( - new Cluster().withNodes(nodes) - .ofType(type) - .named(name) - .withDescription(description) - .withTerminateOnIdle(terminateOnIdle) - .withTimeToIdle(timeToIdle) - )).withParentApiInstance(this); + return client.execute(new CreateCluster( + new Cluster().withNodes(nodes) + .ofType(type) + .named(name) + .withDescription(description) + .withTerminateOnIdle(terminateOnIdle) + .withTimeToIdle(timeToIdle) + )).withParentApiInstance(this); } /** @@ -230,10 +203,10 @@ public Cluster createCluster(int nodes, ClusterType type, String name, String de * @return */ public Cluster updateCluster(long id, Integer nodes, String name, String description, Boolean terminateOnIdle, Long timeToIdle) { - return this.execute(new UpdateCluster( - new Cluster().withId(id).withNodes(nodes).named(name).withDescription(description) - .withTerminateOnIdle(terminateOnIdle).withTimeToIdle(timeToIdle) - )).withParentApiInstance(this); + return client.execute(new UpdateCluster( + new Cluster().withId(id).withNodes(nodes).named(name).withDescription(description) + .withTerminateOnIdle(terminateOnIdle).withTimeToIdle(timeToIdle) + )).withParentApiInstance(this); } /** @@ -242,7 +215,7 @@ public Cluster updateCluster(long id, Integer nodes, String name, String descrip * @return */ public Cluster terminateCluster(long clusterId) { - return this.execute(new TerminateCluster(clusterId)).withParentApiInstance(this); + return client.execute(new TerminateCluster(clusterId)).withParentApiInstance(this); } /** @@ -272,7 +245,7 @@ public List listJobs(int offset, int limit) { * @return list of jobs */ public List listJobs(Properties params) { - return this.execute(new ListJobs(params)); + return client.execute(new ListJobs(params)); } /** @@ -281,7 +254,7 @@ public List listJobs(Properties params) { * @return */ public Job jobInformation(long jobId) { - return this.execute(new JobInfo(jobId)).withParentApiInstance(this); + return client.execute(new JobInfo(jobId)).withParentApiInstance(this); } /** @@ -292,7 +265,7 @@ public Job jobInformation(long jobId) { * @return */ public Job runJob(long clusterId, long packageId, Map variables) { - return this.execute(new RunJob(new Job().onCluster(clusterId).withPackage(packageId).withVariables(variables))).withParentApiInstance(this); + return client.execute(new RunJob(new Job().onCluster(clusterId).withPackage(packageId).withVariables(variables))).withParentApiInstance(this); } /** @@ -301,32 +274,32 @@ public Job runJob(long clusterId, long packageId, Map variables) * @return */ public Job stopJob(long jobId) { - return this.execute(new StopJob(jobId)).withParentApiInstance(this); + return client.execute(new StopJob(jobId)).withParentApiInstance(this); } public List listClusterWatchers(long clusterId) { - return this.execute(new ListWatchers(Xplenty.SubjectType.CLUSTER, clusterId)); + return client.execute(new ListWatchers(Xplenty.SubjectType.CLUSTER, clusterId)); } public List listJobWatchers(long clusterId) { - return this.execute(new ListWatchers(Xplenty.SubjectType.JOB, clusterId)); + return client.execute(new ListWatchers(Xplenty.SubjectType.JOB, clusterId)); } public ClusterWatchingLogEntry addClusterWatchers(long clusterId) { - return this.execute(new AddClusterWatcher(clusterId)).withParentApiInstance(this); + return client.execute(new AddClusterWatcher(clusterId)).withParentApiInstance(this); } public JobWatchingLogEntry addJobWatchers(long jobId) { - return this.execute(new AddJobWatcher(jobId)).withParentApiInstance(this); + return client.execute(new AddJobWatcher(jobId)).withParentApiInstance(this); } public Boolean removeClusterWatchers(long clusterId) { - return this.execute(new WatchingStop(Xplenty.SubjectType.CLUSTER, clusterId)); + return client.execute(new WatchingStop(Xplenty.SubjectType.CLUSTER, clusterId)); } public Boolean removeJobWatchers(long jobId) { - return this.execute(new WatchingStop(Xplenty.SubjectType.JOB, jobId)); + return client.execute(new WatchingStop(Xplenty.SubjectType.JOB, jobId)); } /** @@ -338,7 +311,7 @@ public Schedule createSchedule(Schedule schedule) { if (schedule.getId() != null) { schedule.setId(null); } - return this.execute(new CreateSchedule(schedule)); + return client.execute(new CreateSchedule(schedule)); } /** @@ -350,7 +323,7 @@ public Schedule updateSchedule(Schedule schedule) { if (schedule.getId() == null) { throw new XplentyAPIException("No id specified!"); } - return this.execute(new UpdateSchedule(schedule)); + return client.execute(new UpdateSchedule(schedule)); } /** @@ -362,7 +335,7 @@ public Schedule deleteSchedule(long scheduleId) { if (scheduleId == 0) { throw new XplentyAPIException("No id specified!"); } - return this.execute(new DeleteSchedule(scheduleId)); + return client.execute(new DeleteSchedule(scheduleId)); } /** @@ -374,7 +347,7 @@ public Schedule cloneSchedule(long scheduleId) { if (scheduleId == 0) { throw new XplentyAPIException("No id specified!"); } - return this.execute(new CloneSchedule(scheduleId)); + return client.execute(new CloneSchedule(scheduleId)); } /** @@ -386,46 +359,46 @@ public Schedule getScheduleInfo(long scheduleId) { if (scheduleId == 0) { throw new XplentyAPIException("No id specified!"); } - return this.execute(new ScheduleInfo(scheduleId)); + return client.execute(new ScheduleInfo(scheduleId)); } /** * Account name this XplentyAPI instance is associated with - * @return + * @return Account name */ public String getAccountName() { - return super.getAccountName(); + return client.getAccountName(); } /** * API key used by this XplentyAPI instance - * @return + * @return API Key */ public String getApiKey() { - return super.getApiKey(); + return client.getApiKey(); } /** * API host this instance uses - * @return + * @return API host */ public String getHost() { - return super.getHost(); + return client.getHost(); } /** * Protocol this API instance uses - * @return + * @return protocol */ public Http.Protocol getProtocol() { - return super.getProtocol(); + return client.getProtocol(); } /** * API version - * @return + * @return version */ public Version getVersion() { - return super.getVersion(); + return client.getVersion(); } } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java new file mode 100644 index 0000000..ecd0e08 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java @@ -0,0 +1,88 @@ +/** + * + */ +package com.xplenty.api.http; + +/** + * Convenience structures for HTTP communication + * + * @author Yuriy Kovalek + * + */ +public class Http { + + /** + * Media types supported by Xplenty API + */ + public static enum MediaType { + JSON("application/vnd.xplenty+json"); + + public final String value; + + MediaType(String type) { + value = type; + } + } + + /** + * HTTP methods supported by Xplenty API + */ + public enum Method { + GET, POST, PUT, DELETE + } + + public static enum Protocol { + Http("http"), + Https("https"); + + public final String value; + + Protocol(String value) { + this.value = value; + } + } + + public static enum HttpClientImpl { + SyncNetty, AsyncNetty, Jersey; + } + + public static enum ResponseStatus { + HTTP_200(200, "OK: Request succeeded."), + HTTP_201(201, "Created: The requested resource was created successfully."), + HTTP_204(204, "No Content: Request succeeded. No content is returned."), + HTTP_304(304, "Not Modified: There was no new data to return."), + HTTP_400(400, "Bad Request: The request was invalid. An accompanying error message will explain why."), + HTTP_401(401, "Unauthorized: You are attempting to access the API with invalid credentials."), + HTTP_402(402, "Payment Required: You must confirm your billing info to use this API."), + HTTP_403(403, "Forbidden: The request has been refused. An accompanying error message will explain why."), + HTTP_404(404, "Not Found: The URI requested is invalid or the resource requested does not exist."), + HTTP_406(406, "Not Acceptable: The requested mime-type is not acceptable."), + HTTP_415(415, "Unsupported Media Type: The specified media type is not supported."), + HTTP_422(422, "Unprocessable Entity: You have sent invalid fields."), + HTTP_429(429, "Too Many Requests: The request exceeded the rate limitations."), + HTTP_500(500, "Internal Server Error: An internal error occurred in the request."), + HTTP_502(502, "Bad Gateway: Xplenty is down or being upgraded."), + HTTP_503(503, "Service Unavailable: The Xplenty servers are up, but overloaded with requests. Try again later."); + + private final int code; + private final String description; + + ResponseStatus(int code, String description) { + this.code = code; + this.description = description; + } + + public int getCode() { + return code; + } + + public String getDescription() { + return description; + } + + public static ResponseStatus fromCode(int code) { + return ResponseStatus.valueOf(String.format("HTTP_%d", code)); + } + } + +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java new file mode 100644 index 0000000..98e0d73 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java @@ -0,0 +1,31 @@ +package com.xplenty.api.http; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.request.Request; + +/** + * Author: Xardas + * Date: 02.01.16 + * Time: 15:36 + */ +public interface HttpClient { + static final int DEFAULT_TIMEOUT = 30; + static final int DEFAULT_HTTPS_PORT = 443; + static final int DEFAULT_HTTP_PORT = 80; + static final String API_PATH = "api"; + + T execute(Request xplentyRequest) throws XplentyAPIException; + + void shutdown(); + + String getAccountName(); + + String getApiKey(); + + String getHost(); + + Http.Protocol getProtocol(); + + Xplenty.Version getVersion(); +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClientBuilder.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClientBuilder.java new file mode 100644 index 0000000..24ca3f3 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClientBuilder.java @@ -0,0 +1,78 @@ +package com.xplenty.api.http; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; + +/** + * Author: Xardas + * Date: 02.01.16 + * Time: 15:32 + */ +public class HttpClientBuilder { + + private int timeout = SyncNettyClient.DEFAULT_TIMEOUT; + private String host = "api.xplenty.com"; + private String apiKey; + private Http.Protocol protocol = Http.Protocol.Https; + private Http.HttpClientImpl clientImpl = Http.HttpClientImpl.Jersey; + private boolean logHttpCommunication = false; + private String accountName; + private Xplenty.Version version = Xplenty.Version.V1; + + + public HttpClientBuilder withTimeout(int timeout) { + this.timeout = timeout; + return this; + } + + public HttpClientBuilder withApiKey(String apiKey) { + this.apiKey = apiKey; + return this; + } + + public HttpClientBuilder withProtocol(Http.Protocol protocol) { + this.protocol = protocol; + return this; + } + + public HttpClientBuilder withLogHttpCommunication(boolean logHttpCommunication) { + this.logHttpCommunication = logHttpCommunication; + return this; + } + + public HttpClientBuilder withAccount(String accountName) { + this.accountName = accountName; + return this; + } + + public HttpClientBuilder withHost(String host) { + this.host = host; + return this; + } + + public HttpClientBuilder withClientImpl(Http.HttpClientImpl impl) { + this.clientImpl = impl; + return this; + } + + public HttpClientBuilder withVersion(Xplenty.Version version) { + this.version = version; + return this; + } + + public HttpClient build() throws XplentyAPIException { + if (apiKey == null) { + throw new XplentyAPIException("Api Key not set!"); + } + if (accountName == null) { + throw new XplentyAPIException("Account name not set!"); + } + switch (clientImpl) { + case SyncNetty: + return new SyncNettyClient(accountName, apiKey, host, protocol, timeout, logHttpCommunication); + default: + return new JerseyClient(accountName, apiKey, host, protocol, timeout, logHttpCommunication); + } + } + +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyWebConnectivity.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java similarity index 65% rename from xplenty.jar-core/src/main/java/com/xplenty/api/XplentyWebConnectivity.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java index 00a795f..111add7 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyWebConnectivity.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api; +package com.xplenty.api.http; import com.fasterxml.jackson.databind.ObjectMapper; import com.sun.jersey.api.client.Client; @@ -16,27 +16,25 @@ import com.xplenty.api.exceptions.RequestFailedException; import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.request.Request; -import com.xplenty.api.util.Http; +import javax.ws.rs.core.MultivaluedMap; import java.io.StringWriter; -import java.text.DateFormat; -import java.text.SimpleDateFormat; +import java.util.HashMap; +import java.util.Map; /** * Proxy for connecting to the XplentyAPI over HTTP * - * @author Yuriy Kovalek + * @author Yuriy Kovalek and Xardas * */ -class XplentyWebConnectivity { - private static final String API_PATH = "api"; - protected final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); - - private String HOST = "api.xplenty.com"; - private Http.Protocol PROTOCOL = Http.Protocol.Https; - private final String ACCOUNT_NAME; - private final String API_KEY; - +public class JerseyClient implements HttpClient { + + private final String host; + private final Http.Protocol protocol; + private final String accountName; + private final String apiKey; + private final Client client; private Version version = null; @@ -44,21 +42,17 @@ class XplentyWebConnectivity { * Construct a new instance for given account and API key * @param accountName name of the associated account, used in URL's * @param apiKey used for authentication + * @param host host to connect + * @param protocol protocol to use + * @param timeout timeout for response. + * @param logHttpCommunication enables logging of requests and responses */ - XplentyWebConnectivity(String accountName, String apiKey) { - this(accountName, apiKey, false); - } + JerseyClient(String accountName, String apiKey, String host, Http.Protocol protocol, int timeout, boolean logHttpCommunication) { + this.accountName = accountName; + this.apiKey = apiKey; + this.host = host; + this.protocol = protocol; - /** - * Construct a new instance for given account and API key - * @param accountName name of the associated account, used in URL's - * @param apiKey used for authentication - * @param logHttpCommunication enables logging of requests and responses - */ - XplentyWebConnectivity(String accountName, String apiKey, boolean logHttpCommunication) { - ACCOUNT_NAME = accountName; - API_KEY = apiKey; - ClientConfig config = new DefaultClientConfig(); client = Client.create(config); client.addFilter(new HTTPBasicAuthFilter(apiKey, "")); @@ -82,11 +76,21 @@ public T execute(Request request) { case DELETE: response = builder.delete(ClientResponse.class); break; } validate(request, response); - return request.getResponse(response); + Response processedResponse = Response.forContentType(request.getResponseType(), response.getEntity(String.class), + response.getStatus(), convertJerseyHeaders(response.getHeaders())); + return request.getResponse(processedResponse); } - + + private Map convertJerseyHeaders(MultivaluedMap headers) { + final Map convertedHeaders = new HashMap<>(); + for (String header : headers.keySet()) { + convertedHeaders.put(header, headers.getFirst(header)); + } + return convertedHeaders; + } + /** - * Convenience method for getting a configured {@link WebResource.Builder} for given request + * Convenience method for getting a configured {@link com.sun.jersey.api.client.WebResource.Builder} for given request * @param request that would be submitted to the XPlenty Server * @return builder */ @@ -98,34 +102,32 @@ private WebResource.Builder getConfiguredResource(Request request) { if (request.hasBody()) { StringWriter sw = new StringWriter(); try { - final ObjectMapper objectMapper = new ObjectMapper(); - - objectMapper.setDateFormat(dateFormat); + ObjectMapper objectMapper = JsonMapperFactory.getInstance(); objectMapper.writeValue(sw, request.getBody()); } catch (Exception e) { throw new XplentyAPIException(e); } b.entity(sw.toString()).type(Http.MediaType.JSON.value); } - + return b; } - + /** * Constructs the actual URL * @param methodEndpoint - describes the action type * @return filly qualified URL */ private String getMethodURL(String methodEndpoint) { - return PROTOCOL + "://" + HOST + "/" + ACCOUNT_NAME + "/" + API_PATH + "/" + methodEndpoint; + return protocol + "://" + host + "/" + accountName + "/" + API_PATH + "/" + methodEndpoint; } - + /** * Check the response status and throws exception on errors * @param request used request * @param response received response - * @throws AuthFailedException - * @throws RequestFailedException + * @throws com.xplenty.api.exceptions.AuthFailedException + * @throws com.xplenty.api.exceptions.RequestFailedException */ private void validate(Request request, ClientResponse response) { if (response.getClientResponseStatus() != null) @@ -137,35 +139,34 @@ private void validate(Request request, ClientResponse response) { throw new RequestFailedException(request.getName() + " failed", response.getStatus(), response.getEntity(String.class)); } - String getAccountName() { - return ACCOUNT_NAME; + public String getAccountName() { + return accountName; } - String getApiKey() { - return API_KEY; + public String getApiKey() { + return apiKey; } public void setVersion(Version ver) { version = ver; } - public void setHost(String host) { - HOST = host; - } - - public void setProtocol(Http.Protocol proto) { - PROTOCOL = proto; - } - public Http.Protocol getProtocol() { - return PROTOCOL; + return protocol; } public String getHost() { - return HOST; + return host; } public Version getVersion() { return version; } + + @Override + public void shutdown() { + if (client != null) { + client.destroy(); + } + } } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonMapperFactory.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonMapperFactory.java new file mode 100644 index 0000000..c6d805e --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonMapperFactory.java @@ -0,0 +1,29 @@ +package com.xplenty.api.http; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +import java.text.SimpleDateFormat; + +public class JsonMapperFactory { + private static final ObjectMapper mapper = new ObjectMapper(); + + static { + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + //mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); + mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC); + mapper.enable(SerializationFeature.INDENT_OUTPUT); // for pretty print + mapper.enable(SerializationFeature.WRITE_NULL_MAP_VALUES); + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")); + } + + public static ObjectMapper getInstance() { + + return mapper; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonResponse.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonResponse.java new file mode 100644 index 0000000..1adeb5a --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonResponse.java @@ -0,0 +1,58 @@ +package com.xplenty.api.http; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.xplenty.api.exceptions.XplentyAPIException; + +import java.io.IOException; +import java.util.Map; + +/** + * Author: Xardas + * Date: 05.11.15 + * Time: 16:01 + */ +public class JsonResponse extends Response { + private JsonNode jsoncontent; // lazily initialized + + + public JsonResponse(String content, int status, Map headers) { + super(content, status, headers); + } + + private JsonNode getContent() throws XplentyAPIException { + if (jsoncontent == null && content != null) { + try { + this.jsoncontent = getJsonMapper().readTree(content); + } catch (IOException e) { + throw new XplentyAPIException("Error parsing JSON tree", e); + } + } else if (content == null) { + this.jsoncontent = null; + } + return this.jsoncontent; + } + + private ObjectMapper getJsonMapper() { + return JsonMapperFactory.getInstance(); + } + + @Override + public T getContent(TypeReference typeReference) throws XplentyAPIException { + if (!isValid()) { + throw new XplentyAPIException("Response doesn't contain any valid content!"); + } + checkTypedInfo(typeReference); + return getJsonMapper().convertValue(getContent(), typeReference); + } + + public T getContent(Class typeReference) throws XplentyAPIException { + if (!isValid()) { + throw new XplentyAPIException("Response doesn't contain any valid content!"); + } + return getJsonMapper().convertValue(getContent(), typeReference); + } + + +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java new file mode 100644 index 0000000..7e8b9e1 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java @@ -0,0 +1,88 @@ +package com.xplenty.api.http; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.model.XplentyObject; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.List; +import java.util.Map; + +/** + * Author: Xardas + * Date: 28.12.15 + * Time: 19:13 + */ +public abstract class Response { + protected static final String INVALID_TYPE_INFORMATION = "Invalid type information!"; + protected final Http.ResponseStatus status; + protected final String content; + protected final Map headers; + + protected Response(String content, int status, Map headers) { + this.content = content; + this.status = Http.ResponseStatus.fromCode(status); + this.headers = headers; + } + + + public static Response forContentType(Http.MediaType type, String content, int status, Map headers) { + switch (type) { + case JSON: + return new JsonResponse(content, status, headers); + default: + throw new UnsupportedOperationException(String.format("This media type [%s] not supported!", type)); + } + } + + public abstract T getContent(TypeReference typeReference) throws XplentyAPIException; + + public abstract T getContent(Class typeReference) throws XplentyAPIException; + + public boolean isValid() { + return content != null && content.length() > 0 && status.getCode() < 400 && status.getCode() > 100; + } + + @SuppressWarnings("unchecked") + protected void checkTypedInfo(TypeReference typeReference) { + if (typeReference.getType() instanceof Class) { + if (XplentyObject.class.isAssignableFrom((Class) typeReference.getType())) { + return; + } else { + throw new XplentyAPIException(INVALID_TYPE_INFORMATION); + } + } + final ParameterizedType type = (ParameterizedType) typeReference.getType(); + final Class rawType = (Class) type.getRawType(); + final Type[] actualTypeArguments = type.getActualTypeArguments(); + if (actualTypeArguments.length == 0) { + throw new XplentyAPIException(INVALID_TYPE_INFORMATION); + } + Class cls; + if (List.class.isAssignableFrom(rawType)) { + cls = (Class) actualTypeArguments[0]; + if (XplentyObject.class.isAssignableFrom(cls)) { + return; + } + } else if (Map.class.isAssignableFrom(rawType)) { + cls = (Class) actualTypeArguments[1]; + if (XplentyObject.class.isAssignableFrom(cls)) { + return; + } + } + throw new XplentyAPIException(INVALID_TYPE_INFORMATION); + } + + public Http.ResponseStatus getStatus() { + return status; + } + + public String getRawContent() { + return content; + } + + public Map getHeaders() { + return headers; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SSLUntruster.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SSLUntruster.java new file mode 100644 index 0000000..4c9f316 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SSLUntruster.java @@ -0,0 +1,55 @@ +/* + * CopyRight Alexey Gromov 2012-... + * You can freely improve this code + */ + +package com.xplenty.api.http; + +import javax.net.ssl.*; +import java.security.SecureRandom; +import java.security.cert.X509Certificate; + + +/** + * + * @author xardas + */ +public class SSLUntruster { + private static SSLContext sc; + private static SSLContext wc; + private static HostnameVerifier hv; + static { + TrustManager[] trustAllCerts = new TrustManager[] { + new X509TrustManager() { + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + public void checkClientTrusted(X509Certificate[] certs, String authType) {} + public void checkServerTrusted(X509Certificate[] certs, String authType) {} + }}; + + // Ignore differences between given hostname and certificate hostname + hv = new HostnameVerifier() { + public boolean verify(String hostname, SSLSession session) { return true; } + }; + + try { + sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, new SecureRandom()); + wc = SSLContext.getInstance("TLS"); + wc.init(null, trustAllCerts, new SecureRandom()); + } catch (Exception ex) { } + + } + + public static SSLContext getUntruster() { + return sc; + } + + public static SSLContext getWebUntruster() { + return wc; + } + + + +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java new file mode 100644 index 0000000..68ce2d0 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java @@ -0,0 +1,288 @@ +package com.xplenty.api.http; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.sun.jersey.core.util.Base64; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.request.Request; +import org.jboss.netty.bootstrap.ClientBootstrap; +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.*; +import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; +import org.jboss.netty.handler.codec.http.*; +import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor; +import org.jboss.netty.handler.logging.LoggingHandler; +import org.jboss.netty.handler.ssl.SslHandler; +import org.jboss.netty.logging.InternalLogLevel; + +import javax.net.ssl.SSLEngine; +import java.io.StringWriter; +import java.io.UnsupportedEncodingException; +import java.net.InetSocketAddress; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.jboss.netty.channel.Channels.pipeline; + +/** + * Author: Xardas + * Date: 23.12.15 + * Time: 19:11 + */ +public class SyncNettyClient extends SimpleChannelUpstreamHandler implements HttpClient { + public static final String MUTEX_KEY = "mutex"; + private final ClientBootstrap client; + private final NioClientSocketChannelFactory chanfac; + + private final int timeout; + private final String authHeader; + private final String host; + private final Http.Protocol protocol; + private final String accountName; + private final String apiKey; + + + + SyncNettyClient(String accountName, String apiKey, String host, Http.Protocol protocol, int timeout, boolean logHttpCommunication) throws XplentyAPIException { + this.timeout = timeout; + this.accountName = accountName; + this.host = host; + this.protocol = protocol; + this.apiKey = apiKey; + try { + this.authHeader = String.format("Basic %s", new String(Base64.encode(String.format("%s:", apiKey)), "ASCII")); + } catch (UnsupportedEncodingException e) { + throw new XplentyAPIException("Error encoding API key", e); + } + + chanfac = new NioClientSocketChannelFactory( + new OrderedMemoryAwareThreadPoolExecutor(1, 400000000, 2000000000, 120, TimeUnit.SECONDS), + new OrderedMemoryAwareThreadPoolExecutor(2, 400000000, 2000000000, 60, TimeUnit.SECONDS), + 2); + client = new ClientBootstrap(chanfac); + client.setOption("child.tcpNoDelay", true); + client.setOption("child.keepAlive", true); + client.setOption("reuseAddress", true); + client.setOption("connectTimeoutMillis", 30000); +// Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { +// @Override +// public void run() { +// if (eventLoop != null && !eventLoop.isShutdown() && !eventLoop.isTerminated()) { +// eventLoop.shutdownGracefully(); +// } +// } +// })); + client.setPipelineFactory(new PipelineFactory(this, protocol.equals(Http.Protocol.Https))); + } + + @Override + public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { + HttpResponse response = (HttpResponse) e.getMessage(); + + System.out.println(channelBuffer2String(response.getContent())); + + final Channel channel = ctx.getChannel(); + NettyResponse nettyResponse = (NettyResponse) channel.getAttachment(); + + + if (nettyResponse != null) { + nettyResponse.setResponse(response); + CountDownLatch mutex = nettyResponse.getMutex(); + mutex.countDown(); + } + } + + public static String channelBuffer2String(ChannelBuffer channelBuffer) { + return channelBuffer.toString(StandardCharsets.UTF_8); + } + + private HttpMethod convertRequestMethod(Http.Method method) { + switch (method) { + case POST: + return HttpMethod.POST; + case PUT: + return HttpMethod.PUT; + case DELETE: + return HttpMethod.DELETE; + default: + return HttpMethod.GET; + } + } + + + @Override + public T execute(Request xplentyRequest) throws XplentyAPIException { + try { + URL url = new URL(getMethodURL(xplentyRequest.getEndpoint())); + Channel channel = client.connect(new InetSocketAddress(url.getHost(), getPort(url))).awaitUninterruptibly().getChannel(); + + HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, convertRequestMethod(xplentyRequest.getHttpMethod()), url.toString()); + HttpHeaders headers = request.headers(); + headers.set(HttpHeaders.Names.HOST, url.getHost()); + headers.set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); + headers.set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP + ',' + HttpHeaders.Values.DEFLATE); + headers.set(HttpHeaders.Names.ACCEPT_CHARSET, "utf-8;q=0.7,*;q=0.7"); + headers.set(HttpHeaders.Names.USER_AGENT, "Xplenty Netty client by Xardas"); + headers.set(HttpHeaders.Names.ACCEPT, xplentyRequest.getResponseType().value); + headers.set(HttpHeaders.Names.AUTHORIZATION, authHeader); + + if (xplentyRequest.hasBody()) { + final ChannelBuffer cb = ChannelBuffers.dynamicBuffer(8000); + StringWriter sw = new StringWriter(); + ObjectMapper objectMapper = JsonMapperFactory.getInstance(); + objectMapper.writeValue(sw, xplentyRequest.getBody()); + final byte[] contentBytes = sw.getBuffer().toString().getBytes(StandardCharsets.UTF_8); + cb.writeBytes(contentBytes); + request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, contentBytes.length); + request.headers().set(HttpHeaders.Names.CONTENT_TYPE, Http.MediaType.JSON.value); + request.setContent(cb); + } + + NettyResponse response = new NettyResponse(); + channel.setAttachment(response); + channel.write(request).awaitUninterruptibly(); + response.getMutex().await(timeout, TimeUnit.SECONDS); + + final HttpResponse httpBody = response.getResponse(); + int httpStatus = parseStatus(httpBody.headers().get("Status")); + Response processedResponse = Response.forContentType(xplentyRequest.getResponseType(), channelBuffer2String(httpBody.getContent()), httpStatus, convertNettyHeaders(httpBody.headers())); + return xplentyRequest.getResponse(processedResponse); + } catch (Exception e) { + throw new XplentyAPIException(e); + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent cause) throws Exception { + throw new XplentyAPIException("Exception while communicating with remote Xplenty server", cause.getCause()); + } + + private Map convertNettyHeaders(HttpHeaders headers) { + final Map convertedHeaders = new HashMap<>(); + for (Map.Entry header : headers.entries()) { + convertedHeaders.put(header.getKey(), header.getValue()); + } + return convertedHeaders; + } + + private int parseStatus(String status) { + return Integer.parseInt(status.substring(0, 3)); + } + + /** + * Constructs the actual URL + * @param methodEndpoint - describes the action type + * @return filly qualified URL + */ + private String getMethodURL(String methodEndpoint) { + return String.format("%s://%s/%s/%s/%s", protocol, host, accountName, API_PATH, methodEndpoint); + } + + + private int getPort(URL url) { + if (url.getPort() > 0) { + return url.getPort(); + } + if (url.getProtocol().equals("https")) { + return DEFAULT_HTTPS_PORT; + } + return DEFAULT_HTTP_PORT; + } + + + @Override + public void shutdown() { + client.releaseExternalResources(); + } + + + public class PipelineFactory implements ChannelPipelineFactory { + + private final boolean ssl; + private final ChannelUpstreamHandler handler; + + public PipelineFactory(ChannelUpstreamHandler handler, boolean ssl) { + this.handler = handler; + this.ssl = ssl; + } + + public ChannelPipeline getPipeline() throws Exception { + // Create a default pipeline implementation. + ChannelPipeline pipeline = pipeline(); + + // Enable HTTPS if necessary. + if (ssl) { + SSLEngine engine = + SSLUntruster.getWebUntruster().createSSLEngine(); + engine.setUseClientMode(true); + + pipeline.addLast("ssl", new SslHandler(engine)); + } + + pipeline.addLast("logger", new LoggingHandler("global", InternalLogLevel.DEBUG, true)); + + pipeline.addLast("codec", new HttpClientCodec()); + + pipeline.addLast("aggregator", new HttpChunkAggregator(102467890)); + + // Remove the following line if you don't want automatic content decompression. + pipeline.addLast("deflater", new HttpContentDecompressor()); + + pipeline.addLast("handler", handler); + return pipeline; + } + } + + + + private static class NettyResponse { + private final CountDownLatch mutex = new CountDownLatch(1); + private volatile HttpResponse response; + + public CountDownLatch getMutex() { + return mutex; + } + + public HttpResponse getResponse() { + return response; + } + + public void setResponse(HttpResponse response) { + this.response = response; + } + } + + public int getTimeout() { + return timeout; + } + + @Override + public String getHost() { + return host; + } + + @Override + public Http.Protocol getProtocol() { + return protocol; + } + + @Override + public Xplenty.Version getVersion() { + return null; + } + + @Override + public String getAccountName() { + return accountName; + } + + @Override + public String getApiKey() { + return apiKey; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java index 175cca5..d6de756 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java @@ -1,9 +1,8 @@ package com.xplenty.api.request; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.ClientResponse; import com.xplenty.api.exceptions.XplentyAPIException; -import com.xplenty.api.util.Http; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -28,10 +27,9 @@ protected AbstractDeleteRequest(Long entityId) { } @Override - public T getResponse(ClientResponse response) { - String json = response.getEntity(String.class); + public T getResponse(Response response) { try { - final T value = new ObjectMapper().readValue(json, this.clazz); + final T value = response.getContent(this.clazz); return value; } catch (Exception e) { throw new XplentyAPIException(getName() + ": error parsing response object", e); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractInfoRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractInfoRequest.java index cb9d0e0..1c51440 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractInfoRequest.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractInfoRequest.java @@ -1,9 +1,8 @@ package com.xplenty.api.request; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.ClientResponse; import com.xplenty.api.exceptions.XplentyAPIException; -import com.xplenty.api.util.Http; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -28,10 +27,9 @@ protected AbstractInfoRequest(long entityId) { } @Override - public T getResponse(ClientResponse response) { - String json = response.getEntity(String.class); + public T getResponse(Response response) { try { - final T value = new ObjectMapper().readValue(json, this.clazz); + final T value = response.getContent(this.clazz); return value; } catch (Exception e) { throw new XplentyAPIException(getName() + ": error parsing response object", e); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java index 73ab94d..51b7b91 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java @@ -1,9 +1,8 @@ package com.xplenty.api.request; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.ClientResponse; import com.xplenty.api.exceptions.XplentyAPIException; -import com.xplenty.api.util.Http; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -30,10 +29,9 @@ protected AbstractManipulationRequest(T entity) { } @Override - public T getResponse(ClientResponse response) { - String json = response.getEntity(String.class); + public T getResponse(Response response) { try { - final T value = new ObjectMapper().readValue(json, this.clazz); + final T value = response.getContent(this.clazz); return value; } catch (Exception e) { throw new XplentyAPIException(getName() + ": error parsing response object", e); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CloneSchedule.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/CloneSchedule.java index 49708f2..f7465c0 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CloneSchedule.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/CloneSchedule.java @@ -1,11 +1,10 @@ package com.xplenty.api.request; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.ClientResponse; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; -import com.xplenty.api.util.Http; /** * Author: Xardas @@ -21,10 +20,9 @@ public CloneSchedule(long entityId) { } @Override - public Schedule getResponse(ClientResponse response) { - String json = response.getEntity(String.class); + public Schedule getResponse(Response response) { try { - final Schedule value = new ObjectMapper().readValue(json, Schedule.class); + final Schedule value = response.getContent(Schedule.class); return value; } catch (Exception e) { throw new XplentyAPIException(getName() + ": error parsing response object", e); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateCluster.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateCluster.java index 6e0669e..3ac588e 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateCluster.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateCluster.java @@ -2,8 +2,8 @@ import com.xplenty.api.Xplenty; import com.xplenty.api.model.Cluster; -import com.xplenty.api.util.Http; -import com.xplenty.api.util.Http.Method; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Http.Method; public class CreateCluster extends AbstractManipulationRequest { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateSchedule.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateSchedule.java index c117cbb..b17f152 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateSchedule.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateSchedule.java @@ -2,7 +2,7 @@ import com.xplenty.api.Xplenty; import com.xplenty.api.model.Schedule; -import com.xplenty.api.util.Http; +import com.xplenty.api.http.Http; /** * Author: Xardas diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java index d489396..6fb7326 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java @@ -4,15 +4,14 @@ package com.xplenty.api.request; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.ClientResponse; import com.xplenty.api.Xplenty; import com.xplenty.api.Xplenty.ClusterStatus; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Http.MediaType; +import com.xplenty.api.http.Http.Method; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; -import com.xplenty.api.util.Http; -import com.xplenty.api.util.Http.MediaType; -import com.xplenty.api.util.Http.Method; import java.util.List; import java.util.Properties; @@ -52,10 +51,9 @@ protected String getEndpointRoot() { } @Override - public List getResponse(ClientResponse response) { - String json = response.getEntity(String.class); + public List getResponse(Response response) { try { - return new ObjectMapper().readValue(json, new TypeReference>() {}); + return response.getContent(new TypeReference>() {}); } catch (Exception e) { throw new XplentyAPIException(getName() + ": error parsing response object", e); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListJobs.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListJobs.java index 1571a20..c33aa32 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListJobs.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListJobs.java @@ -4,15 +4,14 @@ package com.xplenty.api.request; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.ClientResponse; import com.xplenty.api.Xplenty; import com.xplenty.api.Xplenty.ClusterStatus; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Http.MediaType; +import com.xplenty.api.http.Http.Method; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; -import com.xplenty.api.util.Http; -import com.xplenty.api.util.Http.MediaType; -import com.xplenty.api.util.Http.Method; import java.util.List; import java.util.Properties; @@ -57,13 +56,12 @@ protected String getEndpointRoot() { } @Override - public List getResponse(ClientResponse response) { - String json = response.getEntity(String.class); - try { - return new ObjectMapper().readValue(json, new TypeReference>() {}); - } catch (Exception e) { - throw new XplentyAPIException(getName() + ": error parsing response object", e); - } + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } } @Override diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListPackages.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListPackages.java index 1f1127c..e9d15ec 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListPackages.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListPackages.java @@ -4,14 +4,13 @@ package com.xplenty.api.request; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.ClientResponse; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http.MediaType; +import com.xplenty.api.http.Http.Method; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; import com.xplenty.api.model.Package; -import com.xplenty.api.util.Http.MediaType; -import com.xplenty.api.util.Http.Method; import java.util.List; import java.util.Properties; @@ -45,13 +44,12 @@ protected String getEndpointRoot() { } @Override - public List getResponse(ClientResponse response) { - String json = response.getEntity(String.class); - try { - return new ObjectMapper().readValue(json, new TypeReference>() {}); - } catch (Exception e) { - throw new XplentyAPIException(getName() + ": error parsing response object", e); - } + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } } @Override diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListSchedules.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListSchedules.java index 10216fa..969402f 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListSchedules.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListSchedules.java @@ -4,14 +4,13 @@ package com.xplenty.api.request; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.ClientResponse; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http.MediaType; +import com.xplenty.api.http.Http.Method; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; import com.xplenty.api.model.Schedule; -import com.xplenty.api.util.Http.MediaType; -import com.xplenty.api.util.Http.Method; import java.util.List; import java.util.Properties; @@ -52,13 +51,12 @@ public MediaType getResponseType() { @Override - public List getResponse(ClientResponse response) { - String json = response.getEntity(String.class); - try { - return new ObjectMapper().readValue(json, new TypeReference>() {}); - } catch (Exception e) { - throw new XplentyAPIException(getName() + ": error parsing response object", e); - } + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } } @Override diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/Request.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/Request.java index 4c48841..9a21f33 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/Request.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/Request.java @@ -3,8 +3,8 @@ */ package com.xplenty.api.request; -import com.sun.jersey.api.client.ClientResponse; -import com.xplenty.api.util.Http; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; /** * @author Yuriy Kovalek @@ -32,5 +32,5 @@ public interface Request { Object getBody(); - T getResponse(ClientResponse response); + T getResponse(Response response); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/RunJob.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/RunJob.java index d498b5c..8e8cf78 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/RunJob.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/RunJob.java @@ -5,8 +5,8 @@ import com.xplenty.api.Xplenty; import com.xplenty.api.model.Job; -import com.xplenty.api.util.Http; -import com.xplenty.api.util.Http.Method; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Http.Method; /** * @author Yuriy Kovalek diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCluster.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCluster.java index 0aeb5d2..99aba1d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCluster.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCluster.java @@ -2,8 +2,8 @@ import com.xplenty.api.Xplenty; import com.xplenty.api.model.Cluster; -import com.xplenty.api.util.Http; -import com.xplenty.api.util.Http.Method; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Http.Method; public class UpdateCluster extends AbstractManipulationRequest { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateSchedule.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateSchedule.java index 0e825b0..d4af024 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateSchedule.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateSchedule.java @@ -2,7 +2,7 @@ import com.xplenty.api.Xplenty; import com.xplenty.api.model.Schedule; -import com.xplenty.api.util.Http; +import com.xplenty.api.http.Http; /** * Author: Xardas diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java index 93c277a..dc4d94b 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java @@ -1,15 +1,14 @@ package com.xplenty.api.request.watching; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.ClientResponse; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.ClusterWatchingLogEntry; import com.xplenty.api.request.Request; -import com.xplenty.api.util.Http; -import static com.xplenty.api.util.Http.Method.POST; -import static com.xplenty.api.Xplenty.*; +import static com.xplenty.api.Xplenty.Resource; +import static com.xplenty.api.http.Http.Method.POST; public class AddClusterWatcher implements Request { private Long _clusterId; @@ -39,10 +38,9 @@ public String getName() { public Object getBody() { return null; } @Override - public ClusterWatchingLogEntry getResponse(ClientResponse response) { - String json = response.getEntity(String.class); + public ClusterWatchingLogEntry getResponse(Response response) { try { - return new ObjectMapper().readValue(json, new TypeReference() {}); + return response.getContent(new TypeReference() {}); } catch (Exception e) { throw new XplentyAPIException(getName() + ": error parsing response object", e); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java index 16ae594..8a49834 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java @@ -1,15 +1,14 @@ package com.xplenty.api.request.watching; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.ClientResponse; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.JobWatchingLogEntry; import com.xplenty.api.request.Request; -import com.xplenty.api.util.Http; -import static com.xplenty.api.util.Http.Method.POST; +import static com.xplenty.api.http.Http.Method.POST; public class AddJobWatcher implements Request { Long _jobId; @@ -41,10 +40,9 @@ public String getEndpoint() { public Object getBody() { return null; } @Override - public JobWatchingLogEntry getResponse(ClientResponse response) { - String json = response.getEntity(String.class); + public JobWatchingLogEntry getResponse(Response response) { try { - return new ObjectMapper().readValue(json, new TypeReference() {}); + return response.getContent(new TypeReference() {}); } catch (Exception e) { throw new XplentyAPIException(getName() + ": error parsing response object", e); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/ListWatchers.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/ListWatchers.java index 58c380a..3987c0f 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/ListWatchers.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/ListWatchers.java @@ -1,19 +1,18 @@ package com.xplenty.api.request.watching; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.ClientResponse; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Watcher; import com.xplenty.api.request.Request; -import com.xplenty.api.util.Http; import java.util.List; import static com.xplenty.api.Xplenty.Resource; import static com.xplenty.api.Xplenty.SubjectType; -import static com.xplenty.api.util.Http.Method.GET; +import static com.xplenty.api.http.Http.Method.GET; public class ListWatchers implements Request> { private SubjectType _kind = null; @@ -55,10 +54,9 @@ public String getEndpoint() { public Object getBody() { return null; } @Override - public List getResponse(ClientResponse response) { - String json = response.getEntity(String.class); + public List getResponse(Response response) { try { - return new ObjectMapper().readValue(json, new TypeReference>() {}); + return response.getContent(new TypeReference>() {}); } catch (Exception e) { throw new XplentyAPIException(getName() + ": error parsing response object", e); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java index 3c920dd..f299ee6 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java @@ -1,13 +1,14 @@ package com.xplenty.api.request.watching; -import com.sun.jersey.api.client.ClientResponse; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.RequestFailedException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.request.Request; -import com.xplenty.api.util.Http; -import static com.xplenty.api.util.Http.Method.DELETE; -import static com.xplenty.api.Xplenty.*; +import static com.xplenty.api.Xplenty.Resource; +import static com.xplenty.api.Xplenty.SubjectType; +import static com.xplenty.api.http.Http.Method.DELETE; public class WatchingStop implements Request { private Long _subjectId; @@ -45,9 +46,9 @@ public String getEndpoint() { public Object getBody() { return null; } @Override - public Boolean getResponse(ClientResponse response) { - int code = response.getStatus(); - if (code==204) + public Boolean getResponse(Response response) { + int code = response.getStatus().getCode(); + if (code == 204) return true; else throw new RequestFailedException("204 expected, but something went wrong", code, ""); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/util/Http.java b/xplenty.jar-core/src/main/java/com/xplenty/api/util/Http.java deleted file mode 100644 index ebd7493..0000000 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/util/Http.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * - */ -package com.xplenty.api.util; - -/** - * Convenience structures for HTTP communication - * - * @author Yuriy Kovalek - * - */ -public class Http { - - /** - * Media types supported by Xplenty API - */ - public static enum MediaType { - JSON("application/vnd.xplenty+json"); - - public final String value; - - MediaType(String type) { - value = type; - } - } - - /** - * HTTP methods supported by Xplenty API - */ - public enum Method { - GET, POST, PUT, DELETE - } - - public static enum Protocol { - Http("http"), - Https("https"); - - public final String value; - - Protocol(String value) { - this.value = value; - } - } - -} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java b/xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java index 2023270..6449988 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java @@ -1,12 +1,13 @@ package com.xplenty.api; +import com.xplenty.api.http.HttpClientBuilder; import com.xplenty.api.model.ClusterWatchingLogEntry; import com.xplenty.api.model.JobWatchingLogEntry; import com.xplenty.api.model.Watcher; import junit.framework.Assert; import org.joda.time.DateTime; import org.junit.Ignore; -import com.xplenty.api.util.Http; +import com.xplenty.api.http.Http; import junit.framework.TestCase; @Ignore @@ -20,7 +21,9 @@ public class WatchersTestAgainstMockServer extends TestCase { @Override public void setUp(){ - api = new XplentyAPI(accountID, apiKey, host, Http.Protocol.Http); + HttpClientBuilder builder = new HttpClientBuilder().withAccount(accountID).withApiKey(apiKey).withHost(host). + withProtocol(Http.Protocol.Http); + api = new XplentyAPI(builder); } public void testListClusterWatchers() { diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java index a299387..1fd791e 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java @@ -3,20 +3,20 @@ */ package com.xplenty.api; -import com.sun.jersey.api.client.ClientResponse; import com.xplenty.api.Xplenty.ClusterType; -import com.xplenty.api.model.*; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.HttpClient; +import com.xplenty.api.http.HttpClientBuilder; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Cluster; +import com.xplenty.api.model.Job; import com.xplenty.api.request.Request; - import junit.framework.Assert; - +import junit.framework.TestCase; import org.joda.time.DateTime; -import com.xplenty.api.util.Http; - -import junit.framework.TestCase; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import java.util.HashMap; /** * @author Yuriy Kovalek @@ -33,9 +33,9 @@ public void testConstructor() { } public void testBuilder() { - XplentyAPI api = new XplentyAPI("testAcc", "testKey").withHost("www.example.com") - .withProtocol(Http.Protocol.Http) - .withVersion(Xplenty.Version.V1); + HttpClientBuilder builder = new HttpClientBuilder().withAccount("testAcc").withApiKey("testKey"). + withHost("www.example.com").withProtocol(Http.Protocol.Http).withVersion(Xplenty.Version.V1); + XplentyAPI api = new XplentyAPI(builder); assertNotNull(api); assertEquals("www.example.com", api.getHost()); @@ -44,23 +44,54 @@ public void testBuilder() { } public void testListClusters() { - XplentyAPI api = new XplentyAPI("dontcare", "dontcare"){ + HttpClient client = new HttpClient() { @Override - public T execute(Request request) { - ClientResponse mockedResponse = mock(ClientResponse.class); - when(mockedResponse.getEntity(String.class)).thenReturn( + public T execute(Request xplentyRequest) throws XplentyAPIException { + Response mockedResponse = Response.forContentType(Http.MediaType.JSON, "[{\"nodes\":\"1\", " + - "\"type\": \"sandbox\", " + - "\"available_since\":\"2000-01-01T00:00:00Z\"," + - "\"terminated_at\":\"2000-01-01T00:00:00Z\", " + - "\"nodes\":\"1\","+ - "\"type\":\"sandbox\"," + - "\"available_since\":\"2000-01-01T00:00:00Z\"," + - "\"terminated_at\":\"2000-01-01T00:00:00Z\"}]"); - - return request.getResponse(mockedResponse); + "\"type\": \"sandbox\", " + + "\"available_since\":\"2000-01-01T00:00:00Z\"," + + "\"terminated_at\":\"2000-01-01T00:00:00Z\", " + + "\"nodes\":\"1\","+ + "\"type\":\"sandbox\"," + + "\"available_since\":\"2000-01-01T02:00:00Z\"," + + "\"terminated_at\":\"2000-01-01T02:00:00Z\"}]", + 200, new HashMap()); + + return xplentyRequest.getResponse(mockedResponse); + } + + @Override + public void shutdown() { + + } + + @Override + public String getAccountName() { + return null; + } + + @Override + public String getApiKey() { + return null; + } + + @Override + public String getHost() { + return null; + } + + @Override + public Http.Protocol getProtocol() { + return null; + } + + @Override + public Xplenty.Version getVersion() { + return null; } }; + XplentyAPI api = new XplentyAPI(client); Cluster res = api.listClusters().get(0); Assert.assertEquals(res.getNodes(), new Integer(1)); Assert.assertEquals(res.getType(), ClusterType.sandbox); @@ -69,16 +100,47 @@ public T execute(Request request) { } public void testListJobs() { - XplentyAPI api = new XplentyAPI("don't care", "don't care"){ + HttpClient client = new HttpClient() { + @Override + public T execute(Request xplentyRequest) throws XplentyAPIException { + Response mockedResponse = Response.forContentType(Http.MediaType.JSON, + "[{\"failed_at\":\"2000-01-01T02:00:00Z\"," + + "\"completed_at\":\"2000-01-01T02:00:00Z\"}]", + 200, new HashMap()); + return xplentyRequest.getResponse(mockedResponse); + } + + @Override + public void shutdown() { + + } + + @Override + public String getAccountName() { + return null; + } + + @Override + public String getApiKey() { + return null; + } + + @Override + public String getHost() { + return null; + } + + @Override + public Http.Protocol getProtocol() { + return null; + } + @Override - public T execute(Request request) { - ClientResponse mockedResponse = mock(ClientResponse.class); - when(mockedResponse.getEntity(String.class)).thenReturn( - "[{\"failed_at\":\"2000-01-01T00:00:00Z\"," + - "\"completed_at\":\"2000-01-01T00:00:00Z\"}]"); - return request.getResponse(mockedResponse); + public Xplenty.Version getVersion() { + return null; } }; + XplentyAPI api = new XplentyAPI(client); Job res = api.listJobs().get(0); Assert.assertEquals(res.getFailedAt(), new DateTime(2000, 1, 1, 2, 0, 0).toDate()); Assert.assertEquals(res.getCompletedAt(), new DateTime(2000, 1, 1, 2, 0, 0).toDate()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CloneScheduleTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/CloneScheduleTest.java index ad95e95..125077b 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CloneScheduleTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/CloneScheduleTest.java @@ -5,22 +5,20 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; import com.xplenty.api.model.ScheduleTest; -import com.xplenty.api.util.Http; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.util.Date; +import java.util.HashMap; /** * @author xardas @@ -54,10 +52,10 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor Schedule c = ScheduleTest.createMockSchedule(now); CloneSchedule cc = new CloneSchedule(666); - c = cc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(TEST_JSON.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + TEST_JSON, + Status.OK.getStatusCode(), + new HashMap())); assertNotNull(c); assertEquals(new Long(666), c.getId()); @@ -74,10 +72,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(c).replace("{", "one"); CloneSchedule cc = new CloneSchedule(666); try { - c = cc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertTrue(false); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.CloneSchedule.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ClusterInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ClusterInfoTest.java index fd78949..ef21d30 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ClusterInfoTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ClusterInfoTest.java @@ -5,25 +5,23 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.Xplenty.ClusterStatus; import com.xplenty.api.Xplenty.ClusterType; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; import com.xplenty.api.model.ClusterTest; -import com.xplenty.api.util.Http; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.text.ParseException; import java.util.Date; +import java.util.HashMap; /** * @author Yuriy Kovalek @@ -53,10 +51,10 @@ public void testValidResponseHandling() throws UnsupportedEncodingException, Par Date now = new Date(); String json = new ObjectMapper().writeValueAsString(ClusterTest.createMockCluster(now)); - Cluster c = ci.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + Cluster c = ci.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertNotNull(c); assertEquals(new Long(3), c.getId()); @@ -81,10 +79,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(c).replace("1", "one"); try { - c = ci.getResponse(new ClientResponse(Status.CREATED.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = ci.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); assertTrue(false); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.Cluster.name + ": error parsing response object", e.getMessage()); @@ -94,10 +92,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp json = new ObjectMapper().writeValueAsString(c).replace("available", "ready"); try { - c = ci.getResponse(new ClientResponse(Status.CREATED.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = ci.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); fail(); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.Cluster.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateClusterTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateClusterTest.java index 8ccf499..af6e5d8 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateClusterTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateClusterTest.java @@ -3,29 +3,24 @@ */ package com.xplenty.api.request; -import java.io.ByteArrayInputStream; -import java.io.UnsupportedEncodingException; -import java.util.Date; - -import org.junit.Before; -import org.junit.Test; - import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.Xplenty.ClusterStatus; import com.xplenty.api.Xplenty.ClusterType; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; import com.xplenty.api.model.ClusterTest; -import com.xplenty.api.request.CreateCluster; -import com.xplenty.api.util.Http; - import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; /** * @author Yuriy Kovalek @@ -61,10 +56,10 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor String json = new ObjectMapper().writeValueAsString(c); CreateCluster cc = new CreateCluster(c); - c = cc.getResponse(new ClientResponse(Status.CREATED.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); assertNotNull(c); assertEquals(new Long(3), c.getId()); @@ -90,10 +85,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(c).replace("1", "one"); CreateCluster cc = new CreateCluster(c); try { - c = cc.getResponse(new ClientResponse(Status.CREATED.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); assertTrue(false); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.CreateCluster.name + ": error parsing response object", e.getMessage()); @@ -104,10 +99,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp json = new ObjectMapper().writeValueAsString(c).replace("available", "ready"); cc = new CreateCluster(c); try { - c = cc.getResponse(new ClientResponse(Status.CREATED.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); fail(); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.CreateCluster.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateScheduleTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateScheduleTest.java index 603bb21..273e6ef 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateScheduleTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateScheduleTest.java @@ -5,22 +5,20 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; import com.xplenty.api.model.ScheduleTest; -import com.xplenty.api.util.Http; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.util.Date; +import java.util.HashMap; /** * @author xardas @@ -55,10 +53,10 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor String json = new ObjectMapper().writeValueAsString(c); CreateSchedule cc = new CreateSchedule(c); - c = cc.getResponse(new ClientResponse(Status.CREATED.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(TEST_JSON.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + TEST_JSON, + Status.CREATED.getStatusCode(), + new HashMap())); assertNotNull(c); assertEquals(new Long(666), c.getId()); @@ -75,10 +73,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(c).replace("{", "one"); CreateSchedule cc = new CreateSchedule(c); try { - c = cc.getResponse(new ClientResponse(Status.CREATED.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); assertTrue(false); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.CreateSchedule.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/DeleteScheduleTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/DeleteScheduleTest.java index e3c46cd..d428884 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/DeleteScheduleTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/DeleteScheduleTest.java @@ -5,22 +5,20 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; import com.xplenty.api.model.ScheduleTest; -import com.xplenty.api.util.Http; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.util.Date; +import java.util.HashMap; /** * @author xardas @@ -54,10 +52,10 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor Schedule c = ScheduleTest.createMockSchedule(now); DeleteSchedule cc = new DeleteSchedule(666); - c = cc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(TEST_JSON.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + TEST_JSON, + Status.OK.getStatusCode(), + new HashMap())); assertNotNull(c); assertEquals(new Long(666), c.getId()); @@ -74,10 +72,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(c).replace("{", "one"); DeleteSchedule cc = new DeleteSchedule(666); try { - c = cc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertTrue(false); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.RemoveSchedule.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/JobInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/JobInfoTest.java index b59ab0d..b63c243 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/JobInfoTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/JobInfoTest.java @@ -5,22 +5,20 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.Xplenty.JobStatus; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; import com.xplenty.api.model.JobTest; -import com.xplenty.api.util.Http; import junit.framework.TestCase; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.util.Date; +import java.util.HashMap; /** * @author Yuriy Kovalek @@ -47,10 +45,10 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor Job j = JobTest.createMockJob(now); String json = new ObjectMapper().writeValueAsString(j); - j = ji.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + j = ji.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertNotNull(j); assertEquals(new Long(7), j.getId()); @@ -79,10 +77,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(j).replace("running", "success"); try { - j = ji.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + j = ji.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertTrue(false); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.Job.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListClustersTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListClustersTest.java index 39052b0..5ce42f2 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListClustersTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListClustersTest.java @@ -3,28 +3,20 @@ */ package com.xplenty.api.request; -import java.io.ByteArrayInputStream; -import java.io.UnsupportedEncodingException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Properties; - -import org.junit.Test; - import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; import com.xplenty.api.model.ClusterTest; -import com.xplenty.api.util.Http; - import junit.framework.TestCase; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; /** * @author Yuriy Kovalek @@ -50,10 +42,10 @@ public void testValidResponcehandling() throws JsonProcessingException, Unsuppor list.add(ClusterTest.createMockCluster(new Date())); String json = new ObjectMapper().writeValueAsString(list); - list = lc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + list = lc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertNotNull(list); assertFalse(list.isEmpty()); } @@ -66,10 +58,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(list).replace("[", ""); try { - list = lc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + list = lc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertTrue(false); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.Clusters.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListJobsTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListJobsTest.java index e93df35..348f2c5 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListJobsTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListJobsTest.java @@ -3,28 +3,20 @@ */ package com.xplenty.api.request; -import java.io.ByteArrayInputStream; -import java.io.UnsupportedEncodingException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Properties; - -import junit.framework.TestCase; - -import org.junit.Test; - import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; import com.xplenty.api.model.JobTest; -import com.xplenty.api.util.Http; +import junit.framework.TestCase; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; /** * @author Yuriy Kovalek @@ -50,10 +42,10 @@ public void testValidResponcehandling() throws JsonProcessingException, Unsuppor list.add(JobTest.createMockJob(new Date())); String json = new ObjectMapper().writeValueAsString(list); - list = lj.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + list = lj.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertNotNull(list); assertFalse(list.isEmpty()); } @@ -66,10 +58,10 @@ public void testInvalidResponcehandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(list).replace("[", ""); try { - list = lj.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + list = lj.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertTrue(false); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.Jobs.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java index 164f590..23f50de 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java @@ -5,24 +5,18 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; import com.xplenty.api.model.ScheduleTest; -import com.xplenty.api.util.Http; import junit.framework.TestCase; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Properties; +import java.util.*; /** * Author: Xardas @@ -47,10 +41,10 @@ public void testValidResponcehandling() throws JsonProcessingException, Unsuppor list.add(ScheduleTest.createMockSchedule(new Date())); String json = new ObjectMapper().writeValueAsString(list); - list = ls.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + list = ls.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertNotNull(list); assertFalse(list.isEmpty()); } @@ -63,10 +57,10 @@ public void testInvalidResponcehandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(list).replace("{", ""); try { - list = ls.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + list = ls.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertTrue(false); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.Schedules.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/RunJobTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/RunJobTest.java index 784efa3..a587234 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/RunJobTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/RunJobTest.java @@ -3,25 +3,21 @@ */ package com.xplenty.api.request; -import java.io.ByteArrayInputStream; -import java.io.UnsupportedEncodingException; -import java.util.Date; - -import junit.framework.TestCase; - -import org.junit.Test; - import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; import com.xplenty.api.model.JobTest; -import com.xplenty.api.util.Http; +import junit.framework.TestCase; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; /** * @author Yuriy Kovalek @@ -46,10 +42,10 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor Job j = JobTest.createMockJob(new Date()); String json = new ObjectMapper().writeValueAsString(j); - j = lc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + j = lc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertNotNull(j); } @@ -60,10 +56,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(j).replace("7", "seven"); try { - j = lc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + j = lc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); fail(); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.RunJob.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ScheduleInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ScheduleInfoTest.java index 30ebe9a..323674b 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ScheduleInfoTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ScheduleInfoTest.java @@ -5,22 +5,20 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; import com.xplenty.api.model.ScheduleTest; -import com.xplenty.api.util.Http; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.util.Date; +import java.util.HashMap; /** * @author xardas @@ -54,10 +52,10 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor Schedule c = ScheduleTest.createMockSchedule(now); ScheduleInfo cc = new ScheduleInfo(666); - c = cc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(TEST_JSON.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + TEST_JSON, + Status.OK.getStatusCode(), + new HashMap())); assertNotNull(c); assertEquals(new Long(666), c.getId()); @@ -74,10 +72,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(c).replace("{", "one"); ScheduleInfo cc = new ScheduleInfo(666); try { - c = cc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertTrue(false); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.Schedule.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/StopJobTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/StopJobTest.java index e0f4f85..320118d 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/StopJobTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/StopJobTest.java @@ -5,21 +5,19 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; import com.xplenty.api.model.JobTest; -import com.xplenty.api.util.Http; import junit.framework.TestCase; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.util.Date; +import java.util.HashMap; /** * @author Yuriy Kovalek @@ -44,10 +42,10 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor Job j = JobTest.createMockJob(new Date()); String json = new ObjectMapper().writeValueAsString(j); - j = sj.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + j = sj.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertNotNull(j); } @@ -58,10 +56,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(j).replace("7", "seven"); try { - j = sj.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + j = sj.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertTrue(false); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.StopJob.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/TerminateClusterTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/TerminateClusterTest.java index 027baf7..1898848 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/TerminateClusterTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/TerminateClusterTest.java @@ -5,21 +5,19 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; import com.xplenty.api.model.ClusterTest; -import com.xplenty.api.util.Http; import junit.framework.TestCase; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.util.Date; +import java.util.HashMap; /** * @author Yuriy Kovalek @@ -44,10 +42,10 @@ public void testValidResponcehandling() throws JsonProcessingException, Unsuppor Cluster c = ClusterTest.createMockCluster(new Date()); String json = new ObjectMapper().writeValueAsString(c); - c = tc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = tc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertNotNull(c); } @@ -58,10 +56,10 @@ public void testInvalidResponcehandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(c).replace("2", "two"); try { - c = tc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = tc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertTrue(false); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.TerminateCluster.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateClusterTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateClusterTest.java index 0063126..bf6b6a5 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateClusterTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateClusterTest.java @@ -3,29 +3,24 @@ */ package com.xplenty.api.request; -import java.io.ByteArrayInputStream; -import java.io.UnsupportedEncodingException; -import java.util.Date; - -import org.junit.Before; -import org.junit.Test; - import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.Xplenty.ClusterStatus; import com.xplenty.api.Xplenty.ClusterType; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; import com.xplenty.api.model.ClusterTest; -import com.xplenty.api.request.CreateCluster; -import com.xplenty.api.util.Http; - import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; /** * @author Yuriy Kovalek @@ -62,10 +57,10 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor String json = new ObjectMapper().writeValueAsString(c); CreateCluster cc = new CreateCluster(c); - c = cc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertNotNull(c); assertEquals(new Long(3), c.getId()); @@ -91,10 +86,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(c).replace("1", "one"); UpdateCluster cc = new UpdateCluster(c); try { - c = cc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); fail(); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.UpdateCluster.name + ": error parsing response object", e.getMessage()); @@ -105,10 +100,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp json = new ObjectMapper().writeValueAsString(c).replace("available", "ready"); cc = new UpdateCluster(c); try { - c = cc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); fail(); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.UpdateCluster.name + ": error parsing response object", e.getMessage()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateScheduleTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateScheduleTest.java index f7c834e..5b0ef19 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateScheduleTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateScheduleTest.java @@ -5,22 +5,20 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; import com.xplenty.api.model.ScheduleTest; -import com.xplenty.api.util.Http; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.util.Date; +import java.util.HashMap; /** * @author xardas @@ -55,10 +53,11 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor String json = new ObjectMapper().writeValueAsString(c); UpdateSchedule cc = new UpdateSchedule(c); - c = cc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(TEST_JSON.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + TEST_JSON, + Status.OK.getStatusCode(), + new HashMap())); assertNotNull(c); assertEquals(new Long(666), c.getId()); @@ -75,10 +74,10 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = new ObjectMapper().writeValueAsString(c).replace("{", "one"); UpdateSchedule cc = new UpdateSchedule(c); try { - c = cc.getResponse(new ClientResponse(Status.OK.getStatusCode(), - new InBoundHeaders(), - new ByteArrayInputStream(json.getBytes("UTF-8")), - Client.create().getMessageBodyWorkers())); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); assertTrue(false); } catch (XplentyAPIException e) { assertEquals(Xplenty.Resource.UpdateSchedule.name + ": error parsing response object", e.getMessage()); From 11253453a4190a6a4501afce931eaea6d327f5bc Mon Sep 17 00:00:00 2001 From: xardas Date: Sun, 3 Jan 2016 19:36:02 +0300 Subject: [PATCH 02/35] set netty and jersey dependencies as optional (as one will probably choose only one implementation) refactored validation and RequestFaultException changed readme to reflect changes --- README.md | 9 +- xplenty.jar-core/pom.xml | 4 + .../main/java/com/xplenty/api/XplentyAPI.java | 4 + .../api/exceptions/AuthFailedException.java | 4 +- .../exceptions/RequestFailedException.java | 18 +++- .../main/java/com/xplenty/api/http/Http.java | 8 +- .../java/com/xplenty/api/http/HttpClient.java | 38 +++++++ .../xplenty/api/http/HttpClientBuilder.java | 51 ++++++++- .../com/xplenty/api/http/JerseyClient.java | 40 +++---- .../java/com/xplenty/api/http/Response.java | 15 +++ ...truster.java => SSLEngineDefaultImpl.java} | 11 +- .../com/xplenty/api/http/SyncNettyClient.java | 100 +++++++++++++----- .../api/request/watching/WatchingStop.java | 2 +- .../java/com/xplenty/api/XplentyAPITest.java | 18 +++- .../api/request/ListSchedulesTest.java | 5 +- 15 files changed, 246 insertions(+), 81 deletions(-) rename xplenty.jar-core/src/main/java/com/xplenty/api/http/{SSLUntruster.java => SSLEngineDefaultImpl.java} (79%) diff --git a/README.md b/README.md index 9fac5e0..a37369a 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ String api_key = "V4eyfgNqYcSasXGhzNxS"; XplentyAPI xplentyAPI = new XplentyAPI(account_id , api_key); ``` -If you want to supply custom values for the version, protocol or host that the XplentyAPI object will use, +If you want to supply custom values for the version, protocol, host, timeout, logging or client implementation that the XplentyAPI object will use, you can use XplentyAPI builder methods to customize these properties. ```java @@ -24,10 +24,9 @@ String api_key = "V4eyfgNqYcSasXGhzNxS"; Xplenty.Version version = Xplenty.Version.V1; String host = 'myHost'; Http.Protocol proto = Http.Protocol.Https; -XplentyAPI xplentyAPI = new XplentyAPI(account_id , api_key) - .withVersion(version) - .withHost(host) - .withProtocol(proto); +HttpClientBuilder builder = new HttpClientBuilder().withAccount(account_id).withApiKey(api_key). + withHost(host).withLogHttpCommunication(true).withClientImpl(Http.HttpClientImpl.SyncNetty); +XplentyAPI xplentyAPI = new XplentyAPI(builder); ``` ### List the Cluster Plans diff --git a/xplenty.jar-core/pom.xml b/xplenty.jar-core/pom.xml index 61629c0..a96c05b 100644 --- a/xplenty.jar-core/pom.xml +++ b/xplenty.jar-core/pom.xml @@ -24,18 +24,21 @@ com.sun.jersey jersey-client 1.9.1 + true com.sun.jersey jersey-core 1.9.1 + true com.sun.jersey jersey-json 1.9.1 + true @@ -60,6 +63,7 @@ io.netty netty 3.10.5.Final + true diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index 6c7222e..b5bd448 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -401,4 +401,8 @@ public Http.Protocol getProtocol() { public Version getVersion() { return client.getVersion(); } + + public int getTimeout() { + return client.getTimeout(); + } } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/AuthFailedException.java b/xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/AuthFailedException.java index ff557d4..51ab188 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/AuthFailedException.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/AuthFailedException.java @@ -4,6 +4,8 @@ package com.xplenty.api.exceptions; +import com.xplenty.api.http.Http; + /** * @author Yuriy Kovalek * @@ -11,7 +13,7 @@ public class AuthFailedException extends RequestFailedException { private static final long serialVersionUID = 3015805619788286689L; - public AuthFailedException(int status, String response) { + public AuthFailedException(Http.ResponseStatus status, String response) { super("Server declined authorization", status, response); } } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/RequestFailedException.java b/xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/RequestFailedException.java index 47251ef..cfbd334 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/RequestFailedException.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/RequestFailedException.java @@ -3,6 +3,8 @@ */ package com.xplenty.api.exceptions; +import com.xplenty.api.http.Http; + /** * @author Yuriy Kovalek * @@ -10,13 +12,15 @@ public class RequestFailedException extends XplentyAPIException { private static final long serialVersionUID = -456749863406425145L; - private int status; - private String response; + private final int status; + private final String response; + private final String statusDescription; - public RequestFailedException(String msg, int status, String response) { - super(msg + " HTTP status code: " + status + ", server response: " + response); - this.status = status; + public RequestFailedException(String msg, Http.ResponseStatus responseStatus, String response) { + super(String.format("%s, HTTP status code: %s[%s], server response: [%s]" , msg, responseStatus.getCode(), responseStatus.getDescription(), response)); + this.status = responseStatus.getCode(); this.response = response; + this.statusDescription = responseStatus.getDescription(); } public int getStatus() { @@ -26,4 +30,8 @@ public int getStatus() { public String getResponse() { return response; } + + public String getStatusDescription() { + return statusDescription; + } } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java index ecd0e08..f0665fb 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java @@ -31,6 +31,9 @@ public enum Method { GET, POST, PUT, DELETE } + /** + * Protocol used by Xplenty API Server + */ public static enum Protocol { Http("http"), Https("https"); @@ -42,8 +45,11 @@ public static enum Protocol { } } + /** + * Http Client Implementation used to connect + */ public static enum HttpClientImpl { - SyncNetty, AsyncNetty, Jersey; + SyncNetty, /* for future impl AsyncNetty, */ Jersey; } public static enum ResponseStatus { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java index 98e0d73..debd3e2 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java @@ -5,6 +5,7 @@ import com.xplenty.api.request.Request; /** + * Http Client interface for Request processing * Author: Xardas * Date: 02.01.16 * Time: 15:36 @@ -15,17 +16,54 @@ public interface HttpClient { static final int DEFAULT_HTTP_PORT = 80; static final String API_PATH = "api"; + /** + * Synchronously execute given request + * @param xplentyRequest request to execute + * @param XplentyObject itself or a collection of XplentyObjects + * @return respective response type + * @throws XplentyAPIException if any error occurs + */ T execute(Request xplentyRequest) throws XplentyAPIException; + /** + * Some client implementations may create thread pool for socket handling + * This method terminates these thread pools and frees any resources obtained by the client + */ void shutdown(); + /** + * + * @return Account name used + */ String getAccountName(); + /** + * + * @return API Key used + */ String getApiKey(); + /** + * + * @return Host used + */ String getHost(); + /** + * + * @return protocol used (usually secure https) + */ Http.Protocol getProtocol(); + /** + * + * @return Xplenty API Version + */ Xplenty.Version getVersion(); + + /** + * + * @return timeout for connection/reading + */ + int getTimeout(); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClientBuilder.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClientBuilder.java index 24ca3f3..9c37dcb 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClientBuilder.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClientBuilder.java @@ -4,6 +4,7 @@ import com.xplenty.api.exceptions.XplentyAPIException; /** + * Builder for Http client * Author: Xardas * Date: 02.01.16 * Time: 15:32 @@ -19,47 +20,91 @@ public class HttpClientBuilder { private String accountName; private Xplenty.Version version = Xplenty.Version.V1; - + /** + * + * @param timeout timeout for connection/reading + * @return builder + */ public HttpClientBuilder withTimeout(int timeout) { this.timeout = timeout; return this; } + /** + * + * @param apiKey User's API key found at https://www.xplenty.com/settings/edit + * @return builder + */ public HttpClientBuilder withApiKey(String apiKey) { this.apiKey = apiKey; return this; } + /** + * + * @param protocol protocol to use for connection + * @return builder + */ public HttpClientBuilder withProtocol(Http.Protocol protocol) { this.protocol = protocol; return this; } + /** + * + * @param logHttpCommunication Log raw packets transfered? (before encryption / after decryption) + * @return builder + */ public HttpClientBuilder withLogHttpCommunication(boolean logHttpCommunication) { this.logHttpCommunication = logHttpCommunication; return this; } + /** + * + * @param accountName account name used for Xplenty sign-up + * @return builder + */ public HttpClientBuilder withAccount(String accountName) { this.accountName = accountName; return this; } + /** + * + * @param host API hostname + * @return builder + */ public HttpClientBuilder withHost(String host) { this.host = host; return this; } + /** + * + * @param impl client implementation to use + * @return builder + */ public HttpClientBuilder withClientImpl(Http.HttpClientImpl impl) { this.clientImpl = impl; return this; } + /** + * + * @param version API version to use + * @return builder + */ public HttpClientBuilder withVersion(Xplenty.Version version) { this.version = version; return this; } + /** + * Creates configured Http client + * @return Configured Http Client + * @throws XplentyAPIException if API KEY or Account Name is missing + */ public HttpClient build() throws XplentyAPIException { if (apiKey == null) { throw new XplentyAPIException("Api Key not set!"); @@ -69,9 +114,9 @@ public HttpClient build() throws XplentyAPIException { } switch (clientImpl) { case SyncNetty: - return new SyncNettyClient(accountName, apiKey, host, protocol, timeout, logHttpCommunication); + return new SyncNettyClient(accountName, apiKey, host, protocol, version, timeout, logHttpCommunication); default: - return new JerseyClient(accountName, apiKey, host, protocol, timeout, logHttpCommunication); + return new JerseyClient(accountName, apiKey, host, protocol, version, timeout, logHttpCommunication); } } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java index 111add7..d036047 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java @@ -12,8 +12,6 @@ import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; import com.sun.jersey.api.client.filter.LoggingFilter; import com.xplenty.api.Xplenty.Version; -import com.xplenty.api.exceptions.AuthFailedException; -import com.xplenty.api.exceptions.RequestFailedException; import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.request.Request; @@ -34,9 +32,11 @@ public class JerseyClient implements HttpClient { private final Http.Protocol protocol; private final String accountName; private final String apiKey; + private final Version version; + private final int timeout; private final Client client; - private Version version = null; + /** * Construct a new instance for given account and API key @@ -47,13 +47,17 @@ public class JerseyClient implements HttpClient { * @param timeout timeout for response. * @param logHttpCommunication enables logging of requests and responses */ - JerseyClient(String accountName, String apiKey, String host, Http.Protocol protocol, int timeout, boolean logHttpCommunication) { + JerseyClient(String accountName, String apiKey, String host, Http.Protocol protocol, Version version, int timeout, boolean logHttpCommunication) { this.accountName = accountName; this.apiKey = apiKey; this.host = host; this.protocol = protocol; + this.version = version; + this.timeout = timeout; ClientConfig config = new DefaultClientConfig(); + config.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, timeout); + config.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, timeout); client = Client.create(config); client.addFilter(new HTTPBasicAuthFilter(apiKey, "")); if (logHttpCommunication) { @@ -75,9 +79,9 @@ public T execute(Request request) { case PUT: response = builder.put(ClientResponse.class); break; case DELETE: response = builder.delete(ClientResponse.class); break; } - validate(request, response); Response processedResponse = Response.forContentType(request.getResponseType(), response.getEntity(String.class), response.getStatus(), convertJerseyHeaders(response.getHeaders())); + processedResponse.validate(request.getName()); return request.getResponse(processedResponse); } @@ -122,23 +126,6 @@ private String getMethodURL(String methodEndpoint) { return protocol + "://" + host + "/" + accountName + "/" + API_PATH + "/" + methodEndpoint; } - /** - * Check the response status and throws exception on errors - * @param request used request - * @param response received response - * @throws com.xplenty.api.exceptions.AuthFailedException - * @throws com.xplenty.api.exceptions.RequestFailedException - */ - private void validate(Request request, ClientResponse response) { - if (response.getClientResponseStatus() != null) - switch (response.getClientResponseStatus()){ - case OK : case CREATED : case NO_CONTENT : return; - case UNAUTHORIZED : throw new AuthFailedException(response.getStatus(), response.getEntity(String.class)); - default: break; - } - throw new RequestFailedException(request.getName() + " failed", response.getStatus(), response.getEntity(String.class)); - } - public String getAccountName() { return accountName; } @@ -147,10 +134,6 @@ public String getApiKey() { return apiKey; } - public void setVersion(Version ver) { - version = ver; - } - public Http.Protocol getProtocol() { return protocol; } @@ -163,6 +146,11 @@ public Version getVersion() { return version; } + @Override + public int getTimeout() { + return timeout; + } + @Override public void shutdown() { if (client != null) { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java index 7e8b9e1..44d775d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java @@ -1,6 +1,8 @@ package com.xplenty.api.http; import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.exceptions.AuthFailedException; +import com.xplenty.api.exceptions.RequestFailedException; import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.model.XplentyObject; @@ -26,6 +28,19 @@ protected Response(String content, int status, Map headers) { this.headers = headers; } + /** + * Check the response status and throws exception on errors + * @param requestName RequestName (used for exception construction) + * @throws com.xplenty.api.exceptions.AuthFailedException + * @throws com.xplenty.api.exceptions.RequestFailedException + */ + public void validate(String requestName) { + switch (this.getStatus()) { + case HTTP_200: case HTTP_201: case HTTP_204 : return; + case HTTP_401: throw new AuthFailedException(this.getStatus(), this.getRawContent()); + default: throw new RequestFailedException(requestName + " failed", this.getStatus(), this.getRawContent()); + } + } public static Response forContentType(Http.MediaType type, String content, int status, Map headers) { switch (type) { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SSLUntruster.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SSLEngineDefaultImpl.java similarity index 79% rename from xplenty.jar-core/src/main/java/com/xplenty/api/http/SSLUntruster.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/http/SSLEngineDefaultImpl.java index 4c9f316..de9e079 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SSLUntruster.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SSLEngineDefaultImpl.java @@ -14,8 +14,7 @@ * * @author xardas */ -public class SSLUntruster { - private static SSLContext sc; +public class SSLEngineDefaultImpl { private static SSLContext wc; private static HostnameVerifier hv; static { @@ -34,19 +33,13 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {} }; try { - sc = SSLContext.getInstance("SSL"); - sc.init(null, trustAllCerts, new SecureRandom()); wc = SSLContext.getInstance("TLS"); wc.init(null, trustAllCerts, new SecureRandom()); } catch (Exception ex) { } } - public static SSLContext getUntruster() { - return sc; - } - - public static SSLContext getWebUntruster() { + public static SSLContext getSSLContext() { return wc; } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java index 68ce2d0..b393022 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java @@ -17,7 +17,7 @@ import org.jboss.netty.logging.InternalLogLevel; import javax.net.ssl.SSLEngine; -import java.io.StringWriter; +import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; import java.net.InetSocketAddress; import java.net.URL; @@ -36,8 +36,11 @@ */ public class SyncNettyClient extends SimpleChannelUpstreamHandler implements HttpClient { public static final String MUTEX_KEY = "mutex"; + protected static final int MAX_CHANNEL_MEMORY_SIZE = 50 * 1024 * 1024; // 50 MB for channel + protected static final long MAX_TOTAL_MEMORY_SIZE = 1 * 1024 * 1024 * 1024; // 1 G private final ClientBootstrap client; private final NioClientSocketChannelFactory chanfac; + private final ThreadLocal channel = new ThreadLocal<>(); private final int timeout; private final String authHeader; @@ -45,15 +48,18 @@ public class SyncNettyClient extends SimpleChannelUpstreamHandler implements Htt private final Http.Protocol protocol; private final String accountName; private final String apiKey; + private final Xplenty.Version version; - - SyncNettyClient(String accountName, String apiKey, String host, Http.Protocol protocol, int timeout, boolean logHttpCommunication) throws XplentyAPIException { + SyncNettyClient(String accountName, String apiKey, String host, Http.Protocol protocol, Xplenty.Version version, int timeout, boolean logHttpCommunication) throws XplentyAPIException { this.timeout = timeout; this.accountName = accountName; this.host = host; this.protocol = protocol; this.apiKey = apiKey; + this.version = version; + + // generate basic auth header try { this.authHeader = String.format("Basic %s", new String(Base64.encode(String.format("%s:", apiKey)), "ASCII")); } catch (UnsupportedEncodingException e) { @@ -61,23 +67,21 @@ public class SyncNettyClient extends SimpleChannelUpstreamHandler implements Htt } chanfac = new NioClientSocketChannelFactory( - new OrderedMemoryAwareThreadPoolExecutor(1, 400000000, 2000000000, 120, TimeUnit.SECONDS), - new OrderedMemoryAwareThreadPoolExecutor(2, 400000000, 2000000000, 60, TimeUnit.SECONDS), + new OrderedMemoryAwareThreadPoolExecutor(1, MAX_CHANNEL_MEMORY_SIZE, MAX_TOTAL_MEMORY_SIZE, 120, TimeUnit.SECONDS), + new OrderedMemoryAwareThreadPoolExecutor(2, MAX_CHANNEL_MEMORY_SIZE, MAX_TOTAL_MEMORY_SIZE, 120, TimeUnit.SECONDS), 2); client = new ClientBootstrap(chanfac); client.setOption("child.tcpNoDelay", true); client.setOption("child.keepAlive", true); client.setOption("reuseAddress", true); - client.setOption("connectTimeoutMillis", 30000); -// Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { -// @Override -// public void run() { -// if (eventLoop != null && !eventLoop.isShutdown() && !eventLoop.isTerminated()) { -// eventLoop.shutdownGracefully(); -// } -// } -// })); - client.setPipelineFactory(new PipelineFactory(this, protocol.equals(Http.Protocol.Https))); + client.setOption("connectTimeoutMillis", timeout * 1000); + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + @Override + public void run() { + client.releaseExternalResources(); + } + })); + client.setPipelineFactory(new PipelineFactory(this, protocol.equals(Http.Protocol.Https), logHttpCommunication)); } @Override @@ -119,24 +123,24 @@ private HttpMethod convertRequestMethod(Http.Method method) { public T execute(Request xplentyRequest) throws XplentyAPIException { try { URL url = new URL(getMethodURL(xplentyRequest.getEndpoint())); - Channel channel = client.connect(new InetSocketAddress(url.getHost(), getPort(url))).awaitUninterruptibly().getChannel(); + Channel channel = getChannel(url); HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, convertRequestMethod(xplentyRequest.getHttpMethod()), url.toString()); HttpHeaders headers = request.headers(); headers.set(HttpHeaders.Names.HOST, url.getHost()); - headers.set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); + headers.set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); headers.set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP + ',' + HttpHeaders.Values.DEFLATE); headers.set(HttpHeaders.Names.ACCEPT_CHARSET, "utf-8;q=0.7,*;q=0.7"); - headers.set(HttpHeaders.Names.USER_AGENT, "Xplenty Netty client by Xardas"); - headers.set(HttpHeaders.Names.ACCEPT, xplentyRequest.getResponseType().value); + headers.set(HttpHeaders.Names.USER_AGENT, "Xplenty Netty client by Xardazz"); + headers.set(HttpHeaders.Names.ACCEPT, getAcceptHeaderValue(xplentyRequest.getResponseType().value)); headers.set(HttpHeaders.Names.AUTHORIZATION, authHeader); if (xplentyRequest.hasBody()) { final ChannelBuffer cb = ChannelBuffers.dynamicBuffer(8000); - StringWriter sw = new StringWriter(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectMapper objectMapper = JsonMapperFactory.getInstance(); - objectMapper.writeValue(sw, xplentyRequest.getBody()); - final byte[] contentBytes = sw.getBuffer().toString().getBytes(StandardCharsets.UTF_8); + objectMapper.writeValue(bos, xplentyRequest.getBody()); + final byte[] contentBytes = bos.toByteArray(); cb.writeBytes(contentBytes); request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, contentBytes.length); request.headers().set(HttpHeaders.Names.CONTENT_TYPE, Http.MediaType.JSON.value); @@ -149,19 +153,51 @@ public T execute(Request xplentyRequest) throws XplentyAPIException { response.getMutex().await(timeout, TimeUnit.SECONDS); final HttpResponse httpBody = response.getResponse(); + if (httpBody == null) { + throw new XplentyAPIException("Request timed out!"); + } int httpStatus = parseStatus(httpBody.headers().get("Status")); Response processedResponse = Response.forContentType(xplentyRequest.getResponseType(), channelBuffer2String(httpBody.getContent()), httpStatus, convertNettyHeaders(httpBody.headers())); + processedResponse.validate(xplentyRequest.getName()); return xplentyRequest.getResponse(processedResponse); } catch (Exception e) { throw new XplentyAPIException(e); } } + private Channel getChannel(URL url) throws XplentyAPIException { + Channel chan = channel.get(); + // if channel is valid no need to reconnect + if (chan == null || !chan.isConnected() || !chan.isOpen()) { + try { + final ChannelFuture channelFuture = client.connect(new InetSocketAddress(url.getHost(), getPort(url))); + channelFuture.await(timeout, TimeUnit.SECONDS); + if (channelFuture.isDone() && channelFuture.isSuccess()) { + chan = channelFuture.getChannel(); + channel.set(chan); + } else { + throw new XplentyAPIException("Error connecting to Xplenty server:", channelFuture.getCause()); + } + } catch (InterruptedException ex) { + throw new XplentyAPIException("Interrupted while getting channel!", ex); + } + } + return chan; + } + @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent cause) throws Exception { + ctx.getChannel().close(); throw new XplentyAPIException("Exception while communicating with remote Xplenty server", cause.getCause()); } + private String getAcceptHeaderValue(String baseValue) { + if (this.version == null) { + return baseValue; + } + return String.format("%s; %s", baseValue, this.version.format()); + } + private Map convertNettyHeaders(HttpHeaders headers) { final Map convertedHeaders = new HashMap<>(); for (Map.Entry header : headers.entries()) { @@ -197,18 +233,21 @@ private int getPort(URL url) { @Override public void shutdown() { + //client.shutdown(); // does the same client.releaseExternalResources(); } - public class PipelineFactory implements ChannelPipelineFactory { + private static class PipelineFactory implements ChannelPipelineFactory { private final boolean ssl; + private final boolean logCommunication; private final ChannelUpstreamHandler handler; - public PipelineFactory(ChannelUpstreamHandler handler, boolean ssl) { + public PipelineFactory(ChannelUpstreamHandler handler, boolean ssl, boolean logCommunication) { this.handler = handler; this.ssl = ssl; + this.logCommunication = logCommunication; } public ChannelPipeline getPipeline() throws Exception { @@ -217,14 +256,18 @@ public ChannelPipeline getPipeline() throws Exception { // Enable HTTPS if necessary. if (ssl) { - SSLEngine engine = - SSLUntruster.getWebUntruster().createSSLEngine(); + // trust to any server certificate + // example implementation with comparison to keystore + // http://www.programcreek.com/java-api-examples/index.php?source_dir=search-guard-master/src/main/java/com/floragunn/searchguard/transport/SSLNettyTransport.java + SSLEngine engine = SSLEngineDefaultImpl.getSSLContext().createSSLEngine(); engine.setUseClientMode(true); pipeline.addLast("ssl", new SslHandler(engine)); } - pipeline.addLast("logger", new LoggingHandler("global", InternalLogLevel.DEBUG, true)); + if (logCommunication) { + pipeline.addLast("logger", new LoggingHandler("nettyclient", InternalLogLevel.DEBUG, true)); + } pipeline.addLast("codec", new HttpClientCodec()); @@ -257,6 +300,7 @@ public void setResponse(HttpResponse response) { } } + @Override public int getTimeout() { return timeout; } @@ -273,7 +317,7 @@ public Http.Protocol getProtocol() { @Override public Xplenty.Version getVersion() { - return null; + return version; } @Override diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java index f299ee6..02ca439 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java @@ -51,6 +51,6 @@ public Boolean getResponse(Response response) { if (code == 204) return true; else - throw new RequestFailedException("204 expected, but something went wrong", code, ""); + throw new RequestFailedException("204 expected, but something went wrong", response.getStatus(), ""); } } diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java index 1fd791e..8bd2307 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java @@ -34,13 +34,18 @@ public void testConstructor() { public void testBuilder() { HttpClientBuilder builder = new HttpClientBuilder().withAccount("testAcc").withApiKey("testKey"). - withHost("www.example.com").withProtocol(Http.Protocol.Http).withVersion(Xplenty.Version.V1); + withHost("www.example.com").withProtocol(Http.Protocol.Http).withVersion(Xplenty.Version.V1). + withClientImpl(Http.HttpClientImpl.SyncNetty).withLogHttpCommunication(true).withTimeout(10); XplentyAPI api = new XplentyAPI(builder); assertNotNull(api); assertEquals("www.example.com", api.getHost()); assertEquals(Http.Protocol.Http, api.getProtocol()); assertEquals(Xplenty.Version.V1, api.getVersion()); + assertEquals("testAcc", api.getAccountName()); + assertEquals(10, api.getTimeout()); + + } public void testListClusters() { @@ -90,6 +95,12 @@ public Http.Protocol getProtocol() { public Xplenty.Version getVersion() { return null; } + + @Override + public int getTimeout() { + return 0; + } + }; XplentyAPI api = new XplentyAPI(client); Cluster res = api.listClusters().get(0); @@ -139,6 +150,11 @@ public Http.Protocol getProtocol() { public Xplenty.Version getVersion() { return null; } + + @Override + public int getTimeout() { + return 0; + } }; XplentyAPI api = new XplentyAPI(client); Job res = api.listJobs().get(0); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java index 23f50de..0615110 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java @@ -38,9 +38,12 @@ public void testIntegrity() { public void testValidResponcehandling() throws JsonProcessingException, UnsupportedEncodingException { ListSchedules ls = new ListSchedules(new Properties()); List list = new ArrayList(); - list.add(ScheduleTest.createMockSchedule(new Date())); + final Schedule mockSchedule = ScheduleTest.createMockSchedule(new Date()); + mockSchedule.getTask().getPackages().clear(); + list.add(mockSchedule); String json = new ObjectMapper().writeValueAsString(list); + json = json.replace("\"packages\":{}", "\"packages\":[]"); // sent and recieved json for schedules slightly differ. list = ls.getResponse(Response.forContentType(Http.MediaType.JSON, json, Status.OK.getStatusCode(), From 6eb99d3951aae079bd69963f623b565a6984ab2b Mon Sep 17 00:00:00 2001 From: xardas Date: Sun, 3 Jan 2016 19:48:33 +0300 Subject: [PATCH 03/35] fix merge conflicts --- .../test/java/com/xplenty/api/request/ListSchedulesTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java index b3a22ce..ae3cccd 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java @@ -5,21 +5,16 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.ClientResponse.Status; -import com.sun.jersey.core.header.InBoundHeaders; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.http.Http; import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; import com.xplenty.api.model.ScheduleTest; -import com.xplenty.api.util.Http; import junit.framework.TestCase; import org.junit.Test; -import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.util.*; From 7d92520d293b928e79b8226d34fdc9539d4c9b7d Mon Sep 17 00:00:00 2001 From: xardas Date: Sun, 3 Jan 2016 21:55:33 +0300 Subject: [PATCH 04/35] add new entities --- .../main/java/com/xplenty/api/Xplenty.java | 57 +++++ .../java/com/xplenty/api/model/Account.java | 223 ++++++++++++++++++ .../xplenty/api/model/ClusterInstance.java | 102 ++++++++ .../com/xplenty/api/model/Connection.java | 75 ++++++ .../java/com/xplenty/api/model/Member.java | 161 +++++++++++++ .../com/xplenty/api/model/Notification.java | 59 +++++ .../xplenty/api/model/PackageTemplate.java | 70 ++++++ .../api/model/PackageTemplateAuthor.java | 44 ++++ .../xplenty/api/model/PackageValidation.java | 120 ++++++++++ 9 files changed, 911 insertions(+) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/Account.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterInstance.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/Connection.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/Notification.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplate.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index 60e94f3..1ddaad9 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -108,6 +108,63 @@ public enum ClusterType { } + public enum AccountRole { + admin("admin"), + member("member"); + + @SuppressWarnings("unused") + private final String role; + + AccountRole(String role) { + this.role = role; + } + } + + public enum ClusterInstanceStatus { + available("available"), + terminated("terminated"); + + @SuppressWarnings("unused") + private final String status; + + ClusterInstanceStatus(String status) { + this.status = status; + } + } + + public enum ConnectionType { + s3("s3"), + swift("swift"), + gs("gs"), + rackspace("rackspace"), + softlayer("softlayer"), + hdfs("hdfs"), + adwords("adwords"), + postgres("postgres"), + redshift("redshift"), + mongo("mongo"), + mysql("mysql"), + hana("hana"), + sqlserver("sqlserver"), + herokupostgres("herokupostgres"), + googlecloudsql("googlecloudsql"), + bigquery("bigquery"), + segment("segment"); + + @SuppressWarnings("unused") + private final String type; + + ConnectionType(String type) { + this.type = type; + } + } + + public enum PackageValidationStatus { + running, + completed, + failed; + } + @JsonFormat(shape= JsonFormat.Shape.SCALAR) public static enum ClusterStatus { pending("pending"), diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Account.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Account.java new file mode 100644 index 0000000..99936f3 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Account.java @@ -0,0 +1,223 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; + +import java.util.Date; + +/** + * Data model for Xplenty account + * An Xplenty account represents a related group (usually a company) of Xplenty users. + * Author: Xardas + * Date: 03.01.16 + * Time: 18:52 + */ +public class Account extends XplentyObject { + @JsonProperty + private Long id; + @JsonProperty("account_id") + private String accountId; + @JsonProperty + private String uname; + @JsonProperty + private String region; + @JsonProperty + private String location; + @JsonProperty("billing_email") + private String billingEmail; + @JsonProperty("gravatar_email") + private String gravatarEmail; + @JsonProperty("avatar_url") + private String avatarUrl; + @JsonProperty("created_at") + private Date createdAt; + @JsonProperty("updated_at") + private Date updatedAt; + @JsonProperty("schedules_count") + private Integer schedulesCount; + @JsonProperty("connections_count") + private Integer connectionsCount; + @JsonProperty + private Xplenty.AccountRole role; + @JsonProperty("owner_id") + private Long ownerId; + @JsonProperty("members_count") + private Integer membersCount; + @JsonProperty("packages_count") + private Integer packagesCount; + @JsonProperty("jobs_count") + private Integer jobsCount; + @JsonProperty("running_jobs_count") + private Integer runningJobsCount; + @JsonProperty + private String url; + @JsonProperty("public_key") + private String publicKey; + + + private Account() { + super(Account.class); + } + + + /** + * + * @return the account's numeric identifier + */ + public Long getId() { + return id; + } + + /** + * + * @return the account's unique identifier + */ + public String getAccountId() { + return accountId; + } + + /** + * + * @return the account's numeric identifier with u_ prefix + */ + public String getUname() { + return uname; + } + + /** + * + * @return the account's region + */ + public String getRegion() { + return region; + } + + /** + * + * @return the account's location + */ + public String getLocation() { + return location; + } + + /** + * + * @return the account's billing email + */ + public String getBillingEmail() { + return billingEmail; + } + + /** + * + * @return the account's gravatar email + */ + public String getGravatarEmail() { + return gravatarEmail; + } + + /** + * + * @return the url for the account's avatar + */ + public String getAvatarUrl() { + return avatarUrl; + } + + /** + * + * @return the date and time the account was created + */ + public Date getCreatedAt() { + return createdAt; + } + + /** + * + * @return the date and time the account was last updated + */ + public Date getUpdatedAt() { + return updatedAt; + } + + /** + * + * @return the number of schedules for the account + */ + public Integer getSchedulesCount() { + return schedulesCount; + } + + /** + * + * @return the number of connections for the account + */ + public Integer getConnectionsCount() { + return connectionsCount; + } + + /** + * + * @return the member's role in the account + */ + public Xplenty.AccountRole getRole() { + return role; + } + + /** + * + * @return the numeric identifier of the account's owner + */ + public Long getOwnerId() { + return ownerId; + } + + /** + * + * @return the number of members in the account + */ + public Integer getMembersCount() { + return membersCount; + } + + /** + * + * @return the number of packages for the account + */ + public Integer getPackagesCount() { + return packagesCount; + } + + /** + * + * @return the number of jobs for the account + */ + public Integer getJobsCount() { + return jobsCount; + } + + /** + * + * @return the number of running jobs for the account + */ + public Integer getRunningJobsCount() { + return runningJobsCount; + } + + /** + * + * @return the account resource URL + */ + public String getUrl() { + return url; + } + + /** + * + * @return the account ssh public key generated upon creation + */ + public String getPublicKey() { + return publicKey; + } + +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterInstance.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterInstance.java new file mode 100644 index 0000000..745904c --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterInstance.java @@ -0,0 +1,102 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; + +/** + * Data model for cluster instance + * An Xplenty cluster instance is a machine (node) that were allocated for a given cluster. + * Author: Xardas + * Date: 03.01.16 + * Time: 19:41 + */ +public class ClusterInstance extends XplentyObject { + @JsonProperty("instance_id") + private String instanceId; + @JsonProperty("private_dns") + private String privateDns; + @JsonProperty("public_dns") + private String publicDns; + @JsonProperty + private Xplenty.ClusterInstanceStatus status; + @JsonProperty + private Boolean master; + @JsonProperty + private Boolean spot; + @JsonProperty + private Boolean vpc; + @JsonProperty + private String zone; + @JsonProperty("instance_type") + private String instanceType; + + private ClusterInstance() { + super(ClusterInstance.class); + } + + + /** + * @return the provider specific identifier of the instance + */ + public String getInstanceId() { + return instanceId; + } + + /** + * @return the private fully qualified DNS of the instance + */ + public String getPrivateDns() { + return privateDns; + } + + /** + * @return the public fully qualified DNS of the instance + */ + public String getpublicDns() { + return publicDns; + } + + /** + * @return the instance's status. Possible values are: + * available - the instance is available + * terminated - the instance is no longer accessible + */ + public Xplenty.ClusterInstanceStatus getStatus() { + return status; + } + + /** + * @return indicates whether the instance is the master of the cluster + */ + public Boolean getMaster() { + return master; + } + + /** + * @return indicates whether the instance is a spot instance + */ + public Boolean getSpot() { + return spot; + } + + /** + * @return indicates whether the instance is part of a vpc + */ + public Boolean getVpc() { + return vpc; + } + + /** + * @return the provider specific zone the instance was provisioned at. Returns 'Unsupported' for regions with no support for zones. + */ + public String getZone() { + return zone; + } + + /** + * @return the instance type + */ + public String getInstanceType() { + return instanceType; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Connection.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Connection.java new file mode 100644 index 0000000..e37119e --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Connection.java @@ -0,0 +1,75 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; + +import java.util.Date; + +/** + * Data model for connection + * An Xplenty connection contain access information required to connect to your various data stores. + * The access information is stored securely and can only be used by your account's members. + * Author: Xardas + * Date: 03.01.16 + * Time: 19:55 + */ +public class Connection extends XplentyObject { + @JsonProperty + private Long id; + @JsonProperty + private String name; + @JsonProperty("created_at") + private Date createdAt; + @JsonProperty("updated_at") + private Date updatedAt; + @JsonProperty + private Xplenty.ConnectionType type; + @JsonProperty + private String url; + + private Connection() { + super(Connection.class); + } + + /** + * @return the connection's numeric identifier + */ + public Long getId() { + return id; + } + + /** + * @return the descriptive name given to the connection + */ + public String getName() { + return name; + } + + /** + * @return the date and time the connection was created + */ + public Date getCreatedAt() { + return createdAt; + } + + /** + * @return the date and time the connection was last updated + */ + public Date getUpdatedAt() { + return updatedAt; + } + + /** + * @return the type of the connection. + */ + public Xplenty.ConnectionType getType() { + return type; + } + + /** + * @return the connection resource URL + */ + public String getUrl() { + return url; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java new file mode 100644 index 0000000..eccc265 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java @@ -0,0 +1,161 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; + +import java.util.Date; + +/** + * Data model for Xplenty Member + * A member represents a user that has been given access to an Xplenty account. + * Author: Xardas + * Date: 03.01.16 + * Time: 20:12 + */ +public class Member extends XplentyObject { + @JsonProperty + private Long id; + @JsonProperty + private String name; + @JsonProperty + private String email; + @JsonProperty("gravatar_email") + private String gravatarEmail; + @JsonProperty("avatar_url") + private String avatarUrl; + @JsonProperty("created_at") + private Date createdAt; + @JsonProperty("updated_at") + private Date updatedAt; + @JsonProperty + private String location; + @JsonProperty + private Boolean confirmed; + @JsonProperty("confirmed_at") + private Date confirmedAt; + @JsonProperty + private Xplenty.AccountRole role; + @JsonProperty + private Boolean owner; + @JsonProperty + private String url; + @JsonProperty("html_url") + private String htmlUrl; + + protected Member() { + super(Member.class); + } + + /** + * + * @return the users's numeric identifier + */ + public Long getId() { + return id; + } + + /** + * + * @return the full name of the user + */ + public String getName() { + return name; + } + + /** + * + * @return the email of the user (also used to login) + */ + public String getEmail() { + return email; + } + + /** + * + * @return the user's gravatar email + */ + public String getGravatarEmail() { + return gravatarEmail; + } + + /** + * + * @return the url for the user's avatar + */ + public String getAvatarUrl() { + return avatarUrl; + } + + /** + * + * @return the date and time the account was created + */ + public Date getCreatedAt() { + return createdAt; + } + + /** + * + * @return the date and time the account was last updated + */ + public Date getUpdatedAt() { + return updatedAt; + } + + /** + * + * @return the user's location + */ + public String getLocation() { + return location; + } + + /** + * + * @return indicates if the user is confirmed + */ + public Boolean getConfirmed() { + return confirmed; + } + + /** + * + * @return confirmation date and time + */ + public Date getConfirmedAt() { + return confirmedAt; + } + + /** + * + * @return the user's role in the sepcified account + */ + public Xplenty.AccountRole getRole() { + return role; + } + + /** + * + * @return indicator if the user is the owner of the specified account + */ + public Boolean getOwner() { + return owner; + } + + /** + * + * @return the member resource url (API) + */ + public String getUrl() { + return url; + } + + /** + * + * @return the member resource url (Web UI) + */ + public String getHtmlUrl() { + return htmlUrl; + } + +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Notification.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Notification.java new file mode 100644 index 0000000..d0a33dd --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Notification.java @@ -0,0 +1,59 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Date; + +/** + * Data model for Xplenty Notification + * A notification represents events that you've received by watching clusters or jobs. + * Author: Xardas + * Date: 03.01.16 + * Time: 20:28 + */ +public class Notification extends XplentyObject { + @JsonProperty + private Long id; + @JsonProperty + private String title; + @JsonProperty + private String message; + @JsonProperty("last_read_at") + private Date lastReadAt; + + protected Notification() { + super(Notification.class); + } + + /** + * + * @return the notification's numeric identifier + */ + public Long getId() { + return id; + } + + /** + * + * @return the title of the event + */ + public String getTitle() { + return title; + } + + /** + * + * @return the description of the event + */ + public String getMessage() { + return message; + } + + /** + * + * @return the last point that the notification was checked + */ + public Date getLastReadAt() { + return lastReadAt; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplate.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplate.java new file mode 100644 index 0000000..fa09938 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplate.java @@ -0,0 +1,70 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Data model for Xplenty package template + * An Xplenty package template a data flow definition that is predefined in application. + * It describes the data to process (location, schema, fields), data manipulation to perform, + * and the output destinations (location, schema). The package's workflow is implemented by jobs. + * Author: Xardas + * Date: 03.01.16 + * Time: 20:45 + */ +public class PackageTemplate extends XplentyObject { + @JsonProperty + private Long id; + @JsonProperty + private String name; + @JsonProperty + private String description; + @JsonProperty + private Integer position; + @JsonProperty + private PackageTemplateAuthor author; + + protected PackageTemplate() { + super(PackageTemplate.class); + } + + /** + * + * @return the numeric package template ID + */ + public Long getId() { + return id; + } + + + /** + * + * @return the name given to the package template upon creation + */ + public String getName() { + return name; + } + + /** + * + * @return the description given to the package template upon creation + */ + public String getDescription() { + return description; + } + + /** + * + * @return the index for ordering the template + */ + public Integer getPosition() { + return position; + } + + /** + * + * @return information about the template's owner + */ + public PackageTemplateAuthor getAuthor() { + return author; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java new file mode 100644 index 0000000..9e5fd9b --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java @@ -0,0 +1,44 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Author: Xardas + * Date: 03.01.16 + * Time: 20:50 + */ +public class PackageTemplateAuthor { + @JsonProperty + private Long id; + @JsonProperty + private String name; + @JsonProperty("avatar_url") + private String avatarUrl; + + protected PackageTemplateAuthor() { + } + + /** + * + * @return the numeric ID of the author + */ + public Long getId() { + return id; + } + + /** + * + * @return the full name of the author + */ + public String getName() { + return name; + } + + /** + * + * @return the url for the author's avatar + */ + public String getAvatarUrl() { + return avatarUrl; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java new file mode 100644 index 0000000..15fcd8e --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java @@ -0,0 +1,120 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; + +import java.util.Date; +import java.util.List; + +/** + * Data model for Xplenty Package Validation + * An Xplenty package validation contains information about status of the validation process for the package. + * Author: Xardas + * Date: 03.01.16 + * Time: 20:31 + */ +public class PackageValidation extends XplentyObject { + @JsonProperty + private Long id; + @JsonProperty + private Xplenty.PackageValidationStatus status; + @JsonProperty("status_message") + private String statusMessage; + @JsonProperty + private Long runtime; + @JsonProperty("package_id") + private Long packageId; + @JsonProperty("owner_id") + private Long ownerId; + @JsonProperty("account_id") + private Long accountId; + @JsonProperty + private List errors; + @JsonProperty("created_at") + private Date createdAt; + @JsonProperty("updated_at") + private Date updatedAt; + @JsonProperty + private String url; + + protected PackageValidation() { + super(PackageValidation.class); + } + + /** + * @return the numeric package validation ID + */ + public Long getId() { + return id; + } + + /** + * @return the status of the validation process. + */ + public Xplenty.PackageValidationStatus getStatus() { + return status; + } + + /** + * @return information about current status + */ + public String getStatusMessage() { + return statusMessage; + } + + /** + * @return current duration of the validation process + */ + public Long getRuntime() { + return runtime; + } + + /** + * @return the numeric package ID + */ + public Long getPackageId() { + return packageId; + } + + /** + * @return the numeric user ID of the package validation owner + */ + public Long getOwnerId() { + return ownerId; + } + + /** + * @return the numeric ID of the account, where package is assigned + */ + public Long getAccountId() { + return accountId; + } + + /** + * @return the list of the errors which were detected in the validation process + */ + public List getErrors() { + return errors; + } + + /** + * @return the date and time the validation was started + */ + public Date getCreatedAt() { + return createdAt; + } + + /** + * @return the date and time the validation status was last updated + */ + public Date getUpdatedAt() { + return updatedAt; + } + + /** + * @return the package validation resource URL + */ + public String getUrl() { + return url; + } +} From cf7c76e83df331675b13e17164c971d10871ddd9 Mon Sep 17 00:00:00 2001 From: xardas Date: Mon, 4 Jan 2016 20:20:06 +0300 Subject: [PATCH 05/35] add new entities --- .../main/java/com/xplenty/api/Xplenty.java | 35 +++ .../main/java/com/xplenty/api/model/Plan.java | 209 +++++++++++++++ .../java/com/xplenty/api/model/PublicKey.java | 90 +++++++ .../com/xplenty/api/model/Subscription.java | 79 ++++++ .../main/java/com/xplenty/api/model/User.java | 242 ++++++++++++++++++ .../api/model/UserNotificationSettings.java | 39 +++ .../java/com/xplenty/api/model/WebHook.java | 103 ++++++++ .../com/xplenty/api/model/WebHookEvent.java | 72 ++++++ .../xplenty/api/model/WebHookSettings.java | 94 +++++++ 9 files changed, 963 insertions(+) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/Plan.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/PublicKey.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/UserNotificationSettings.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookSettings.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index 1ddaad9..eeb2871 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -165,6 +165,41 @@ public enum PackageValidationStatus { failed; } + public enum PriceUnit { + year, + month, + day, + hour; + } + + public enum WebHookEvent { + job_all("job"), + job_submitted("job.submitted"), + job_started("job.started"), + job_stopped("job.stopped"), + job_completed("job.completed"), + job_failed("job.failed"), + cluster("cluster"), + cluster_requested("cluster.requested"), + cluster_available("cluster.available"), + cluster_terminated("cluster.terminated"), + cluster_idled("cluster.idled"), + cluster_error("cluster.error"); + + @SuppressWarnings("unused") + private final String event; + + WebHookEvent(String event) { + this.event = event; + } + + @Override + public String toString() { + return event; + } + } + + @JsonFormat(shape= JsonFormat.Shape.SCALAR) public static enum ClusterStatus { pending("pending"), diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Plan.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Plan.java new file mode 100644 index 0000000..e9c8cf4 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Plan.java @@ -0,0 +1,209 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; + +import java.util.Date; + +/** + * Data model for Xplenty plan + * An account plan defines price and limits for members, clusters, etc. + * Author: Xardas + * Date: 03.01.16 + * Time: 20:57 + */ +public class Plan extends XplentyObject { + @JsonProperty + private String id; + @JsonProperty + private String name; + @JsonProperty + private String description; + @JsonProperty("price_cents") + private Long priceCents; + @JsonProperty("price_currency") + private String priceCurrency; + @JsonProperty("price_unit") + private Xplenty.PriceUnit priceUnit; + @JsonProperty("cluster_node_hours_included") + private Integer clusterNodeHoursIncluded; + @JsonProperty("cluster_node_hours_limit") + private Integer clusterNodeHoursLimit; + @JsonProperty("cluster_node_price_cents") + private Long clusterNodePriceCents; + @JsonProperty("cluster_node_price_currency") + private String clusterNodePriceCurrency; + @JsonProperty("cluster_node_price_unit") + private Xplenty.PriceUnit clusterNodePriceUnit; + @JsonProperty("cluster_nodes_limit") + private Integer clusterNodesLimit; + @JsonProperty("cluster_size_limit") + private Integer clusterSizeLimit; + @JsonProperty("clusters_limit") + private Integer clustersLimit; + @JsonProperty("sandbox_clusters_limit") + private Integer sandboxClustersLimit; + @JsonProperty("sandbox_node_hours_included") + private Integer sandboxNodeHoursIncluded; + @JsonProperty("sandbox_node_hours_limit") + private Integer sandboxNodeHoursLimit; + @JsonProperty("members_limit") + private Integer membersLimit; + @JsonProperty + private Integer position; + @JsonProperty("created_at") + private Date createdAt; + @JsonProperty("updated_at") + private Date updatedAt; + + protected Plan() { + super(Plan.class); + } + + /** + * @return the plan's string identifier + */ + public String getId() { + return id; + } + + /** + * @return the name given to the plan + */ + public String getName() { + return name; + } + + /** + * @return the description of the plan + */ + public String getDescription() { + return description; + } + + /** + * @return + */ + public Long getPriceCents() { + return priceCents; + } + + /** + * @return the currency of the plan + */ + public String getPriceCurrency() { + return priceCurrency; + } + + /** + * @return the price's unit of time. + */ + public Xplenty.PriceUnit getPriceUnit() { + return priceUnit; + } + + /** + * @return + */ + public Integer getClusterNodeHoursIncluded() { + return clusterNodeHoursIncluded; + } + + /** + * @return + */ + public Integer getClusterNodeHoursLimit() { + return clusterNodeHoursLimit; + } + + /** + * @return + */ + public Long getClusterNodePriceCents() { + return clusterNodePriceCents; + } + + /** + * @return + */ + public String getClusterNodePriceCurrency() { + return clusterNodePriceCurrency; + } + + /** + * @return + */ + public Xplenty.PriceUnit getClusterNodePriceUnit() { + return clusterNodePriceUnit; + } + + /** + * @return + */ + public Integer getClusterNodesLimit() { + return clusterNodesLimit; + } + + /** + * @return + */ + public Integer getClusterSizeLimit() { + return clusterSizeLimit; + } + + /** + * @return + */ + public Integer getClustersLimit() { + return clustersLimit; + } + + /** + * @return + */ + public Integer getSandboxClustersLimit() { + return sandboxClustersLimit; + } + + /** + * @return + */ + public Integer getSandboxNodeHoursIncluded() { + return sandboxNodeHoursIncluded; + } + + /** + * @return + */ + public Integer getSandboxNodeHoursLimit() { + return sandboxNodeHoursLimit; + } + + /** + * @return the maximum number of members for an account + */ + public Integer getMembersLimit() { + return membersLimit; + } + + /** + * @return + */ + public Integer getPosition() { + return position; + } + + /** + * @return the date and time the plan was created + */ + public Date getCreatedAt() { + return createdAt; + } + + /** + * @return the date and time the plan was last updated + */ + public Date getUpdatedAt() { + return updatedAt; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PublicKey.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PublicKey.java new file mode 100644 index 0000000..9163e1f --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PublicKey.java @@ -0,0 +1,90 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Date; + +/** + * Data model for Xplenty public key + * A public key represent public SSH key associated with a user and is used to authorize users with cluster instances. + * Author: Xardas + * Date: 04.01.16 + * Time: 17:24 + */ +public class PublicKey extends XplentyObject { + + @JsonProperty + private Long id; + @JsonProperty + private String name; + @JsonProperty + private String comment; + @JsonProperty + private String fingerprint; + @JsonProperty("created_at") + private Date createdAt; + @JsonProperty("updated_at") + private Date updatedAt; + @JsonProperty + private String url; + + protected PublicKey() { + super(PublicKey.class); + } + + /** + * + * @return the key's numeric identifier + */ + public Long getId() { + return id; + } + + /** + * + * @return the descriptive name given to the key + */ + public String getName() { + return name; + } + + /** + * + * @return the comment part of the given key + */ + public String getComment() { + return comment; + } + + /** + * + * @return fingerprint of the key + */ + public String getFingerprint() { + return fingerprint; + } + + /** + * + * @return the date and time the key was created + */ + public Date getCreatedAt() { + return createdAt; + } + + /** + * + * @return the date and time the key was last updated + */ + public Date getUpdatedAt() { + return updatedAt; + } + + /** + * + * @return the key resource URL + */ + public String getUrl() { + return url; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java new file mode 100644 index 0000000..6e0ad76 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java @@ -0,0 +1,79 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Date; + +/** + * Data model for Xplenty subscription + * An Xplenty subscription contains information about current acount plan. + * Author: Xardas + * Date: 04.01.16 + * Time: 17:32 + */ +public class Subscription extends XplentyObject { + @JsonProperty("trial_period_days") + private Integer trialPeriodDays; + @JsonProperty("plan_id") + private Long planId; + @JsonProperty("trial_start") + private Date trialStart; + @JsonProperty("trial_end") + private Date trialEnd; + @JsonProperty("trialling") + private Boolean isTrial; + @JsonProperty + private String url; + + protected Subscription() { + super(Subscription.class); + } + + /** + * + * @return the period of the trial plan + */ + public Integer getTrialPeriodDays() { + return trialPeriodDays; + } + + /** + * + * @return the numeric identifier of the current plan + */ + public Long getPlanId() { + return planId; + } + + /** + * + * @return the date and time the trial plan was started + */ + public Date getTrialStart() { + return trialStart; + } + + /** + * + * @return the date and time the trial plan was ended + */ + public Date getTrialEnd() { + return trialEnd; + } + + /** + * + * @return indicates if current plan is trial + */ + public Boolean isTrial() { + return isTrial; + } + + /** + * + * @return the subscription resource URL + */ + public String getUrl() { + return url; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java new file mode 100644 index 0000000..7f30aa0 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java @@ -0,0 +1,242 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Date; + +/** + * Data model for Xplenty user + * An Xplenty user represents an individual signed up to use the Xplenty platform. + * Author: Xardas + * Date: 04.01.16 + * Time: 17:41 + */ +public class User extends XplentyObject { + + @JsonProperty + private Long id; + @JsonProperty + private String name; + @JsonProperty + private String email; + @JsonProperty("gravatar_email") + private String gravatarEmail; + @JsonProperty("avatar_url") + private String avatarUrl; + @JsonProperty("time_zone") + private String timeZone; + @JsonProperty + private String location; + @JsonProperty + private Boolean confirmed; + @JsonProperty("confirmed_at") + private Date confirmedAt; + @JsonProperty("notifications_count") + private Integer notificationsCount; + @JsonProperty("unread_notifications_count") + private Integer unreadNotificationsCount; + @JsonProperty("notification_settings") + private UserNotificationSettings notificationSettings; + @JsonProperty("receive_newsletter") + private Boolean receiveNewsLetter; + @JsonProperty("created_at") + private Date createdAt; + @JsonProperty("updated_at") + private Date updatedAt; + @JsonProperty("api_key") + private String apiKey; + @JsonProperty + private String url; + + @JsonProperty("current_password") + private String currentPassword; + @JsonProperty("new_password") + private String newPassword; + + protected User() { + super(User.class); + } + + /** + * @return the user's numeric identifier + */ + public Long getId() { + return id; + } + + /** + * @return the full name of the user + */ + public String getName() { + return name; + } + + /** + * @return the email of the user (also used to login) + */ + public String getEmail() { + return email; + } + + /** + * @return the user's gravatar email + */ + public String getGravatarEmail() { + return gravatarEmail; + } + + /** + * @return the url for the user's avatar + */ + public String getAvatarUrl() { + return avatarUrl; + } + + /** + * @return the user's time zone + */ + public String getTimeZone() { + return timeZone; + } + + /** + * @return the user's location + */ + public String getLocation() { + return location; + } + + /** + * @return indicates if the user is confirmed + */ + public Boolean getConfirmed() { + return confirmed; + } + + /** + * @return confirmation date and time + */ + public Date getConfirmedAt() { + return confirmedAt; + } + + /** + * @return user's notifications count + */ + public Integer getNotificationsCount() { + return notificationsCount; + } + + /** + * @return user's unread notifications count + */ + public Integer getUnreadNotificationsCount() { + return unreadNotificationsCount; + } + + /** + * @return user's notification settings + */ + public UserNotificationSettings getNotificationSettings() { + return notificationSettings; + } + + /** + * @return indicates if user subscribed to recieve newsletter + */ + public Boolean getReceiveNewsLetter() { + return receiveNewsLetter; + } + + /** + * @return the date and time the user was created + */ + public Date getCreatedAt() { + return createdAt; + } + + /** + * @return the date and time the user was last updated + */ + public Date getUpdatedAt() { + return updatedAt; + } + + /** + * @return the user's api authentication key. Returned in response only if user provided password in input parameters. + */ + public String getApiKey() { + return apiKey; + } + + /** + * @return the user resource URL + */ + public String getUrl() { + return url; + } + + /** + * + * @param name the full name of the user + */ + public void setName(String name) { + this.name = name; + } + + /** + * + * @param email the email of the user (also used to login) + */ + public void setEmail(String email) { + this.email = email; + } + + /** + * + * @param gravatarEmail the user's gravatar email + */ + public void setGravatarEmail(String gravatarEmail) { + this.gravatarEmail = gravatarEmail; + } + + /** + * + * @param timeZone the user's time zone + */ + public void setTimeZone(String timeZone) { + this.timeZone = timeZone; + } + + /** + * + * @param location the user's location + */ + public void setLocation(String location) { + this.location = location; + } + + /** + * + * @param currentPassword The user's current password, which is required to set new password. + */ + public void setCurrentPassword(String currentPassword) { + this.currentPassword = currentPassword; + } + + /** + * + * @param newPassword The user's new password + */ + public void setNewPassword(String newPassword) { + this.newPassword = newPassword; + } + + /** + * + * @param receiveNewsLetter indicates if user subscribed to recieve newsletter + */ + public void setReceiveNewsLetter(Boolean receiveNewsLetter) { + this.receiveNewsLetter = receiveNewsLetter; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/UserNotificationSettings.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/UserNotificationSettings.java new file mode 100644 index 0000000..5d0d542 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/UserNotificationSettings.java @@ -0,0 +1,39 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Author: Xardas + * Date: 04.01.16 + * Time: 19:00 + */ +public class UserNotificationSettings { + @JsonProperty + private Boolean email; + @JsonProperty + private Boolean web; + + protected UserNotificationSettings() { + } + + public UserNotificationSettings(Boolean email, Boolean web) { + this.email = email; + this.web = web; + } + + /** + * + * @return whether email notifications enabled + */ + public Boolean getEmail() { + return email; + } + + /** + * + * @return whether web notifications enabled + */ + public Boolean getWeb() { + return web; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java new file mode 100644 index 0000000..e2392fa --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java @@ -0,0 +1,103 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; + +import java.util.List; + +/** + * Data model for Xplenty Web hook + * An Xplenty web hook is kind of notification that will be fired as a HTTP request to specified URL when state of resource (cluster or job) is changed. + * Types of notifications can be specified with events variable. + * Notification request contains hash which is generated using SHA-1 from string created by concatenation of the following fields: + *

+ * Notified application can verify with salt if request was sent by Xplenty. + * Author: Xardas + * Date: 04.01.16 + * Time: 17:42 + */ +public class WebHook extends XplentyObject { + @JsonProperty + private Long id; + @JsonProperty + private Boolean active; + @JsonProperty + private WebHookSettings settings; + @JsonProperty + private String salt; + @JsonProperty + private List events; + @JsonProperty("add_events") + private List addEvents; + @JsonProperty("remove_events") + private List removeEvents; + + protected WebHook() { + super(WebHook.class); + } + + public WebHook(WebHookSettings settings, List events) { + super(WebHook.class); + this.settings = settings; + this.events = events; + } + + public WebHook(Long id, WebHookSettings settings, List addEvents, List removeEvents) { + super(WebHook.class); + this.addEvents = addEvents; + this.removeEvents = removeEvents; + this.id = id; + this.settings = settings; + } + + public WebHook(Long id, Boolean active) { + super(WebHook.class); + this.id = id; + this.active = active; + } + + /** + * + * @return the numeric hook ID + */ + public Long getId() { + return id; + } + + /** + * + * @return indicates whether the web hook is active + */ + public Boolean getActive() { + return active; + } + + /** + * + * @return settings specific for the web hook. It contains the following attributes + */ + public WebHookSettings getSettings() { + return settings; + } + + /** + * + * @return salt needed for verification + */ + public String getSalt() { + return salt; + } + + /** + * + * @return list of notification events. + */ + public List getEvents() { + return events; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java new file mode 100644 index 0000000..b926f88 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java @@ -0,0 +1,72 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; + +import java.util.Date; + +/** + * Data Model for Xplenty Web Hook event + * Author: Xardas + * Date: 04.01.16 + * Time: 18:01 + */ +public class WebHookEvent { + @JsonProperty + private Long id; + @JsonProperty + private Xplenty.WebHookEvent name; + @JsonIgnore + private String lastResponse; + @JsonProperty("last_trigger_status") + private String lastTriggerStatus; + @JsonProperty("last_trigger_time") + private Date lastTriggerTime; + + public WebHookEvent() {} + + public WebHookEvent(Xplenty.WebHookEvent name) { + this.name = name; + } + + /** + * + * @return id of the event + */ + public Long getId() { + return id; + } + + /** + * + * @return name constant of the event + */ + public Xplenty.WebHookEvent getName() { + return name; + } + + /** + * + * @return last response + */ + public String getLastResponse() { + return lastResponse; + } + + /** + * + * @return last trigger status + */ + public String getLastTriggerStatus() { + return lastTriggerStatus; + } + + /** + * + * @return time web hook was last triggered + */ + public Date getLastTriggerTime() { + return lastTriggerTime; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookSettings.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookSettings.java new file mode 100644 index 0000000..3d243a8 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookSettings.java @@ -0,0 +1,94 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Data model for Xplenty Web Hook settings + * Author: Xardas + * Date: 04.01.16 + * Time: 17:52 + */ +public class WebHookSettings { + @JsonProperty + private String url; + @JsonProperty("insecure_ssl") + private Boolean insecureSSL; + @JsonProperty("basic_auth") + private Boolean basicAuth; + @JsonProperty("basic_auth_data") + private String basicAuthData; + + public WebHookSettings() { + } + + public WebHookSettings(String url, Boolean insecureSSL, Boolean basicAuth, String basicAuthData) { + this.url = url; + this.insecureSSL = insecureSSL; + this.basicAuth = basicAuth; + this.basicAuthData = basicAuthData; + } + + /** + * + * @return URL of the target server + */ + public String getUrl() { + return url; + } + + /** + * + * @return indicates whether SSL certificate is verified + */ + public Boolean getInsecureSSL() { + return insecureSSL; + } + + /** + * + * @return indicates whether the basic authentication is required + */ + public Boolean getBasicAuth() { + return basicAuth; + } + + /** + * + * @return data needed for basic authentication (user:password encoded with base64) + */ + public String getBasicAuthData() { + return basicAuthData; + } + + /** + * + * @param url URL of the target server + */ + public void setUrl(String url) { + this.url = url; + } + + /** + * + * @param insecureSSL indicates whether SSL certificate is verified + */ + public void setInsecureSSL(Boolean insecureSSL) { + this.insecureSSL = insecureSSL; + } + + /** + * + * @param basicAuth indicates whether the basic authentication is required + */ + public void setBasicAuth(Boolean basicAuth) { + this.basicAuth = basicAuth; + } + + /** + * + * @param basicAuthData data needed for basic authentication (user:password encoded with base64) + */ + public void setBasicAuthData(String basicAuthData) { + this.basicAuthData = basicAuthData; + } +} From d6d1441e8286d6215c741cac2ec76bbb62f9231c Mon Sep 17 00:00:00 2001 From: xardas Date: Mon, 4 Jan 2016 21:23:55 +0300 Subject: [PATCH 06/35] refactor endpoint genration add user info/update methods --- .../main/java/com/xplenty/api/Xplenty.java | 4 +- .../main/java/com/xplenty/api/XplentyAPI.java | 12 +++++ .../main/java/com/xplenty/api/http/Http.java | 3 +- .../java/com/xplenty/api/http/HttpClient.java | 1 - .../com/xplenty/api/http/JerseyClient.java | 8 ++-- .../com/xplenty/api/http/SyncNettyClient.java | 10 ++-- .../main/java/com/xplenty/api/model/User.java | 8 +++- .../api/request/AbstractDeleteRequest.java | 2 +- .../api/request/AbstractInfoRequest.java | 2 +- .../request/AbstractManipulationRequest.java | 12 +++-- .../request/AbstractParametrizedRequest.java | 2 +- .../xplenty/api/request/AbstractRequest.java | 17 +++++++ .../xplenty/api/request/CloneSchedule.java | 2 +- .../xplenty/api/request/CurrentUserInfo.java | 47 +++++++++++++++++++ .../java/com/xplenty/api/request/Request.java | 12 ++--- .../api/request/UpdateCurrentUser.java | 42 +++++++++++++++++ .../request/watching/AddClusterWatcher.java | 4 +- .../api/request/watching/AddJobWatcher.java | 4 +- .../api/request/watching/ListWatchers.java | 4 +- .../api/request/watching/WatchingStop.java | 4 +- 20 files changed, 163 insertions(+), 37 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractRequest.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/CurrentUserInfo.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCurrentUser.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index eeb2871..47df780 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -242,7 +242,9 @@ public static enum Resource { CloneSchedule("schedules/%s/clone", "Clone schedule"), UpdateSchedule("schedules/%s", "Update schedule"), RemoveSchedule("schedules/%s", "Remove schedule"), - Schedule("schedules/%s", "Get schedule information") + Schedule("schedules/%s", "Get schedule information"), + User("user", "Get current user information"), + UpdateUser("user", "Update current user information") ; public final String value; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index 1808b8b..b9bc222 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -379,6 +379,18 @@ public Schedule getScheduleInfo(long scheduleId) { return client.execute(new ScheduleInfo(scheduleId)); } + public User getCurrentUserInfo() { + return getCurrentUserInfo(null); + } + + public User getCurrentUserInfo(String currentPassword) { + return client.execute(new CurrentUserInfo(currentPassword)); + } + + public User updateCurrentUser(User user) { + return client.execute(new UpdateCurrentUser(user)); + } + /** * Account name this XplentyAPI instance is associated with * @return Account name diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java index f0665fb..12667ec 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java @@ -68,7 +68,8 @@ public static enum ResponseStatus { HTTP_429(429, "Too Many Requests: The request exceeded the rate limitations."), HTTP_500(500, "Internal Server Error: An internal error occurred in the request."), HTTP_502(502, "Bad Gateway: Xplenty is down or being upgraded."), - HTTP_503(503, "Service Unavailable: The Xplenty servers are up, but overloaded with requests. Try again later."); + HTTP_503(503, "Service Unavailable: The Xplenty servers are up, but overloaded with requests. Try again later."), + HTTP_505(505, "Version not supported"); private final int code; private final String description; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java index debd3e2..ba66265 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java @@ -14,7 +14,6 @@ public interface HttpClient { static final int DEFAULT_TIMEOUT = 30; static final int DEFAULT_HTTPS_PORT = 443; static final int DEFAULT_HTTP_PORT = 80; - static final String API_PATH = "api"; /** * Synchronously execute given request diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java index d036047..99d8a74 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java @@ -56,8 +56,8 @@ public class JerseyClient implements HttpClient { this.timeout = timeout; ClientConfig config = new DefaultClientConfig(); - config.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, timeout); - config.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, timeout); + config.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, timeout * 1000); + config.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, timeout * 1000); client = Client.create(config); client.addFilter(new HTTPBasicAuthFilter(apiKey, "")); if (logHttpCommunication) { @@ -99,7 +99,7 @@ private Map convertJerseyHeaders(MultivaluedMap * @return builder */ private WebResource.Builder getConfiguredResource(Request request) { - WebResource.Builder b = client.resource(getMethodURL(request.getEndpoint())) + WebResource.Builder b = client.resource(getMethodURL(request.getEndpoint(host, accountName))) .accept(request.getResponseType().value + (version == null ? "" : "; " + version.format()) ); @@ -123,7 +123,7 @@ private WebResource.Builder getConfiguredResource(Request request) { * @return filly qualified URL */ private String getMethodURL(String methodEndpoint) { - return protocol + "://" + host + "/" + accountName + "/" + API_PATH + "/" + methodEndpoint; + return String.format("%s://%s", protocol, methodEndpoint); } public String getAccountName() { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java index b393022..a3cd4fb 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java @@ -122,7 +122,7 @@ private HttpMethod convertRequestMethod(Http.Method method) { @Override public T execute(Request xplentyRequest) throws XplentyAPIException { try { - URL url = new URL(getMethodURL(xplentyRequest.getEndpoint())); + URL url = new URL(getMethodURL(xplentyRequest.getEndpoint(host, accountName))); Channel channel = getChannel(url); HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, convertRequestMethod(xplentyRequest.getHttpMethod()), url.toString()); @@ -156,7 +156,7 @@ public T execute(Request xplentyRequest) throws XplentyAPIException { if (httpBody == null) { throw new XplentyAPIException("Request timed out!"); } - int httpStatus = parseStatus(httpBody.headers().get("Status")); + int httpStatus = httpBody.getStatus().getCode(); Response processedResponse = Response.forContentType(xplentyRequest.getResponseType(), channelBuffer2String(httpBody.getContent()), httpStatus, convertNettyHeaders(httpBody.headers())); processedResponse.validate(xplentyRequest.getName()); return xplentyRequest.getResponse(processedResponse); @@ -206,17 +206,13 @@ private Map convertNettyHeaders(HttpHeaders headers) { return convertedHeaders; } - private int parseStatus(String status) { - return Integer.parseInt(status.substring(0, 3)); - } - /** * Constructs the actual URL * @param methodEndpoint - describes the action type * @return filly qualified URL */ private String getMethodURL(String methodEndpoint) { - return String.format("%s://%s/%s/%s/%s", protocol, host, accountName, API_PATH, methodEndpoint); + return String.format("%s://%s", protocol, methodEndpoint); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java index 7f30aa0..9841aca 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java @@ -53,10 +53,16 @@ public class User extends XplentyObject { @JsonProperty("new_password") private String newPassword; - protected User() { + public User() { super(User.class); } + public User(String currentPassword) { + super(User.class); + this.currentPassword = currentPassword; + } + + /** * @return the user's numeric identifier */ diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java index d6de756..5420d86 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java @@ -12,7 +12,7 @@ * Date: 18.12.15 * Time: 20:20 */ -public abstract class AbstractDeleteRequest implements Request { +public abstract class AbstractDeleteRequest extends AbstractRequest { protected final long entityId; private final Class clazz; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractInfoRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractInfoRequest.java index 1c51440..01144b7 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractInfoRequest.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractInfoRequest.java @@ -12,7 +12,7 @@ * Date: 20.12.15 * Time: 19:48 */ -public abstract class AbstractInfoRequest implements Request { +public abstract class AbstractInfoRequest extends AbstractRequest { protected final long entityId; private final Class clazz; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java index 51b7b91..705f4a9 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java @@ -14,7 +14,7 @@ * Date: 18.12.15 * Time: 18:49 */ -public abstract class AbstractManipulationRequest implements Request { +public abstract class AbstractManipulationRequest extends AbstractRequest { protected final T entity; private final Class clazz; @@ -51,9 +51,13 @@ public Http.MediaType getResponseType() { @Override public Object getBody() { - Map packedEntity = new HashMap(); - packedEntity.put(getPackKey(), entity); - return packedEntity; + final String packKey = getPackKey(); + if (packKey != null) { + Map packedEntity = new HashMap(); + packedEntity.put(packKey, entity); + return packedEntity; + } + return entity; } protected abstract String getPackKey(); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractParametrizedRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractParametrizedRequest.java index e3956ff..d1bdd11 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractParametrizedRequest.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractParametrizedRequest.java @@ -10,7 +10,7 @@ * Date: 17.12.15 * Time: 20:46 */ -public abstract class AbstractParametrizedRequest implements Request { +public abstract class AbstractParametrizedRequest extends AbstractRequest { public static final String PARAMETER_STATUS = "status"; public static final String PARAMETER_SORT = "sort"; public static final String PARAMETER_DIRECTION = "direction"; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractRequest.java new file mode 100644 index 0000000..f5414da --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractRequest.java @@ -0,0 +1,17 @@ +package com.xplenty.api.request; + +/** + * Author: Xardas + * Date: 04.01.16 + * Time: 19:41 + */ +public abstract class AbstractRequest implements Request { + static final String API_PATH = "api"; + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s/%s/%s", apiHost, accountName, API_PATH, getEndpoint()); + } + + protected abstract String getEndpoint(); +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CloneSchedule.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/CloneSchedule.java index f7465c0..1be8f1e 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CloneSchedule.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/CloneSchedule.java @@ -11,7 +11,7 @@ * Date: 18.12.15 * Time: 20:25 */ -public class CloneSchedule implements Request { +public class CloneSchedule extends AbstractRequest { private final Long entityId; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CurrentUserInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/CurrentUserInfo.java new file mode 100644 index 0000000..0d5ebf0 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/CurrentUserInfo.java @@ -0,0 +1,47 @@ +package com.xplenty.api.request; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.User; + +/** + * Get Info for the authenticated user. + * Author: Xardas + * Date: 04.01.16 + * Time: 19:23 + */ +public class CurrentUserInfo extends AbstractInfoRequest { + private final String currentPassword; + + public CurrentUserInfo(String currentPassword) { + super(-1L); + this.currentPassword = currentPassword; + } + + @Override + public String getName() { + return Xplenty.Resource.User.name; + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + public String getEndpoint() { + return Xplenty.Resource.User.value; + } + + @Override + public boolean hasBody() { + return currentPassword != null; + } + + @Override + public Object getBody() { + if (currentPassword != null) { + return new User(currentPassword); + } + return null; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/Request.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/Request.java index 9a21f33..ce3d747 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/Request.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/Request.java @@ -11,7 +11,7 @@ * */ public interface Request { - + String getName(); /** @@ -25,12 +25,12 @@ public interface Request { * @return The path and query parameters as a String. */ Http.MediaType getResponseType(); - - String getEndpoint(); - + + String getEndpoint(String apiHost, String accountName); + boolean hasBody(); - + Object getBody(); - + T getResponse(Response response); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCurrentUser.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCurrentUser.java new file mode 100644 index 0000000..e8a70f1 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCurrentUser.java @@ -0,0 +1,42 @@ +package com.xplenty.api.request; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.User; + +/** + * Author: Xardas + * Date: 04.01.16 + * Time: 20:01 + */ +public class UpdateCurrentUser extends AbstractManipulationRequest { + + public UpdateCurrentUser(User entity) { + super(entity); + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.UpdateUser.value; + } + + @Override + public String getName() { + return Xplenty.Resource.UpdateUser.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.PUT; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java index dc4d94b..2c0fdd8 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java @@ -5,12 +5,12 @@ import com.xplenty.api.http.Http; import com.xplenty.api.http.Response; import com.xplenty.api.model.ClusterWatchingLogEntry; -import com.xplenty.api.request.Request; +import com.xplenty.api.request.AbstractRequest; import static com.xplenty.api.Xplenty.Resource; import static com.xplenty.api.http.Http.Method.POST; -public class AddClusterWatcher implements Request { +public class AddClusterWatcher extends AbstractRequest { private Long _clusterId; public AddClusterWatcher(Long subjectId) { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java index 8a49834..c448200 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java @@ -6,11 +6,11 @@ import com.xplenty.api.http.Http; import com.xplenty.api.http.Response; import com.xplenty.api.model.JobWatchingLogEntry; -import com.xplenty.api.request.Request; +import com.xplenty.api.request.AbstractRequest; import static com.xplenty.api.http.Http.Method.POST; -public class AddJobWatcher implements Request { +public class AddJobWatcher extends AbstractRequest { Long _jobId; public AddJobWatcher(Long subjectId) { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/ListWatchers.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/ListWatchers.java index 3987c0f..a136a00 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/ListWatchers.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/ListWatchers.java @@ -6,7 +6,7 @@ import com.xplenty.api.http.Http; import com.xplenty.api.http.Response; import com.xplenty.api.model.Watcher; -import com.xplenty.api.request.Request; +import com.xplenty.api.request.AbstractRequest; import java.util.List; @@ -14,7 +14,7 @@ import static com.xplenty.api.Xplenty.SubjectType; import static com.xplenty.api.http.Http.Method.GET; -public class ListWatchers implements Request> { +public class ListWatchers extends AbstractRequest> { private SubjectType _kind = null; private Long _subjectId = null; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java index 02ca439..a31bd2e 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java @@ -4,13 +4,13 @@ import com.xplenty.api.exceptions.RequestFailedException; import com.xplenty.api.http.Http; import com.xplenty.api.http.Response; -import com.xplenty.api.request.Request; +import com.xplenty.api.request.AbstractRequest; import static com.xplenty.api.Xplenty.Resource; import static com.xplenty.api.Xplenty.SubjectType; import static com.xplenty.api.http.Http.Method.DELETE; -public class WatchingStop implements Request { +public class WatchingStop extends AbstractRequest { private Long _subjectId; private SubjectType _kind; From aaa1da45715440c49ade9d10050bd8ec6e24c7e3 Mon Sep 17 00:00:00 2001 From: xardas Date: Tue, 5 Jan 2016 15:53:50 +0300 Subject: [PATCH 07/35] add tests --- .../main/java/com/xplenty/api/model/User.java | 48 ++++--- .../java/com/xplenty/api/model/UserTest.java | 49 +++++++ .../api/request/CurrentUserInfoTest.java | 120 ++++++++++++++++++ .../api/request/UpdateCurrentUserTest.java | 85 +++++++++++++ 4 files changed, 283 insertions(+), 19 deletions(-) create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/UserTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/CurrentUserInfoTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateCurrentUserTest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java index 9841aca..9b03900 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java @@ -14,39 +14,41 @@ public class User extends XplentyObject { @JsonProperty - private Long id; + protected Long id; @JsonProperty - private String name; + protected String name; @JsonProperty - private String email; + protected String email; @JsonProperty("gravatar_email") - private String gravatarEmail; + protected String gravatarEmail; @JsonProperty("avatar_url") - private String avatarUrl; + protected String avatarUrl; @JsonProperty("time_zone") - private String timeZone; + protected String timeZone; @JsonProperty - private String location; + protected String location; @JsonProperty - private Boolean confirmed; + protected Boolean confirmed; @JsonProperty("confirmed_at") - private Date confirmedAt; + protected Date confirmedAt; @JsonProperty("notifications_count") - private Integer notificationsCount; + protected Integer notificationsCount; @JsonProperty("unread_notifications_count") - private Integer unreadNotificationsCount; + protected Integer unreadNotificationsCount; @JsonProperty("notification_settings") - private UserNotificationSettings notificationSettings; + protected UserNotificationSettings notificationSettings; @JsonProperty("receive_newsletter") - private Boolean receiveNewsLetter; + protected Boolean receiveNewsLetter; @JsonProperty("created_at") - private Date createdAt; + protected Date createdAt; @JsonProperty("updated_at") - private Date updatedAt; + protected Date updatedAt; @JsonProperty("api_key") - private String apiKey; + protected String apiKey; @JsonProperty - private String url; + protected String url; + @JsonProperty("last_login") + protected Date lastLogin; @JsonProperty("current_password") private String currentPassword; @@ -115,7 +117,7 @@ public String getLocation() { /** * @return indicates if the user is confirmed */ - public Boolean getConfirmed() { + public Boolean isConfirmed() { return confirmed; } @@ -150,7 +152,7 @@ public UserNotificationSettings getNotificationSettings() { /** * @return indicates if user subscribed to recieve newsletter */ - public Boolean getReceiveNewsLetter() { + public Boolean isReceiveNewsLetter() { return receiveNewsLetter; } @@ -182,6 +184,14 @@ public String getUrl() { return url; } + /** + * + * @return date and time of the last user login + */ + public Date getLastLogin() { + return lastLogin; + } + /** * * @param name the full name of the user diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/UserTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/UserTest.java new file mode 100644 index 0000000..e611952 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/UserTest.java @@ -0,0 +1,49 @@ +package com.xplenty.api.model; + +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.Date; + +/** + * Author: Xardas + * Date: 05.01.16 + * Time: 14:18 + */ +public class UserTest extends TestCase { + @Test + public void testBuilder() { + final Date now = new Date(); + User user = createMockUser(now, false); + assertNotNull(user); + assertEquals(now.getTime(), user.getConfirmedAt().getTime()); + + } + + public static User createMockUser(Date now, boolean hasApiKey) { + User user = new User(); + user.email ="test@xplenty.com"; + user.receiveNewsLetter = true; + user.confirmedAt = now; + user.name = "Vasya Pupkin"; + user.id = 34L; + user.location = "Colorado"; + user.timeZone = "UTC"; + user.notificationsCount = 7; + user.unreadNotificationsCount = 3; + user.notificationSettings = new UserNotificationSettings(true, false); + user.createdAt = now; + user.updatedAt = now; + user.confirmed = true; + user.lastLogin = now; + user.gravatarEmail = "test@gravatar.com"; + user.avatarUrl = "https://secure.gravatar.com/avatar/8318e89006033f0f8eec181f1fcec54e276.png"; + user.url = "https://api.xplenty.com/user"; + if (hasApiKey) { + user.apiKey = "yepitsapikey"; + } + return user; + + } + +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CurrentUserInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/CurrentUserInfoTest.java new file mode 100644 index 0000000..45b9c26 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/CurrentUserInfoTest.java @@ -0,0 +1,120 @@ +/** + * + */ +package com.xplenty.api.request; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.User; +import com.xplenty.api.model.UserTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class CurrentUserInfoTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + final Date now = new Date(); + CurrentUserInfo cc = new CurrentUserInfo(null); + assertEquals(Xplenty.Resource.User.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.User.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + + cc = new CurrentUserInfo("hhh"); + assertTrue(cc.hasBody()); + assertNotNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + User c = UserTest.createMockUser(now, true); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + CurrentUserInfo cc = new CurrentUserInfo("hhh"); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(34), c.getId()); + assertEquals("yepitsapikey", c.getApiKey()); + assertEquals("Vasya Pupkin", c.getName()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); + assertTrue(Math.abs(now.getTime() - c.getConfirmedAt().getTime()) < 1000); + assertTrue(Math.abs(now.getTime() - c.getLastLogin().getTime()) < 1000); + assertEquals(true, c.isReceiveNewsLetter().booleanValue()); + assertEquals("https://secure.gravatar.com/avatar/8318e89006033f0f8eec181f1fcec54e276.png", c.getAvatarUrl()); + assertEquals("test@xplenty.com", c.getEmail()); + assertEquals("test@gravatar.com", c.getGravatarEmail()); + assertEquals("UTC", c.getTimeZone()); + assertEquals("Colorado", c.getLocation()); + assertEquals(true, c.isConfirmed().booleanValue()); + assertEquals(7, c.getNotificationsCount().intValue()); + assertEquals(3, c.getUnreadNotificationsCount().intValue()); + assertEquals(true, c.getNotificationSettings().getEmail().booleanValue()); + assertEquals(false, c.getNotificationSettings().getWeb().booleanValue()); + assertEquals("https://api.xplenty.com/user", c.getUrl()); + + c = UserTest.createMockUser(now, false); + + json = JsonMapperFactory.getInstance().writeValueAsString(c); + cc = new CurrentUserInfo(null); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(34), c.getId()); + assertEquals(null, c.getApiKey()); + assertEquals("Vasya Pupkin", c.getName()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); + assertTrue(Math.abs(now.getTime() - c.getConfirmedAt().getTime()) < 1000); + assertTrue(Math.abs(now.getTime() - c.getLastLogin().getTime()) < 1000); + assertEquals("https://api.xplenty.com/user", c.getUrl()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + User c = UserTest.createMockUser(now, false); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "]"); + CurrentUserInfo cc = new CurrentUserInfo(null); + try { + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + fail(); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.User.name + ": error parsing response object", e.getMessage()); + } + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateCurrentUserTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateCurrentUserTest.java new file mode 100644 index 0000000..1f5b1b3 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateCurrentUserTest.java @@ -0,0 +1,85 @@ +/** + * + */ +package com.xplenty.api.request; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.User; +import com.xplenty.api.model.UserTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class UpdateCurrentUserTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + final Date now = new Date(); + UpdateCurrentUser cc = new UpdateCurrentUser(UserTest.createMockUser(now, false)); + assertEquals(Xplenty.Resource.UpdateUser.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.UpdateUser.name, cc.getName()); + assertEquals(Http.Method.PUT, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertTrue(cc.hasBody()); + assertNotNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + User c = UserTest.createMockUser(now, false); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + UpdateCurrentUser cc = new UpdateCurrentUser(c); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(34), c.getId()); + assertEquals("Vasya Pupkin", c.getName()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); + assertTrue(Math.abs(now.getTime() - c.getConfirmedAt().getTime()) < 1000); + assertTrue(Math.abs(now.getTime() - c.getLastLogin().getTime()) < 1000); + assertEquals("https://api.xplenty.com/user", c.getUrl()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + User c = UserTest.createMockUser(now, false); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "]"); + UpdateCurrentUser cc = new UpdateCurrentUser(c); + try { + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + fail(); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.UpdateUser.name + ": error parsing response object", e.getMessage()); + } + } +} From aa4c879fc56a721f36f83b33a6c2a4698d840479 Mon Sep 17 00:00:00 2001 From: xardas Date: Tue, 5 Jan 2016 15:55:24 +0300 Subject: [PATCH 08/35] reanme builder --- .../main/java/com/xplenty/api/XplentyAPI.java | 6 +++--- ...tpClientBuilder.java => ClientBuilder.java} | 18 +++++++++--------- .../api/WatchersTestAgainstMockServer.java | 4 ++-- .../java/com/xplenty/api/XplentyAPITest.java | 4 ++-- 4 files changed, 16 insertions(+), 16 deletions(-) rename xplenty.jar-core/src/main/java/com/xplenty/api/http/{HttpClientBuilder.java => ClientBuilder.java} (83%) diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index 1808b8b..0ed7493 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -7,7 +7,7 @@ import com.xplenty.api.Xplenty.Version; import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.http.HttpClient; -import com.xplenty.api.http.HttpClientBuilder; +import com.xplenty.api.http.ClientBuilder; import com.xplenty.api.model.*; import com.xplenty.api.model.Package; import com.xplenty.api.request.*; @@ -45,7 +45,7 @@ public class XplentyAPI { * @param apiKey User's API key found at https://www.xplenty.com/settings/edit */ public XplentyAPI(String accountName, String apiKey) { - client = new HttpClientBuilder().withAccount(accountName).withApiKey(apiKey).build(); + client = new ClientBuilder().withAccount(accountName).withApiKey(apiKey).build(); } /** @@ -59,7 +59,7 @@ public XplentyAPI(String accountName, String apiKey) { * Constructs a XplentyAPI with a {@link com.xplenty.api.http.HttpClient} based on the given http client builder * @param clientBuilder configured Http client builder. At least account name and API Key must be set! */ - public XplentyAPI(HttpClientBuilder clientBuilder) { + public XplentyAPI(ClientBuilder clientBuilder) { client = clientBuilder.build(); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClientBuilder.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/ClientBuilder.java similarity index 83% rename from xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClientBuilder.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/http/ClientBuilder.java index 9c37dcb..8c84e08 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClientBuilder.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/ClientBuilder.java @@ -9,7 +9,7 @@ * Date: 02.01.16 * Time: 15:32 */ -public class HttpClientBuilder { +public class ClientBuilder { private int timeout = SyncNettyClient.DEFAULT_TIMEOUT; private String host = "api.xplenty.com"; @@ -25,7 +25,7 @@ public class HttpClientBuilder { * @param timeout timeout for connection/reading * @return builder */ - public HttpClientBuilder withTimeout(int timeout) { + public ClientBuilder withTimeout(int timeout) { this.timeout = timeout; return this; } @@ -35,7 +35,7 @@ public HttpClientBuilder withTimeout(int timeout) { * @param apiKey User's API key found at https://www.xplenty.com/settings/edit * @return builder */ - public HttpClientBuilder withApiKey(String apiKey) { + public ClientBuilder withApiKey(String apiKey) { this.apiKey = apiKey; return this; } @@ -45,7 +45,7 @@ public HttpClientBuilder withApiKey(String apiKey) { * @param protocol protocol to use for connection * @return builder */ - public HttpClientBuilder withProtocol(Http.Protocol protocol) { + public ClientBuilder withProtocol(Http.Protocol protocol) { this.protocol = protocol; return this; } @@ -55,7 +55,7 @@ public HttpClientBuilder withProtocol(Http.Protocol protocol) { * @param logHttpCommunication Log raw packets transfered? (before encryption / after decryption) * @return builder */ - public HttpClientBuilder withLogHttpCommunication(boolean logHttpCommunication) { + public ClientBuilder withLogHttpCommunication(boolean logHttpCommunication) { this.logHttpCommunication = logHttpCommunication; return this; } @@ -65,7 +65,7 @@ public HttpClientBuilder withLogHttpCommunication(boolean logHttpCommunication) * @param accountName account name used for Xplenty sign-up * @return builder */ - public HttpClientBuilder withAccount(String accountName) { + public ClientBuilder withAccount(String accountName) { this.accountName = accountName; return this; } @@ -75,7 +75,7 @@ public HttpClientBuilder withAccount(String accountName) { * @param host API hostname * @return builder */ - public HttpClientBuilder withHost(String host) { + public ClientBuilder withHost(String host) { this.host = host; return this; } @@ -85,7 +85,7 @@ public HttpClientBuilder withHost(String host) { * @param impl client implementation to use * @return builder */ - public HttpClientBuilder withClientImpl(Http.HttpClientImpl impl) { + public ClientBuilder withClientImpl(Http.HttpClientImpl impl) { this.clientImpl = impl; return this; } @@ -95,7 +95,7 @@ public HttpClientBuilder withClientImpl(Http.HttpClientImpl impl) { * @param version API version to use * @return builder */ - public HttpClientBuilder withVersion(Xplenty.Version version) { + public ClientBuilder withVersion(Xplenty.Version version) { this.version = version; return this; } diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java b/xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java index 6449988..4e5f438 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java @@ -1,6 +1,6 @@ package com.xplenty.api; -import com.xplenty.api.http.HttpClientBuilder; +import com.xplenty.api.http.ClientBuilder; import com.xplenty.api.model.ClusterWatchingLogEntry; import com.xplenty.api.model.JobWatchingLogEntry; import com.xplenty.api.model.Watcher; @@ -21,7 +21,7 @@ public class WatchersTestAgainstMockServer extends TestCase { @Override public void setUp(){ - HttpClientBuilder builder = new HttpClientBuilder().withAccount(accountID).withApiKey(apiKey).withHost(host). + ClientBuilder builder = new ClientBuilder().withAccount(accountID).withApiKey(apiKey).withHost(host). withProtocol(Http.Protocol.Http); api = new XplentyAPI(builder); } diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java index 8bd2307..e48c836 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java @@ -7,7 +7,7 @@ import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.http.Http; import com.xplenty.api.http.HttpClient; -import com.xplenty.api.http.HttpClientBuilder; +import com.xplenty.api.http.ClientBuilder; import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; import com.xplenty.api.model.Job; @@ -33,7 +33,7 @@ public void testConstructor() { } public void testBuilder() { - HttpClientBuilder builder = new HttpClientBuilder().withAccount("testAcc").withApiKey("testKey"). + ClientBuilder builder = new ClientBuilder().withAccount("testAcc").withApiKey("testKey"). withHost("www.example.com").withProtocol(Http.Protocol.Http).withVersion(Xplenty.Version.V1). withClientImpl(Http.HttpClientImpl.SyncNetty).withLogHttpCommunication(true).withTimeout(10); XplentyAPI api = new XplentyAPI(builder); From 443794b93f944940747b01a8eb39c45146bb7bf8 Mon Sep 17 00:00:00 2001 From: xardas Date: Tue, 5 Jan 2016 19:33:05 +0300 Subject: [PATCH 09/35] add support for web hook methods --- .../main/java/com/xplenty/api/Xplenty.java | 16 +- .../main/java/com/xplenty/api/XplentyAPI.java | 179 ++++++++++++++++-- .../java/com/xplenty/api/model/HookEvent.java | 45 +++++ .../com/xplenty/api/model/Subscription.java | 4 +- .../java/com/xplenty/api/model/WebHook.java | 7 +- .../com/xplenty/api/model/WebHookEvent.java | 29 ++- ...dRequest.java => AbstractListRequest.java} | 27 ++- .../request/AbstractManipulationRequest.java | 6 +- .../com/xplenty/api/request/ListClusters.java | 25 +-- .../xplenty/api/request/ListHookEvents.java | 46 +++++ .../com/xplenty/api/request/ListJobs.java | 24 +-- .../com/xplenty/api/request/ListPackages.java | 25 +-- .../xplenty/api/request/ListSchedules.java | 27 +-- .../api/request/webhook/CreateWebHook.java | 69 +++++++ .../api/request/webhook/DeleteWebHook.java | 28 +++ .../api/request/webhook/ListWebHooks.java | 44 +++++ .../api/request/webhook/PingWebHook.java | 27 +++ .../api/request/webhook/ToggleWebHook.java | 39 ++++ .../api/request/webhook/UpdateWebHook.java | 64 +++++++ .../api/request/webhook/WebHookInfo.java | 28 +++ .../api/request/webhook/WebHookResetSalt.java | 62 ++++++ .../api/request/ListSchedulesTest.java | 12 +- 22 files changed, 694 insertions(+), 139 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/HookEvent.java rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{AbstractParametrizedRequest.java => AbstractListRequest.java} (84%) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/ListHookEvents.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/CreateWebHook.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/DeleteWebHook.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ListWebHooks.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/PingWebHook.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ToggleWebHook.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/UpdateWebHook.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookInfo.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookResetSalt.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index 47df780..fd57124 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -235,16 +235,24 @@ public static enum Resource { Job("jobs/%s", "Get job info"), RunJob("jobs", "Run job"), StopJob("jobs/%s", "Stop job"), - ClusterWatcher("clusters/%s/watchers", "adding/removing a cluster watchers"), - JobWatcher("jobs/%s/watchers", "adding/removing a job watchers"), - Schedules("schedules", "list schedules"), + ClusterWatcher("clusters/%s/watchers", "Adding/removing a cluster watchers"), + JobWatcher("jobs/%s/watchers", "Adding/removing a job watchers"), + Schedules("schedules", "List schedules"), CreateSchedule("schedules", "Create schedule"), CloneSchedule("schedules/%s/clone", "Clone schedule"), UpdateSchedule("schedules/%s", "Update schedule"), RemoveSchedule("schedules/%s", "Remove schedule"), Schedule("schedules/%s", "Get schedule information"), User("user", "Get current user information"), - UpdateUser("user", "Update current user information") + UpdateUser("user", "Update current user information"), + HookEvents("hook_events", "List supported Hook Events"), + CreateWebHook("hooks/web", "Create new Web hook"), + UpdateWebHook("hooks/web/%s", "Update existing Web hook"), + DeleteWebHook("hooks/web/%s", "Delete Web hook"), + PingWebHook("hooks/web/%s/ping", "Ping(fire test notification) for Web hook"), + WebHookResetSalt("hooks/web/%s/reset_salt", "Reset Web hook's salt"), + WebHook("hooks/web/%s", "Update existing Web hook"), + WebHooks("hooks/web", "List Web hooks") ; public final String value; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index c6ec8af..a134d74 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -16,6 +16,7 @@ import com.xplenty.api.request.watching.ListWatchers; import com.xplenty.api.request.watching.WatchingStop; import com.xplenty.api.http.Http; +import com.xplenty.api.request.webhook.*; import java.util.List; import java.util.Map; @@ -97,8 +98,8 @@ public List listPackages(Properties props) { */ public List listPackages(int offset, int limit) { final Properties props = new Properties(); - props.put(AbstractParametrizedRequest.PARAMETER_LIMIT, limit); - props.put(AbstractParametrizedRequest.PARAMETER_OFFSET, offset); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); return listPackages(props); } @@ -118,8 +119,8 @@ public List listSchedules() { */ public List listSchedules(int offset, int limit) { final Properties props = new Properties(); - props.put(AbstractParametrizedRequest.PARAMETER_LIMIT, limit); - props.put(AbstractParametrizedRequest.PARAMETER_OFFSET, offset); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); return listSchedules(props); } @@ -148,8 +149,8 @@ public List listClusters() { */ public List listClusters(int offset, int limit) { final Properties props = new Properties(); - props.put(AbstractParametrizedRequest.PARAMETER_LIMIT, limit); - props.put(AbstractParametrizedRequest.PARAMETER_OFFSET, offset); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); return listClusters(props); } @@ -234,8 +235,8 @@ public List listJobs() { */ public List listJobs(int offset, int limit) { final Properties props = new Properties(); - props.put(AbstractParametrizedRequest.PARAMETER_LIMIT, limit); - props.put(AbstractParametrizedRequest.PARAMETER_OFFSET, offset); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); return listJobs(props); } @@ -337,9 +338,7 @@ public Schedule createSchedule(Schedule schedule) { * @return updated schedule object */ public Schedule updateSchedule(Schedule schedule) { - if (schedule.getId() == null) { - throw new XplentyAPIException("No id specified!"); - } + checkId(schedule.getId()); return client.execute(new UpdateSchedule(schedule)); } @@ -349,9 +348,7 @@ public Schedule updateSchedule(Schedule schedule) { * @return deleted schedule object */ public Schedule deleteSchedule(long scheduleId) { - if (scheduleId == 0) { - throw new XplentyAPIException("No id specified!"); - } + checkId(scheduleId); return client.execute(new DeleteSchedule(scheduleId)); } @@ -361,9 +358,7 @@ public Schedule deleteSchedule(long scheduleId) { * @return cloned schedule object */ public Schedule cloneSchedule(long scheduleId) { - if (scheduleId == 0) { - throw new XplentyAPIException("No id specified!"); - } + checkId(scheduleId); return client.execute(new CloneSchedule(scheduleId)); } @@ -373,24 +368,168 @@ public Schedule cloneSchedule(long scheduleId) { * @return schedule object */ public Schedule getScheduleInfo(long scheduleId) { - if (scheduleId == 0) { - throw new XplentyAPIException("No id specified!"); - } + checkId(scheduleId); return client.execute(new ScheduleInfo(scheduleId)); } + /** + * Get current user information. + * @return user object + */ public User getCurrentUserInfo() { return getCurrentUserInfo(null); } + /** + * Get current user information including API Key. + * @param currentPassword current user password. If you pass null, API KEy won't be retrieved + * @return user object + */ public User getCurrentUserInfo(String currentPassword) { return client.execute(new CurrentUserInfo(currentPassword)); } + /** + * Update current user information like notifications settings, timezone, etc + * @param user user object containing required changes + * @return updated user object + */ public User updateCurrentUser(User user) { return client.execute(new UpdateCurrentUser(user)); } + /** + * Get all supported hook events to use when creating/updating web hooks + * @return list of hook events + */ + public List getHookEvents() { + return client.execute(new ListHookEvents()); + } + + /** + * Create new Web hook to recieve notifications for events subscribed + * @param settings settings used to connect to your server + * @param events list of events, retrieved using {@link #getHookEvents() getHookEvents}, you want to subscribe to + * @return created web hook object + */ + public WebHook createWebHook(WebHookSettings settings, List events) { + return client.execute(new CreateWebHook(settings, events)); + } + + /** + * Create new Web hook to recieve notifications for events subscribed + * @param events list of predefined events you want to subscribe to + * @param settings settings used to connect to your server + * @return created web hook object + */ + public WebHook createWebHook(List events, WebHookSettings settings) { + return client.execute(new CreateWebHook(events, settings)); + } + + /** + * Update web hook + * @param webHookId id of the web hook + * @param settings settings used to connect to your server, pass null if no change required + * @param addEvents subscribe to these events. Leave null if no change required + * @param removeEvents unsubscribe from these events. Leave null if no change required. + * @return updated web hook object + */ + public WebHook updateWebHook(long webHookId, WebHookSettings settings, List addEvents, List removeEvents) { + checkId(webHookId); + return client.execute(new UpdateWebHook(webHookId, settings, addEvents, removeEvents)); + } + + /** + * Update web hook + * @param webHookId id of the web hook + * @param addEvents subscribe to these events. Leave null if no change required + * @param removeEvents unsubscribe from these events. Leave null if no change required. + * @param settings settings used to connect to your server, pass null if no change required + * @return updated web hook object + */ + public WebHook updateWebHook(long webHookId, List addEvents, List removeEvents, WebHookSettings settings) { + checkId(webHookId); + return client.execute(new UpdateWebHook(webHookId, addEvents, removeEvents, settings)); + } + + /** + * Enable/disable Web hook + * @param webHookId id of the web hook + * @param active true to enable web hook, false otherwise + * @return updated web hook object + */ + public WebHook toggleWebHook(long webHookId, boolean active) { + checkId(webHookId); + return client.execute(new ToggleWebHook(webHookId, active)); + } + + /** + * List web hooks associated with the account + * @return list of web hooks + */ + public List listWebHooks() { + return listWebHooks(new Properties()); + } + + /** + * List web hooks associated with the account + * @param offset number of record to start results from + * @param limit number of results + * @return list of web hooks + */ + public List listWebHooks(int offset, int limit) { + final Properties props = new Properties(); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); + return listWebHooks(props); + } + + /** + * List web hooks associated with the account + * @param params map of request parameters, see {@link Xplenty.Sort}, {@link Xplenty.SortDirection}. + * @return list of web hooks + */ + public List listWebHooks(Properties params) { + return client.execute(new ListWebHooks(params)); + } + + /** + * Delete webhook with specified id + * @param webHookId id of the webhook to delete + * @return deleted web hook object + */ + public WebHook deleteWebHook(long webHookId) { + checkId(webHookId); + return client.execute(new DeleteWebHook(webHookId)); + } + + /** + * Reset salt for webhook with specified id + * @param webHookId id of the webhook to reset salt for + * @return newly generated salt + */ + public String webHookResetSalt(long webHookId) { + checkId(webHookId); + return client.execute(new WebHookResetSalt(webHookId)); + } + + public WebHook pingWebHook(long webHookId) { + checkId(webHookId); + return client.execute(new PingWebHook(webHookId)); + } + + public WebHook getWebHookInfo(long webHookId) { + checkId(webHookId); + return client.execute(new WebHookInfo(webHookId)); + } + + private void checkId(long id) { + if (id == 0) { + throw new XplentyAPIException("No Id specified!"); + } + } + + /** * Account name this XplentyAPI instance is associated with * @return Account name diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/HookEvent.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/HookEvent.java new file mode 100644 index 0000000..5721bbb --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/HookEvent.java @@ -0,0 +1,45 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Author: Xardas + * Date: 05.01.16 + * Time: 15:15 + */ +public class HookEvent extends XplentyObject { + @JsonProperty + private String id; + @JsonProperty("group_name") + private String groupName; + @JsonProperty("name") + private String description; + + protected HookEvent() { + super(HookEvent.class); + } + + /** + * + * @return id of the hook event. This id can be passed to web hook create/update requests to subscribe for that type of event + */ + public String getId() { + return id; + } + + /** + * + * @return Group that this hook event belongs to + */ + public String getGroupName() { + return groupName; + } + + /** + * + * @return description of the event + */ + public String getDescription() { + return description; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java index 6e0ad76..24b9ff7 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java @@ -15,7 +15,7 @@ public class Subscription extends XplentyObject { @JsonProperty("trial_period_days") private Integer trialPeriodDays; @JsonProperty("plan_id") - private Long planId; + private String planId; @JsonProperty("trial_start") private Date trialStart; @JsonProperty("trial_end") @@ -41,7 +41,7 @@ public Integer getTrialPeriodDays() { * * @return the numeric identifier of the current plan */ - public Long getPlanId() { + public String getPlanId() { return planId; } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java index e2392fa..5b66229 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java @@ -1,7 +1,6 @@ package com.xplenty.api.model; import com.fasterxml.jackson.annotation.JsonProperty; -import com.xplenty.api.Xplenty; import java.util.List; @@ -33,9 +32,9 @@ public class WebHook extends XplentyObject { @JsonProperty private List events; @JsonProperty("add_events") - private List addEvents; + private List addEvents; @JsonProperty("remove_events") - private List removeEvents; + private List removeEvents; protected WebHook() { super(WebHook.class); @@ -47,7 +46,7 @@ public WebHook(WebHookSettings settings, List events) { this.events = events; } - public WebHook(Long id, WebHookSettings settings, List addEvents, List removeEvents) { + public WebHook(Long id, WebHookSettings settings, List addEvents, List removeEvents) { super(WebHook.class); this.addEvents = addEvents; this.removeEvents = removeEvents; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java index b926f88..c3d7444 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java @@ -2,8 +2,14 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.xplenty.api.Xplenty; +import java.io.IOException; import java.util.Date; /** @@ -12,11 +18,12 @@ * Date: 04.01.16 * Time: 18:01 */ +@JsonSerialize(using = WebHookEvent.WebHookEventSerializer.class) public class WebHookEvent { @JsonProperty private Long id; @JsonProperty - private Xplenty.WebHookEvent name; + private String name; @JsonIgnore private String lastResponse; @JsonProperty("last_trigger_status") @@ -26,7 +33,7 @@ public class WebHookEvent { public WebHookEvent() {} - public WebHookEvent(Xplenty.WebHookEvent name) { + public WebHookEvent(String name) { this.name = name; } @@ -42,10 +49,18 @@ public Long getId() { * * @return name constant of the event */ - public Xplenty.WebHookEvent getName() { + public String getName() { return name; } + public Xplenty.WebHookEvent getEvent() { + try { + return Xplenty.WebHookEvent.valueOf(name); + } catch (IllegalArgumentException ex) { + return null; + } + } + /** * * @return last response @@ -69,4 +84,12 @@ public String getLastTriggerStatus() { public Date getLastTriggerTime() { return lastTriggerTime; } + + protected static class WebHookEventSerializer extends JsonSerializer { + + @Override + public void serialize(WebHookEvent webHookEvent, JsonGenerator jGen, SerializerProvider sp) throws IOException, JsonProcessingException { + jGen.writeString(webHookEvent.getName()); + } + } } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractParametrizedRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractListRequest.java similarity index 84% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractParametrizedRequest.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractListRequest.java index d1bdd11..0b349cc 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractParametrizedRequest.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractListRequest.java @@ -2,6 +2,7 @@ import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; import java.util.Properties; @@ -10,7 +11,7 @@ * Date: 17.12.15 * Time: 20:46 */ -public abstract class AbstractParametrizedRequest extends AbstractRequest { +public abstract class AbstractListRequest extends AbstractRequest { public static final String PARAMETER_STATUS = "status"; public static final String PARAMETER_SORT = "sort"; public static final String PARAMETER_DIRECTION = "direction"; @@ -19,13 +20,16 @@ public abstract class AbstractParametrizedRequest extends AbstractRequest protected Properties parameters; - protected AbstractParametrizedRequest(Properties parameters, boolean validateSort) { + protected AbstractListRequest(Properties parameters, boolean validateSort) { validateParameters(parameters, validateSort); this.parameters = parameters; } @SuppressWarnings("unchecked") private void validateParameters(Properties params, boolean validateSort) { + if (params == null) { + return; + } if (validateSort) { if (params.containsKey(PARAMETER_SORT) @@ -77,6 +81,25 @@ public String getEndpoint() { return params.insert(0, getEndpointRoot()).toString(); } + @Override + public boolean hasBody() { + return false; + } + + @Override + public T getBody() { + return null; + } + + @Override + public Http.MediaType getResponseType() { + return Http.MediaType.JSON; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.GET; + } protected abstract String getEndpointRoot(); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java index 705f4a9..76a9f0e 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java @@ -15,7 +15,7 @@ * Time: 18:49 */ public abstract class AbstractManipulationRequest extends AbstractRequest { - protected final T entity; + protected T entity; private final Class clazz; @SuppressWarnings("unchecked") @@ -28,6 +28,10 @@ protected AbstractManipulationRequest(T entity) { this.clazz = (Class) ((ParameterizedType) superclass).getActualTypeArguments()[0]; } + protected void setEntity(T entity) { + this.entity = entity; + } + @Override public T getResponse(Response response) { try { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java index 6fb7326..07b61ae 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java @@ -7,9 +7,6 @@ import com.xplenty.api.Xplenty; import com.xplenty.api.Xplenty.ClusterStatus; import com.xplenty.api.exceptions.XplentyAPIException; -import com.xplenty.api.http.Http; -import com.xplenty.api.http.Http.MediaType; -import com.xplenty.api.http.Http.Method; import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; @@ -20,7 +17,7 @@ * @author Yuriy Kovalek * */ -public class ListClusters extends AbstractParametrizedRequest> { +public class ListClusters extends AbstractListRequest> { public ListClusters(Properties params) { super(params, true); @@ -35,16 +32,6 @@ private void validateParameters(Properties params) { throw new XplentyAPIException("Invalid 'status' parameter"); } - @Override - public Method getHttpMethod() { - return Http.Method.GET; - } - - @Override - public MediaType getResponseType() { - return Http.MediaType.JSON; - } - @Override protected String getEndpointRoot() { return Xplenty.Resource.Clusters.value; @@ -64,14 +51,4 @@ public String getName() { return Xplenty.Resource.Clusters.name; } - @Override - public boolean hasBody() { - return false; - } - - @Override - public List getBody() { - return null; - } - } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListHookEvents.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListHookEvents.java new file mode 100644 index 0000000..d332749 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListHookEvents.java @@ -0,0 +1,46 @@ +package com.xplenty.api.request; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.HookEvent; + +import java.util.List; + +/** + * List supported Hook events + * Author: Xardas + * Date: 05.01.16 + * Time: 15:12 + */ +public class ListHookEvents extends AbstractListRequest> { + + public ListHookEvents() { + super(null, false); + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpointRoot()); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.HookEvents.value; + } + + @Override + public String getName() { + return Xplenty.Resource.HookEvents.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListJobs.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListJobs.java index c33aa32..2704b2e 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListJobs.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListJobs.java @@ -7,9 +7,6 @@ import com.xplenty.api.Xplenty; import com.xplenty.api.Xplenty.ClusterStatus; import com.xplenty.api.exceptions.XplentyAPIException; -import com.xplenty.api.http.Http; -import com.xplenty.api.http.Http.MediaType; -import com.xplenty.api.http.Http.Method; import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; @@ -20,7 +17,7 @@ * @author Yuriy Kovalek * */ -public class ListJobs extends AbstractParametrizedRequest> { +public class ListJobs extends AbstractListRequest> { public ListJobs(Properties params) { super(params, true); @@ -40,15 +37,6 @@ public String getName() { return Xplenty.Resource.Jobs.name; } - @Override - public Method getHttpMethod() { - return Http.Method.GET; - } - - @Override - public MediaType getResponseType() { - return Http.MediaType.JSON; - } @Override protected String getEndpointRoot() { @@ -64,14 +52,4 @@ public List getResponse(Response response) { } } - @Override - public boolean hasBody() { - return false; - } - - @Override - public List getBody() { - return null; - } - } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListPackages.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListPackages.java index e9d15ec..d2ec6a3 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListPackages.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListPackages.java @@ -6,10 +6,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; -import com.xplenty.api.http.Http.MediaType; -import com.xplenty.api.http.Http.Method; import com.xplenty.api.http.Response; -import com.xplenty.api.model.Cluster; import com.xplenty.api.model.Package; import java.util.List; @@ -22,22 +19,12 @@ * Date: 16.12.15 * Time: 18:08 */ -public class ListPackages extends AbstractParametrizedRequest> { +public class ListPackages extends AbstractListRequest> { public ListPackages(Properties params) { super(params, false); } - @Override - public Method getHttpMethod() { - return Method.GET; - } - - @Override - public MediaType getResponseType() { - return MediaType.JSON; - } - @Override protected String getEndpointRoot() { return Xplenty.Resource.Packages.value; @@ -57,14 +44,4 @@ public String getName() { return Xplenty.Resource.Packages.name; } - @Override - public boolean hasBody() { - return false; - } - - @Override - public List getBody() { - return null; - } - } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListSchedules.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListSchedules.java index 969402f..118391c 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListSchedules.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListSchedules.java @@ -6,10 +6,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; -import com.xplenty.api.http.Http.MediaType; -import com.xplenty.api.http.Http.Method; import com.xplenty.api.http.Response; -import com.xplenty.api.model.Cluster; import com.xplenty.api.model.Schedule; import java.util.List; @@ -22,7 +19,7 @@ * Date: 16.12.15 * Time: 18:08 */ -public class ListSchedules extends AbstractParametrizedRequest> { +public class ListSchedules extends AbstractListRequest> { public ListSchedules(Properties params) { @@ -38,18 +35,6 @@ private void validateParameters(Properties params) { } } - @Override - public Method getHttpMethod() { - return Method.GET; - } - - @Override - public MediaType getResponseType() { - return MediaType.JSON; - } - - - @Override public List getResponse(Response response) { try { @@ -64,16 +49,6 @@ public String getName() { return Xplenty.Resource.Schedules.name; } - @Override - public boolean hasBody() { - return false; - } - - @Override - public List getBody() { - return null; - } - @Override protected String getEndpointRoot() { return Xplenty.Resource.Schedules.value; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/CreateWebHook.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/CreateWebHook.java new file mode 100644 index 0000000..1180ee2 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/CreateWebHook.java @@ -0,0 +1,69 @@ +package com.xplenty.api.request.webhook; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.model.WebHookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.request.AbstractManipulationRequest; + +import java.util.ArrayList; +import java.util.List; + +/** + * Create new Web hook + * Author: Xardas + * Date: 05.01.16 + * Time: 15:47 + */ +public class CreateWebHook extends AbstractManipulationRequest { + + public CreateWebHook(WebHookSettings settings, List events) { + super(null); + if (events == null || events.size() == 0) { + throw new XplentyAPIException("You must subscribe to at least 1 event!"); + } + List convEvents = new ArrayList<>(events.size()); + for (String event : events) { + WebHookEvent webHookEvent = new WebHookEvent(event); + convEvents.add(webHookEvent); + } + WebHook entity = new WebHook(settings, convEvents); + setEntity(entity); + } + + public CreateWebHook(List events, WebHookSettings settings) { + super(null); + if (events == null || events.size() == 0) { + throw new XplentyAPIException("You must subscribe to at least 1 event!"); + } + List convEvents = new ArrayList<>(events.size()); + for (Xplenty.WebHookEvent event : events) { + WebHookEvent webHookEvent = new WebHookEvent(event.toString()); + convEvents.add(webHookEvent); + } + WebHook entity = new WebHook(settings, convEvents); + setEntity(entity); + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.CreateWebHook.value; + } + + @Override + public String getName() { + return Xplenty.Resource.CreateWebHook.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.POST; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/DeleteWebHook.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/DeleteWebHook.java new file mode 100644 index 0000000..690046b --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/DeleteWebHook.java @@ -0,0 +1,28 @@ +package com.xplenty.api.request.webhook; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.request.AbstractDeleteRequest; + +/** + * Delete web hook + * Author: Xardas + * Date: 18.12.15 + * Time: 20:25 + */ +public class DeleteWebHook extends AbstractDeleteRequest { + + public DeleteWebHook(long entityId) { + super(entityId); + } + + @Override + public String getName() { + return Xplenty.Resource.DeleteWebHook.name; + } + + @Override + public String getEndpoint() { + return Xplenty.Resource.DeleteWebHook.format(String.valueOf(entityId)); + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ListWebHooks.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ListWebHooks.java new file mode 100644 index 0000000..8fa022b --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ListWebHooks.java @@ -0,0 +1,44 @@ +package com.xplenty.api.request.webhook; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; +import java.util.Properties; + +/** + * List web hooks + * Author: Xardas + * Date: 05.01.16 + * Time: 17:38 + */ +public class ListWebHooks extends AbstractListRequest> { + + + public ListWebHooks(Properties parameters) { + super(parameters, true); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.WebHooks.value; + } + + @Override + public String getName() { + return Xplenty.Resource.WebHooks.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/PingWebHook.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/PingWebHook.java new file mode 100644 index 0000000..ebb8315 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/PingWebHook.java @@ -0,0 +1,27 @@ +package com.xplenty.api.request.webhook; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.request.AbstractInfoRequest; + +/** + * Ping (fire test notification) web hook and return web hook info + * Author: Xardas + * Date: 05.01.16 + * Time: 18:19 + */ +public class PingWebHook extends AbstractInfoRequest { + public PingWebHook(long entityId) { + super(entityId); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.PingWebHook.format(String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.PingWebHook.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ToggleWebHook.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ToggleWebHook.java new file mode 100644 index 0000000..d6ed46d --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ToggleWebHook.java @@ -0,0 +1,39 @@ +package com.xplenty.api.request.webhook; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.request.AbstractManipulationRequest; + +/** + * Enable/Disable Web hook + * Author: Xardas + * Date: 05.01.16 + * Time: 17:28 + */ +public class ToggleWebHook extends AbstractManipulationRequest { + + public ToggleWebHook(Long id, Boolean active) { + super(new WebHook(id, active)); + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.UpdateWebHook.format(entity.getId().toString()); + } + + @Override + public String getName() { + return Xplenty.Resource.UpdateWebHook.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.PUT; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/UpdateWebHook.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/UpdateWebHook.java new file mode 100644 index 0000000..7ee36cc --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/UpdateWebHook.java @@ -0,0 +1,64 @@ +package com.xplenty.api.request.webhook; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.request.AbstractManipulationRequest; + +import java.util.ArrayList; +import java.util.List; + +/** + * Update existing Web hook + * Author: Xardas + * Date: 05.01.16 + * Time: 15:47 + */ +public class UpdateWebHook extends AbstractManipulationRequest { + + public UpdateWebHook(Long id, WebHookSettings settings, List addEvents, List removeEvents) { + super(new WebHook(id, settings, addEvents, removeEvents)); + } + + public UpdateWebHook(Long id, List addEvents, List removeEvents, WebHookSettings settings) { + super(null); + List convAddEvents = null; + List convRemoveEvents = null; + if (addEvents != null) { + convAddEvents = new ArrayList<>(addEvents.size()); + for (Xplenty.WebHookEvent event : addEvents) { + convAddEvents.add(event.toString()); + } + } + if (removeEvents != null) { + convRemoveEvents = new ArrayList<>(removeEvents.size()); + for (Xplenty.WebHookEvent event : removeEvents) { + convRemoveEvents.add(event.toString()); + } + } + + WebHook entity = new WebHook(id, settings, convAddEvents, convRemoveEvents); + setEntity(entity); + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.UpdateWebHook.format(entity.getId().toString()); + } + + @Override + public String getName() { + return Xplenty.Resource.UpdateWebHook.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.PUT; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookInfo.java new file mode 100644 index 0000000..749c362 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookInfo.java @@ -0,0 +1,28 @@ +package com.xplenty.api.request.webhook; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.request.AbstractInfoRequest; + +/** + * Get web hook information + * Author: Xardas + * Date: 05.01.16 + * Time: 18:26 + */ +public class WebHookInfo extends AbstractInfoRequest { + + public WebHookInfo(long entityId) { + super(entityId); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.WebHook.format(String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.WebHook.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookResetSalt.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookResetSalt.java new file mode 100644 index 0000000..ddb1ff4 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookResetSalt.java @@ -0,0 +1,62 @@ +package com.xplenty.api.request.webhook; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.request.AbstractRequest; + +/** + * Reset salt for web hook + * Author: Xardas + * Date: 05.01.16 + * Time: 18:07 + */ +public class WebHookResetSalt extends AbstractRequest { + private final Long webHookId; + + public WebHookResetSalt(Long webHookId) { + this.webHookId = webHookId; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.WebHookResetSalt.format(webHookId.toString()); + } + + @Override + public String getName() { + return Xplenty.Resource.WebHookResetSalt.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.PUT; + } + + @Override + public Http.MediaType getResponseType() { + return Http.MediaType.JSON; + } + + @Override + public boolean hasBody() { + return false; + } + + @Override + public Object getBody() { + return null; + } + + @Override + public String getResponse(Response response) { + try { + WebHook webHook = response.getContent(WebHook.class); + return webHook.getSalt(); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java index ae3cccd..e9a2c46 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java @@ -73,8 +73,8 @@ public void testInvalidResponceHandling() throws JsonProcessingException, Unsupp @Test public void testPaging() throws Exception { final Properties params = new Properties(); - params.put(AbstractParametrizedRequest.PARAMETER_LIMIT, 1); - params.put(AbstractParametrizedRequest.PARAMETER_OFFSET, 2); + params.put(AbstractListRequest.PARAMETER_LIMIT, 1); + params.put(AbstractListRequest.PARAMETER_OFFSET, 2); ListSchedules ls = new ListSchedules(params); assertEquals(ls.getEndpoint(), String.format("%s?limit=1&offset=2", ls.getEndpointRoot())); @@ -85,8 +85,8 @@ public void testPaging() throws Exception { @Test public void testInvalidPaging() throws Exception { final Properties params = new Properties(); - params.put(AbstractParametrizedRequest.PARAMETER_LIMIT, -1); - params.put(AbstractParametrizedRequest.PARAMETER_OFFSET, -2); + params.put(AbstractListRequest.PARAMETER_LIMIT, -1); + params.put(AbstractListRequest.PARAMETER_OFFSET, -2); try { ListSchedules ls = new ListSchedules(params); @@ -95,14 +95,14 @@ public void testInvalidPaging() throws Exception { } try { - params.put(AbstractParametrizedRequest.PARAMETER_LIMIT, 101); + params.put(AbstractListRequest.PARAMETER_LIMIT, 101); ListSchedules ls = new ListSchedules(params); } catch (XplentyAPIException ex) { assertEquals(String.format("'limit' parameter should be less or equal to %s and greater than 0", Xplenty.MAX_LIMIT), ex.getMessage()); } try { - params.put(AbstractParametrizedRequest.PARAMETER_LIMIT, 1); + params.put(AbstractListRequest.PARAMETER_LIMIT, 1); ListSchedules ls = new ListSchedules(params); } catch (XplentyAPIException ex) { assertEquals("'offset' parameter should be greater than 0", ex.getMessage()); From 5b400ee3af95021f730a22b4e5d38f8e6f1fbf39 Mon Sep 17 00:00:00 2001 From: xardas Date: Tue, 5 Jan 2016 19:33:38 +0300 Subject: [PATCH 10/35] remove console output --- .../src/main/java/com/xplenty/api/http/SyncNettyClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java index b393022..c5fce59 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java @@ -88,7 +88,7 @@ public void run() { public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { HttpResponse response = (HttpResponse) e.getMessage(); - System.out.println(channelBuffer2String(response.getContent())); + //System.out.println(channelBuffer2String(response.getContent())); final Channel channel = ctx.getChannel(); NettyResponse nettyResponse = (NettyResponse) channel.getAttachment(); From 5a84590f68534b425c847ceede26fa0c6b415d61 Mon Sep 17 00:00:00 2001 From: xardas Date: Tue, 5 Jan 2016 21:20:24 +0300 Subject: [PATCH 11/35] add tests --- .../main/java/com/xplenty/api/Xplenty.java | 2 +- .../java/com/xplenty/api/model/WebHook.java | 14 +-- .../com/xplenty/api/model/WebHookEvent.java | 10 +- .../api/request/webhook/WebHookResetSalt.java | 6 +- .../com/xplenty/api/model/WebHookTest.java | 49 ++++++++ .../request/webhook/CreateWebHookTest.java | 108 ++++++++++++++++ .../request/webhook/DeleteWebHookTest.java | 93 ++++++++++++++ .../api/request/webhook/ListWebHookTest.java | 116 ++++++++++++++++++ .../api/request/webhook/PingWebHookTest.java | 93 ++++++++++++++ .../request/webhook/ToggleWebHookTest.java | 93 ++++++++++++++ .../request/webhook/UpdateWebHookTest.java | 109 ++++++++++++++++ .../api/request/webhook/WebHookInfoTest.java | 93 ++++++++++++++ .../request/webhook/WebHookResetSaltTest.java | 76 ++++++++++++ 13 files changed, 846 insertions(+), 16 deletions(-) create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/WebHookTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/CreateWebHookTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/DeleteWebHookTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/ListWebHookTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/PingWebHookTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/ToggleWebHookTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/UpdateWebHookTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookInfoTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookResetSaltTest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index fd57124..c98fea2 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -251,7 +251,7 @@ public static enum Resource { DeleteWebHook("hooks/web/%s", "Delete Web hook"), PingWebHook("hooks/web/%s/ping", "Ping(fire test notification) for Web hook"), WebHookResetSalt("hooks/web/%s/reset_salt", "Reset Web hook's salt"), - WebHook("hooks/web/%s", "Update existing Web hook"), + WebHook("hooks/web/%s", "Get Web hook information"), WebHooks("hooks/web", "List Web hooks") ; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java index 5b66229..d3b6df3 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java @@ -22,19 +22,19 @@ */ public class WebHook extends XplentyObject { @JsonProperty - private Long id; + protected Long id; @JsonProperty - private Boolean active; + protected Boolean active; @JsonProperty - private WebHookSettings settings; + protected WebHookSettings settings; @JsonProperty - private String salt; + protected String salt; @JsonProperty - private List events; + protected List events; @JsonProperty("add_events") - private List addEvents; + protected List addEvents; @JsonProperty("remove_events") - private List removeEvents; + protected List removeEvents; protected WebHook() { super(WebHook.class); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java index c3d7444..40779de 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java @@ -21,15 +21,15 @@ @JsonSerialize(using = WebHookEvent.WebHookEventSerializer.class) public class WebHookEvent { @JsonProperty - private Long id; + protected Long id; @JsonProperty - private String name; + protected String name; @JsonIgnore - private String lastResponse; + protected String lastResponse; @JsonProperty("last_trigger_status") - private String lastTriggerStatus; + protected String lastTriggerStatus; @JsonProperty("last_trigger_time") - private Date lastTriggerTime; + protected Date lastTriggerTime; public WebHookEvent() {} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookResetSalt.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookResetSalt.java index ddb1ff4..de6ff04 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookResetSalt.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookResetSalt.java @@ -14,15 +14,15 @@ * Time: 18:07 */ public class WebHookResetSalt extends AbstractRequest { - private final Long webHookId; + private final long webHookId; - public WebHookResetSalt(Long webHookId) { + public WebHookResetSalt(long webHookId) { this.webHookId = webHookId; } @Override protected String getEndpoint() { - return Xplenty.Resource.WebHookResetSalt.format(webHookId.toString()); + return Xplenty.Resource.WebHookResetSalt.format(String.valueOf(webHookId)); } @Override diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/WebHookTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/WebHookTest.java new file mode 100644 index 0000000..34f173d --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/WebHookTest.java @@ -0,0 +1,49 @@ +package com.xplenty.api.model; + +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * Author: Xardas + * Date: 05.01.16 + * Time: 19:02 + */ +public class WebHookTest extends TestCase { + @Test + public void testBuilder() { + final Date now = new Date(); + WebHook webhook = createMockWebHook(now); + assertNotNull(webhook); + assertEquals(now.getTime(), webhook.getEvents().get(0).getLastTriggerTime().getTime()); + + } + + + public static WebHook createMockWebHook(Date now) { + WebHook webHook = new WebHook(); + webHook.id = 666L; + webHook.active = true; + webHook.salt = "000abcdead"; + WebHookSettings whs = new WebHookSettings("http://localhost/test", true, false, "somedata"); + webHook.settings = whs; + List events = new ArrayList<>(); + WebHookEvent whe = new WebHookEvent(); + whe.id = 111L; + whe.lastTriggerStatus = "omg!"; + whe.lastTriggerTime = now; + whe.name = "job"; + events.add(whe); + whe = new WebHookEvent(); + whe.id = 222L; + whe.lastTriggerStatus = "omg2!"; + whe.lastTriggerTime = now; + whe.name = "cluster"; + events.add(whe); + webHook.events = events; + return webHook; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/CreateWebHookTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/CreateWebHookTest.java new file mode 100644 index 0000000..f077886 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/CreateWebHookTest.java @@ -0,0 +1,108 @@ +/** + * + */ +package com.xplenty.api.request.webhook; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.model.WebHookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.WebHookTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; + +/** + * @author xardas + * + */ +public class CreateWebHookTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + WebHookSettings whs = new WebHookSettings("http://localhost", false, false, "np"); + List events = new ArrayList<>(); + events.add("job"); + + CreateWebHook cc = new CreateWebHook(whs, events); + assertEquals(Xplenty.Resource.CreateWebHook.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.CreateWebHook.name, cc.getName()); + assertEquals(Http.Method.POST, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertTrue(cc.hasBody()); + assertNotNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + WebHook c = WebHookTest.createMockWebHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + WebHookSettings whs = new WebHookSettings("http://localhost/test", true, false, "somedata"); + List events = new ArrayList<>(); + events.add("job"); + + CreateWebHook cc = new CreateWebHook(whs, events); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals(true, c.getActive().booleanValue()); + assertEquals("000abcdead", c.getSalt()); + final WebHookSettings settings = c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + final WebHookEvent event = c.getEvents().get(0); + // we've got custom json serializer that removes everything except name + assertNull(event.getId()); + assertNull(event.getLastTriggerStatus()); + assertEquals("job", event.getName()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + WebHook c = WebHookTest.createMockWebHook(now); + + WebHookSettings whs = new WebHookSettings("http://localhost/test", true, false, "somedata"); + List events = new ArrayList<>(); + events.add("job"); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + try { + CreateWebHook cc = new CreateWebHook(whs, events); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.CreateWebHook.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/DeleteWebHookTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/DeleteWebHookTest.java new file mode 100644 index 0000000..4cd18ab --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/DeleteWebHookTest.java @@ -0,0 +1,93 @@ +package com.xplenty.api.request.webhook; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.model.WebHookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.WebHookTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * Author: Xardas + * Date: 05.01.16 + * Time: 19:43 + */ +public class DeleteWebHookTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + DeleteWebHook cc = new DeleteWebHook(1L); + assertEquals(Xplenty.Resource.DeleteWebHook.format("1"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.DeleteWebHook.name, cc.getName()); + assertEquals(Http.Method.DELETE, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + WebHook c = WebHookTest.createMockWebHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + DeleteWebHook cc = new DeleteWebHook(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals(true, c.getActive().booleanValue()); + assertEquals("000abcdead", c.getSalt()); + final WebHookSettings settings = c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + final WebHookEvent event = c.getEvents().get(0); + // we've got custom json serializer that removes everything except name + assertNull(event.getId()); + assertNull(event.getLastTriggerStatus()); + assertEquals("job", event.getName()); + assertEquals("cluster", c.getEvents().get(1).getName()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + WebHook c = WebHookTest.createMockWebHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + try { + + DeleteWebHook cc = new DeleteWebHook(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.DeleteWebHook.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/ListWebHookTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/ListWebHookTest.java new file mode 100644 index 0000000..c29c1d3 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/ListWebHookTest.java @@ -0,0 +1,116 @@ +package com.xplenty.api.request.webhook; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.model.WebHookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.WebHookTest; +import com.xplenty.api.request.AbstractListRequest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; + +/** + * Author: Xardas + * Date: 05.01.16 + * Time: 19:43 + */ +public class ListWebHookTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + ListWebHooks cc = new ListWebHooks(new Properties()); + assertEquals(Xplenty.Resource.WebHooks.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.WebHooks.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + + + final Properties props = new Properties(); + props.put(AbstractListRequest.PARAMETER_SORT, Xplenty.Sort.created); + props.put(AbstractListRequest.PARAMETER_DIRECTION, Xplenty.SortDirection.descending); + cc = new ListWebHooks(props); + assertEquals(Xplenty.Resource.WebHooks.value + "?" + AbstractListRequest.PARAMETER_SORT + "=created&" + + AbstractListRequest.PARAMETER_DIRECTION + "=desc", cc.getEndpoint()); + assertEquals(Xplenty.Resource.WebHooks.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + List whList = new ArrayList<>(); + + WebHook c = WebHookTest.createMockWebHook(now); + whList.add(c); + + + String json = JsonMapperFactory.getInstance().writeValueAsString(whList); + + ListWebHooks cc = new ListWebHooks(new Properties()); + whList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + c = whList.get(0); + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals(true, c.getActive().booleanValue()); + assertEquals("000abcdead", c.getSalt()); + final WebHookSettings settings = c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + final WebHookEvent event = c.getEvents().get(0); + // we've got custom json serializer that removes everything except name + assertNull(event.getId()); + assertNull(event.getLastTriggerStatus()); + assertEquals("job", event.getName()); + assertEquals("cluster", c.getEvents().get(1).getName()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + List whList = new ArrayList<>(); + + WebHook c = WebHookTest.createMockWebHook(now); + whList.add(c); + + + String json = JsonMapperFactory.getInstance().writeValueAsString(whList).replace("{", "["); + + try { + + ListWebHooks cc = new ListWebHooks(new Properties()); + whList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.WebHooks.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/PingWebHookTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/PingWebHookTest.java new file mode 100644 index 0000000..9a3e457 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/PingWebHookTest.java @@ -0,0 +1,93 @@ +package com.xplenty.api.request.webhook; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.model.WebHookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.WebHookTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * Author: Xardas + * Date: 05.01.16 + * Time: 19:43 + */ +public class PingWebHookTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + PingWebHook cc = new PingWebHook(1L); + assertEquals(Xplenty.Resource.PingWebHook.format("1"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.PingWebHook.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + WebHook c = WebHookTest.createMockWebHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + PingWebHook cc = new PingWebHook(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals(true, c.getActive().booleanValue()); + assertEquals("000abcdead", c.getSalt()); + final WebHookSettings settings = c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + final WebHookEvent event = c.getEvents().get(0); + // we've got custom json serializer that removes everything except name + assertNull(event.getId()); + assertNull(event.getLastTriggerStatus()); + assertEquals("job", event.getName()); + assertEquals("cluster", c.getEvents().get(1).getName()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + WebHook c = WebHookTest.createMockWebHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + try { + + PingWebHook cc = new PingWebHook(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.PingWebHook.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/ToggleWebHookTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/ToggleWebHookTest.java new file mode 100644 index 0000000..f71fd51 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/ToggleWebHookTest.java @@ -0,0 +1,93 @@ +package com.xplenty.api.request.webhook; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.model.WebHookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.WebHookTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * Author: Xardas + * Date: 05.01.16 + * Time: 19:43 + */ +public class ToggleWebHookTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + ToggleWebHook cc = new ToggleWebHook(1L, false); + assertEquals(Xplenty.Resource.UpdateWebHook.format("1"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.UpdateWebHook.name, cc.getName()); + assertEquals(Http.Method.PUT, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertTrue(cc.hasBody()); + assertNotNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + WebHook c = WebHookTest.createMockWebHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + ToggleWebHook cc = new ToggleWebHook(1L, true); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals(true, c.getActive().booleanValue()); + assertEquals("000abcdead", c.getSalt()); + final WebHookSettings settings = c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + final WebHookEvent event = c.getEvents().get(0); + // we've got custom json serializer that removes everything except name + assertNull(event.getId()); + assertNull(event.getLastTriggerStatus()); + assertEquals("job", event.getName()); + assertEquals("cluster", c.getEvents().get(1).getName()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + WebHook c = WebHookTest.createMockWebHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + try { + + ToggleWebHook cc = new ToggleWebHook(1L, true); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.UpdateWebHook.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/UpdateWebHookTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/UpdateWebHookTest.java new file mode 100644 index 0000000..fb8e099 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/UpdateWebHookTest.java @@ -0,0 +1,109 @@ +package com.xplenty.api.request.webhook; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.model.WebHookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.WebHookTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; + +/** + * Author: Xardas + * Date: 05.01.16 + * Time: 19:43 + */ +public class UpdateWebHookTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + WebHookSettings whs = new WebHookSettings("http://localhost", false, false, "np"); + List events = new ArrayList<>(); + events.add("job"); + + UpdateWebHook cc = new UpdateWebHook(1L, whs, events, events); + assertEquals(Xplenty.Resource.UpdateWebHook.format("1"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.UpdateWebHook.name, cc.getName()); + assertEquals(Http.Method.PUT, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertTrue(cc.hasBody()); + assertNotNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + WebHook c = WebHookTest.createMockWebHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + WebHookSettings whs = new WebHookSettings("http://localhost/test", true, false, "somedata"); + List events = new ArrayList<>(); + events.add("job"); + events.add("cluster"); + + UpdateWebHook cc = new UpdateWebHook(666L, whs, events, null); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals(true, c.getActive().booleanValue()); + assertEquals("000abcdead", c.getSalt()); + final WebHookSettings settings = c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + final WebHookEvent event = c.getEvents().get(0); + // we've got custom json serializer that removes everything except name + assertNull(event.getId()); + assertNull(event.getLastTriggerStatus()); + assertEquals("job", event.getName()); + assertEquals("cluster", c.getEvents().get(1).getName()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + WebHook c = WebHookTest.createMockWebHook(now); + + WebHookSettings whs = new WebHookSettings("http://localhost/test", true, false, "somedata"); + List events = new ArrayList<>(); + events.add("job"); + events.add("cluster"); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + try { + + UpdateWebHook cc = new UpdateWebHook(666L, whs, events, null); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.UpdateWebHook.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookInfoTest.java new file mode 100644 index 0000000..47f581b --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookInfoTest.java @@ -0,0 +1,93 @@ +package com.xplenty.api.request.webhook; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.model.WebHookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.WebHookTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * Author: Xardas + * Date: 05.01.16 + * Time: 19:43 + */ +public class WebHookInfoTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + WebHookInfo cc = new WebHookInfo(1L); + assertEquals(Xplenty.Resource.WebHook.format("1"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.WebHook.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + WebHook c = WebHookTest.createMockWebHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + WebHookInfo cc = new WebHookInfo(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals(true, c.getActive().booleanValue()); + assertEquals("000abcdead", c.getSalt()); + final WebHookSettings settings = c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + final WebHookEvent event = c.getEvents().get(0); + // we've got custom json serializer that removes everything except name + assertNull(event.getId()); + assertNull(event.getLastTriggerStatus()); + assertEquals("job", event.getName()); + assertEquals("cluster", c.getEvents().get(1).getName()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + WebHook c = WebHookTest.createMockWebHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + try { + + WebHookInfo cc = new WebHookInfo(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.WebHook.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookResetSaltTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookResetSaltTest.java new file mode 100644 index 0000000..cbef25c --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookResetSaltTest.java @@ -0,0 +1,76 @@ +package com.xplenty.api.request.webhook; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.WebHook; +import com.xplenty.api.model.WebHookTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * Author: Xardas + * Date: 05.01.16 + * Time: 19:43 + */ +public class WebHookResetSaltTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + WebHookResetSalt cc = new WebHookResetSalt(1L); + assertEquals(Xplenty.Resource.WebHookResetSalt.format("1"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.WebHookResetSalt.name, cc.getName()); + assertEquals(Http.Method.PUT, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + WebHook c = WebHookTest.createMockWebHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + WebHookResetSalt cc = new WebHookResetSalt(666); + String newSalt = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + "{ \"salt\" : \"deadabc\" }", + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + assertEquals("deadabc", newSalt); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + + try { + + WebHookResetSalt cc = new WebHookResetSalt(666); + String newSalt = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + "[[ \"salt\" : \"deadabc\" }", + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.WebHookResetSalt.name + ": error parsing response object", e.getMessage()); + } + + } +} From 1c8e3d7efe5a7db5211421354a6ad6828ceab6c5 Mon Sep 17 00:00:00 2001 From: xardas Date: Tue, 5 Jan 2016 22:22:20 +0300 Subject: [PATCH 12/35] add normal console logger --- .../xplenty/api/http/ConsoleNettyLogger.java | 81 +++++++++++++++++++ .../com/xplenty/api/http/SyncNettyClient.java | 18 +++-- 2 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/http/ConsoleNettyLogger.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/ConsoleNettyLogger.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/ConsoleNettyLogger.java new file mode 100644 index 0000000..4f92146 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/ConsoleNettyLogger.java @@ -0,0 +1,81 @@ +package com.xplenty.api.http; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.channel.*; +import org.jboss.netty.handler.codec.http.HttpMessage; + +import java.nio.charset.StandardCharsets; +import java.text.DateFormat; +import java.text.SimpleDateFormat; + +/** + * Author: Xardas + * Date: 05.01.16 + * Time: 20:24 + */ +public class ConsoleNettyLogger implements ChannelUpstreamHandler, ChannelDownstreamHandler { + + private ThreadLocal df = new ThreadLocal<>(); + private final boolean logHttpBodyOnly; + + public ConsoleNettyLogger(boolean logHttpBodyOnly) { + this.logHttpBodyOnly = logHttpBodyOnly; + } + + + @Override + public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent channelEvent) throws Exception { + log(channelEvent, false); + ctx.sendDownstream(channelEvent); + } + + @Override + public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent channelEvent) throws Exception { + log(channelEvent, true); + ctx.sendUpstream(channelEvent); + } + + private void log(ChannelEvent event, boolean isIncoming) { + if (event instanceof MessageEvent) { + MessageEvent me = (MessageEvent) event; + if (me.getMessage() instanceof ChannelBuffer) { + logMessage((ChannelBuffer) me.getMessage(), isIncoming); + } else if (HttpMessage.class.isAssignableFrom(me.getMessage().getClass()) && logHttpBodyOnly) { + logHttpMessage(((HttpMessage) me.getMessage()).getContent(), true); + } + } else if (event instanceof ExceptionEvent && !logHttpBodyOnly) { + logException(((ExceptionEvent) event).getCause()); + } else if (!logHttpBodyOnly) { + logStateMessage(event.toString()); + } + } + + private void logStateMessage(String str) { + System.err.println(String.format("%s %s", getDateFormat().format(System.currentTimeMillis()), str)); + } + + private void logMessage(ChannelBuffer buf, boolean isIncoming) { + System.err.println(String.format("%s %s", getDateFormat().format(System.currentTimeMillis()), isIncoming ? "<<<" : ">>>")); + System.err.println(buf.toString(StandardCharsets.UTF_8)); + System.err.println(isIncoming ? "<<<" : ">>>"); + } + + private void logHttpMessage(ChannelBuffer buf, boolean isIncoming) { + System.err.println(String.format("%s [DECODED]%s", getDateFormat().format(System.currentTimeMillis()), isIncoming ? "<<<" : ">>>")); + System.err.println(buf.toString(StandardCharsets.UTF_8)); + System.err.println(isIncoming ? "[DECODED]<<<" : "[DECODED]>>>"); + } + + private void logException(Throwable ex) { + System.err.println(ex.getMessage()); + } + + private DateFormat getDateFormat() { + DateFormat dateFormat = df.get(); + if (dateFormat == null) { + dateFormat = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss.SSS"); + df.set(dateFormat); + } + return dateFormat; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java index 0a9ccd0..ccbac8b 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java @@ -12,9 +12,7 @@ import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.handler.codec.http.*; import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor; -import org.jboss.netty.handler.logging.LoggingHandler; import org.jboss.netty.handler.ssl.SslHandler; -import org.jboss.netty.logging.InternalLogLevel; import javax.net.ssl.SSLEngine; import java.io.ByteArrayOutputStream; @@ -261,16 +259,22 @@ public ChannelPipeline getPipeline() throws Exception { pipeline.addLast("ssl", new SslHandler(engine)); } - if (logCommunication) { - pipeline.addLast("logger", new LoggingHandler("nettyclient", InternalLogLevel.DEBUG, true)); - } +// if (logCommunication) { +// pipeline.addLast("logger", new LoggingHandler("nettyclient", InternalLogLevel.DEBUG, true)); +// } + + pipeline.addLast("rawconsolelogger", new ConsoleNettyLogger(false)); + + // unfortunately supports only response compression + // pipeline.addLast("inflater", new HttpContentCompressor()); pipeline.addLast("codec", new HttpClientCodec()); + pipeline.addLast("deflater", new HttpContentDecompressor()); + pipeline.addLast("aggregator", new HttpChunkAggregator(102467890)); - // Remove the following line if you don't want automatic content decompression. - pipeline.addLast("deflater", new HttpContentDecompressor()); + pipeline.addLast("httpconsolelogger", new ConsoleNettyLogger(true)); pipeline.addLast("handler", handler); return pipeline; From 6317471e00b2aaa3138a28973db78dfc4d4bbec6 Mon Sep 17 00:00:00 2001 From: xardas Date: Tue, 5 Jan 2016 22:23:18 +0300 Subject: [PATCH 13/35] add conditions --- .../java/com/xplenty/api/http/SyncNettyClient.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java index ccbac8b..90a49a8 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java @@ -259,11 +259,9 @@ public ChannelPipeline getPipeline() throws Exception { pipeline.addLast("ssl", new SslHandler(engine)); } -// if (logCommunication) { -// pipeline.addLast("logger", new LoggingHandler("nettyclient", InternalLogLevel.DEBUG, true)); -// } - - pipeline.addLast("rawconsolelogger", new ConsoleNettyLogger(false)); + if (logCommunication) { + pipeline.addLast("rawconsolelogger", new ConsoleNettyLogger(false)); + } // unfortunately supports only response compression // pipeline.addLast("inflater", new HttpContentCompressor()); @@ -274,7 +272,9 @@ public ChannelPipeline getPipeline() throws Exception { pipeline.addLast("aggregator", new HttpChunkAggregator(102467890)); - pipeline.addLast("httpconsolelogger", new ConsoleNettyLogger(true)); + if (logCommunication) { + pipeline.addLast("httpconsolelogger", new ConsoleNettyLogger(true)); + } pipeline.addLast("handler", handler); return pipeline; From 271ef06448aed0c1ec4d3a8a479b3122d60d990b Mon Sep 17 00:00:00 2001 From: xardas Date: Wed, 6 Jan 2016 21:33:39 +0300 Subject: [PATCH 14/35] improve request structure --- .../main/java/com/xplenty/api/XplentyAPI.java | 16 +++++++++++++--- .../api/request/{ => cluster}/ClusterInfo.java | 3 ++- .../api/request/{ => cluster}/CreateCluster.java | 3 ++- .../api/request/{ => cluster}/ListClusters.java | 3 ++- .../request/{ => cluster}/TerminateCluster.java | 3 ++- .../api/request/{ => cluster}/UpdateCluster.java | 3 ++- .../xplenty/api/request/{ => job}/JobInfo.java | 3 ++- .../xplenty/api/request/{ => job}/ListJobs.java | 3 ++- .../xplenty/api/request/{ => job}/RunJob.java | 3 ++- .../xplenty/api/request/{ => job}/StopJob.java | 3 ++- .../request/{ => schedule}/CloneSchedule.java | 3 ++- .../request/{ => schedule}/CreateSchedule.java | 3 ++- .../request/{ => schedule}/DeleteSchedule.java | 3 ++- .../request/{ => schedule}/ListSchedules.java | 3 ++- .../api/request/{ => schedule}/ScheduleInfo.java | 3 ++- .../request/{ => schedule}/UpdateSchedule.java | 3 ++- .../api/request/{ => user}/CurrentUserInfo.java | 3 ++- .../request/{ => user}/UpdateCurrentUser.java | 3 ++- .../request/{ => webhook}/ListHookEvents.java | 3 ++- .../api/request/{ => xpackage}/ListPackages.java | 3 ++- .../api/request/{ => xpackage}/PackageInfo.java | 4 +++- .../request/{ => cluster}/ClusterInfoTest.java | 3 ++- .../request/{ => cluster}/CreateClusterTest.java | 3 ++- .../request/{ => cluster}/ListClustersTest.java | 3 ++- .../{ => cluster}/TerminateClusterTest.java | 3 ++- .../request/{ => cluster}/UpdateClusterTest.java | 4 +++- .../api/request/{ => job}/JobInfoTest.java | 3 ++- .../api/request/{ => job}/ListJobsTest.java | 3 ++- .../api/request/{ => job}/RunJobTest.java | 3 ++- .../api/request/{ => job}/StopJobTest.java | 3 ++- .../{ => schedule}/CloneScheduleTest.java | 3 ++- .../{ => schedule}/CreateScheduleTest.java | 3 ++- .../{ => schedule}/DeleteScheduleTest.java | 3 ++- .../{ => schedule}/ListSchedulesTest.java | 3 ++- .../request/{ => schedule}/ScheduleInfoTest.java | 3 ++- .../{ => schedule}/UpdateScheduleTest.java | 3 ++- .../request/{ => user}/CurrentUserInfoTest.java | 2 +- .../{ => user}/UpdateCurrentUserTest.java | 2 +- 38 files changed, 87 insertions(+), 40 deletions(-) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => cluster}/ClusterInfo.java (83%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => cluster}/CreateCluster.java (86%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => cluster}/ListClusters.java (93%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => cluster}/TerminateCluster.java (83%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => cluster}/UpdateCluster.java (86%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => job}/JobInfo.java (82%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => job}/ListJobs.java (93%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => job}/RunJob.java (86%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => job}/StopJob.java (82%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => schedule}/CloneSchedule.java (93%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => schedule}/CreateSchedule.java (87%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => schedule}/DeleteSchedule.java (84%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => schedule}/ListSchedules.java (93%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => schedule}/ScheduleInfo.java (84%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => schedule}/UpdateSchedule.java (88%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => user}/CurrentUserInfo.java (91%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => user}/UpdateCurrentUser.java (89%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => webhook}/ListHookEvents.java (92%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => xpackage}/ListPackages.java (91%) rename xplenty.jar-core/src/main/java/com/xplenty/api/request/{ => xpackage}/PackageInfo.java (79%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => cluster}/ClusterInfoTest.java (97%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => cluster}/CreateClusterTest.java (97%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => cluster}/ListClustersTest.java (96%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => cluster}/TerminateClusterTest.java (95%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => cluster}/UpdateClusterTest.java (96%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => job}/JobInfoTest.java (97%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => job}/ListJobsTest.java (96%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => job}/RunJobTest.java (96%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => job}/StopJobTest.java (96%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => schedule}/CloneScheduleTest.java (97%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => schedule}/CreateScheduleTest.java (97%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => schedule}/DeleteScheduleTest.java (97%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => schedule}/ListSchedulesTest.java (97%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => schedule}/ScheduleInfoTest.java (97%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => schedule}/UpdateScheduleTest.java (97%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => user}/CurrentUserInfoTest.java (99%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/{ => user}/UpdateCurrentUserTest.java (98%) diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index a134d74..209f130 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -11,12 +11,22 @@ import com.xplenty.api.model.*; import com.xplenty.api.model.Package; import com.xplenty.api.request.*; +import com.xplenty.api.request.cluster.*; +import com.xplenty.api.request.job.JobInfo; +import com.xplenty.api.request.job.ListJobs; +import com.xplenty.api.request.job.RunJob; +import com.xplenty.api.request.job.StopJob; +import com.xplenty.api.request.schedule.*; +import com.xplenty.api.request.user.CurrentUserInfo; +import com.xplenty.api.request.user.UpdateCurrentUser; import com.xplenty.api.request.watching.AddClusterWatcher; import com.xplenty.api.request.watching.AddJobWatcher; import com.xplenty.api.request.watching.ListWatchers; import com.xplenty.api.request.watching.WatchingStop; import com.xplenty.api.http.Http; import com.xplenty.api.request.webhook.*; +import com.xplenty.api.request.xpackage.ListPackages; +import com.xplenty.api.request.xpackage.PackageInfo; import java.util.List; import java.util.Map; @@ -126,7 +136,7 @@ public List listSchedules(int offset, int limit) { /** * List of schedules associated with the account - * @param props map of request parameters, see {@link com.xplenty.api.Xplenty.ScheduleStatus}, {@link Xplenty.Sort}, {@link Xplenty.SortDirection}, for keys see constants in {@link com.xplenty.api.request.ListSchedules} + * @param props map of request parameters, see {@link com.xplenty.api.Xplenty.ScheduleStatus}, {@link Xplenty.Sort}, {@link Xplenty.SortDirection}, for keys see constants in {@link com.xplenty.api.request.schedule.ListSchedules} * @return list of schedules */ public List listSchedules(Properties props) { @@ -157,7 +167,7 @@ public List listClusters(int offset, int limit) { /** * List of clusters associated with the account - * @param props map of request parameters, see {@link Xplenty.ClusterStatus}, {@link Xplenty.Sort}, {@link Xplenty.SortDirection}, for keys see constants in {@link ListClusters} + * @param props map of request parameters, see {@link Xplenty.ClusterStatus}, {@link Xplenty.Sort}, {@link Xplenty.SortDirection}, for keys see constants in {@link com.xplenty.api.request.cluster.ListClusters} * @return list of clusters */ public List listClusters(Properties props) { @@ -242,7 +252,7 @@ public List listJobs(int offset, int limit) { /** * List of jobs associated with the account - * @param params map of request parameters, see {@link Xplenty.JobStatus}, {@link Xplenty.Sort}, {@link Xplenty.SortDirection}, for keys see constants in {@link ListJobs} + * @param params map of request parameters, see {@link Xplenty.JobStatus}, {@link Xplenty.Sort}, {@link Xplenty.SortDirection}, for keys see constants in {@link com.xplenty.api.request.job.ListJobs} * @return list of jobs */ public List listJobs(Properties params) { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ClusterInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ClusterInfo.java similarity index 83% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/ClusterInfo.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ClusterInfo.java index ff82999..648f2a7 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ClusterInfo.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ClusterInfo.java @@ -1,10 +1,11 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.cluster; import com.xplenty.api.Xplenty; import com.xplenty.api.model.Cluster; +import com.xplenty.api.request.AbstractInfoRequest; /** * diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateCluster.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/CreateCluster.java similarity index 86% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateCluster.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/CreateCluster.java index 3ac588e..1d7c722 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateCluster.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/CreateCluster.java @@ -1,9 +1,10 @@ -package com.xplenty.api.request; +package com.xplenty.api.request.cluster; import com.xplenty.api.Xplenty; import com.xplenty.api.model.Cluster; import com.xplenty.api.http.Http; import com.xplenty.api.http.Http.Method; +import com.xplenty.api.request.AbstractManipulationRequest; public class CreateCluster extends AbstractManipulationRequest { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ListClusters.java similarity index 93% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ListClusters.java index 07b61ae..22349f0 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ListClusters.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.cluster; import com.fasterxml.jackson.core.type.TypeReference; import com.xplenty.api.Xplenty; @@ -9,6 +9,7 @@ import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; +import com.xplenty.api.request.AbstractListRequest; import java.util.List; import java.util.Properties; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/TerminateCluster.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/TerminateCluster.java similarity index 83% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/TerminateCluster.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/TerminateCluster.java index d32876f..fa5ae87 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/TerminateCluster.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/TerminateCluster.java @@ -1,10 +1,11 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.cluster; import com.xplenty.api.Xplenty; import com.xplenty.api.model.Cluster; +import com.xplenty.api.request.AbstractDeleteRequest; /** * @author Yuriy Kovalek diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCluster.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/UpdateCluster.java similarity index 86% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCluster.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/UpdateCluster.java index 99aba1d..2a74ddf 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCluster.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/UpdateCluster.java @@ -1,9 +1,10 @@ -package com.xplenty.api.request; +package com.xplenty.api.request.cluster; import com.xplenty.api.Xplenty; import com.xplenty.api.model.Cluster; import com.xplenty.api.http.Http; import com.xplenty.api.http.Http.Method; +import com.xplenty.api.request.AbstractManipulationRequest; public class UpdateCluster extends AbstractManipulationRequest { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/JobInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobInfo.java similarity index 82% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/JobInfo.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobInfo.java index 9d160b5..dd0434d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/JobInfo.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobInfo.java @@ -1,10 +1,11 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.job; import com.xplenty.api.Xplenty; import com.xplenty.api.model.Job; +import com.xplenty.api.request.AbstractInfoRequest; /** * @author Yuriy Kovalek diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListJobs.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/ListJobs.java similarity index 93% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/ListJobs.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/job/ListJobs.java index 2704b2e..49bafca 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListJobs.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/ListJobs.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.job; import com.fasterxml.jackson.core.type.TypeReference; import com.xplenty.api.Xplenty; @@ -9,6 +9,7 @@ import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; +import com.xplenty.api.request.AbstractListRequest; import java.util.List; import java.util.Properties; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/RunJob.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/RunJob.java similarity index 86% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/RunJob.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/job/RunJob.java index 8e8cf78..ed05a7d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/RunJob.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/RunJob.java @@ -1,12 +1,13 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.job; import com.xplenty.api.Xplenty; import com.xplenty.api.model.Job; import com.xplenty.api.http.Http; import com.xplenty.api.http.Http.Method; +import com.xplenty.api.request.AbstractManipulationRequest; /** * @author Yuriy Kovalek diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/StopJob.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/StopJob.java similarity index 82% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/StopJob.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/job/StopJob.java index f66d420..15b2b81 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/StopJob.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/StopJob.java @@ -1,10 +1,11 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.job; import com.xplenty.api.Xplenty; import com.xplenty.api.model.Job; +import com.xplenty.api.request.AbstractDeleteRequest; /** * @author Yuriy Kovalek diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CloneSchedule.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/CloneSchedule.java similarity index 93% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/CloneSchedule.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/CloneSchedule.java index 1be8f1e..17c5491 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CloneSchedule.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/CloneSchedule.java @@ -1,10 +1,11 @@ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.http.Http; import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; +import com.xplenty.api.request.AbstractRequest; /** * Author: Xardas diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateSchedule.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/CreateSchedule.java similarity index 87% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateSchedule.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/CreateSchedule.java index b17f152..499c8a3 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateSchedule.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/CreateSchedule.java @@ -1,8 +1,9 @@ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; import com.xplenty.api.Xplenty; import com.xplenty.api.model.Schedule; import com.xplenty.api.http.Http; +import com.xplenty.api.request.AbstractManipulationRequest; /** * Author: Xardas diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/DeleteSchedule.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/DeleteSchedule.java similarity index 84% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/DeleteSchedule.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/DeleteSchedule.java index d3eef40..bdb0bd7 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/DeleteSchedule.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/DeleteSchedule.java @@ -1,7 +1,8 @@ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; import com.xplenty.api.Xplenty; import com.xplenty.api.model.Schedule; +import com.xplenty.api.request.AbstractDeleteRequest; /** * Author: Xardas diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListSchedules.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/ListSchedules.java similarity index 93% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/ListSchedules.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/ListSchedules.java index 118391c..0e23597 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListSchedules.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/ListSchedules.java @@ -1,13 +1,14 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; import com.fasterxml.jackson.core.type.TypeReference; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; +import com.xplenty.api.request.AbstractListRequest; import java.util.List; import java.util.Properties; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ScheduleInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/ScheduleInfo.java similarity index 84% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/ScheduleInfo.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/ScheduleInfo.java index dc5102b..bf0770d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ScheduleInfo.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/ScheduleInfo.java @@ -1,7 +1,8 @@ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; import com.xplenty.api.Xplenty; import com.xplenty.api.model.Schedule; +import com.xplenty.api.request.AbstractInfoRequest; /** * Author: Xardas diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateSchedule.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/UpdateSchedule.java similarity index 88% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateSchedule.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/UpdateSchedule.java index d4af024..5f7f7bc 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateSchedule.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/UpdateSchedule.java @@ -1,8 +1,9 @@ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; import com.xplenty.api.Xplenty; import com.xplenty.api.model.Schedule; import com.xplenty.api.http.Http; +import com.xplenty.api.request.AbstractManipulationRequest; /** * Author: Xardas diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CurrentUserInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/CurrentUserInfo.java similarity index 91% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/CurrentUserInfo.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/user/CurrentUserInfo.java index 0d5ebf0..754ee3d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CurrentUserInfo.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/CurrentUserInfo.java @@ -1,7 +1,8 @@ -package com.xplenty.api.request; +package com.xplenty.api.request.user; import com.xplenty.api.Xplenty; import com.xplenty.api.model.User; +import com.xplenty.api.request.AbstractInfoRequest; /** * Get Info for the authenticated user. diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCurrentUser.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/UpdateCurrentUser.java similarity index 89% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCurrentUser.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/user/UpdateCurrentUser.java index e8a70f1..343616d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCurrentUser.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/UpdateCurrentUser.java @@ -1,8 +1,9 @@ -package com.xplenty.api.request; +package com.xplenty.api.request.user; import com.xplenty.api.Xplenty; import com.xplenty.api.http.Http; import com.xplenty.api.model.User; +import com.xplenty.api.request.AbstractManipulationRequest; /** * Author: Xardas diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListHookEvents.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ListHookEvents.java similarity index 92% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/ListHookEvents.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ListHookEvents.java index d332749..831f2c8 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListHookEvents.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ListHookEvents.java @@ -1,10 +1,11 @@ -package com.xplenty.api.request; +package com.xplenty.api.request.webhook; import com.fasterxml.jackson.core.type.TypeReference; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.http.Response; import com.xplenty.api.model.HookEvent; +import com.xplenty.api.request.AbstractListRequest; import java.util.List; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListPackages.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java similarity index 91% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/ListPackages.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java index d2ec6a3..9307447 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListPackages.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java @@ -1,13 +1,14 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.xpackage; import com.fasterxml.jackson.core.type.TypeReference; import com.xplenty.api.Xplenty; import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.http.Response; import com.xplenty.api.model.Package; +import com.xplenty.api.request.AbstractListRequest; import java.util.List; import java.util.Properties; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/PackageInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java similarity index 79% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/PackageInfo.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java index 6c4a722..5322078 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/PackageInfo.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java @@ -1,6 +1,8 @@ -package com.xplenty.api.request; +package com.xplenty.api.request.xpackage; import com.xplenty.api.Xplenty; +import com.xplenty.api.model.*; +import com.xplenty.api.request.AbstractInfoRequest; /** * Author: Xardas diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ClusterInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java similarity index 97% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/ClusterInfoTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java index ef21d30..ce14bd9 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ClusterInfoTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.cluster; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -14,6 +14,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; import com.xplenty.api.model.ClusterTest; +import com.xplenty.api.request.cluster.ClusterInfo; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateClusterTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/CreateClusterTest.java similarity index 97% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateClusterTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/CreateClusterTest.java index af6e5d8..370628c 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateClusterTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/CreateClusterTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.cluster; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -14,6 +14,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; import com.xplenty.api.model.ClusterTest; +import com.xplenty.api.request.cluster.CreateCluster; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListClustersTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ListClustersTest.java similarity index 96% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/ListClustersTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ListClustersTest.java index f84a514..6b6198c 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListClustersTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ListClustersTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.cluster; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,6 +12,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; import com.xplenty.api.model.ClusterTest; +import com.xplenty.api.request.cluster.ListClusters; import junit.framework.TestCase; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/TerminateClusterTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/TerminateClusterTest.java similarity index 95% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/TerminateClusterTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/TerminateClusterTest.java index 58c8122..febf270 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/TerminateClusterTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/TerminateClusterTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.cluster; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,6 +12,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; import com.xplenty.api.model.ClusterTest; +import com.xplenty.api.request.cluster.TerminateCluster; import junit.framework.TestCase; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateClusterTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/UpdateClusterTest.java similarity index 96% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateClusterTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/UpdateClusterTest.java index bf6b6a5..807109f 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateClusterTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/UpdateClusterTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.cluster; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -14,6 +14,8 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; import com.xplenty.api.model.ClusterTest; +import com.xplenty.api.request.cluster.CreateCluster; +import com.xplenty.api.request.cluster.UpdateCluster; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/JobInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobInfoTest.java similarity index 97% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/JobInfoTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobInfoTest.java index b63c243..9e3f669 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/JobInfoTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobInfoTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.job; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -13,6 +13,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; import com.xplenty.api.model.JobTest; +import com.xplenty.api.request.job.JobInfo; import junit.framework.TestCase; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListJobsTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/ListJobsTest.java similarity index 96% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/ListJobsTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/job/ListJobsTest.java index 7eff753..51c69c5 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListJobsTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/ListJobsTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.job; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,6 +12,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; import com.xplenty.api.model.JobTest; +import com.xplenty.api.request.job.ListJobs; import junit.framework.TestCase; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/RunJobTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/RunJobTest.java similarity index 96% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/RunJobTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/job/RunJobTest.java index a587234..ca3df6d 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/RunJobTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/RunJobTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.job; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,6 +12,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; import com.xplenty.api.model.JobTest; +import com.xplenty.api.request.job.RunJob; import junit.framework.TestCase; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/StopJobTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/StopJobTest.java similarity index 96% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/StopJobTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/job/StopJobTest.java index 320118d..6c40b91 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/StopJobTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/StopJobTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.job; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,6 +12,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; import com.xplenty.api.model.JobTest; +import com.xplenty.api.request.job.StopJob; import junit.framework.TestCase; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CloneScheduleTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/CloneScheduleTest.java similarity index 97% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/CloneScheduleTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/CloneScheduleTest.java index 125077b..0fcba14 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CloneScheduleTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/CloneScheduleTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,6 +12,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; import com.xplenty.api.model.ScheduleTest; +import com.xplenty.api.request.schedule.CloneSchedule; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateScheduleTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/CreateScheduleTest.java similarity index 97% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateScheduleTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/CreateScheduleTest.java index 273e6ef..8a2b184 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateScheduleTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/CreateScheduleTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,6 +12,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; import com.xplenty.api.model.ScheduleTest; +import com.xplenty.api.request.schedule.CreateSchedule; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/DeleteScheduleTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/DeleteScheduleTest.java similarity index 97% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/DeleteScheduleTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/DeleteScheduleTest.java index d428884..8af66ca 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/DeleteScheduleTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/DeleteScheduleTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,6 +12,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; import com.xplenty.api.model.ScheduleTest; +import com.xplenty.api.request.schedule.DeleteSchedule; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/ListSchedulesTest.java similarity index 97% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/ListSchedulesTest.java index e9a2c46..7217978 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/ListSchedulesTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,6 +12,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; import com.xplenty.api.model.ScheduleTest; +import com.xplenty.api.request.AbstractListRequest; import junit.framework.TestCase; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ScheduleInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/ScheduleInfoTest.java similarity index 97% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/ScheduleInfoTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/ScheduleInfoTest.java index 323674b..6ca9429 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ScheduleInfoTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/ScheduleInfoTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,6 +12,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; import com.xplenty.api.model.ScheduleTest; +import com.xplenty.api.request.schedule.ScheduleInfo; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateScheduleTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/UpdateScheduleTest.java similarity index 97% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateScheduleTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/UpdateScheduleTest.java index 5b0ef19..e11d3da 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateScheduleTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/UpdateScheduleTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,6 +12,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; import com.xplenty.api.model.ScheduleTest; +import com.xplenty.api.request.schedule.UpdateSchedule; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CurrentUserInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/CurrentUserInfoTest.java similarity index 99% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/CurrentUserInfoTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/user/CurrentUserInfoTest.java index 45b9c26..a655d5e 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CurrentUserInfoTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/CurrentUserInfoTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.user; import com.fasterxml.jackson.core.JsonProcessingException; import com.sun.jersey.api.client.ClientResponse.Status; diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateCurrentUserTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/UpdateCurrentUserTest.java similarity index 98% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateCurrentUserTest.java rename to xplenty.jar-core/src/test/java/com/xplenty/api/request/user/UpdateCurrentUserTest.java index 1f5b1b3..a275bd2 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateCurrentUserTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/UpdateCurrentUserTest.java @@ -1,7 +1,7 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.user; import com.fasterxml.jackson.core.JsonProcessingException; import com.sun.jersey.api.client.ClientResponse.Status; From 821f1ed0150dea1c2f283909c181f482378c6a2d Mon Sep 17 00:00:00 2001 From: xardas Date: Wed, 6 Jan 2016 23:19:11 +0300 Subject: [PATCH 15/35] add public key methods --- .../main/java/com/xplenty/api/Xplenty.java | 14 +++- .../main/java/com/xplenty/api/XplentyAPI.java | 73 +++++++++++++++++++ .../java/com/xplenty/api/model/PublicKey.java | 22 ++++-- .../xplenty/api/model/WebHookSettings.java | 10 +++ .../request/public_key/CreatePublicKey.java | 44 +++++++++++ .../request/public_key/DeletePublicKey.java | 34 +++++++++ .../request/public_key/ListPublicKeys.java | 48 ++++++++++++ .../api/request/public_key/PublicKeyInfo.java | 33 +++++++++ .../com/xplenty/api/model/PublicKeyTest.java | 40 ++++++++++ .../com/xplenty/api/model/WebHookTest.java | 1 + .../api/request/webhook/WebHookInfoTest.java | 1 + 11 files changed, 311 insertions(+), 9 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/CreatePublicKey.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/DeletePublicKey.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/ListPublicKeys.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/PublicKeyInfo.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/PublicKeyTest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index c98fea2..cf61c98 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -16,7 +16,12 @@ public class Xplenty { public enum Sort { updated("updated"), - created("created"); + created("created"), + // todo include all entities that support name sort + /** + * Note that name sort is available only for {@link com.xplenty.api.model.PublicKey Public Key List} + */ + name("name"); public final String value; @@ -28,6 +33,7 @@ public String toString() { return value; } } + public enum SortDirection { ascending("asc"), @@ -252,7 +258,11 @@ public static enum Resource { PingWebHook("hooks/web/%s/ping", "Ping(fire test notification) for Web hook"), WebHookResetSalt("hooks/web/%s/reset_salt", "Reset Web hook's salt"), WebHook("hooks/web/%s", "Get Web hook information"), - WebHooks("hooks/web", "List Web hooks") + WebHooks("hooks/web", "List Web hooks"), + CreatePublicKey("user/keys", "Create Public Key"), + PublicKey("user/keys/%s", "Get Public Key information"), + PublicKeys("user/keys", "List Public Keys"), + DeletePublicKey("user/keys/%s", "Delete Public Key") ; public final String value; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index 209f130..29eca90 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -16,6 +16,10 @@ import com.xplenty.api.request.job.ListJobs; import com.xplenty.api.request.job.RunJob; import com.xplenty.api.request.job.StopJob; +import com.xplenty.api.request.public_key.CreatePublicKey; +import com.xplenty.api.request.public_key.DeletePublicKey; +import com.xplenty.api.request.public_key.ListPublicKeys; +import com.xplenty.api.request.public_key.PublicKeyInfo; import com.xplenty.api.request.schedule.*; import com.xplenty.api.request.user.CurrentUserInfo; import com.xplenty.api.request.user.UpdateCurrentUser; @@ -523,16 +527,85 @@ public String webHookResetSalt(long webHookId) { return client.execute(new WebHookResetSalt(webHookId)); } + /** + * Ping (fire test notification) to url configured in specified web hook + * @param webHookId id of the webhook to ping + * @return web hook object + */ public WebHook pingWebHook(long webHookId) { checkId(webHookId); return client.execute(new PingWebHook(webHookId)); } + /** + * Get web hook information + * @param webHookId id of the webhook to get info for + * @return web hook object + */ public WebHook getWebHookInfo(long webHookId) { checkId(webHookId); return client.execute(new WebHookInfo(webHookId)); } + /** + * Create (add) new public key + * @param name name to distinguish public keys from each other + * @param publicKey SSH Public key which contains information about type of encryption at the beginning of string, like: 'ssh-rsa'. + * @return newly created public key object + */ + public PublicKey createPublicKey(String name, String publicKey) { + return client.execute(new CreatePublicKey(name, publicKey)); + } + + /** + * List public keys associated with the account + * @return list of public keys + */ + public List listPublicKeys() { + return listPublicKeys(new Properties()); + } + + /** + * List public keys associated with the account + * @param offset number of record to start results from + * @param limit number of results + * @return list of public keys + */ + public List listPublicKeys(int offset, int limit) { + final Properties props = new Properties(); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); + return listPublicKeys(props); + } + + /** + * List public keys associated with the account + * @param params map of request parameters, see {@link Xplenty.Sort}, {@link Xplenty.SortDirection}. + * @return list of public keys + */ + public List listPublicKeys(Properties params) { + return client.execute(new ListPublicKeys(params)); + } + + /** + * Deletes public key with specified id + * @param publicKeyId id of public key to delete + * @return deleted public key object + */ + public PublicKey deletePublicKey(long publicKeyId) { + return client.execute(new DeletePublicKey(publicKeyId)); + } + + /** + * Get public key information + * @param publicKeyId id of public key to get + * @return public key object + */ + public PublicKey getPublicKeyInfo(long publicKeyId) { + return client.execute(new PublicKeyInfo(publicKeyId)); + } + + private void checkId(long id) { if (id == 0) { throw new XplentyAPIException("No Id specified!"); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PublicKey.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PublicKey.java index 9163e1f..b0e7a6e 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PublicKey.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PublicKey.java @@ -14,24 +14,32 @@ public class PublicKey extends XplentyObject { @JsonProperty - private Long id; + protected Long id; @JsonProperty - private String name; + protected String name; @JsonProperty - private String comment; + protected String comment; @JsonProperty - private String fingerprint; + protected String fingerprint; @JsonProperty("created_at") - private Date createdAt; + protected Date createdAt; @JsonProperty("updated_at") - private Date updatedAt; + protected Date updatedAt; @JsonProperty - private String url; + protected String url; + @JsonProperty("public_key") + private String publicKey; protected PublicKey() { super(PublicKey.class); } + public PublicKey(String name, String publicKey) { + super(PublicKey.class); + this.publicKey = publicKey; + this.name = name; + } + /** * * @return the key's numeric identifier diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookSettings.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookSettings.java index 3d243a8..6edf505 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookSettings.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookSettings.java @@ -17,6 +17,8 @@ public class WebHookSettings { private Boolean basicAuth; @JsonProperty("basic_auth_data") private String basicAuthData; + @JsonProperty("encrypted_basic_auth_data") + protected String encryptedBasicAuthData; public WebHookSettings() { } @@ -60,6 +62,14 @@ public String getBasicAuthData() { return basicAuthData; } + /** + * + * @return server encrypted data needed for basic authentication + */ + public String getEncryptedBasicAuthData() { + return encryptedBasicAuthData; + } + /** * * @param url URL of the target server diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/CreatePublicKey.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/CreatePublicKey.java new file mode 100644 index 0000000..b08a28a --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/CreatePublicKey.java @@ -0,0 +1,44 @@ +package com.xplenty.api.request.public_key; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.PublicKey; +import com.xplenty.api.request.AbstractManipulationRequest; + +/** + * Create new public key + * Author: Xardas + * Date: 06.01.16 + * Time: 20:49 + */ +public class CreatePublicKey extends AbstractManipulationRequest { + + public CreatePublicKey(String name, String publicKey) { + super(new PublicKey(name, publicKey)); + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.CreatePublicKey.value; + } + + @Override + public String getName() { + return Xplenty.Resource.CreatePublicKey.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.POST; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/DeletePublicKey.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/DeletePublicKey.java new file mode 100644 index 0000000..5841201 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/DeletePublicKey.java @@ -0,0 +1,34 @@ +package com.xplenty.api.request.public_key; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.PublicKey; +import com.xplenty.api.request.AbstractDeleteRequest; + +/** + * Delete public key + * Author: Xardas + * Date: 06.01.16 + * Time: 21:25 + */ +public class DeletePublicKey extends AbstractDeleteRequest { + + public DeletePublicKey(long entityId) { + super(entityId); + } + + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.DeletePublicKey.format(String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.DeletePublicKey.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/ListPublicKeys.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/ListPublicKeys.java new file mode 100644 index 0000000..e634df8 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/ListPublicKeys.java @@ -0,0 +1,48 @@ +package com.xplenty.api.request.public_key; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.PublicKey; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; +import java.util.Properties; + +/** + * List public keys + * Author: Xardas + * Date: 06.01.16 + * Time: 21:07 + */ +public class ListPublicKeys extends AbstractListRequest> { + + public ListPublicKeys(Properties parameters) { + super(parameters, true); + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpointRoot()); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.PublicKeys.value; + } + + @Override + public String getName() { + return Xplenty.Resource.PublicKeys.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/PublicKeyInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/PublicKeyInfo.java new file mode 100644 index 0000000..1e75cb0 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/PublicKeyInfo.java @@ -0,0 +1,33 @@ +package com.xplenty.api.request.public_key; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.PublicKey; +import com.xplenty.api.request.AbstractInfoRequest; + +/** + * Get public key information + * Author: Xardas + * Date: 06.01.16 + * Time: 21:59 + */ +public class PublicKeyInfo extends AbstractInfoRequest { + + public PublicKeyInfo(long entityId) { + super(entityId); + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.PublicKey.format(String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.PublicKey.name; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/PublicKeyTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/PublicKeyTest.java new file mode 100644 index 0000000..ef69981 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/PublicKeyTest.java @@ -0,0 +1,40 @@ +package com.xplenty.api.model; + +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.Date; + +/** + * Author: Xardas + * Date: 06.01.16 + * Time: 22:07 + */ +public class PublicKeyTest extends TestCase { + @Test + public void testBuilder() { + final Date now = new Date(); + PublicKey publicKey = createMockPublicKey(now); + assertNotNull(publicKey); + assertEquals(now.getTime(), publicKey.getCreatedAt().getTime()); + publicKey = createMockPublicKeyForCreation(); + assertNotNull(publicKey); + } + + public static PublicKey createMockPublicKeyForCreation() { + PublicKey pk = new PublicKey("test", "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAA...AAA xardazz@github.com"); + return pk; + } + + public static PublicKey createMockPublicKey(Date now) { + PublicKey pk = new PublicKey(); + pk.id = 666L; + pk.comment = "xardazz@github.com"; + pk.name = "test"; + pk.fingerprint = "ff:ff:ff:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:ff:ff:ff"; + pk.createdAt = now; + pk.updatedAt = now; + pk.url = "https://testapi.xplenty.com/user/keys/666"; + return pk; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/WebHookTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/WebHookTest.java index 34f173d..29416fe 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/model/WebHookTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/WebHookTest.java @@ -29,6 +29,7 @@ public static WebHook createMockWebHook(Date now) { webHook.active = true; webHook.salt = "000abcdead"; WebHookSettings whs = new WebHookSettings("http://localhost/test", true, false, "somedata"); + whs.encryptedBasicAuthData = "wtjnIcvVp1fLC2fy9rAsSQ==\\n"; webHook.settings = whs; List events = new ArrayList<>(); WebHookEvent whe = new WebHookEvent(); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookInfoTest.java index 47f581b..393869f 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookInfoTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookInfoTest.java @@ -63,6 +63,7 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor assertEquals("http://localhost/test", settings.getUrl()); assertEquals(false, settings.getBasicAuth().booleanValue()); assertEquals(true, settings.getInsecureSSL().booleanValue()); + assertEquals("wtjnIcvVp1fLC2fy9rAsSQ==\\n", settings.getEncryptedBasicAuthData()); final WebHookEvent event = c.getEvents().get(0); // we've got custom json serializer that removes everything except name assertNull(event.getId()); From c1fec7d0c0c4be88796d1bec2acba4bd0ecdf826 Mon Sep 17 00:00:00 2001 From: xardas Date: Thu, 7 Jan 2016 16:30:37 +0300 Subject: [PATCH 16/35] add tests --- .../public_key/CreatePublicKeyTest.java | 91 ++++++++++++++++ .../public_key/DeletePublicKeyTest.java | 88 +++++++++++++++ .../public_key/ListPublicKeysTest.java | 102 ++++++++++++++++++ .../request/public_key/PublicKeyInfoTest.java | 88 +++++++++++++++ 4 files changed, 369 insertions(+) create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/CreatePublicKeyTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/DeletePublicKeyTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/ListPublicKeysTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/PublicKeyInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/CreatePublicKeyTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/CreatePublicKeyTest.java new file mode 100644 index 0000000..58d880a --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/CreatePublicKeyTest.java @@ -0,0 +1,91 @@ +/** + * + */ +package com.xplenty.api.request.public_key; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.PublicKey; +import com.xplenty.api.model.PublicKeyTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class CreatePublicKeyTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + CreatePublicKey cc = new CreatePublicKey("test", "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAA...AAA xardazz@github.com"); + assertEquals(Xplenty.Resource.CreatePublicKey.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.CreatePublicKey.name, cc.getName()); + assertEquals(Http.Method.POST, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertTrue(cc.hasBody()); + assertNotNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + PublicKey c = PublicKeyTest.createMockPublicKey(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(PublicKeyTest.createMockPublicKeyForCreation()); + assertNotNull(json); + + json = JsonMapperFactory.getInstance().writeValueAsString(c); + + CreatePublicKey cc = new CreatePublicKey("test", "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAA...AAA xardazz@github.com"); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("xardazz@github.com", c.getComment()); + assertEquals("ff:ff:ff:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:ff:ff:ff", c.getFingerprint()); + assertEquals("test", c.getName()); + assertEquals("https://testapi.xplenty.com/user/keys/666", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + PublicKey c = PublicKeyTest.createMockPublicKey(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + CreatePublicKey cc = new CreatePublicKey("test", "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAA...AAA xardazz@github.com"); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.CreatePublicKey.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/DeletePublicKeyTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/DeletePublicKeyTest.java new file mode 100644 index 0000000..99bbd02 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/DeletePublicKeyTest.java @@ -0,0 +1,88 @@ +/** + * + */ +package com.xplenty.api.request.public_key; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.PublicKey; +import com.xplenty.api.model.PublicKeyTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class DeletePublicKeyTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + DeletePublicKey cc = new DeletePublicKey(666); + assertEquals(Xplenty.Resource.DeletePublicKey.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.DeletePublicKey.name, cc.getName()); + assertEquals(Http.Method.DELETE, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + PublicKey c = PublicKeyTest.createMockPublicKey(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + DeletePublicKey cc = new DeletePublicKey(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("xardazz@github.com", c.getComment()); + assertEquals("ff:ff:ff:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:ff:ff:ff", c.getFingerprint()); + assertEquals("test", c.getName()); + assertEquals("https://testapi.xplenty.com/user/keys/666", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + PublicKey c = PublicKeyTest.createMockPublicKey(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + DeletePublicKey cc = new DeletePublicKey(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.DeletePublicKey.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/ListPublicKeysTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/ListPublicKeysTest.java new file mode 100644 index 0000000..1627853 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/ListPublicKeysTest.java @@ -0,0 +1,102 @@ +/** + * + */ +package com.xplenty.api.request.public_key; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.PublicKey; +import com.xplenty.api.model.PublicKeyTest; +import com.xplenty.api.request.AbstractListRequest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; + +/** + * @author xardas + * + */ +public class ListPublicKeysTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Properties props = new Properties(); + ListPublicKeys cc = new ListPublicKeys(props); + assertEquals(Xplenty.Resource.PublicKeys.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.PublicKeys.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + + props.put(AbstractListRequest.PARAMETER_SORT, Xplenty.Sort.name); + props.put(AbstractListRequest.PARAMETER_DIRECTION, Xplenty.SortDirection.descending); + cc = new ListPublicKeys(props); + assertEquals(Xplenty.Resource.PublicKeys.value + "?" + AbstractListRequest.PARAMETER_SORT + "=name&" + + AbstractListRequest.PARAMETER_DIRECTION + "=desc", cc.getEndpoint()); + + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + + List publicKeys = new ArrayList<>(); + publicKeys.add(PublicKeyTest.createMockPublicKey(now)); + + String json = JsonMapperFactory.getInstance().writeValueAsString(publicKeys); + + ListPublicKeys cc = new ListPublicKeys(new Properties()); + publicKeys = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(publicKeys); + assertTrue(publicKeys.size() > 0); + + PublicKey c = publicKeys.get(0); + assertEquals(new Long(666), c.getId()); + assertEquals("xardazz@github.com", c.getComment()); + assertEquals("ff:ff:ff:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:ff:ff:ff", c.getFingerprint()); + assertEquals("test", c.getName()); + assertEquals("https://testapi.xplenty.com/user/keys/666", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + List publicKeys = new ArrayList<>(); + publicKeys.add(PublicKeyTest.createMockPublicKey(now)); + String json = JsonMapperFactory.getInstance().writeValueAsString(publicKeys).replace("{", "["); + + try { + ListPublicKeys cc = new ListPublicKeys(new Properties()); + publicKeys = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.PublicKeys.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/PublicKeyInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/PublicKeyInfoTest.java new file mode 100644 index 0000000..284cedf --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/PublicKeyInfoTest.java @@ -0,0 +1,88 @@ +/** + * + */ +package com.xplenty.api.request.public_key; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.PublicKey; +import com.xplenty.api.model.PublicKeyTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class PublicKeyInfoTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + PublicKeyInfo cc = new PublicKeyInfo(666); + assertEquals(Xplenty.Resource.PublicKey.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.PublicKey.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + PublicKey c = PublicKeyTest.createMockPublicKey(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + PublicKeyInfo cc = new PublicKeyInfo(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("xardazz@github.com", c.getComment()); + assertEquals("ff:ff:ff:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:ff:ff:ff", c.getFingerprint()); + assertEquals("test", c.getName()); + assertEquals("https://testapi.xplenty.com/user/keys/666", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + PublicKey c = PublicKeyTest.createMockPublicKey(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + PublicKeyInfo cc = new PublicKeyInfo(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.PublicKey.name + ": error parsing response object", e.getMessage()); + } + + } +} From 282943277c9bf0d53b03b8b22d4b70027056dccb Mon Sep 17 00:00:00 2001 From: xardas Date: Thu, 7 Jan 2016 18:46:52 +0300 Subject: [PATCH 17/35] add member methods add tests --- .../main/java/com/xplenty/api/Xplenty.java | 24 +++- .../main/java/com/xplenty/api/XplentyAPI.java | 78 ++++++++++++ .../java/com/xplenty/api/model/Member.java | 40 ++++--- .../api/request/AbstractListRequest.java | 34 ++++-- .../api/request/member/CreateMember.java | 40 +++++++ .../api/request/member/DeleteMember.java | 28 +++++ .../api/request/member/ListMembers.java | 55 +++++++++ .../api/request/member/MemberInfo.java | 28 +++++ .../api/request/member/SetMemberRole.java | 39 ++++++ .../com/xplenty/api/model/MemberTest.java | 48 ++++++++ .../api/request/member/CreateMemberTest.java | 96 +++++++++++++++ .../api/request/member/DeleteMemberTest.java | 93 ++++++++++++++ .../api/request/member/MemberInfoTest.java | 93 ++++++++++++++ .../api/request/member/MemberListTest.java | 113 ++++++++++++++++++ .../api/request/member/SetMemberRoleTest.java | 93 ++++++++++++++ 15 files changed, 877 insertions(+), 25 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/member/CreateMember.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/member/DeleteMember.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/member/ListMembers.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/member/MemberInfo.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/member/SetMemberRole.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/MemberTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/member/CreateMemberTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/member/DeleteMemberTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/member/MemberInfoTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/member/MemberListTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/member/SetMemberRoleTest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index cf61c98..d56815d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -19,9 +19,18 @@ public enum Sort { created("created"), // todo include all entities that support name sort /** - * Note that name sort is available only for {@link com.xplenty.api.model.PublicKey Public Key List} + * Note that name sort is available only for {@link com.xplenty.api.model.PublicKey Public Key List}, + * {@link com.xplenty.api.model.Member Member List} */ - name("name"); + name("name"), + /** + * Note that email sort is available only for {@link com.xplenty.api.model.Member Memeber List} + */ + email("email"), + /** + * used only for filtering meaning all the statuses above + */ + all("all"); public final String value; @@ -115,6 +124,10 @@ public enum ClusterType { } public enum AccountRole { + /** + * only current owner can transfer ownership to another member + */ + owner("owner"), admin("admin"), member("member"); @@ -262,7 +275,12 @@ public static enum Resource { CreatePublicKey("user/keys", "Create Public Key"), PublicKey("user/keys/%s", "Get Public Key information"), PublicKeys("user/keys", "List Public Keys"), - DeletePublicKey("user/keys/%s", "Delete Public Key") + DeletePublicKey("user/keys/%s", "Delete Public Key"), + CreateMember("members", "Create new Member"), + DeleteMember("members/%s", "Delete member"), + Member("members/%s", "Get member information"), + Members("members", "List account members"), + SetMemberRole("members/%s", "Change member role") ; public final String value; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index 29eca90..d823664 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -16,6 +16,7 @@ import com.xplenty.api.request.job.ListJobs; import com.xplenty.api.request.job.RunJob; import com.xplenty.api.request.job.StopJob; +import com.xplenty.api.request.member.*; import com.xplenty.api.request.public_key.CreatePublicKey; import com.xplenty.api.request.public_key.DeletePublicKey; import com.xplenty.api.request.public_key.ListPublicKeys; @@ -593,6 +594,7 @@ public List listPublicKeys(Properties params) { * @return deleted public key object */ public PublicKey deletePublicKey(long publicKeyId) { + checkId(publicKeyId); return client.execute(new DeletePublicKey(publicKeyId)); } @@ -602,9 +604,85 @@ public PublicKey deletePublicKey(long publicKeyId) { * @return public key object */ public PublicKey getPublicKeyInfo(long publicKeyId) { + checkId(publicKeyId); return client.execute(new PublicKeyInfo(publicKeyId)); } + /** + * Creates a new member on an account. The call sends an invitation to join Xplenty in case the user is not yet a user of Xplenty. + * @param email email of the member to add + * @param role role of the member + * @return newly created member object + */ + public Member createMember(String email, Xplenty.AccountRole role) { + return client.execute(new CreateMember(email, role)); + } + + + /** + * List existing account members. Optionally, you can supply the input parameters to filter the member list so that + * it contains user with specific role or email only and to determine the order by which the list will be sorted. + * @return list of account members + */ + public List listMembers() { + return listMembers(new Properties()); + } + + /** + * List existing account members. Optionally, you can supply the input parameters to filter the member list so that + * it contains user with specific role or email only and to determine the order by which the list will be sorted. + * @param offset number of record to start results from + * @param limit number of results + * @return list of account members + */ + public List listMembers(int offset, int limit) { + final Properties props = new Properties(); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); + return listMembers(props); + } + + /** + * List existing account members. Optionally, you can supply the input parameters to filter the member list so that + * it contains user with specific role or email only and to determine the order by which the list will be sorted. + * @param params map of request parameters, see {@link Xplenty.Sort}, {@link Xplenty.SortDirection}. + * @return list of account members + */ + public List listMembers(Properties params) { + return client.execute(new ListMembers(params)); + } + + /** + * Set existing account member's role + * @param memberId id of member to set role for + * @param role new role for member + * @return updated member object + */ + public Member setMemberRole(long memberId, Xplenty.AccountRole role) { + checkId(memberId); + return client.execute(new SetMemberRole(memberId, role)); + } + + /** + * Information about member of the account. + * @param memberId id of member to get + * @return member object + */ + public Member getMemberInfo(long memberId) { + checkId(memberId); + return client.execute(new MemberInfo(memberId)); + } + + /** + * Delete an existing member from an account. This call does not delete the user, just the account membership. + * @param memberId id of member to delete + * @return Deleted member object + */ + public Member deleteMember(long memberId) { + checkId(memberId); + return client.execute(new DeleteMember(memberId)); + } + private void checkId(long id) { if (id == 0) { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java index eccc265..045d87d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java @@ -14,38 +14,50 @@ */ public class Member extends XplentyObject { @JsonProperty - private Long id; + protected Long id; @JsonProperty - private String name; + protected String name; @JsonProperty - private String email; + protected String email; @JsonProperty("gravatar_email") - private String gravatarEmail; + protected String gravatarEmail; @JsonProperty("avatar_url") - private String avatarUrl; + protected String avatarUrl; @JsonProperty("created_at") - private Date createdAt; + protected Date createdAt; @JsonProperty("updated_at") - private Date updatedAt; + protected Date updatedAt; @JsonProperty - private String location; + protected String location; @JsonProperty - private Boolean confirmed; + protected Boolean confirmed; @JsonProperty("confirmed_at") - private Date confirmedAt; + protected Date confirmedAt; @JsonProperty - private Xplenty.AccountRole role; + protected Xplenty.AccountRole role; @JsonProperty - private Boolean owner; + protected Boolean owner; @JsonProperty - private String url; + protected String url; @JsonProperty("html_url") - private String htmlUrl; + protected String htmlUrl; protected Member() { super(Member.class); } + public Member(String email, Xplenty.AccountRole role) { + super(Member.class); + this.email = email; + this.role = role; + } + + public Member(Long id, Xplenty.AccountRole role) { + super(Member.class); + this.id = id; + this.role = role; + } + /** * * @return the users's numeric identifier diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractListRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractListRequest.java index 0b349cc..52724d1 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractListRequest.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractListRequest.java @@ -4,6 +4,9 @@ import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.http.Http; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.Properties; /** @@ -12,12 +15,15 @@ * Time: 20:46 */ public abstract class AbstractListRequest extends AbstractRequest { + public static final String PARAMETER_STATUS = "status"; + public static final String PARAMETER_SINCE = "since"; public static final String PARAMETER_SORT = "sort"; public static final String PARAMETER_DIRECTION = "direction"; public static final String PARAMETER_OFFSET = "offset"; public static final String PARAMETER_LIMIT = "limit"; + protected final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); protected Properties parameters; protected AbstractListRequest(Properties parameters, boolean validateSort) { @@ -34,33 +40,38 @@ private void validateParameters(Properties params, boolean validateSort) { if (validateSort) { if (params.containsKey(PARAMETER_SORT) && !(params.get(PARAMETER_SORT) instanceof Xplenty.Sort)) { - throw new XplentyAPIException("Invalid 'sort' parameter"); + throw new XplentyAPIException(String.format("Invalid '%s' parameter, should be Sort", PARAMETER_SORT)); } if (!params.containsKey(PARAMETER_SORT) && params.containsKey(PARAMETER_DIRECTION)) { - throw new XplentyAPIException("Missing the 'sort' parameter"); + throw new XplentyAPIException(String.format("Missing the '%s' parameter", PARAMETER_SORT)); } if (params.containsKey(PARAMETER_DIRECTION) && !(params.get(PARAMETER_DIRECTION) instanceof Xplenty.SortDirection)) { - throw new XplentyAPIException("Invalid 'direction' parameter"); + throw new XplentyAPIException(String.format("Invalid '%s' parameter, should be SortDirection", PARAMETER_DIRECTION)); } } + if (params.contains(PARAMETER_SINCE) && !(params.get(PARAMETER_SINCE) instanceof Date)) { + throw new XplentyAPIException(String.format("Invalid '%s' parameter, should be date", PARAMETER_SINCE)); + } + if (params.containsKey(PARAMETER_LIMIT) && !(params.get(PARAMETER_LIMIT) instanceof Number)) { - throw new XplentyAPIException("Invalid 'limit' parameter"); + throw new XplentyAPIException(String.format("Invalid '%s' parameter, should be number", PARAMETER_LIMIT)); } else if (params.containsKey(PARAMETER_LIMIT) && ((((Number) params.get(PARAMETER_LIMIT)).intValue() > Xplenty.MAX_LIMIT) || ((Number) params.get(PARAMETER_LIMIT)).intValue() < 0)) { // we've already checked if it's number, so it's safe to cast - throw new XplentyAPIException(String.format("'limit' parameter should be less or equal to %s and greater than 0", Xplenty.MAX_LIMIT)); + throw new XplentyAPIException(String.format("'%s' parameter should be less or equal to %s and greater than 0", + PARAMETER_LIMIT, Xplenty.MAX_LIMIT)); } if (params.containsKey(PARAMETER_OFFSET) && !(params.get(PARAMETER_OFFSET) instanceof Number)) { - throw new XplentyAPIException("Invalid 'offset' parameter"); + throw new XplentyAPIException(String.format("Invalid '%s' parameter, should be number", PARAMETER_OFFSET)); } else if (params.containsKey(PARAMETER_OFFSET) && ((Number) params.get(PARAMETER_OFFSET)).intValue() < 0) { // we've already checked if it's number, so it's safe to cast - throw new XplentyAPIException("'offset' parameter should be greater than 0"); + throw new XplentyAPIException(String.format("'%s' parameter should be greater than 0", PARAMETER_OFFSET)); } } @@ -71,7 +82,14 @@ public String getEndpoint() { } StringBuilder params = new StringBuilder("?"); for (Object var: parameters.keySet()) { - params.append(var).append("=").append(parameters.get(var).toString()).append("&"); + params.append(var).append("="); + final Object param = parameters.get(var); + if (param instanceof Date) { + params.append(dateFormat.format(param)); + } else { + params.append(param.toString()); + } + params.append("&"); } if (params.length() > 1) { params.setLength(params.length() - 1); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/CreateMember.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/CreateMember.java new file mode 100644 index 0000000..51922c9 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/CreateMember.java @@ -0,0 +1,40 @@ +package com.xplenty.api.request.member; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.Member; +import com.xplenty.api.request.AbstractManipulationRequest; + +/** + * Creates a new member on an account. The call sends an invitation to join Xplenty in case the user is not yet a user of Xplenty. + * Author: Xardas + * Date: 07.01.16 + * Time: 15:38 + */ +public class CreateMember extends AbstractManipulationRequest { + + + public CreateMember(String email, Xplenty.AccountRole role) { + super(new Member(email, role)); + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.CreateMember.value; + } + + @Override + public String getName() { + return Xplenty.Resource.CreateMember.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.POST; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/DeleteMember.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/DeleteMember.java new file mode 100644 index 0000000..c632ad2 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/DeleteMember.java @@ -0,0 +1,28 @@ +package com.xplenty.api.request.member; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.Member; +import com.xplenty.api.request.AbstractDeleteRequest; + +/** + * Delete an existing member from an account. This call does not delete the user, just the account membership. + * Author: Xardas + * Date: 07.01.16 + * Time: 16:48 + */ +public class DeleteMember extends AbstractDeleteRequest { + + public DeleteMember(long entityId) { + super(entityId); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.DeleteMember.format(String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.DeleteMember.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/ListMembers.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/ListMembers.java new file mode 100644 index 0000000..b841571 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/ListMembers.java @@ -0,0 +1,55 @@ +package com.xplenty.api.request.member; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Member; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; +import java.util.Properties; + +/** + * List existing account members. Optionally, you can supply the input parameters to filter the member list so that + * it contains user with specific role or email only and to determine the order by which the list will be sorted. + * Author: Xardas + * Date: 07.01.16 + * Time: 15:58 + */ +public class ListMembers extends AbstractListRequest> { + public static final String PARAMETER_ROLE = "role"; + public static final String PARAMETER_EMAIL = "email"; + + public ListMembers(Properties parameters) { + super(parameters, true); + validateParameters(parameters); + } + + + private void validateParameters(Properties params) { + if (params.containsKey(PARAMETER_ROLE) + && !(params.get(PARAMETER_ROLE) instanceof Xplenty.AccountRole)) { + throw new XplentyAPIException(String.format("Invalid %s parameter", PARAMETER_ROLE)); + } + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.Members.value; + } + + @Override + public String getName() { + return Xplenty.Resource.Members.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/MemberInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/MemberInfo.java new file mode 100644 index 0000000..707bc86 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/MemberInfo.java @@ -0,0 +1,28 @@ +package com.xplenty.api.request.member; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.Member; +import com.xplenty.api.request.AbstractInfoRequest; + +/** + * Information about member of the account. + * Author: Xardas + * Date: 07.01.16 + * Time: 16:43 + */ +public class MemberInfo extends AbstractInfoRequest { + + public MemberInfo(long entityId) { + super(entityId); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.Member.format(String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.Member.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/SetMemberRole.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/SetMemberRole.java new file mode 100644 index 0000000..12f69fe --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/SetMemberRole.java @@ -0,0 +1,39 @@ +package com.xplenty.api.request.member; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.Member; +import com.xplenty.api.request.AbstractManipulationRequest; + +/** + * Set existing account member's role + * Author: Xardas + * Date: 07.01.16 + * Time: 16:27 + */ +public class SetMemberRole extends AbstractManipulationRequest { + + public SetMemberRole(long id, Xplenty.AccountRole role) { + super(new Member(id, role)); + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.SetMemberRole.format(entity.getId().toString()); + } + + @Override + public String getName() { + return Xplenty.Resource.SetMemberRole.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.PUT; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/MemberTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/MemberTest.java new file mode 100644 index 0000000..ffa806a --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/MemberTest.java @@ -0,0 +1,48 @@ +package com.xplenty.api.model; + +import com.xplenty.api.Xplenty; +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.Date; + +/** + * Author: Xardas + * Date: 06.01.16 + * Time: 22:07 + */ +public class MemberTest extends TestCase { + @Test + public void testBuilder() { + final Date now = new Date(); + Member member = createMockMember(now); + assertNotNull(member); + assertEquals(now.getTime(), member.getCreatedAt().getTime()); + member = createMockMemberForCreation(); + assertNotNull(member); + } + + public static Member createMockMemberForCreation() { + Member mem = new Member("xardazz@github.com", Xplenty.AccountRole.admin); + return mem; + } + + public static Member createMockMember(Date now) { + Member mem = new Member(); + mem.id = 666L; + mem.email = "xardazz@github.com"; + mem.name = "test"; + mem.createdAt = now; + mem.updatedAt = now; + mem.url = "https://testapi.xplenty.com/api/members/666"; + mem.avatarUrl = "https://secure.gravatar.com/avatar"; + mem.confirmed = true; + mem.confirmedAt = now; + mem.gravatarEmail = "gravatar@gravatar.com"; + mem.location = "Colorado"; + mem.role = Xplenty.AccountRole.admin; + mem.owner = true; + mem.htmlUrl = "https://testapi.xplenty.com/settings/members/666"; + return mem; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/CreateMemberTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/CreateMemberTest.java new file mode 100644 index 0000000..dc6eb78 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/CreateMemberTest.java @@ -0,0 +1,96 @@ +/** + * + */ +package com.xplenty.api.request.member; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Member; +import com.xplenty.api.model.MemberTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class CreateMemberTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + CreateMember cc = new CreateMember("xardazz@github.com", Xplenty.AccountRole.admin); + assertEquals(Xplenty.Resource.CreateMember.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.CreateMember.name, cc.getName()); + assertEquals(Http.Method.POST, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertTrue(cc.hasBody()); + assertNotNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Member c = MemberTest.createMockMember(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(MemberTest.createMockMemberForCreation()); + assertNotNull(json); + + json = JsonMapperFactory.getInstance().writeValueAsString(c); + + CreateMember cc = new CreateMember("xardazz@github.com", Xplenty.AccountRole.admin); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("test", c.getName()); + assertEquals("xardazz@github.com", c.getEmail()); + assertEquals("https://testapi.xplenty.com/settings/members/666", c.getHtmlUrl()); + assertEquals("https://secure.gravatar.com/avatar", c.getAvatarUrl()); + assertTrue(c.getConfirmed()); + assertEquals("Colorado", c.getLocation()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals("https://testapi.xplenty.com/api/members/666", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getConfirmedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Member c = MemberTest.createMockMember(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + CreateMember cc = new CreateMember("xardazz@github.com", Xplenty.AccountRole.admin); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.CreateMember.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/DeleteMemberTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/DeleteMemberTest.java new file mode 100644 index 0000000..b7f52a5 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/DeleteMemberTest.java @@ -0,0 +1,93 @@ +/** + * + */ +package com.xplenty.api.request.member; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Member; +import com.xplenty.api.model.MemberTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class DeleteMemberTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + DeleteMember cc = new DeleteMember(666); + assertEquals(Xplenty.Resource.DeleteMember.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.DeleteMember.name, cc.getName()); + assertEquals(Http.Method.DELETE, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Member c = MemberTest.createMockMember(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + DeleteMember cc = new DeleteMember(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("test", c.getName()); + assertEquals("xardazz@github.com", c.getEmail()); + assertEquals("https://testapi.xplenty.com/settings/members/666", c.getHtmlUrl()); + assertEquals("https://secure.gravatar.com/avatar", c.getAvatarUrl()); + assertTrue(c.getConfirmed()); + assertEquals("Colorado", c.getLocation()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals("https://testapi.xplenty.com/api/members/666", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getConfirmedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Member c = MemberTest.createMockMember(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + DeleteMember cc = new DeleteMember(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.DeleteMember.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/MemberInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/MemberInfoTest.java new file mode 100644 index 0000000..2bd69ea --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/MemberInfoTest.java @@ -0,0 +1,93 @@ +/** + * + */ +package com.xplenty.api.request.member; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Member; +import com.xplenty.api.model.MemberTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class MemberInfoTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + MemberInfo cc = new MemberInfo(666); + assertEquals(Xplenty.Resource.Member.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.Member.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Member c = MemberTest.createMockMember(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + MemberInfo cc = new MemberInfo(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("test", c.getName()); + assertEquals("xardazz@github.com", c.getEmail()); + assertEquals("https://testapi.xplenty.com/settings/members/666", c.getHtmlUrl()); + assertEquals("https://secure.gravatar.com/avatar", c.getAvatarUrl()); + assertTrue(c.getConfirmed()); + assertEquals("Colorado", c.getLocation()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals("https://testapi.xplenty.com/api/members/666", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getConfirmedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Member c = MemberTest.createMockMember(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + MemberInfo cc = new MemberInfo(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Member.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/MemberListTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/MemberListTest.java new file mode 100644 index 0000000..1f0f9c7 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/MemberListTest.java @@ -0,0 +1,113 @@ +/** + * + */ +package com.xplenty.api.request.member; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Member; +import com.xplenty.api.model.MemberTest; +import com.xplenty.api.request.AbstractListRequest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.*; + +/** + * @author xardas + * + */ +public class MemberListTest extends TestCase { + protected final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + Date now = new Date(); + + final Properties props = new Properties(); + ListMembers cc = new ListMembers(props); + assertEquals(Xplenty.Resource.Members.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.Members.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + + props.put(AbstractListRequest.PARAMETER_SORT, Xplenty.Sort.name); + props.put(AbstractListRequest.PARAMETER_DIRECTION, Xplenty.SortDirection.descending); + props.put(AbstractListRequest.PARAMETER_SINCE, now); + cc = new ListMembers(props); + assertEquals(Xplenty.Resource.Members.value + "?" + AbstractListRequest.PARAMETER_SORT + "=name&" + + AbstractListRequest.PARAMETER_DIRECTION + "=desc&" + AbstractListRequest.PARAMETER_SINCE + + "=" +dateFormat.format(now), cc.getEndpoint()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + List memberList = new ArrayList<>(); + Member c = MemberTest.createMockMember(now); + memberList.add(c); + + String json = JsonMapperFactory.getInstance().writeValueAsString(memberList); + + ListMembers cc = new ListMembers(new Properties()); + memberList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(memberList); + assertTrue(memberList.size() > 0); + c = memberList.get(0); + assertEquals(new Long(666), c.getId()); + assertEquals("test", c.getName()); + assertEquals("xardazz@github.com", c.getEmail()); + assertEquals("https://testapi.xplenty.com/settings/members/666", c.getHtmlUrl()); + assertEquals("https://secure.gravatar.com/avatar", c.getAvatarUrl()); + assertTrue(c.getConfirmed()); + assertEquals("Colorado", c.getLocation()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals("https://testapi.xplenty.com/api/members/666", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getConfirmedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + List memberList = new ArrayList<>(); + Member c = MemberTest.createMockMember(now); + memberList.add(c); + + String json = JsonMapperFactory.getInstance().writeValueAsString(memberList).replace("{", "["); + + try { + ListMembers cc = new ListMembers(new Properties()); + memberList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Members.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/SetMemberRoleTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/SetMemberRoleTest.java new file mode 100644 index 0000000..363a17e --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/SetMemberRoleTest.java @@ -0,0 +1,93 @@ +/** + * + */ +package com.xplenty.api.request.member; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Member; +import com.xplenty.api.model.MemberTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class SetMemberRoleTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + SetMemberRole cc = new SetMemberRole(666, Xplenty.AccountRole.member); + assertEquals(Xplenty.Resource.SetMemberRole.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.SetMemberRole.name, cc.getName()); + assertEquals(Http.Method.PUT, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertTrue(cc.hasBody()); + assertNotNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Member c = MemberTest.createMockMember(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + SetMemberRole cc = new SetMemberRole(666, Xplenty.AccountRole.member); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("test", c.getName()); + assertEquals("xardazz@github.com", c.getEmail()); + assertEquals("https://testapi.xplenty.com/settings/members/666", c.getHtmlUrl()); + assertEquals("https://secure.gravatar.com/avatar", c.getAvatarUrl()); + assertTrue(c.getConfirmed()); + assertEquals("Colorado", c.getLocation()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals("https://testapi.xplenty.com/api/members/666", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getConfirmedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Member c = MemberTest.createMockMember(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + SetMemberRole cc = new SetMemberRole(666, Xplenty.AccountRole.member); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.SetMemberRole.name + ": error parsing response object", e.getMessage()); + } + + } +} From 434c9eb5bb3ffe7cc7290f3849df26d36bebfb77 Mon Sep 17 00:00:00 2001 From: xardas Date: Thu, 7 Jan 2016 21:40:06 +0300 Subject: [PATCH 18/35] add account methods add tests --- .../main/java/com/xplenty/api/Xplenty.java | 23 +++- .../main/java/com/xplenty/api/XplentyAPI.java | 95 +++++++++++++- .../java/com/xplenty/api/model/Account.java | 121 +++++++++++++++--- .../java/com/xplenty/api/model/Region.java | 35 +++++ .../api/request/AbstractDeleteRequest.java | 2 +- .../api/request/account/AccountInfo.java | 35 +++++ .../api/request/account/CreateAccount.java | 45 +++++++ .../api/request/account/DeleteAccount.java | 35 +++++ .../request/account/ListAccountRegions.java | 44 +++++++ .../api/request/account/ListAccounts.java | 60 +++++++++ .../api/request/account/UpdateAccount.java | 44 +++++++ .../com/xplenty/api/model/AccountTest.java | 55 ++++++++ .../com/xplenty/api/model/RegionTest.java | 28 ++++ .../api/request/account/AccountInfoTest.java | 103 +++++++++++++++ .../request/account/CreateAccountTest.java | 106 +++++++++++++++ .../request/account/DeleteAccountTest.java | 103 +++++++++++++++ .../account/ListAccountRegionsTest.java | 94 ++++++++++++++ .../api/request/account/ListAccountsTest.java | 117 +++++++++++++++++ .../request/account/UpdateAccountTest.java | 106 +++++++++++++++ 19 files changed, 1222 insertions(+), 29 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/Region.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/account/AccountInfo.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/account/CreateAccount.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/account/DeleteAccount.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/account/ListAccountRegions.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/account/ListAccounts.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/account/UpdateAccount.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/AccountTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/RegionTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/account/AccountInfoTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/account/CreateAccountTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/account/DeleteAccountTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/account/ListAccountRegionsTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/account/ListAccountsTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/account/UpdateAccountTest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index d56815d..d1ee74d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -20,17 +20,18 @@ public enum Sort { // todo include all entities that support name sort /** * Note that name sort is available only for {@link com.xplenty.api.model.PublicKey Public Key List}, - * {@link com.xplenty.api.model.Member Member List} + * {@link com.xplenty.api.model.Member Member List}, {@link com.xplenty.api.model.Account Account List} */ name("name"), /** - * Note that email sort is available only for {@link com.xplenty.api.model.Member Memeber List} + * Note that email sort is available only for {@link com.xplenty.api.model.Member Member List} */ email("email"), /** - * used only for filtering meaning all the statuses above + * Note that id sort is available only for {@link com.xplenty.api.model.Account Account List} */ - all("all"); + id("id") + ; public final String value; @@ -129,7 +130,11 @@ public enum AccountRole { */ owner("owner"), admin("admin"), - member("member"); + member("member"), + /** + * used only for filtering meaning all the roles above + */ + all("all"); @SuppressWarnings("unused") private final String role; @@ -280,7 +285,13 @@ public static enum Resource { DeleteMember("members/%s", "Delete member"), Member("members/%s", "Get member information"), Members("members", "List account members"), - SetMemberRole("members/%s", "Change member role") + SetMemberRole("members/%s", "Change member role"), + Regions("regions", "List available account regions"), + CreateAccount("accounts", "Create new account"), + DeleteAccount("accounts/%s", "Delete account"), + Account("accounts/%s", "Get account information"), + Accounts("accounts", "List accounts"), + UpdateAccount("accounts/%s", "Update account information") ; public final String value; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index d823664..71c1069 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -11,6 +11,7 @@ import com.xplenty.api.model.*; import com.xplenty.api.model.Package; import com.xplenty.api.request.*; +import com.xplenty.api.request.account.*; import com.xplenty.api.request.cluster.*; import com.xplenty.api.request.job.JobInfo; import com.xplenty.api.request.job.ListJobs; @@ -645,7 +646,7 @@ public List listMembers(int offset, int limit) { /** * List existing account members. Optionally, you can supply the input parameters to filter the member list so that * it contains user with specific role or email only and to determine the order by which the list will be sorted. - * @param params map of request parameters, see {@link Xplenty.Sort}, {@link Xplenty.SortDirection}. + * @param params map of request parameters, see {@link Xplenty.Sort}, {@link Xplenty.SortDirection}, {@link com.xplenty.api.request.member.ListMembers}. * @return list of account members */ public List listMembers(Properties params) { @@ -683,6 +684,92 @@ public Member deleteMember(long memberId) { return client.execute(new DeleteMember(memberId)); } + /** + * This call returns information for the list of regions that are available for your account. + * You can use this information to verify the regions in which you can create a cluster + * @return list of available regions + */ + public List listAvailableRegions() { + return client.execute(new ListAccountRegions(new Properties())); + } + + /** + * Creates a new account. This operation is possible only by confirmed users. + * @param name The name of the account. + * @param region The default region for the account. Possible values can be obtained through {@link #listAvailableRegions()} + * @param accountId Unique identifier of the account. Optional parameter. If null is passed will be autogenerated. + * @return newly created account object + */ + public Account createAccount(String name, String region, String accountId) { + return client.execute(new CreateAccount(name, region, accountId)); + } + + /** + * List active accounts in which the authenticated user is a member of (with either admin or member role). + * Optionally, you can supply the input parameters to filter the account list so that it contains only accounts with a + * specific role or id, and to determine the order by which the list will be sorted. + * @return list of accounts + */ + public List listAccounts() { + return listAccounts(new Properties()); + } + + /** + * List active accounts in which the authenticated user is a member of (with either admin or member role). + * Optionally, you can supply the input parameters to filter the account list so that it contains only accounts with a + * specific role or id, and to determine the order by which the list will be sorted. + * @param offset number of record to start results from + * @param limit number of results + * @return list of accounts + */ + public List listAccounts(int offset, int limit) { + final Properties props = new Properties(); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); + return listAccounts(props); + } + + /** + * List active accounts in which the authenticated user is a member of (with either admin or member role). + * Optionally, you can supply the input parameters to filter the account list so that it contains only accounts with a + * specific role or id, and to determine the order by which the list will be sorted. + * @param params map of request parameters, see {@link Xplenty.Sort}, {@link Xplenty.SortDirection}, {@link com.xplenty.api.request.account.ListAccounts}. + * @return list of accounts + */ + public List listAccounts(Properties params) { + return client.execute(new ListAccounts(params)); + } + + /** + * This call updates account. You must use {@link com.xplenty.api.model.Account#Account(String)} for account object creation + * @param account account to be updated + * @return updated account object + */ + public Account updateAccount(Account account) { + checkStringId(account.getCurrentAccountId()); + return client.execute(new UpdateAccount(account)); + } + + /** + * Delete an existing account. The operation can be executed only by the account owner. + * @param accountId unique string id of the account to delete + * @return deleted account object + */ + public Account deleteAccount(String accountId) { + checkStringId(accountId); + return client.execute(new DeleteAccount(accountId)); + } + + /** + * Get information about account. The authenticated user must be a member of this account (with either admin or member role). + * @param accountId unique string id of the account to get + * @return account object + */ + public Account getAccountInfo(String accountId) { + checkStringId(accountId); + return client.execute(new AccountInfo(accountId)); + } + private void checkId(long id) { if (id == 0) { @@ -690,6 +777,12 @@ private void checkId(long id) { } } + private void checkStringId(String id) { + if (id == null || id.length() == 0) { + throw new XplentyAPIException("No Id specified!"); + } + } + /** * Account name this XplentyAPI instance is associated with diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Account.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Account.java index 99936f3..674b58a 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Account.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Account.java @@ -1,5 +1,6 @@ package com.xplenty.api.model; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import com.xplenty.api.Xplenty; @@ -14,51 +15,66 @@ */ public class Account extends XplentyObject { @JsonProperty - private Long id; + public Long id; @JsonProperty("account_id") - private String accountId; + public String accountId; @JsonProperty - private String uname; + public String name; @JsonProperty - private String region; + public String uname; @JsonProperty - private String location; + public String region; + @JsonProperty + public String location; @JsonProperty("billing_email") - private String billingEmail; + public String billingEmail; @JsonProperty("gravatar_email") - private String gravatarEmail; + public String gravatarEmail; @JsonProperty("avatar_url") - private String avatarUrl; + public String avatarUrl; @JsonProperty("created_at") - private Date createdAt; + public Date createdAt; @JsonProperty("updated_at") - private Date updatedAt; + public Date updatedAt; @JsonProperty("schedules_count") - private Integer schedulesCount; + public Integer schedulesCount; @JsonProperty("connections_count") - private Integer connectionsCount; + public Integer connectionsCount; @JsonProperty - private Xplenty.AccountRole role; + public Xplenty.AccountRole role; @JsonProperty("owner_id") - private Long ownerId; + public Long ownerId; @JsonProperty("members_count") - private Integer membersCount; + public Integer membersCount; @JsonProperty("packages_count") - private Integer packagesCount; + public Integer packagesCount; @JsonProperty("jobs_count") - private Integer jobsCount; + public Integer jobsCount; @JsonProperty("running_jobs_count") - private Integer runningJobsCount; + public Integer runningJobsCount; @JsonProperty - private String url; + public String url; @JsonProperty("public_key") - private String publicKey; + public String publicKey; + @JsonIgnore + public String currentAccountId; + + protected Account() { + super(Account.class); + } - private Account() { + public Account(String name, String region, String accountId) { super(Account.class); + this.accountId = accountId; + this.name = name; + this.region = region; } + public Account(String accountId) { + super(Account.class); + this.accountId = accountId; + } /** * @@ -76,6 +92,14 @@ public String getAccountId() { return accountId; } + /** + * + * @return the name given to the account upon creation + */ + public String getName() { + return name; + } + /** * * @return the account's numeric identifier with u_ prefix @@ -220,4 +244,59 @@ public String getPublicKey() { return publicKey; } + /** + * + * @return account current unique string identifier + */ + public String getCurrentAccountId() { + return currentAccountId != null ? currentAccountId : accountId; + } + + /** + * + * @param accountId the account's unique identifier + */ + public void setAccountId(String accountId) { + this.accountId = accountId; + } + + /** + * + * @param name the name given to the account + */ + public void setName(String name) { + this.name = name; + } + + /** + * + * @param region the account's region + */ + public void setRegion(String region) { + this.region = region; + } + + /** + * + * @param location the account's location + */ + public void setLocation(String location) { + this.location = location; + } + + /** + * + * @param billingEmail the account's billing email + */ + public void setBillingEmail(String billingEmail) { + this.billingEmail = billingEmail; + } + + /** + * + * @param gravatarEmail the account's gravatar email + */ + public void setGravatarEmail(String gravatarEmail) { + this.gravatarEmail = gravatarEmail; + } } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Region.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Region.java new file mode 100644 index 0000000..5e419c5 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Region.java @@ -0,0 +1,35 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Data model for Xplenty region + * Author: Xardas + * Date: 07.01.16 + * Time: 17:52 + */ +public class Region extends XplentyObject { + @JsonProperty + protected String id; + @JsonProperty + protected String name; + @JsonProperty("group_name") + protected String groupName; + + protected Region() { + super(Region.class); + } + + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public String getGroupName() { + return groupName; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java index 5420d86..b29bad3 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java @@ -17,7 +17,7 @@ public abstract class AbstractDeleteRequest extends AbstractRequest { private final Class clazz; @SuppressWarnings("unchecked") - protected AbstractDeleteRequest(Long entityId) { + protected AbstractDeleteRequest(long entityId) { this.entityId = entityId; final Type superclass = this.getClass().getGenericSuperclass(); if (superclass instanceof Class) { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/AccountInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/AccountInfo.java new file mode 100644 index 0000000..84dfbe8 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/AccountInfo.java @@ -0,0 +1,35 @@ +package com.xplenty.api.request.account; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.Account; +import com.xplenty.api.request.AbstractInfoRequest; + +/** + * Information about active account. The authenticated user must be a member of this account (with either admin or member role). + * Author: Xardas + * Date: 07.01.16 + * Time: 19:41 + */ +public class AccountInfo extends AbstractInfoRequest { + private String entityId; + + public AccountInfo(String entityId) { + super(0); + this.entityId = entityId; + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.Account.format(entityId); + } + + @Override + public String getName() { + return Xplenty.Resource.Account.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/CreateAccount.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/CreateAccount.java new file mode 100644 index 0000000..df6118e --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/CreateAccount.java @@ -0,0 +1,45 @@ +package com.xplenty.api.request.account; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.Account; +import com.xplenty.api.request.AbstractManipulationRequest; + +/** + * Creates a new account. This operation is possible only by confirmed users. + * Author: Xardas + * Date: 07.01.16 + * Time: 18:02 + */ +public class CreateAccount extends AbstractManipulationRequest { + + public CreateAccount(String name, String region, String accountId) { + super(new Account(name, region, accountId)); + } + + @Override + protected String getPackKey() { + return null; + } + + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.CreateAccount.value; + } + + @Override + public String getName() { + return Xplenty.Resource.CreateAccount.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.POST; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/DeleteAccount.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/DeleteAccount.java new file mode 100644 index 0000000..15842a0 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/DeleteAccount.java @@ -0,0 +1,35 @@ +package com.xplenty.api.request.account; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.Account; +import com.xplenty.api.request.AbstractDeleteRequest; + +/** + * Delete an existing account. The operation can be executed only by the account owner. + * Author: Xardas + * Date: 07.01.16 + * Time: 19:33 + */ +public class DeleteAccount extends AbstractDeleteRequest { + private String entityId; + + public DeleteAccount(String entityId) { + super(0); + this.entityId = entityId; + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.DeleteAccount.format(entityId); + } + + @Override + public String getName() { + return Xplenty.Resource.DeleteAccount.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/ListAccountRegions.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/ListAccountRegions.java new file mode 100644 index 0000000..f422d60 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/ListAccountRegions.java @@ -0,0 +1,44 @@ +package com.xplenty.api.request.account; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Region; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; +import java.util.Properties; + +/** + * This call returns information for the list of regions that are available for your account. + * You can use this information to verify the regions in which you can create a cluster + * Author: Xardas + * Date: 07.01.16 + * Time: 17:51 + */ +public class ListAccountRegions extends AbstractListRequest> { + + public ListAccountRegions(Properties parameters) { + super(parameters, false); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.Regions.value; + } + + @Override + public String getName() { + return Xplenty.Resource.Regions.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/ListAccounts.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/ListAccounts.java new file mode 100644 index 0000000..a6ec900 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/ListAccounts.java @@ -0,0 +1,60 @@ +package com.xplenty.api.request.account; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Account; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; +import java.util.Properties; + +/** + * List active accounts in which the authenticated user is a member of (with either admin or member role). + * Optionally, you can supply the input parameters to filter the account list so that it contains only accounts with a + * specific role or id, and to determine the order by which the list will be sorted. + * Author: Xardas + * Date: 07.01.16 + * Time: 18:24 + */ +public class ListAccounts extends AbstractListRequest> { + public static final String PARAMETER_ROLE = "role"; + + public ListAccounts(Properties parameters) { + super(parameters, true); + validateParameters(parameters); + } + + + private void validateParameters(Properties params) { + if (params.containsKey(PARAMETER_ROLE) + && !(params.get(PARAMETER_ROLE) instanceof Xplenty.AccountRole)) { + throw new XplentyAPIException(String.format("Invalid %s parameter", PARAMETER_ROLE)); + } + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.Accounts.value; + } + + @Override + public String getName() { + return Xplenty.Resource.Accounts.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/UpdateAccount.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/UpdateAccount.java new file mode 100644 index 0000000..7b4b1ff --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/UpdateAccount.java @@ -0,0 +1,44 @@ +package com.xplenty.api.request.account; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.Account; +import com.xplenty.api.request.AbstractManipulationRequest; + +/** + * This call updates account. + * Author: Xardas + * Date: 07.01.16 + * Time: 18:51 + */ +public class UpdateAccount extends AbstractManipulationRequest { + + public UpdateAccount(Account entity) { + super(entity); + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.UpdateAccount.format(String.valueOf(entity.getAccountId())); + } + + @Override + public String getName() { + return Xplenty.Resource.UpdateAccount.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.PUT; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/AccountTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/AccountTest.java new file mode 100644 index 0000000..2f9ea7c --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/AccountTest.java @@ -0,0 +1,55 @@ +package com.xplenty.api.model; + +import com.xplenty.api.Xplenty; +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.Date; + +/** + * Author: Xardas + * Date: 06.01.16 + * Time: 22:07 + */ +public class AccountTest extends TestCase { + @Test + public void testBuilder() { + final Date now = new Date(); + Account account = createMockAccount(now); + assertNotNull(account); + assertEquals(now.getTime(), account.getCreatedAt().getTime()); + account = createMockAccountForCreation(); + assertNotNull(account); + } + + public static Account createMockAccountForCreation() { + Account account = new Account("test", "gcloud::europe-west", "superunique"); + return account; + } + + public static Account createMockAccount(Date now) { + Account account = new Account(); + account.id = 666L; + account.name = "test"; + account.accountId = "superunique"; + account.region = "gcloud::europe-west"; + account.avatarUrl = "https://secure.gravatar.com"; + account.billingEmail = "xardazz@github.com"; + account.gravatarEmail = "gravatar@gravatar.com"; + account.connectionsCount = 123; + account.jobsCount = 1234; + account.membersCount = 12345; + account.packagesCount = 123456; + account.runningJobsCount = 1234567; + account.schedulesCount = 12345678; + account.publicKey = "ssh-rsa AAAAAAA....AAAAAA Xplenty/superunique"; + account.createdAt = now; + account.updatedAt = now; + account.ownerId = 111L; + account.location = "Private Drive"; + account.uname = "u_666"; + account.role = Xplenty.AccountRole.admin; + account.url = "https://testapi.xplenty.com/accounts/superunique"; + return account; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/RegionTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/RegionTest.java new file mode 100644 index 0000000..210e40a --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/RegionTest.java @@ -0,0 +1,28 @@ +package com.xplenty.api.model; + +import junit.framework.TestCase; +import org.junit.Test; + +/** + * Author: Xardas + * Date: 06.01.16 + * Time: 22:07 + */ +public class RegionTest extends TestCase { + @Test + public void testBuilder() { + Region region = createMockRegion(); + assertNotNull(region); + assertEquals("gcloud", region.getGroupName()); + } + + + public static Region createMockRegion() { + Region region = new Region(); + region.id = "gcloud::europe-west"; + region.groupName = "gcloud"; + region.name = "West Europe Google Cloud"; + + return region; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/AccountInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/AccountInfoTest.java new file mode 100644 index 0000000..70a3ce3 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/AccountInfoTest.java @@ -0,0 +1,103 @@ +/** + * + */ +package com.xplenty.api.request.account; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Account; +import com.xplenty.api.model.AccountTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class AccountInfoTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + AccountInfo cc = new AccountInfo("superunique"); + assertEquals(Xplenty.Resource.Account.format("superunique"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.Account.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Account c = AccountTest.createMockAccount(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + AccountInfo cc = new AccountInfo("superunique"); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + + assertEquals(new Long(666), c.getId()); + assertEquals("test", c.getName()); + assertEquals("superunique", c.getAccountId()); + assertEquals("gcloud::europe-west", c.getRegion()); + assertEquals("https://secure.gravatar.com", c.getAvatarUrl()); + assertEquals("xardazz@github.com", c.getBillingEmail()); + assertEquals("gravatar@gravatar.com", c.getGravatarEmail()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals(123, c.getConnectionsCount().intValue()); + assertEquals(1234, c.getJobsCount().intValue()); + assertEquals(12345, c.getMembersCount().intValue()); + assertEquals(123456, c.getPackagesCount().intValue()); + assertEquals(1234567, c.getRunningJobsCount().intValue()); + assertEquals(12345678, c.getSchedulesCount().intValue()); + assertEquals("ssh-rsa AAAAAAA....AAAAAA Xplenty/superunique", c.getPublicKey()); + assertEquals(111, c.getOwnerId().longValue()); + assertEquals("Private Drive", c.getLocation()); + assertEquals("u_666", c.getUname()); + assertEquals("https://testapi.xplenty.com/accounts/superunique", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Account c = AccountTest.createMockAccount(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + AccountInfo cc = new AccountInfo("superunique"); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Account.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/CreateAccountTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/CreateAccountTest.java new file mode 100644 index 0000000..3a54aee --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/CreateAccountTest.java @@ -0,0 +1,106 @@ +/** + * + */ +package com.xplenty.api.request.account; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Account; +import com.xplenty.api.model.AccountTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class CreateAccountTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + CreateAccount cc = new CreateAccount("test", "gcloud::europe-west", "superunique"); + assertEquals(Xplenty.Resource.CreateAccount.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.CreateAccount.name, cc.getName()); + assertEquals(Http.Method.POST, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertTrue(cc.hasBody()); + assertNotNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Account c = AccountTest.createMockAccount(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(AccountTest.createMockAccountForCreation()); + assertNotNull(json); + + json = JsonMapperFactory.getInstance().writeValueAsString(c); + + CreateAccount cc = new CreateAccount("test", "gcloud::europe-west", "superunique"); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); + + assertNotNull(c); + + assertEquals(new Long(666), c.getId()); + assertEquals("test", c.getName()); + assertEquals("superunique", c.getAccountId()); + assertEquals("gcloud::europe-west", c.getRegion()); + assertEquals("https://secure.gravatar.com", c.getAvatarUrl()); + assertEquals("xardazz@github.com", c.getBillingEmail()); + assertEquals("gravatar@gravatar.com", c.getGravatarEmail()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals(123, c.getConnectionsCount().intValue()); + assertEquals(1234, c.getJobsCount().intValue()); + assertEquals(12345, c.getMembersCount().intValue()); + assertEquals(123456, c.getPackagesCount().intValue()); + assertEquals(1234567, c.getRunningJobsCount().intValue()); + assertEquals(12345678, c.getSchedulesCount().intValue()); + assertEquals("ssh-rsa AAAAAAA....AAAAAA Xplenty/superunique", c.getPublicKey()); + assertEquals(111, c.getOwnerId().longValue()); + assertEquals("Private Drive", c.getLocation()); + assertEquals("u_666", c.getUname()); + assertEquals("https://testapi.xplenty.com/accounts/superunique", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Account c = AccountTest.createMockAccount(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + CreateAccount cc = new CreateAccount("test", "gcloud::europe-west", "superunique"); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.CreateAccount.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/DeleteAccountTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/DeleteAccountTest.java new file mode 100644 index 0000000..a54e949 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/DeleteAccountTest.java @@ -0,0 +1,103 @@ +/** + * + */ +package com.xplenty.api.request.account; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Account; +import com.xplenty.api.model.AccountTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class DeleteAccountTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + DeleteAccount cc = new DeleteAccount("superunique"); + assertEquals(Xplenty.Resource.DeleteAccount.format("superunique"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.DeleteAccount.name, cc.getName()); + assertEquals(Http.Method.DELETE, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Account c = AccountTest.createMockAccount(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + DeleteAccount cc = new DeleteAccount("superunique"); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + + assertEquals(new Long(666), c.getId()); + assertEquals("test", c.getName()); + assertEquals("superunique", c.getAccountId()); + assertEquals("gcloud::europe-west", c.getRegion()); + assertEquals("https://secure.gravatar.com", c.getAvatarUrl()); + assertEquals("xardazz@github.com", c.getBillingEmail()); + assertEquals("gravatar@gravatar.com", c.getGravatarEmail()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals(123, c.getConnectionsCount().intValue()); + assertEquals(1234, c.getJobsCount().intValue()); + assertEquals(12345, c.getMembersCount().intValue()); + assertEquals(123456, c.getPackagesCount().intValue()); + assertEquals(1234567, c.getRunningJobsCount().intValue()); + assertEquals(12345678, c.getSchedulesCount().intValue()); + assertEquals("ssh-rsa AAAAAAA....AAAAAA Xplenty/superunique", c.getPublicKey()); + assertEquals(111, c.getOwnerId().longValue()); + assertEquals("Private Drive", c.getLocation()); + assertEquals("u_666", c.getUname()); + assertEquals("https://testapi.xplenty.com/accounts/superunique", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Account c = AccountTest.createMockAccount(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + DeleteAccount cc = new DeleteAccount("superunique"); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.DeleteAccount.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/ListAccountRegionsTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/ListAccountRegionsTest.java new file mode 100644 index 0000000..6a8a3b0 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/ListAccountRegionsTest.java @@ -0,0 +1,94 @@ +/** + * + */ +package com.xplenty.api.request.account; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Region; +import com.xplenty.api.model.RegionTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; + +/** + * @author xardas + * + */ +public class ListAccountRegionsTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Properties props = new Properties(); + ListAccountRegions cc = new ListAccountRegions(props); + assertEquals(Xplenty.Resource.Regions.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.Regions.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + + List regionList = new ArrayList<>(); + Region c = RegionTest.createMockRegion(); + regionList.add(c); + + String json = JsonMapperFactory.getInstance().writeValueAsString(regionList); + + ListAccountRegions cc = new ListAccountRegions(new Properties()); + regionList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(regionList); + assertTrue(regionList.size() > 0); + + c = regionList.get(0); + + assertEquals("gcloud", c.getGroupName()); + assertEquals("West Europe Google Cloud", c.getName()); + assertEquals("gcloud::europe-west", c.getId()); + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + List regionList = new ArrayList<>(); + Region c = RegionTest.createMockRegion(); + regionList.add(c); + String json = JsonMapperFactory.getInstance().writeValueAsString(regionList).replace("{", "["); + + try { + ListAccountRegions cc = new ListAccountRegions(new Properties()); + regionList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Regions.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/ListAccountsTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/ListAccountsTest.java new file mode 100644 index 0000000..5a5b1d6 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/ListAccountsTest.java @@ -0,0 +1,117 @@ +/** + * + */ +package com.xplenty.api.request.account; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Account; +import com.xplenty.api.model.AccountTest; +import com.xplenty.api.request.AbstractListRequest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; + +/** + * @author xardas + * + */ +public class ListAccountsTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Properties props = new Properties(); + ListAccounts cc = new ListAccounts(props); + assertEquals(Xplenty.Resource.Accounts.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.Accounts.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + + props.put(AbstractListRequest.PARAMETER_SORT, Xplenty.Sort.name); + props.put(AbstractListRequest.PARAMETER_DIRECTION, Xplenty.SortDirection.descending); + cc = new ListAccounts(props); + assertEquals(Xplenty.Resource.Accounts.value + "?" + AbstractListRequest.PARAMETER_SORT + "=name&" + + AbstractListRequest.PARAMETER_DIRECTION + "=desc", cc.getEndpoint()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + + List accountList = new ArrayList<>(); + Account c = AccountTest.createMockAccount(now); + accountList.add(c); + + String json = JsonMapperFactory.getInstance().writeValueAsString(accountList); + + ListAccounts cc = new ListAccounts(new Properties()); + accountList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(accountList); + assertTrue(accountList.size() > 0); + + c = accountList.get(0); + assertEquals(new Long(666), c.getId()); + assertEquals("test", c.getName()); + assertEquals("superunique", c.getAccountId()); + assertEquals("gcloud::europe-west", c.getRegion()); + assertEquals("https://secure.gravatar.com", c.getAvatarUrl()); + assertEquals("xardazz@github.com", c.getBillingEmail()); + assertEquals("gravatar@gravatar.com", c.getGravatarEmail()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals(123, c.getConnectionsCount().intValue()); + assertEquals(1234, c.getJobsCount().intValue()); + assertEquals(12345, c.getMembersCount().intValue()); + assertEquals(123456, c.getPackagesCount().intValue()); + assertEquals(1234567, c.getRunningJobsCount().intValue()); + assertEquals(12345678, c.getSchedulesCount().intValue()); + assertEquals("ssh-rsa AAAAAAA....AAAAAA Xplenty/superunique", c.getPublicKey()); + assertEquals(111, c.getOwnerId().longValue()); + assertEquals("Private Drive", c.getLocation()); + assertEquals("u_666", c.getUname()); + assertEquals("https://testapi.xplenty.com/accounts/superunique", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + List accountList = new ArrayList<>(); + Account c = AccountTest.createMockAccount(now); + accountList.add(c); + String json = JsonMapperFactory.getInstance().writeValueAsString(accountList).replace("{", "["); + + try { + ListAccounts cc = new ListAccounts(new Properties()); + accountList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Accounts.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/UpdateAccountTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/UpdateAccountTest.java new file mode 100644 index 0000000..7e01e03 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/UpdateAccountTest.java @@ -0,0 +1,106 @@ +/** + * + */ +package com.xplenty.api.request.account; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Account; +import com.xplenty.api.model.AccountTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class UpdateAccountTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + Account account = new Account("superunique"); + UpdateAccount cc = new UpdateAccount(account); + assertEquals(Xplenty.Resource.UpdateAccount.format("superunique"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.UpdateAccount.name, cc.getName()); + assertEquals(Http.Method.PUT, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertTrue(cc.hasBody()); + assertNotNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Account c = AccountTest.createMockAccount(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + Account account = new Account("superunique"); + UpdateAccount cc = new UpdateAccount(account); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + + assertEquals(new Long(666), c.getId()); + assertEquals("test", c.getName()); + assertEquals("superunique", c.getAccountId()); + assertEquals("gcloud::europe-west", c.getRegion()); + assertEquals("https://secure.gravatar.com", c.getAvatarUrl()); + assertEquals("xardazz@github.com", c.getBillingEmail()); + assertEquals("gravatar@gravatar.com", c.getGravatarEmail()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals(123, c.getConnectionsCount().intValue()); + assertEquals(1234, c.getJobsCount().intValue()); + assertEquals(12345, c.getMembersCount().intValue()); + assertEquals(123456, c.getPackagesCount().intValue()); + assertEquals(1234567, c.getRunningJobsCount().intValue()); + assertEquals(12345678, c.getSchedulesCount().intValue()); + assertEquals("ssh-rsa AAAAAAA....AAAAAA Xplenty/superunique", c.getPublicKey()); + assertEquals(111, c.getOwnerId().longValue()); + assertEquals("Private Drive", c.getLocation()); + assertEquals("u_666", c.getUname()); + assertEquals("https://testapi.xplenty.com/accounts/superunique", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Account c = AccountTest.createMockAccount(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + Account account = new Account("superunique"); + UpdateAccount cc = new UpdateAccount(account); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.UpdateAccount.name + ": error parsing response object", e.getMessage()); + } + + } +} From 388291d902cd507bbd379a29e03a1789d82e8814 Mon Sep 17 00:00:00 2001 From: xardas Date: Fri, 8 Jan 2016 19:22:53 +0300 Subject: [PATCH 19/35] add connection methods add tests --- .../main/java/com/xplenty/api/Xplenty.java | 25 +++- .../main/java/com/xplenty/api/XplentyAPI.java | 76 +++++++++++- .../com/xplenty/api/model/Connection.java | 14 +-- .../com/xplenty/api/model/ConnectionType.java | 69 +++++++++++ .../api/model/ConnectionTypeGroup.java | 34 ++++++ .../request/connection/ConnectionInfo.java | 30 +++++ .../request/connection/DeleteConnection.java | 31 +++++ .../connection/ListConnectionTypes.java | 43 +++++++ .../request/connection/ListConnections.java | 54 +++++++++ .../com/xplenty/api/model/ConnectionTest.java | 34 ++++++ .../xplenty/api/model/ConnectionTypeTest.java | 39 +++++++ .../connection/ConnectionInfoTest.java | 87 ++++++++++++++ .../connection/DeleteConnectionTest.java | 87 ++++++++++++++ .../connection/ListConnectionTypesTest.java | 101 ++++++++++++++++ .../connection/ListConnectionsTest.java | 108 ++++++++++++++++++ 15 files changed, 817 insertions(+), 15 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/ConnectionType.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/ConnectionTypeGroup.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ConnectionInfo.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/DeleteConnection.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ListConnectionTypes.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ListConnections.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/ConnectionTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/ConnectionTypeTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ConnectionInfoTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/DeleteConnectionTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ListConnectionTypesTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ListConnectionsTest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index d1ee74d..cd29335 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -20,7 +20,8 @@ public enum Sort { // todo include all entities that support name sort /** * Note that name sort is available only for {@link com.xplenty.api.model.PublicKey Public Key List}, - * {@link com.xplenty.api.model.Member Member List}, {@link com.xplenty.api.model.Account Account List} + * {@link com.xplenty.api.model.Member Member List}, {@link com.xplenty.api.model.Account Account List}, + * {@link com.xplenty.api.model.Connection Connection List} */ name("name"), /** @@ -28,9 +29,14 @@ public enum Sort { */ email("email"), /** - * Note that id sort is available only for {@link com.xplenty.api.model.Account Account List} + * Note that id sort is available only for {@link com.xplenty.api.model.Account Account List}, + * {@link com.xplenty.api.model.Connection Connection List} */ - id("id") + id("id"), + /** + * Note that type sort is available only for {@link com.xplenty.api.model.Connection Connection List} + */ + type("type") ; public final String value; @@ -173,7 +179,12 @@ public enum ConnectionType { herokupostgres("herokupostgres"), googlecloudsql("googlecloudsql"), bigquery("bigquery"), - segment("segment"); + segment("segment"), + salesforce("salesforce"), + /** + * used only for filtering meaning all the types above + */ + all("all"); @SuppressWarnings("unused") private final String type; @@ -291,7 +302,11 @@ public static enum Resource { DeleteAccount("accounts/%s", "Delete account"), Account("accounts/%s", "Get account information"), Accounts("accounts", "List accounts"), - UpdateAccount("accounts/%s", "Update account information") + UpdateAccount("accounts/%s", "Update account information"), + Connections("connections", "List Connections"), + ConnectionTypes("connections/types", "List Connection Types"), + DeleteConnection("connections/%s/%s", "Delete Connection"), + Connection("connections/%s/%s", "Get Connection information") ; public final String value; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index 71c1069..bf5b7bc 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -6,13 +6,18 @@ import com.xplenty.api.Xplenty.ClusterType; import com.xplenty.api.Xplenty.Version; import com.xplenty.api.exceptions.XplentyAPIException; -import com.xplenty.api.http.HttpClient; import com.xplenty.api.http.ClientBuilder; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.HttpClient; import com.xplenty.api.model.*; import com.xplenty.api.model.Package; -import com.xplenty.api.request.*; +import com.xplenty.api.request.AbstractListRequest; import com.xplenty.api.request.account.*; import com.xplenty.api.request.cluster.*; +import com.xplenty.api.request.connection.ConnectionInfo; +import com.xplenty.api.request.connection.DeleteConnection; +import com.xplenty.api.request.connection.ListConnectionTypes; +import com.xplenty.api.request.connection.ListConnections; import com.xplenty.api.request.job.JobInfo; import com.xplenty.api.request.job.ListJobs; import com.xplenty.api.request.job.RunJob; @@ -29,7 +34,6 @@ import com.xplenty.api.request.watching.AddJobWatcher; import com.xplenty.api.request.watching.ListWatchers; import com.xplenty.api.request.watching.WatchingStop; -import com.xplenty.api.http.Http; import com.xplenty.api.request.webhook.*; import com.xplenty.api.request.xpackage.ListPackages; import com.xplenty.api.request.xpackage.PackageInfo; @@ -770,6 +774,72 @@ public Account getAccountInfo(String accountId) { return client.execute(new AccountInfo(accountId)); } + /** + * List connections that are accessible by the authenticated user. + * Optionally, you can supply the input parameters to filter the connection list so that it contains only connections + * with specific types and to determine the order by which the list will be sorted. + * @return list of connections + */ + public List listConnections() { + return listConnections(new Properties()); + } + + /** + * List connections that are accessible by the authenticated user. + * Optionally, you can supply the input parameters to filter the connection list so that it contains only connections + * with specific types and to determine the order by which the list will be sorted. + * @param offset number of record to start results from + * @param limit number of results + * @return list of connections + */ + public List listConnections(int offset, int limit) { + final Properties props = new Properties(); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); + return listConnections(props); + } + + /** + * List connections that are accessible by the authenticated user. + * Optionally, you can supply the input parameters to filter the connection list so that it contains only connections + * with specific types and to determine the order by which the list will be sorted. + * @param params map of request parameters, see {@link Xplenty.Sort}, {@link Xplenty.SortDirection}, {@link com.xplenty.api.request.connection.ListConnections}. + * @return list of connections + */ + public List listConnections(Properties params) { + return client.execute(new ListConnections(params)); + } + + + /** + * List all connection types that are available with related groups. + * @return list of connection types + */ + public List listConnectionTypes() { + return client.execute(new ListConnectionTypes()); + } + + /** + * Delete an existing connection. + * Please note that deleting the connection will invalidate all items referencing it. + * @param connectionId id of the connection to delete + * @param conType type of the connection to delete + * @return deleted connection object + */ + public Connection deleteConnection(long connectionId, Xplenty.ConnectionType conType) { + return client.execute(new DeleteConnection(connectionId, conType)); + } + + /** + * Get connection information + * @param connectionId id of the connection to get + * @param conType type of the connection to get + * @return connection object + */ + public Connection getConnectionInfo(long connectionId, Xplenty.ConnectionType conType) { + return client.execute(new ConnectionInfo(connectionId, conType)); + } + private void checkId(long id) { if (id == 0) { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Connection.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Connection.java index e37119e..0d845be 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Connection.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Connection.java @@ -15,19 +15,19 @@ */ public class Connection extends XplentyObject { @JsonProperty - private Long id; + protected Long id; @JsonProperty - private String name; + protected String name; @JsonProperty("created_at") - private Date createdAt; + protected Date createdAt; @JsonProperty("updated_at") - private Date updatedAt; + protected Date updatedAt; @JsonProperty - private Xplenty.ConnectionType type; + protected Xplenty.ConnectionType type; @JsonProperty - private String url; + protected String url; - private Connection() { + protected Connection() { super(Connection.class); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ConnectionType.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/ConnectionType.java new file mode 100644 index 0000000..b96b5ef --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/ConnectionType.java @@ -0,0 +1,69 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +/** + * Data model for Xplenty Connection Type + * Author: Xardas + * Date: 08.01.16 + * Time: 17:09 + */ +public class ConnectionType extends XplentyObject { + @JsonProperty + protected String type; + @JsonProperty + protected String name; + @JsonProperty + protected String description; + @JsonProperty("icon_url") + protected String iconUrl; + @JsonProperty + protected List groups; + + + protected ConnectionType() { + super(ConnectionType.class); + } + + /** + * + * @return type + */ + public String getType() { + return type; + } + + /** + * + * @return connection type name + */ + public String getName() { + return name; + } + + /** + * + * @return connection type description + */ + public String getDescription() { + return description; + } + + /** + * + * @return url of icon + */ + public String getIconUrl() { + return iconUrl; + } + + /** + * + * @return list of groups this connection type belongs to + */ + public List getGroups() { + return groups; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ConnectionTypeGroup.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/ConnectionTypeGroup.java new file mode 100644 index 0000000..30f00f5 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/ConnectionTypeGroup.java @@ -0,0 +1,34 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Author: Xardas + * Date: 08.01.16 + * Time: 17:10 + */ +public class ConnectionTypeGroup { + @JsonProperty("group_type") + protected String groupType; + @JsonProperty("group_name") + protected String groupName; + + protected ConnectionTypeGroup() { + } + + /** + * + * @return type of group + */ + public String getGroupType() { + return groupType; + } + + /** + * + * @return name of group + */ + public String getGroupName() { + return groupName; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ConnectionInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ConnectionInfo.java new file mode 100644 index 0000000..a796aa7 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ConnectionInfo.java @@ -0,0 +1,30 @@ +package com.xplenty.api.request.connection; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.Connection; +import com.xplenty.api.request.AbstractInfoRequest; + +/** + * Get connection information + * Author: Xardas + * Date: 08.01.16 + * Time: 17:23 + */ +public class ConnectionInfo extends AbstractInfoRequest { + private final Xplenty.ConnectionType type; + + public ConnectionInfo(long entityId, Xplenty.ConnectionType type) { + super(entityId); + this.type = type; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.Connection.format(type.toString(), String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.Connection.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/DeleteConnection.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/DeleteConnection.java new file mode 100644 index 0000000..a904dcf --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/DeleteConnection.java @@ -0,0 +1,31 @@ +package com.xplenty.api.request.connection; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.Connection; +import com.xplenty.api.request.AbstractDeleteRequest; + +/** + * Delete an existing connection. + * Please note that deleting the connection will invalidate all items referencing it. + * Author: Xardas + * Date: 08.01.16 + * Time: 17:23 + */ +public class DeleteConnection extends AbstractDeleteRequest { + private final Xplenty.ConnectionType type; + + public DeleteConnection(long entityId, Xplenty.ConnectionType type) { + super(entityId); + this.type = type; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.DeleteConnection.format(type.toString(), String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.DeleteConnection.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ListConnectionTypes.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ListConnectionTypes.java new file mode 100644 index 0000000..c41c533 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ListConnectionTypes.java @@ -0,0 +1,43 @@ +package com.xplenty.api.request.connection; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.ConnectionType; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; +import java.util.Properties; + +/** + * List all connection types that are available with related groups. + * Author: Xardas + * Date: 08.01.16 + * Time: 17:16 + */ +public class ListConnectionTypes extends AbstractListRequest> { + + public ListConnectionTypes() { + super(new Properties(), false); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.ConnectionTypes.value; + } + + @Override + public String getName() { + return Xplenty.Resource.ConnectionTypes.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ListConnections.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ListConnections.java new file mode 100644 index 0000000..e37e131 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ListConnections.java @@ -0,0 +1,54 @@ +package com.xplenty.api.request.connection; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Connection; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; +import java.util.Properties; + +/** + * List connections that are accessible by the authenticated user. + * Optionally, you can supply the input parameters to filter the connection list so that it contains only connections + * with specific types and to determine the order by which the list will be sorted. + * Author: Xardas + * Date: 07.01.16 + * Time: 20:44 + */ +public class ListConnections extends AbstractListRequest> { + public static String PARAMETER_TYPE = "type"; + + public ListConnections(Properties parameters) { + super(parameters, true); + validateParameters(parameters); + } + + private void validateParameters(Properties params) { + if (params.containsKey(PARAMETER_TYPE) + && !(params.get(PARAMETER_TYPE) instanceof Xplenty.ConnectionType)) { + throw new XplentyAPIException(String.format("Invalid %s parameter", PARAMETER_TYPE)); + } + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.Connections.value; + } + + @Override + public String getName() { + return Xplenty.Resource.Connections.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/ConnectionTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/ConnectionTest.java new file mode 100644 index 0000000..07e489c --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/ConnectionTest.java @@ -0,0 +1,34 @@ +package com.xplenty.api.model; + +import com.xplenty.api.Xplenty; +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.Date; + +/** + * Author: Xardas + * Date: 08.01.16 + * Time: 17:49 + */ +public class ConnectionTest extends TestCase { + + @Test + public void testBuilder() { + final Date now = new Date(); + Connection connection = createMockConnection(now); + assertNotNull(connection); + assertEquals(now.getTime(), connection.getCreatedAt().getTime()); + } + + public static Connection createMockConnection(Date now) { + Connection connection = new Connection(); + connection.id = 666L; + connection.name = "test"; + connection.type = Xplenty.ConnectionType.mysql; + connection.createdAt = now; + connection.updatedAt = now; + connection.url = "https://testapi.xplenty.com/api/connections/mysql/666"; + return connection; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/ConnectionTypeTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/ConnectionTypeTest.java new file mode 100644 index 0000000..63beb05 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/ConnectionTypeTest.java @@ -0,0 +1,39 @@ +package com.xplenty.api.model; + +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * Author: Xardas + * Date: 08.01.16 + * Time: 17:54 + */ +public class ConnectionTypeTest extends TestCase { + + @Test + public void testBuilder() { + final Date now = new Date(); + ConnectionType connectionType = createMockConnectionType(); + assertNotNull(connectionType); + assertEquals("MySQL", connectionType.getName()); + } + + public static ConnectionType createMockConnectionType() { + ConnectionType connectionType = new ConnectionType(); + connectionType.name = "MySQL"; + connectionType.type = "mysql"; + connectionType.description = "The world's greatest open source database"; + connectionType.iconUrl = "https://assets.xplenty.com/assets/vendor/mysql.png"; + ConnectionTypeGroup conTypeGroup = new ConnectionTypeGroup(); + conTypeGroup.groupName = "Relational Databases"; + conTypeGroup.groupType = "relational"; + List groupList = new ArrayList<>(); + groupList.add(conTypeGroup); + connectionType.groups = groupList; + return connectionType; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ConnectionInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ConnectionInfoTest.java new file mode 100644 index 0000000..090ad35 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ConnectionInfoTest.java @@ -0,0 +1,87 @@ +/** + * + */ +package com.xplenty.api.request.connection; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Connection; +import com.xplenty.api.model.ConnectionTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class ConnectionInfoTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + ConnectionInfo cc = new ConnectionInfo(666, Xplenty.ConnectionType.mysql); + assertEquals(Xplenty.Resource.Connection.format(Xplenty.ConnectionType.mysql.toString(), String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.Connection.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Connection c = ConnectionTest.createMockConnection(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + ConnectionInfo cc = new ConnectionInfo(666, Xplenty.ConnectionType.mysql); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals(Xplenty.ConnectionType.mysql, c.getType()); + assertEquals("test", c.getName()); + assertEquals("https://testapi.xplenty.com/api/connections/mysql/666", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Connection c = ConnectionTest.createMockConnection(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + ConnectionInfo cc = new ConnectionInfo(666, Xplenty.ConnectionType.mysql); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Connection.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/DeleteConnectionTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/DeleteConnectionTest.java new file mode 100644 index 0000000..6d44b0f --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/DeleteConnectionTest.java @@ -0,0 +1,87 @@ +/** + * + */ +package com.xplenty.api.request.connection; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Connection; +import com.xplenty.api.model.ConnectionTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class DeleteConnectionTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + DeleteConnection cc = new DeleteConnection(666, Xplenty.ConnectionType.mysql); + assertEquals(Xplenty.Resource.DeleteConnection.format(Xplenty.ConnectionType.mysql.toString(), String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.DeleteConnection.name, cc.getName()); + assertEquals(Http.Method.DELETE, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Connection c = ConnectionTest.createMockConnection(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + DeleteConnection cc = new DeleteConnection(666, Xplenty.ConnectionType.mysql); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals(Xplenty.ConnectionType.mysql, c.getType()); + assertEquals("test", c.getName()); + assertEquals("https://testapi.xplenty.com/api/connections/mysql/666", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Connection c = ConnectionTest.createMockConnection(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + DeleteConnection cc = new DeleteConnection(666, Xplenty.ConnectionType.mysql); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.DeleteConnection.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ListConnectionTypesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ListConnectionTypesTest.java new file mode 100644 index 0000000..c76b428 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ListConnectionTypesTest.java @@ -0,0 +1,101 @@ +/** + * + */ +package com.xplenty.api.request.connection; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.ConnectionType; +import com.xplenty.api.model.ConnectionTypeTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; + +/** + * @author xardas + * + */ +public class ListConnectionTypesTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + ListConnectionTypes cc = new ListConnectionTypes(); + assertEquals(Xplenty.Resource.ConnectionTypes.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.ConnectionTypes.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + ConnectionType c = ConnectionTypeTest.createMockConnectionType(); + + List conTypeList = new ArrayList<>(); + conTypeList.add(c); + + String json = JsonMapperFactory.getInstance().writeValueAsString(conTypeList); + + ListConnectionTypes cc = new ListConnectionTypes(); + conTypeList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(conTypeList); + assertTrue(conTypeList.size() > 0); + + c = conTypeList.get(0); + + assertNotNull(c); + assertEquals("mysql", c.getType()); + assertEquals("MySQL", c.getName()); + assertEquals("The world's greatest open source database", c.getDescription()); + assertEquals("https://assets.xplenty.com/assets/vendor/mysql.png", c.getIconUrl()); + assertEquals("Relational Databases", c.getGroups().get(0).getGroupName()); + assertEquals("relational", c.getGroups().get(0).getGroupType()); + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + ConnectionType c = ConnectionTypeTest.createMockConnectionType(); + + List conTypeList = new ArrayList<>(); + conTypeList.add(c); + String json = JsonMapperFactory.getInstance().writeValueAsString(conTypeList).replace("{", "["); + + try { + ListConnectionTypes cc = new ListConnectionTypes(); + conTypeList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.ConnectionTypes.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ListConnectionsTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ListConnectionsTest.java new file mode 100644 index 0000000..6372a99 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ListConnectionsTest.java @@ -0,0 +1,108 @@ +/** + * + */ +package com.xplenty.api.request.connection; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Connection; +import com.xplenty.api.model.ConnectionTest; +import com.xplenty.api.request.AbstractListRequest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; + +/** + * @author xardas + * + */ +public class ListConnectionsTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Properties props = new Properties(); + ListConnections cc = new ListConnections(props); + assertEquals(Xplenty.Resource.Connections.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.Connections.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + + props.put(AbstractListRequest.PARAMETER_SORT, Xplenty.Sort.name); + props.put(AbstractListRequest.PARAMETER_DIRECTION, Xplenty.SortDirection.descending); + props.put(ListConnections.PARAMETER_TYPE, Xplenty.ConnectionType.mysql); + cc = new ListConnections(props); + assertEquals(Xplenty.Resource.Connections.value + "?" + AbstractListRequest.PARAMETER_SORT + "=name&" + + ListConnections.PARAMETER_TYPE + "=mysql&" + + AbstractListRequest.PARAMETER_DIRECTION + "=desc", cc.getEndpoint()); + + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Connection c = ConnectionTest.createMockConnection(now); + + List connectionList = new ArrayList<>(); + connectionList.add(c); + + String json = JsonMapperFactory.getInstance().writeValueAsString(connectionList); + + ListConnections cc = new ListConnections(new Properties()); + connectionList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(connectionList); + assertTrue(connectionList.size() > 0); + + c = connectionList.get(0); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals(Xplenty.ConnectionType.mysql, c.getType()); + assertEquals("test", c.getName()); + assertEquals("https://testapi.xplenty.com/api/connections/mysql/666", c.getUrl()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Connection c = ConnectionTest.createMockConnection(now); + + List connectionList = new ArrayList<>(); + connectionList.add(c); + String json = JsonMapperFactory.getInstance().writeValueAsString(connectionList).replace("{", "["); + + try { + ListConnections cc = new ListConnections(new Properties()); + connectionList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Connections.name + ": error parsing response object", e.getMessage()); + } + + } +} From 02d63230f4c69a96c843e40b1013980e4d403378 Mon Sep 17 00:00:00 2001 From: xardas Date: Fri, 8 Jan 2016 21:31:22 +0300 Subject: [PATCH 20/35] make job changes to support V2 add new job methods add tests add dev support for raw requests --- .../main/java/com/xplenty/api/Xplenty.java | 43 +++- .../main/java/com/xplenty/api/XplentyAPI.java | 23 ++- .../com/xplenty/api/http/JerseyClient.java | 3 + .../java/com/xplenty/api/http/Response.java | 2 +- .../com/xplenty/api/http/SyncNettyClient.java | 3 + .../main/java/com/xplenty/api/model/Job.java | 187 +++++++++++++++--- .../com/xplenty/api/model/JobCreator.java | 65 ++++++ .../java/com/xplenty/api/model/JobLog.java | 36 ++++ .../java/com/xplenty/api/model/JobOutput.java | 32 +-- .../xplenty/api/request/RawGetRequest.java | 54 +++++ .../request/job/JobExecutionVariables.java | 44 +++++ .../com/xplenty/api/request/job/JobLogs.java | 28 +++ .../java/com/xplenty/api/model/JobTest.java | 27 ++- .../job/JobExecutionVariablesTest.java | 82 ++++++++ .../xplenty/api/request/job/JobLogsTest.java | 83 ++++++++ 15 files changed, 654 insertions(+), 58 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/JobCreator.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/JobLog.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/RawGetRequest.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobExecutionVariables.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobLogs.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobExecutionVariablesTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobLogsTest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index cd29335..68c4d79 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -66,15 +66,40 @@ public String toString() { } } - public enum JobStatus { + public enum JobStatus { + /** + * the user sent a request to run the job + */ idle("idle"), - stopped("stopped"), - completed("completed"), - pending("pending"), - failed("failed"), - running("running"), - pending_stoppage("pending_stoppage"), - stopping("stopping"); + /** + * the job is initializing + */ + pending("pending"), + /** + * the job is running + */ + running("running"), + /** + * the job completed successfully + */ + completed("completed"), + /** + * the job failed to complete + */ + failed("failed"), + /** + * the user sent a request to stop the job + */ + pending_stoppage("pending_stoppage"), + /** + * the job is stopping + */ + stopping("stopping"), + /** + * the job has stopped + */ + stopped("stopped") + ; @SuppressWarnings("unused") private final String status; @@ -270,6 +295,8 @@ public static enum Resource { Job("jobs/%s", "Get job info"), RunJob("jobs", "Run job"), StopJob("jobs/%s", "Stop job"), + JobExecVars("jobs/%s/variables", "List job execution variables"), + JobLog("jobs/%s/log", "Get job output log"), ClusterWatcher("clusters/%s/watchers", "Adding/removing a cluster watchers"), JobWatcher("jobs/%s/watchers", "Adding/removing a job watchers"), Schedules("schedules", "List schedules"), diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index bf5b7bc..f1f3660 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -18,10 +18,7 @@ import com.xplenty.api.request.connection.DeleteConnection; import com.xplenty.api.request.connection.ListConnectionTypes; import com.xplenty.api.request.connection.ListConnections; -import com.xplenty.api.request.job.JobInfo; -import com.xplenty.api.request.job.ListJobs; -import com.xplenty.api.request.job.RunJob; -import com.xplenty.api.request.job.StopJob; +import com.xplenty.api.request.job.*; import com.xplenty.api.request.member.*; import com.xplenty.api.request.public_key.CreatePublicKey; import com.xplenty.api.request.public_key.DeletePublicKey; @@ -268,6 +265,24 @@ public List listJobs(int offset, int limit) { public List listJobs(Properties params) { return client.execute(new ListJobs(params)); } + + /** + * List all job variables that were used during job runtime. + * @param jobId id of the job + * @return map var_name => var_value + */ + public Map getJobExecutionVariables(long jobId) { + return client.execute(new JobExecutionVariables(jobId)); + } + + /** + * Log summary for the job. + * @param jobId id of the job + * @return job log object + */ + public JobLog getJobLog(long jobId) { + return client.execute(new JobLogs(jobId)); + } /** * Information about a particular job diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java index 99d8a74..91b5483 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java @@ -123,6 +123,9 @@ private WebResource.Builder getConfiguredResource(Request request) { * @return filly qualified URL */ private String getMethodURL(String methodEndpoint) { + if (methodEndpoint.startsWith("http")) { + return methodEndpoint; + } return String.format("%s://%s", protocol, methodEndpoint); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java index 44d775d..88ef5d2 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java @@ -82,7 +82,7 @@ protected void checkTypedInfo(TypeReference typeReference) { } } else if (Map.class.isAssignableFrom(rawType)) { cls = (Class) actualTypeArguments[1]; - if (XplentyObject.class.isAssignableFrom(cls)) { + if (XplentyObject.class.isAssignableFrom(cls) || String.class.isAssignableFrom(cls)) { return; } } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java index 90a49a8..2cb3fb9 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java @@ -210,6 +210,9 @@ private Map convertNettyHeaders(HttpHeaders headers) { * @return filly qualified URL */ private String getMethodURL(String methodEndpoint) { + if (methodEndpoint.startsWith("http")) { + return methodEndpoint; + } return String.format("%s://%s", protocol, methodEndpoint); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java index 9ecc5e0..bbeae6c 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java @@ -3,43 +3,37 @@ */ package com.xplenty.api.model; -import java.util.Date; -import java.util.List; -import java.util.Map; - -import javax.xml.bind.annotation.XmlRootElement; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonProperty; import com.xplenty.api.Xplenty.JobStatus; import com.xplenty.api.exceptions.XplentyAPIException; +import java.util.Date; +import java.util.List; +import java.util.Map; + /** * Data model for Xplenty job * * @author Yuriy Kovalek * */ -@XmlRootElement -@JsonInclude(Include.NON_NULL) -@JsonIgnoreProperties(ignoreUnknown = true) public class Job extends XplentyObject { - - public Job() { - super(Job.class); - } + + @JsonProperty protected Long id; + @JsonProperty protected JobStatus status; + @JsonProperty protected Map variables; //for backwords compatibility @JsonProperty("dynamic_variables") protected Map dynamicVariables; @JsonProperty("owner_id") protected Long ownerId; + @JsonProperty protected Double progress; @JsonProperty("outputs_count") protected Integer outputsCount; + @JsonProperty protected List outputs; @JsonProperty("started_at") protected Date startedAt; @@ -53,17 +47,30 @@ public Job() { protected Date updatedAt; @JsonProperty("cluster_id") protected Long clusterId; + @JsonProperty + protected Cluster cluster; @JsonProperty("package_id") protected Long packageId; + @JsonProperty + protected Package xpackage; + @JsonProperty protected String errors; + @JsonProperty protected String url; @JsonProperty("runtime_in_seconds") protected Long runtimeInSeconds; + @JsonProperty("html_url") + protected String htmlUrl; + @JsonProperty("log_url") + protected String logUrl; + @JsonProperty + protected JobCreator creator; + public Job() { + super(Job.class); + } - - - /** + /** * Shorthand method for {@code waitForStatus(null, JobStatus...)} Will wait forever until the required status is received. * @param statuses see {@link #waitForStatus(Long, JobStatus...)} */ @@ -92,10 +99,19 @@ public void waitForStatus(Long timeout, JobStatus... statuses) { if (c.getStatus() == status) break statusWait; } - if (System.currentTimeMillis() - timeout*1000 > start) + if (System.currentTimeMillis() - timeout * 1000 > start) throw new XplentyAPIException("Timeout occurred while waiting for required job status"); } } + + /** + * Download job output log + * Be aware that this method doesn't store call result anywhere. + * @return log contents + */ + public JobLog getJobLog() { + return getParentApiInstance().getJobLog(id); + } public Job withId(long id) { this.id = id; @@ -121,60 +137,185 @@ public Job withDynamicVariables(Map dynVars) { this.dynamicVariables = dynVars; return this; } - + + + /** + * + * @return the numeric job ID + */ public Long getId() { return id; } + + /** + * + * @return the job status. + */ public JobStatus getStatus() { return status; } + + /** + * + * @return a list of the variables supplied to the "run" request + */ public Map getVariables() { return variables; } - + + /** + * + * @return the numeric user ID + */ public Long getOwnerId() { return ownerId; } + + /** + * + * @return the job progress in percentages (a value between 0.0 and 1.0) + */ public Double getProgress() { return progress; } + + /** + * + * @return the number of output targets defined in the job's package + */ public Integer getOutputsCount() { return outputsCount; } + + /** + * + * @return list of the output targets defined in the job's package + */ public List getOutputs() { return outputs; } + + /** + * + * @return the date and time the job started running + */ public Date getStartedAt() { return startedAt; } + + /** + * + * @return the date and time the "run" request was made + */ public Date getCreatedAt() { return createdAt; } + + /** + * + * @return the date and time the job failed (if it failed) + */ public Date getFailedAt() { return failedAt; } + + /** + * + * @return the date and time at which the job completed (stopped, failed or completed) + */ public Date getCompletedAt() { return completedAt; } + + /** + * + * @return the date and time the job was last updated (occurs when package tasks are completed) + */ public Date getUpdatedAt() { return updatedAt; } + + /** + * + * @return the ID of the cluster in which the job was run + */ public Long getClusterId() { return clusterId; } + + /** + * + * @return the ID of the package that the job ran (or is running) + */ public Long getPackageId() { return packageId; } + + /** + * + * @return a textual message describing errors encountered while the job was run + */ public String getErrors() { return errors; } + + /** + * + * @return the job resource URL (API) + */ public String getUrl() { return url; } + + /** + * + * @return the time in seconds that the job has run up to the current time + */ public Long getRuntimeInSeconds() { return runtimeInSeconds; } - @SuppressWarnings("unused") + + /** + * + * @return the cluster in which the job was run. Includes all attributes. + */ + public Cluster getCluster() { + return cluster; + } + + /** + * + * @return the package that the job ran (or is running). Includes all attributes. + */ + public Package getPackage() { + return xpackage; + } + + /** + * + * @return the job resource URL (Web UI) + */ + public String getHtmlUrl() { + return htmlUrl; + } + + /** + * + * @return the URL to log summary + */ + public String getLogUrl() { + return logUrl; + } + + /** + * + * @return information about resource which created the job + */ + public JobCreator getCreator() { + return creator; + } + + @SuppressWarnings("unused") private void setId(long id) { this.id = id; } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobCreator.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobCreator.java new file mode 100644 index 0000000..d70026a --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobCreator.java @@ -0,0 +1,65 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Data model for Xplenty job creator + * Author: Xardas + * Date: 08.01.16 + * Time: 18:41 + */ +public class JobCreator { + @JsonProperty + protected String type; + @JsonProperty + protected Long id; + @JsonProperty("display_name") + protected String displayName; + @JsonProperty + protected String url; + @JsonProperty("html_url") + protected String htmlUrl; + + protected JobCreator() { + } + + /** + * + * @return the type of the resource (e.g. Schedule) + */ + public String getType() { + return type; + } + + /** + * + * @return the numeric resource ID + */ + public Long getId() { + return id; + } + + /** + * + * @return display name + */ + public String getDisplayName() { + return displayName; + } + + /** + * + * @return API url + */ + public String getUrl() { + return url; + } + + /** + * + * @return Web url + */ + public String getHtmlUrl() { + return htmlUrl; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobLog.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobLog.java new file mode 100644 index 0000000..e46e357 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobLog.java @@ -0,0 +1,36 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Data model for Xplenty job log + * Author: Xardas + * Date: 08.01.16 + * Time: 19:52 + */ +public class JobLog extends XplentyObject { + @JsonProperty("body") + protected String log; + @JsonProperty + protected String url; + + protected JobLog() { + super(JobLog.class); + } + + /** + * + * @return log itself + */ + public String getLog() { + return log; + } + + /** + * + * @return log resource URL + */ + public String getUrl() { + return url; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobOutput.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobOutput.java index e928a63..4c03958 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobOutput.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobOutput.java @@ -3,18 +3,12 @@ */ package com.xplenty.api.model; -import java.util.Date; -import java.util.List; -import java.util.Map; - -import javax.xml.bind.annotation.XmlRootElement; - import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonProperty; -import com.xplenty.api.Xplenty.JobStatus; -import com.xplenty.api.exceptions.XplentyAPIException; + +import java.util.Date; /** * Data model for Xplenty job @@ -22,18 +16,17 @@ * @author Yuriy Kovalek * */ -@XmlRootElement @JsonInclude(Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) public class JobOutput extends XplentyObject { - - public JobOutput() { - super(JobOutput.class); - } + + @JsonProperty protected Long id; + @JsonProperty protected String name; @JsonProperty("records_count") protected Long recordsCount; + @JsonProperty protected Double progress; @JsonProperty("component_name") protected String componentName; @@ -43,6 +36,12 @@ public JobOutput() { protected Date updatedAt; @JsonProperty("preview_url") protected String previewUrl; + @JsonProperty + protected String url; + + public JobOutput() { + super(JobOutput.class); + } public Long getId() { return id; @@ -68,8 +67,11 @@ public Date getUpdatedAt() { public String getPreviewUrl() { return previewUrl; } - - @SuppressWarnings("unused") + public String getUrl() { + return url; + } + + @SuppressWarnings("unused") private void setId(Long id) { this.id = id; } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/RawGetRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/RawGetRequest.java new file mode 100644 index 0000000..8774a50 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/RawGetRequest.java @@ -0,0 +1,54 @@ +package com.xplenty.api.request; + +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; +import com.xplenty.api.request.Request; + +/** + * Get job output log + * Author: Xardas + * Date: 08.01.16 + * Time: 19:24 + */ +public class RawGetRequest implements Request { + private final String endpoint; + + public RawGetRequest(String endpoint) { + this.endpoint = endpoint; + } + + @Override + public String getName() { + return getClass().getSimpleName(); + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.GET; + } + + @Override + public Http.MediaType getResponseType() { + return Http.MediaType.JSON; + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return endpoint; + } + + @Override + public boolean hasBody() { + return false; + } + + @Override + public Object getBody() { + return null; + } + + @Override + public String getResponse(Response response) { + return response.getRawContent(); + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobExecutionVariables.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobExecutionVariables.java new file mode 100644 index 0000000..e2ea35e --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobExecutionVariables.java @@ -0,0 +1,44 @@ +package com.xplenty.api.request.job; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.Map; +import java.util.Properties; + +/** + * List all job variables that were used during job runtime. + * Author: Xardas + * Date: 08.01.16 + * Time: 18:57 + */ +public class JobExecutionVariables extends AbstractListRequest> { + private final long jobId; + + public JobExecutionVariables(long jobId) { + super(new Properties(), false); + this.jobId = jobId; + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.JobExecVars.format(String.valueOf(jobId)); + } + + @Override + public String getName() { + return Xplenty.Resource.JobExecVars.name; + } + + @Override + public Map getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobLogs.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobLogs.java new file mode 100644 index 0000000..6124ace --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobLogs.java @@ -0,0 +1,28 @@ +package com.xplenty.api.request.job; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.JobLog; +import com.xplenty.api.request.AbstractInfoRequest; + +/** + * Log summary for the job. + * Author: Xardas + * Date: 08.01.16 + * Time: 19:55 + */ +public class JobLogs extends AbstractInfoRequest { + + public JobLogs(long entityId) { + super(entityId); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.JobLog.format(String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.JobLog.name; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/JobTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/JobTest.java index 4b15230..d2805ac 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/model/JobTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/JobTest.java @@ -3,16 +3,15 @@ */ package com.xplenty.api.model; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; - -import org.junit.Test; - import com.xplenty.api.Xplenty; import com.xplenty.api.Xplenty.JobStatus; - import junit.framework.TestCase; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; /** * @author Yuriy Kovalek @@ -67,4 +66,18 @@ public static JobOutput createMockJobOutput(Date now) { return jo; } + + public static JobLog createMockJobLog() { + JobLog jl = new JobLog(); + jl.log = "Invalid output path: couldn't fetch"; + jl.url = "https://testapi.xplenty.com/api/jobs/666/log"; + return jl; + } + + public static Map createMockJobExecutionVariables() { + Map vars = new HashMap<>(); + vars.put("_ACCOUNT_ID", "666"); + vars.put("_ACCOUNT_ID2", "777"); + return vars; + } } diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobExecutionVariablesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobExecutionVariablesTest.java new file mode 100644 index 0000000..2e04085 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobExecutionVariablesTest.java @@ -0,0 +1,82 @@ +package com.xplenty.api.request.job; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.JobTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Author: Xardas + * Date: 08.01.16 + * Time: 20:23 + */ +public class JobExecutionVariablesTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + JobExecutionVariables cc = new JobExecutionVariables(666); + assertEquals(Xplenty.Resource.JobExecVars.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.JobExecVars.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Map c = JobTest.createMockJobExecutionVariables(); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + JobExecutionVariables cc = new JobExecutionVariables(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + + assertEquals("666", c.get("_ACCOUNT_ID")); + assertEquals("777", c.get("_ACCOUNT_ID2")); + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Map c = JobTest.createMockJobExecutionVariables(); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + JobExecutionVariables cc = new JobExecutionVariables(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.JobExecVars.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobLogsTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobLogsTest.java new file mode 100644 index 0000000..a0c0def --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobLogsTest.java @@ -0,0 +1,83 @@ +package com.xplenty.api.request.job; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.JobLog; +import com.xplenty.api.model.JobTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * Author: Xardas + * Date: 08.01.16 + * Time: 20:23 + */ +public class JobLogsTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + JobLogs cc = new JobLogs(666); + assertEquals(Xplenty.Resource.JobLog.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.JobLog.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + JobLog c = JobTest.createMockJobLog(); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + JobLogs cc = new JobLogs(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + + assertEquals("Invalid output path: couldn't fetch", c.getLog()); + assertEquals("https://testapi.xplenty.com/api/jobs/666/log", c.getUrl()); + + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + JobLog c = JobTest.createMockJobLog(); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + JobLogs cc = new JobLogs(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.JobLog.name + ": error parsing response object", e.getMessage()); + } + + } +} From 85f7dcc693be9f4f8da1df3d45a5d305c969818a Mon Sep 17 00:00:00 2001 From: xardas Date: Sat, 9 Jan 2016 18:34:06 +0300 Subject: [PATCH 21/35] add support for miscellaneous list methods add notification methods add tests --- .../main/java/com/xplenty/api/Xplenty.java | 7 +- .../main/java/com/xplenty/api/XplentyAPI.java | 80 +++++++++++++- .../com/xplenty/api/model/Notification.java | 28 ++++- .../java/com/xplenty/api/model/Stack.java | 28 +++++ .../java/com/xplenty/api/model/Timezone.java | 35 ++++++ .../api/request/AbstractListRequest.java | 2 +- .../xplenty/api/request/misc/ListRegions.java | 61 ++++++++++ .../xplenty/api/request/misc/ListStacks.java | 42 +++++++ .../api/request/misc/ListSystemVariables.java | 46 ++++++++ .../api/request/misc/ListTimezones.java | 47 ++++++++ .../api/request/user/ListNotifications.java | 62 +++++++++++ .../request/user/MarkNotificationsRead.java | 58 ++++++++++ .../java/com/xplenty/api/model/MiscTest.java | 52 +++++++++ .../xplenty/api/model/NotificationTest.java | 33 ++++++ .../api/request/misc/ListRegionsTest.java | 104 ++++++++++++++++++ .../api/request/misc/ListStacksTest.java | 90 +++++++++++++++ .../request/misc/ListSystemVariablesTest.java | 84 ++++++++++++++ .../api/request/misc/ListTimezonesTest.java | 90 +++++++++++++++ .../request/user/ListNotificationsTest.java | 101 +++++++++++++++++ .../user/MarkNotificationsReadTest.java | 66 +++++++++++ 20 files changed, 1109 insertions(+), 7 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/Stack.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/Timezone.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListRegions.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListStacks.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListSystemVariables.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListTimezones.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/user/ListNotifications.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/user/MarkNotificationsRead.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/MiscTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/NotificationTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListRegionsTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListStacksTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListSystemVariablesTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListTimezonesTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/user/ListNotificationsTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/user/MarkNotificationsReadTest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index 68c4d79..e524718 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -307,6 +307,8 @@ public static enum Resource { Schedule("schedules/%s", "Get schedule information"), User("user", "Get current user information"), UpdateUser("user", "Update current user information"), + UserNotifications("user/notifications", "List user notifications"), + MarkUserNotificationRead("user/notifications/mark", "Mark user notification as read"), HookEvents("hook_events", "List supported Hook Events"), CreateWebHook("hooks/web", "Create new Web hook"), UpdateWebHook("hooks/web/%s", "Update existing Web hook"), @@ -333,7 +335,10 @@ public static enum Resource { Connections("connections", "List Connections"), ConnectionTypes("connections/types", "List Connection Types"), DeleteConnection("connections/%s/%s", "Delete Connection"), - Connection("connections/%s/%s", "Get Connection information") + Connection("connections/%s/%s", "Get Connection information"), + Stacks("stacks", "List all supported stacks"), + SystemVariables("variables", "List public system variables"), + Timezones("timezones", "List supported Time Zones") ; public final String value; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index f1f3660..82b5695 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -20,12 +20,18 @@ import com.xplenty.api.request.connection.ListConnections; import com.xplenty.api.request.job.*; import com.xplenty.api.request.member.*; +import com.xplenty.api.request.misc.ListRegions; +import com.xplenty.api.request.misc.ListStacks; +import com.xplenty.api.request.misc.ListSystemVariables; +import com.xplenty.api.request.misc.ListTimezones; import com.xplenty.api.request.public_key.CreatePublicKey; import com.xplenty.api.request.public_key.DeletePublicKey; import com.xplenty.api.request.public_key.ListPublicKeys; import com.xplenty.api.request.public_key.PublicKeyInfo; import com.xplenty.api.request.schedule.*; import com.xplenty.api.request.user.CurrentUserInfo; +import com.xplenty.api.request.user.ListNotifications; +import com.xplenty.api.request.user.MarkNotificationsRead; import com.xplenty.api.request.user.UpdateCurrentUser; import com.xplenty.api.request.watching.AddClusterWatcher; import com.xplenty.api.request.watching.AddJobWatcher; @@ -706,7 +712,7 @@ public Member deleteMember(long memberId) { /** * This call returns information for the list of regions that are available for your account. * You can use this information to verify the regions in which you can create a cluster - * @return list of available regions + * @return list of regions, available for account */ public List listAvailableRegions() { return client.execute(new ListAccountRegions(new Properties())); @@ -855,6 +861,78 @@ public Connection getConnectionInfo(long connectionId, Xplenty.ConnectionType co return client.execute(new ConnectionInfo(connectionId, conType)); } + /** + * This call returns information for the list of supported Stacks. + * @return list of stacks + */ + public List listStacks() { + return client.execute(new ListStacks()); + } + + /** + * This call returns list of supported Time Zones. + * @return list of timezones + */ + public List listTimezones() { + return client.execute(new ListTimezones()); + } + + /** + * List public system variables + * @return map var_name => var_value + */ + public Map listSystemVariables() { + return client.execute(new ListSystemVariables()); + } + + /** + * This call returns information for the list of regions supported by Xplenty. + * You can also select regions for particular Brand. You can use this information to verify the regions in which you can create a cluster. + * @return list of regions + */ + public List listRegions() { + return listRegions(new Properties()); + } + + /** + * This call returns information for the list of regions supported by Xplenty. + * You can also select regions for particular Brand. You can use this information to verify the regions in which you can create a cluster. + * @param params map of request parameters, see {@link com.xplenty.api.request.misc.ListRegions}. + * @return list of regions + */ + public List listRegions(Properties params) { + return client.execute(new ListRegions(params)); + } + + /** + * This call returns a list of notifications of the authenticated user. + * Optionally, you can supply the input parameters to filter the list so that it contains only unread notifications + * or all notifications, and to determine the order by which the list will be sorted. + * @return list of user notifications + */ + public List listUserNotifications() { + return listUserNotifications(new Properties()); + } + + /** + * This call returns a list of notifications of the authenticated user. + * Optionally, you can supply the input parameters to filter the list so that it contains only unread notifications + * or all notifications, and to determine the order by which the list will be sorted. + * @param params map of request parameters, see {@link com.xplenty.api.request.user.ListNotifications}. + * @return list of user notifications + */ + public List listUserNotifications(Properties params) { + return client.execute(new ListNotifications(params)); + } + + /** + * Marks the authenticated user's notifications as read. This call returns empty response. + * @return null + */ + public Void markNotificationAsRead() { + return client.execute(new MarkNotificationsRead()); + } + private void checkId(long id) { if (id == 0) { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Notification.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Notification.java index d0a33dd..4ce0f51 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Notification.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Notification.java @@ -13,13 +13,17 @@ */ public class Notification extends XplentyObject { @JsonProperty - private Long id; + protected Long id; @JsonProperty - private String title; + protected String title; @JsonProperty - private String message; + protected String message; @JsonProperty("last_read_at") - private Date lastReadAt; + protected Date lastReadAt; + @JsonProperty("created_at") + protected Date createdAt; + @JsonProperty("updated_at") + protected Date updatedAt; protected Notification() { super(Notification.class); @@ -56,4 +60,20 @@ public String getMessage() { public Date getLastReadAt() { return lastReadAt; } + + /** + * + * @return the date and time the notification was created + */ + public Date getCreatedAt() { + return createdAt; + } + + /** + * + * @return the date and time the notification was last updated + */ + public Date getUpdatedAt() { + return updatedAt; + } } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Stack.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Stack.java new file mode 100644 index 0000000..97175c9 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Stack.java @@ -0,0 +1,28 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Data model for Xplenty stack + * Author: Xardas + * Date: 08.01.16 + * Time: 20:35 + */ +public class Stack extends XplentyObject { + @JsonProperty + protected String id; + @JsonProperty + protected String name; + + protected Stack() { + super(Stack.class); + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Timezone.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Timezone.java new file mode 100644 index 0000000..eb6c9f1 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Timezone.java @@ -0,0 +1,35 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Author: Xardas + * Date: 09.01.16 + * Time: 15:54 + */ +public class Timezone extends XplentyObject { + @JsonProperty + protected String id; + @JsonProperty + protected String name; + + protected Timezone() { + super(Timezone.class); + } + + /** + * + * @return id of timezone + */ + public String getId() { + return id; + } + + /** + * + * @return name of timezone + */ + public String getName() { + return name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractListRequest.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractListRequest.java index 52724d1..89c8c64 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractListRequest.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractListRequest.java @@ -77,7 +77,7 @@ private void validateParameters(Properties params, boolean validateSort) { @Override public String getEndpoint() { - if (parameters.isEmpty()) { + if (parameters == null || parameters.isEmpty()) { return getEndpointRoot(); } StringBuilder params = new StringBuilder("?"); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListRegions.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListRegions.java new file mode 100644 index 0000000..857482b --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListRegions.java @@ -0,0 +1,61 @@ +package com.xplenty.api.request.misc; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Region; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; +import java.util.Properties; + +/** + * This call returns information for the list of regions supported by Xplenty. + * You can also select regions for particular Brand. You can use this information to verify the regions in which you can create a cluster. + * Author: Xardas + * Date: 07.01.16 + * Time: 17:51 + */ +public class ListRegions extends AbstractListRequest> { + /** + * The Brand's numeric identifier for which you want to list regions. + */ + public static final String PARAMETER_BRAND_ID = "brand_id"; + + public ListRegions(Properties parameters) { + super(parameters, false); + validateParameters(parameters); + } + + private void validateParameters(Properties params) { + if (params.containsKey(PARAMETER_BRAND_ID) + && !(params.get(PARAMETER_BRAND_ID) instanceof Number)) { + throw new XplentyAPIException(String.format("Invalid %s parameter, should be a number!", PARAMETER_BRAND_ID)); + } + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.Regions.value; + } + + @Override + public String getName() { + return Xplenty.Resource.Regions.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListStacks.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListStacks.java new file mode 100644 index 0000000..4cac5a1 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListStacks.java @@ -0,0 +1,42 @@ +package com.xplenty.api.request.misc; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Stack; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; + +/** + * This call returns information for the list of supported Stacks. + * Author: Xardas + * Date: 08.01.16 + * Time: 20:37 + */ +public class ListStacks extends AbstractListRequest> { + + public ListStacks() { + super(null, false); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.Stacks.value; + } + + @Override + public String getName() { + return Xplenty.Resource.Stacks.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListSystemVariables.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListSystemVariables.java new file mode 100644 index 0000000..f4cbe62 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListSystemVariables.java @@ -0,0 +1,46 @@ +package com.xplenty.api.request.misc; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.Map; + +/** + * List public system variables + * Author: Xardas + * Date: 08.01.16 + * Time: 20:37 + */ +public class ListSystemVariables extends AbstractListRequest> { + + public ListSystemVariables() { + super(null, false); + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.SystemVariables.value; + } + + @Override + public String getName() { + return Xplenty.Resource.SystemVariables.name; + } + + @Override + public Map getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListTimezones.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListTimezones.java new file mode 100644 index 0000000..68e2fa9 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListTimezones.java @@ -0,0 +1,47 @@ +package com.xplenty.api.request.misc; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Timezone; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; + +/** + * This call returns list of supported Time Zones. + * Author: Xardas + * Date: 08.01.16 + * Time: 20:37 + */ +public class ListTimezones extends AbstractListRequest> { + + public ListTimezones() { + super(null, false); + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.Timezones.value; + } + + @Override + public String getName() { + return Xplenty.Resource.Timezones.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/ListNotifications.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/ListNotifications.java new file mode 100644 index 0000000..7fb6fbb --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/ListNotifications.java @@ -0,0 +1,62 @@ +package com.xplenty.api.request.user; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Notification; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; +import java.util.Properties; + +/** + * This call returns a list of notifications of the authenticated user. + * Optionally, you can supply the input parameters to filter the list so that it contains only unread notifications + * or all notifications, and to determine the order by which the list will be sorted. + * Author: Xardas + * Date: 07.01.16 + * Time: 17:51 + */ +public class ListNotifications extends AbstractListRequest> { + /** + * The call will return only unread notifications if the value is set to 'false'. + */ + public static final String PARAMETER_LIST_ALL = "all"; + + public ListNotifications(Properties parameters) { + super(parameters, false); + validateParameters(parameters); + } + + private void validateParameters(Properties params) { + if (params.containsKey(PARAMETER_LIST_ALL) + && !(params.get(PARAMETER_LIST_ALL) instanceof Boolean)) { + throw new XplentyAPIException(String.format("Invalid %s parameter, should be a boolean!", PARAMETER_LIST_ALL)); + } + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.UserNotifications.value; + } + + @Override + public String getName() { + return Xplenty.Resource.UserNotifications.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/MarkNotificationsRead.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/MarkNotificationsRead.java new file mode 100644 index 0000000..18331fb --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/MarkNotificationsRead.java @@ -0,0 +1,58 @@ +package com.xplenty.api.request.user; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; +import com.xplenty.api.request.AbstractRequest; + +/** + * Marks the authenticated user's notifications as read. This call returns empty response. + * Author: Xardas + * Date: 09.01.16 + * Time: 16:40 + */ +public class MarkNotificationsRead extends AbstractRequest { + + public MarkNotificationsRead() { + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.POST; + } + + @Override + public Http.MediaType getResponseType() { + return Http.MediaType.JSON; + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + public boolean hasBody() { + return false; + } + + @Override + public Object getBody() { + return null; + } + + @Override + public Void getResponse(Response response) { + return null; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.MarkUserNotificationRead.value; + } + + @Override + public String getName() { + return Xplenty.Resource.MarkUserNotificationRead.name; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/MiscTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/MiscTest.java new file mode 100644 index 0000000..3e35603 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/MiscTest.java @@ -0,0 +1,52 @@ +package com.xplenty.api.model; + +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Author: Xardas + * Date: 09.01.16 + * Time: 16:55 + */ +public class MiscTest extends TestCase { + @Test + public void testBuilder() { + final Date now = new Date(); + Timezone timezone = createMockTimeZone(); + assertNotNull(timezone); + assertEquals("St. Petersburg", timezone.getId()); + + Stack stack = createMockStack(); + assertNotNull(stack); + assertEquals("blue-everest", stack.getId()); + + Map systemVars = createMockSystemVars(); + assertNotNull(systemVars); + assertTrue(systemVars.size() > 0); + } + + public static Timezone createMockTimeZone() { + Timezone timezone = new Timezone(); + timezone.id = "St. Petersburg"; + timezone.name = "(GMT+03:00) St. Petersburg"; + return timezone; + } + + public static Stack createMockStack() { + Stack stack = new Stack(); + stack.id = "blue-everest"; + stack.name = "Blue Everest"; + return stack; + } + + public static Map createMockSystemVars() { + Map systemVars = new HashMap<>(); + systemVars.put("_MAX_COMBINED_SPLIT_SIZE", "777777"); + systemVars.put("_BYTES_PER_REDUCER", "666666"); + return systemVars; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/NotificationTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/NotificationTest.java new file mode 100644 index 0000000..903449f --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/NotificationTest.java @@ -0,0 +1,33 @@ +package com.xplenty.api.model; + +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.Date; + +/** + * Author: Xardas + * Date: 09.01.16 + * Time: 16:51 + */ +public class NotificationTest extends TestCase { + @Test + public void testBuilder() { + final Date now = new Date(); + Notification notification = createMockNotification(now); + assertNotNull(notification); + assertEquals(now.getTime(), notification.getCreatedAt().getTime()); + + } + + public static Notification createMockNotification(Date now) { + Notification notification = new Notification(); + notification.id = 666L; + notification.title = "Cluster available"; + notification.message = "Cluster is available"; + notification.createdAt = now; + notification.updatedAt = now; + notification.lastReadAt = now; + return notification; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListRegionsTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListRegionsTest.java new file mode 100644 index 0000000..01315ae --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListRegionsTest.java @@ -0,0 +1,104 @@ +/** + * + */ +package com.xplenty.api.request.misc; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Region; +import com.xplenty.api.model.RegionTest; +import com.xplenty.api.request.AbstractListRequest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; + +/** + * @author xardas + * + */ +public class ListRegionsTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Properties props = new Properties(); + ListRegions cc = new ListRegions(props); + assertEquals(Xplenty.Resource.Regions.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.Regions.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + + props.put(ListRegions.PARAMETER_BRAND_ID, 11); + props.put(AbstractListRequest.PARAMETER_SORT, Xplenty.Sort.name); + props.put(AbstractListRequest.PARAMETER_DIRECTION, Xplenty.SortDirection.descending); + cc = new ListRegions(props); + assertEquals(Xplenty.Resource.Regions.value + "?" + + ListRegions.PARAMETER_BRAND_ID + "=11&" + + AbstractListRequest.PARAMETER_SORT + "=name&" + + AbstractListRequest.PARAMETER_DIRECTION + "=desc", cc.getEndpoint()); + + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + + List regionList = new ArrayList<>(); + Region c = RegionTest.createMockRegion(); + regionList.add(c); + + String json = JsonMapperFactory.getInstance().writeValueAsString(regionList); + + ListRegions cc = new ListRegions(new Properties()); + regionList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(regionList); + assertTrue(regionList.size() > 0); + + c = regionList.get(0); + + assertEquals("gcloud", c.getGroupName()); + assertEquals("West Europe Google Cloud", c.getName()); + assertEquals("gcloud::europe-west", c.getId()); + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + List regionList = new ArrayList<>(); + Region c = RegionTest.createMockRegion(); + regionList.add(c); + String json = JsonMapperFactory.getInstance().writeValueAsString(regionList).replace("{", "["); + + try { + ListRegions cc = new ListRegions(new Properties()); + regionList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Regions.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListStacksTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListStacksTest.java new file mode 100644 index 0000000..1506d8b --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListStacksTest.java @@ -0,0 +1,90 @@ +/** + * + */ +package com.xplenty.api.request.misc; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MiscTest; +import com.xplenty.api.model.Stack; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; + +/** + * @author xardas + * + */ +public class ListStacksTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + ListStacks cc = new ListStacks(); + assertEquals(Xplenty.Resource.Stacks.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.Stacks.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + + List stacks = new ArrayList<>(); + stacks.add(MiscTest.createMockStack()); + + String json = JsonMapperFactory.getInstance().writeValueAsString(stacks); + + ListStacks cc = new ListStacks(); + stacks = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(stacks); + assertTrue(stacks.size() > 0); + + Stack c = stacks.get(0); + + assertEquals("blue-everest", c.getId()); + assertEquals("Blue Everest", c.getName()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + List stacks = new ArrayList<>(); + stacks.add(MiscTest.createMockStack()); + String json = JsonMapperFactory.getInstance().writeValueAsString(stacks).replace("{", "["); + + try { + ListStacks cc = new ListStacks(); + stacks = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Stacks.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListSystemVariablesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListSystemVariablesTest.java new file mode 100644 index 0000000..ba466ac --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListSystemVariablesTest.java @@ -0,0 +1,84 @@ +/** + * + */ +package com.xplenty.api.request.misc; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MiscTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * @author xardas + * + */ +public class ListSystemVariablesTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + ListSystemVariables cc = new ListSystemVariables(); + assertEquals(Xplenty.Resource.SystemVariables.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.SystemVariables.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + + Map sysVars = MiscTest.createMockSystemVars(); + + String json = JsonMapperFactory.getInstance().writeValueAsString(sysVars); + + ListSystemVariables cc = new ListSystemVariables(); + sysVars = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(sysVars); + assertTrue(sysVars.size() > 0); + + assertEquals("777777", sysVars.get("_MAX_COMBINED_SPLIT_SIZE")); + assertEquals("666666", sysVars.get("_BYTES_PER_REDUCER")); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Map sysVars = MiscTest.createMockSystemVars(); + String json = JsonMapperFactory.getInstance().writeValueAsString(sysVars).replace("{", "["); + + try { + ListSystemVariables cc = new ListSystemVariables(); + sysVars = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.SystemVariables.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListTimezonesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListTimezonesTest.java new file mode 100644 index 0000000..f52543e --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListTimezonesTest.java @@ -0,0 +1,90 @@ +/** + * + */ +package com.xplenty.api.request.misc; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MiscTest; +import com.xplenty.api.model.Timezone; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; + +/** + * @author xardas + * + */ +public class ListTimezonesTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + ListTimezones cc = new ListTimezones(); + assertEquals(Xplenty.Resource.Timezones.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.Timezones.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + + List timezones = new ArrayList<>(); + timezones.add(MiscTest.createMockTimeZone()); + + String json = JsonMapperFactory.getInstance().writeValueAsString(timezones); + + ListTimezones cc = new ListTimezones(); + timezones = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(timezones); + assertTrue(timezones.size() > 0); + + Timezone c = timezones.get(0); + + assertEquals("St. Petersburg", c.getId()); + assertEquals("(GMT+03:00) St. Petersburg", c.getName()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + List timezones = new ArrayList<>(); + timezones.add(MiscTest.createMockTimeZone()); + String json = JsonMapperFactory.getInstance().writeValueAsString(timezones).replace("{", "["); + + try { + ListTimezones cc = new ListTimezones(); + timezones = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Timezones.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/ListNotificationsTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/ListNotificationsTest.java new file mode 100644 index 0000000..febaa50 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/ListNotificationsTest.java @@ -0,0 +1,101 @@ +/** + * + */ +package com.xplenty.api.request.user; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Notification; +import com.xplenty.api.model.NotificationTest; +import com.xplenty.api.request.AbstractListRequest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; + +/** + * @author xardas + * + */ +public class ListNotificationsTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Properties props = new Properties(); + ListNotifications cc = new ListNotifications(props); + assertEquals(Xplenty.Resource.UserNotifications.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.UserNotifications.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + + props.put(AbstractListRequest.PARAMETER_SORT, Xplenty.Sort.name); + props.put(AbstractListRequest.PARAMETER_DIRECTION, Xplenty.SortDirection.descending); + cc = new ListNotifications(props); + assertEquals(Xplenty.Resource.UserNotifications.value + "?" + AbstractListRequest.PARAMETER_SORT + "=name&" + + AbstractListRequest.PARAMETER_DIRECTION + "=desc", cc.getEndpoint()); + + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + + List notifications = new ArrayList<>(); + notifications.add(NotificationTest.createMockNotification(now)); + + String json = JsonMapperFactory.getInstance().writeValueAsString(notifications); + + ListNotifications cc = new ListNotifications(new Properties()); + notifications = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(notifications); + assertTrue(notifications.size() > 0); + + Notification c = notifications.get(0); + assertEquals(new Long(666), c.getId()); + assertEquals("Cluster available", c.getTitle()); + assertEquals("Cluster is available", c.getMessage()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getLastReadAt().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + List notifications = new ArrayList<>(); + notifications.add(NotificationTest.createMockNotification(now)); + String json = JsonMapperFactory.getInstance().writeValueAsString(notifications).replace("{", "["); + + try { + ListNotifications cc = new ListNotifications(new Properties()); + notifications = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.UserNotifications.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/MarkNotificationsReadTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/MarkNotificationsReadTest.java new file mode 100644 index 0000000..b7f341f --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/MarkNotificationsReadTest.java @@ -0,0 +1,66 @@ +/** + * + */ +package com.xplenty.api.request.user; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class MarkNotificationsReadTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + MarkNotificationsRead cc = new MarkNotificationsRead(); + assertEquals(Xplenty.Resource.MarkUserNotificationRead.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.MarkUserNotificationRead.name, cc.getName()); + assertEquals(Http.Method.POST, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + + MarkNotificationsRead cc = new MarkNotificationsRead(); + Object c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + null, + Status.OK.getStatusCode(), + new HashMap())); + + assertNull(c); + + } + + @Test + public void testBadResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + MarkNotificationsRead cc = new MarkNotificationsRead(); + Response resp = Response.forContentType(Http.MediaType.JSON, + null, + Status.UNAUTHORIZED.getStatusCode(), + new HashMap()); + assertFalse(resp.isValid()); + + } +} From 651438cc1aa46241626256c90cb7116394700282 Mon Sep 17 00:00:00 2001 From: xardas Date: Sat, 9 Jan 2016 23:38:09 +0300 Subject: [PATCH 22/35] add cluster APIv2 support(new fields and methods) --- .../main/java/com/xplenty/api/Xplenty.java | 28 +- .../main/java/com/xplenty/api/XplentyAPI.java | 27 +- .../java/com/xplenty/api/model/Cluster.java | 364 ++++++++++++++---- .../api/model/ClusterBootstrapAction.java | 41 ++ .../model/{JobCreator.java => Creator.java} | 4 +- .../main/java/com/xplenty/api/model/Job.java | 4 +- .../request/cluster/ListClusterInstances.java | 49 +++ 7 files changed, 439 insertions(+), 78 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterBootstrapAction.java rename xplenty.jar-core/src/main/java/com/xplenty/api/model/{JobCreator.java => Creator.java} (94%) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ListClusterInstances.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index e524718..77274ee 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -262,13 +262,38 @@ public String toString() { @JsonFormat(shape= JsonFormat.Shape.SCALAR) public static enum ClusterStatus { + /** + * the cluster is pending creation + */ pending("pending"), + /** + * the cluster is in erroneous state + */ error("error"), + /** + * the cluster is being created + */ creating("creating"), + /** + * the cluster is available + */ available("available"), + /** + * the cluster is idle + */ + idle("idle"), scaling("scaling"), + /** + * the cluster is pending termination + */ pending_terminate("pending_terminate"), + /** + * the cluster is being terminated + */ terminating("terminating"), + /** + * the cluster is terminated + */ terminated("terminated"); @SuppressWarnings("unused") @@ -286,7 +311,8 @@ public static enum Resource { Package("packages/%s", "Get package info"), Packages("packages", "List packages"), ClusterPlans("cluster_plans", "List cluster plans"), - Clusters("clusters", "List clusters"), + Clusters("clusters", "List clusters"), + ClusterInstances("clusters/%s/instances", "List cluster instances"), Cluster("clusters/%s", "Get cluster information"), CreateCluster("clusters", "Create cluster"), UpdateCluster("clusters/%s", "Update cluster"), diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index 82b5695..7a02377 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -186,6 +186,18 @@ public List listClusters(int offset, int limit) { public List listClusters(Properties props) { return client.execute(new ListClusters(props)); } + + /** + * List existing cluster instances. + * Note: This endpoint is only available for selected plans. + * @param clusterId id of the cluster + * @return list of instances + */ + public List listClusterInstances(long clusterId) { + checkId(clusterId); + return client.execute(new ListClusterInstances(clusterId)); + } + /** * Information about a particular cluster * @param clusterId id of the cluster, see {@link #listClusters()} to get a list of clusters with id's @@ -203,8 +215,9 @@ public Cluster clusterInformation(long clusterId) { * @param description cluster description * @param terminateOnIdle should the cluster terminate on idle status * @param timeToIdle time in seconds before cluster is considered idle - * @return + * @return newly created cluster object */ + @Deprecated public Cluster createCluster(int nodes, ClusterType type, String name, String description, Boolean terminateOnIdle, Long timeToIdle) { return client.execute(new CreateCluster( new Cluster().withNodes(nodes) @@ -215,6 +228,18 @@ public Cluster createCluster(int nodes, ClusterType type, String name, String de .withTimeToIdle(timeToIdle) )).withParentApiInstance(this); } + + /** + * Create new cluster with specified properties. Note that nodes paramter MUST be specified in case you are creating + * production cluster. If you create sandbox - cluster type must be specified. All other parameters are optional and + * will be assigned with default/generated values + * @param cluster Filled cluster object + * @return newly created cluster object + */ + public Cluster createCluster(Cluster cluster) { + cluster.withId(null); + return client.execute(new CreateCluster(cluster)); + } /** * Update cluster with specified properties diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Cluster.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Cluster.java index d0856f7..67bd8d0 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Cluster.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Cluster.java @@ -3,35 +3,35 @@ */ package com.xplenty.api.model; -import java.util.Date; - -import javax.xml.bind.annotation.XmlRootElement; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonProperty; import com.xplenty.api.Xplenty.ClusterStatus; import com.xplenty.api.Xplenty.ClusterType; import com.xplenty.api.exceptions.XplentyAPIException; +import java.util.Date; +import java.util.List; + /** * Data model for Xplenty cluster * * @author Yuriy Kovalek * */ -@XmlRootElement -@JsonInclude(Include.NON_NULL) -@JsonIgnoreProperties(ignoreUnknown = true) public class Cluster extends XplentyObject{ + + @JsonProperty protected Long id; + @JsonProperty protected String name; + @JsonProperty protected String description; + @JsonProperty protected ClusterStatus status; @JsonProperty("owner_id") protected Long ownerId; + @JsonProperty protected Integer nodes; + @JsonProperty protected ClusterType type; @JsonProperty("created_at") protected Date createdAt; @@ -43,6 +43,7 @@ public class Cluster extends XplentyObject{ protected Date terminatedAt; @JsonProperty("running_jobs_count") protected Long runningJobsCount; + @JsonProperty protected String url; @JsonProperty("terminate_on_idle") protected Boolean terminateOnIdle; @@ -50,6 +51,36 @@ public class Cluster extends XplentyObject{ protected Long timeToIdle; @JsonProperty("terminated_on_idle") protected Boolean terminatedOnIdle; + @JsonProperty("plan_id") + protected String planId; + @JsonProperty("idle_since") + protected Date idleSince; + @JsonProperty + protected String region; + @JsonProperty("master_instance_type") + protected String masterInstanceType; + @JsonProperty("slave_instance_type") + protected String slaveInstanceType; + @JsonProperty("master_spot_price") + protected Double masterSpotPrice; + @JsonProperty("slave_spot_price") + protected Double slaveSpotPrice; + @JsonProperty("master_spot_percentage") + protected Double masterSpotPercentage; + @JsonProperty("slave_spot_percentage") + protected Double slaveSpotPercentage; + @JsonProperty("allow_fallback") + protected Boolean allowFallback; + @JsonProperty("html_url") + protected String htmlUrl; + @JsonProperty + protected Creator creator; + @JsonProperty + protected String stack; + @JsonProperty("bootstrap_actions") + protected List bootstrapActions; + @JsonProperty + protected String zone; public Cluster() { super(Cluster.class); @@ -89,7 +120,62 @@ public Cluster withTimeToIdle(Long timeToIdle) { this.timeToIdle = timeToIdle; return this; } - + + public Cluster withRegion(String region) { + this.region = region; + return this; + } + + public Cluster withZone(String zone) { + this.zone = zone; + return this; + } + + public Cluster withMasterInstanceType(String masterInstanceType) { + this.masterInstanceType = masterInstanceType; + return this; + } + + public Cluster withSlaveInstanceType(String slaveInstanceType) { + this.slaveInstanceType = slaveInstanceType; + return this; + } + + public Cluster withMasterSpotPrice(Double masterSpotPrice) { + this.masterSpotPrice = masterSpotPrice; + return this; + } + + public Cluster withSlaveSpotPrice(Double slaveSpotPrice) { + this.slaveSpotPrice = slaveSpotPrice; + return this; + } + + public Cluster withMasterSpotPercentage(Double masterSpotPercentage) { + this.masterSpotPercentage = masterSpotPercentage; + return this; + } + + public Cluster withSlaveSpotPercentage(Double slaveSpotPercentage) { + this.slaveSpotPercentage = slaveSpotPercentage; + return this; + } + + public Cluster withAllowFallback(Boolean allowFallback) { + this.allowFallback = allowFallback; + return this; + } + + public Cluster withStack(String stack) { + this.stack = stack; + return this; + } + + public Cluster withBootstrapActions(List bootstrapActions) { + this.bootstrapActions = bootstrapActions; + return this; + } + /** * Shorthand method for {@code waitForStatus(null, ClusterStatus...)} Will wait forever until the required status is received. * @param statuses see {@link #waitForStatus(Long, ClusterStatus...)} @@ -123,118 +209,252 @@ public void waitForStatus(Long timeout, ClusterStatus... statuses) { throw new XplentyAPIException("Timeout occurred while waiting for required cluster status"); } } - + + /** + * + * @return the cluster's numeric identifier + */ public Long getId() { return id; } + + /** + * + * @return the name given to the cluster upon creation + */ public String getName() { return name; } + + /** + * + * @return the description given to the cluster upon creation + */ public String getDescription() { return description; } + + /** + * + * @return the cluster's status + */ public ClusterStatus getStatus() { return status; } + + /** + * + * @return the numeric user ID of the cluster's owner + */ public Long getOwnerId() { return ownerId; } + + /** + * + * @return the number of compute nodes for the cluster + */ public Integer getNodes() { return nodes; } + + /** + * + * @return the type of the cluster + */ public ClusterType getType() { return type; } + + /** + * + * @return the date and time the cluster was created + */ public Date getCreatedAt() { return createdAt; } + + /** + * + * @return the date and time the cluster was last updated + */ public Date getUpdatedAt() { return updatedAt; } + + /** + * + * @return the date and time the cluster became available + */ public Date getAvailableSince() { return availableSince; } + + /** + * + * @return the date and time the cluster was terminated + */ public Date getTerminatedAt() { return terminatedAt; } + + /** + * + * @return the number of jobs currently running on the cluster + */ public Long getRunningJobsCount() { return runningJobsCount; } + + /** + * + * @return the cluster resource URL (API) + */ public String getUrl() { return url; } + + /** + * + * @return indicates whether the cluster will be terminated after it becomes idle + */ public Boolean getTerminateOnIdle() { return terminateOnIdle; } + + /** + * + * @return the time interval (in seconds) in which the cluster will become idle + */ public Long getTimeToIdle() { return timeToIdle; } + + /** + * + * @return indicates whether the cluster terminated because it became idle + */ public Boolean getTerminatedOnIdle() { return terminatedOnIdle; } - @SuppressWarnings("unused") - private void setId(long id) { - this.id = id; - } - @SuppressWarnings("unused") - private void setName(String name) { - this.name = name; - } - @SuppressWarnings("unused") - private void setDescription(String description) { - this.description = description; - } - @SuppressWarnings("unused") - private void setStatus(ClusterStatus status) { - this.status = status; - } - @SuppressWarnings("unused") - private void setOwnerId(long ownerId) { - this.ownerId = ownerId; - } - @SuppressWarnings("unused") - private void setNodes(Integer nodes) { - this.nodes = nodes; + /** + * + * @return the ID of the cluster's plan + */ + public String getPlanId() { + return planId; } - @SuppressWarnings("unused") - private void setType(ClusterType type) { - this.type = type; + + /** + * + * @return the time since cluster changed status to idle + */ + public Date getIdleSince() { + return idleSince; } - @SuppressWarnings("unused") - private void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - } - @SuppressWarnings("unused") - private void setUpdatedAt(Date updatedAt) { - this.updatedAt = updatedAt; - } - @SuppressWarnings("unused") - private void setAvailableSince(Date as) { - this.availableSince = as; + + /** + * + * @return the region in which the cluster was created + */ + public String getRegion() { + return region; } - @SuppressWarnings("unused") - private void setTerminatedAt(Date terminatedAt) { - this.terminatedAt = terminatedAt; + + /** + * + * @return the type of the master instance + */ + public String getMasterInstanceType() { + return masterInstanceType; + } + + /** + * + * @return the type of the slave instance + */ + public String getSlaveInstanceType() { + return slaveInstanceType; + } + + /** + * + * @return the maximum bid price (in USD) requested for master spot instance + */ + public Double getMasterSpotPrice() { + return masterSpotPrice; + } + + /** + * + * @return the maximum bid price (in USD) requested for slave spot instance + */ + public Double getSlaveSpotPrice() { + return slaveSpotPrice; + } + + /** + * + * @return the percentage of master instances requested as spot (value between 0 and 1) + */ + public Double getMasterSpotPercentage() { + return masterSpotPercentage; + } + + /** + * + * @return the percentage of slave instances requested as spot (value between 0 and 1) + */ + public Double getSlaveSpotPercentage() { + return slaveSpotPercentage; + } + + /** + * + * @return indicates whether instances will be created as on-demand instances if spot requests are not fulfilled + */ + public Boolean getAllowFallback() { + return allowFallback; + } + + /** + * + * @return the cluster resource URL (Web UI) + */ + public String getHtmlUrl() { + return htmlUrl; + } + + /** + * + * @return information about resource which created the job. + */ + public Creator getCreator() { + return creator; + } + + /** + * + * @return the stack of the cluster. + */ + public String getStack() { + return stack; + } + + /** + * + * @return the array of the custom bootstrap actions. + */ + public List getBootstrapActions() { + return bootstrapActions; + } + + /** + * + * @return The zone in which the cluster was created (for availability zone supported regions) + */ + public String getZone() { + return zone; } - @SuppressWarnings("unused") - private void setRunningJobsCount(long runningJobsCount) { - this.runningJobsCount = runningJobsCount; - } - @SuppressWarnings("unused") - private void setUrl(String url) { - this.url = url; - } - @SuppressWarnings("unused") - private void setTerminateOnIdle(Boolean terminateOnIdle) { - this.terminateOnIdle = terminateOnIdle; - } - @SuppressWarnings("unused") - private void setTimeToIdle(Long timeToIdle) { - this.timeToIdle= timeToIdle ; - } - @SuppressWarnings("unused") - private void setTerminatedOnIdle(Boolean terminatedOnIdle) { - this.terminatedOnIdle = terminatedOnIdle; - } } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterBootstrapAction.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterBootstrapAction.java new file mode 100644 index 0000000..0adb695 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterBootstrapAction.java @@ -0,0 +1,41 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +/** + * Author: Xardas + * Date: 09.01.16 + * Time: 20:53 + */ +public class ClusterBootstrapAction { + @JsonProperty("script_path") + protected String scriptPath; + @JsonProperty + protected List args; + + protected ClusterBootstrapAction() { + } + + public ClusterBootstrapAction(String scriptPath, List args) { + this.scriptPath = scriptPath; + this.args = args; + } + + /** + * + * @return the path of the bootstrap action script + */ + public String getScriptPath() { + return scriptPath; + } + + /** + * + * @return the array of script parameters. It is an optional field. + */ + public List getArgs() { + return args; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobCreator.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Creator.java similarity index 94% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/JobCreator.java rename to xplenty.jar-core/src/main/java/com/xplenty/api/model/Creator.java index d70026a..2dc4df7 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobCreator.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Creator.java @@ -8,7 +8,7 @@ * Date: 08.01.16 * Time: 18:41 */ -public class JobCreator { +public class Creator { @JsonProperty protected String type; @JsonProperty @@ -20,7 +20,7 @@ public class JobCreator { @JsonProperty("html_url") protected String htmlUrl; - protected JobCreator() { + protected Creator() { } /** diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java index bbeae6c..9c35f55 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java @@ -64,7 +64,7 @@ public class Job extends XplentyObject { @JsonProperty("log_url") protected String logUrl; @JsonProperty - protected JobCreator creator; + protected Creator creator; public Job() { super(Job.class); @@ -311,7 +311,7 @@ public String getLogUrl() { * * @return information about resource which created the job */ - public JobCreator getCreator() { + public Creator getCreator() { return creator; } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ListClusterInstances.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ListClusterInstances.java new file mode 100644 index 0000000..63b28ed --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ListClusterInstances.java @@ -0,0 +1,49 @@ +/** + * + */ +package com.xplenty.api.request.cluster; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.ClusterInstance; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; + +/** + * List existing cluster instances. + * Note: This endpoint is only available for selected plans. + * @author xardazz + * + */ +public class ListClusterInstances extends AbstractListRequest> { + private final long clusterId; + + public ListClusterInstances(long clusterId) { + super(null, true); + this.clusterId = clusterId; + } + + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.ClusterInstances.format(String.valueOf(clusterId)); + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } + + @Override + public String getName() { + return Xplenty.Resource.ClusterInstances.name; + } + +} From 41701dea8948a6e47b789445a706f9befe42887f Mon Sep 17 00:00:00 2001 From: xardas Date: Sun, 10 Jan 2016 17:10:13 +0300 Subject: [PATCH 23/35] add and renew tests --- .../xplenty/api/model/ClusterInstance.java | 32 ++++--- .../main/java/com/xplenty/api/model/Job.java | 2 + .../com/xplenty/api/model/ClusterTest.java | 62 ++++++++++++-- .../api/request/cluster/ClusterInfoTest.java | 37 +++++++- .../cluster/ListClusterInstancesTest.java | 85 +++++++++++++++++++ 5 files changed, 197 insertions(+), 21 deletions(-) create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ListClusterInstancesTest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterInstance.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterInstance.java index 745904c..9c2535b 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterInstance.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterInstance.java @@ -12,25 +12,27 @@ */ public class ClusterInstance extends XplentyObject { @JsonProperty("instance_id") - private String instanceId; + protected String instanceId; @JsonProperty("private_dns") - private String privateDns; + protected String privateDns; @JsonProperty("public_dns") - private String publicDns; + protected String publicDns; @JsonProperty - private Xplenty.ClusterInstanceStatus status; + protected Xplenty.ClusterInstanceStatus status; @JsonProperty - private Boolean master; + protected Boolean master; @JsonProperty - private Boolean spot; + protected Boolean spot; @JsonProperty - private Boolean vpc; + protected Boolean vpc; @JsonProperty - private String zone; + protected String zone; @JsonProperty("instance_type") - private String instanceType; + protected String instanceType; + @JsonProperty + protected String url; - private ClusterInstance() { + protected ClusterInstance() { super(ClusterInstance.class); } @@ -52,7 +54,7 @@ public String getPrivateDns() { /** * @return the public fully qualified DNS of the instance */ - public String getpublicDns() { + public String getPublicDns() { return publicDns; } @@ -99,4 +101,12 @@ public String getZone() { public String getInstanceType() { return instanceType; } + + /** + * + * @return API resource url + */ + public String getUrl() { + return url; + } } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java index 9c35f55..28777e9 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java @@ -3,6 +3,7 @@ */ package com.xplenty.api.model; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import com.xplenty.api.Xplenty.JobStatus; import com.xplenty.api.exceptions.XplentyAPIException; @@ -109,6 +110,7 @@ public void waitForStatus(Long timeout, JobStatus... statuses) { * Be aware that this method doesn't store call result anywhere. * @return log contents */ + @JsonIgnore public JobLog getJobLog() { return getParentApiInstance().getJobLog(id); } diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/ClusterTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/ClusterTest.java index 4c4bec2..cd8a724 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/model/ClusterTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/ClusterTest.java @@ -3,15 +3,15 @@ */ package com.xplenty.api.model; -import java.util.Date; - -import junit.framework.TestCase; - -import org.junit.Test; - import com.xplenty.api.Xplenty; import com.xplenty.api.Xplenty.ClusterStatus; import com.xplenty.api.Xplenty.ClusterType; +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; /** * @author Yuriy Kovalek @@ -21,13 +21,14 @@ public class ClusterTest extends TestCase { @Test public void testBuilder() { Cluster c = new Cluster().withDescription("description") - .named("my cluster") - .withNodes(2); + .named("my cluster") + .withNodes(2) + .withAllowFallback(true); assertEquals("description", c.getDescription()); assertEquals("my cluster", c.getName()); - assertEquals("description", c.getDescription()); assertEquals(new Integer(2), c.getNodes()); + assertTrue(c.getAllowFallback()); } public static Cluster createMockCluster(Date now) { @@ -48,6 +49,49 @@ public static Cluster createMockCluster(Date now) { c.terminateOnIdle = true; c.timeToIdle = 3600L; c.terminatedOnIdle = false; + c.allowFallback = true; + c.zone = "eu-west1"; + c.idleSince = now; + c.htmlUrl = "https://www.xplenty.com/clusters/3"; + c.masterInstanceType = "sometype"; + c.masterSpotPercentage = 0.3; + c.masterSpotPrice = 42.42; + c.slaveInstanceType = "sometype1"; + c.slaveSpotPercentage = 0.4; + c.slaveSpotPrice = 12.12; + c.region = "eu-west1-amazon:1"; + c.planId = "month-10"; + c.stack = "pinky-everest"; + + List argList = new ArrayList<>(); + argList.add("arg1"); + argList.add("some more"); + ClusterBootstrapAction act = new ClusterBootstrapAction("http://xplenty.s3.amazonaws.com/bootstrap-actions/script_path", argList); + List cbas = new ArrayList<>(); + cbas.add(act); + c.bootstrapActions = cbas; + Creator creator = new Creator(); + creator.displayName = "xardazz"; + creator.htmlUrl = "https://www.xplenty.com/user/3"; + creator.url = "https://www.xplenty.com/api/user/3"; + creator.type = "User"; + creator.id = 666L; + c.creator = creator; return c; } + + public static ClusterInstance createMockClusterInstance() { + ClusterInstance ci = new ClusterInstance(); + ci.instanceId = "i-4d1b39a7"; + ci.privateDns = "ip-10-124-29-23.ec2.internal"; + ci.publicDns = "ec2-55-27-210-201.compute-1.amazonaws.com"; + ci.status = Xplenty.ClusterInstanceStatus.available; + ci.master = true; + ci.spot = false; + ci.vpc = false; + ci.instanceType = "sometype"; + ci.zone ="eu-west1"; + ci.url = "https://testapi.xplenty.com/api/clusters/5/instances/i-4d1b39a7"; + return ci; + } } diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java index ce14bd9..21dd3e8 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java @@ -13,8 +13,9 @@ import com.xplenty.api.http.Http; import com.xplenty.api.http.Response; import com.xplenty.api.model.Cluster; +import com.xplenty.api.model.ClusterBootstrapAction; import com.xplenty.api.model.ClusterTest; -import com.xplenty.api.request.cluster.ClusterInfo; +import com.xplenty.api.model.Creator; import junit.framework.TestCase; import org.junit.Before; import org.junit.Test; @@ -23,6 +24,7 @@ import java.text.ParseException; import java.util.Date; import java.util.HashMap; +import java.util.List; /** * @author Yuriy Kovalek @@ -70,7 +72,40 @@ public void testValidResponseHandling() throws UnsupportedEncodingException, Par assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); assertTrue(Math.abs(now.getTime() - c.getAvailableSince().getTime()) < 1000); assertTrue(Math.abs(now.getTime() - c.getTerminatedAt().getTime()) < 1000); + assertTrue(Math.abs(now.getTime() - c.getIdleSince().getTime()) < 1000); assertEquals("https://www.xplenty.com/api/" + Xplenty.Resource.Cluster.format(Long.toString(3)), c.getUrl()); + assertTrue(c.getAllowFallback()); + assertEquals("eu-west1", c.getZone()); + assertEquals("https://www.xplenty.com/clusters/3", c.getHtmlUrl()); + assertEquals("sometype", c.getMasterInstanceType()); + assertEquals("sometype1", c.getSlaveInstanceType()); + assertEquals("eu-west1-amazon:1", c.getRegion()); + assertEquals("month-10", c.getPlanId()); + assertEquals("pinky-everest", c.getStack()); + assertEquals(0.3, c.getMasterSpotPercentage()); + assertEquals(42.42, c.getMasterSpotPrice()); + assertEquals(0.4, c.getSlaveSpotPercentage()); + assertEquals(12.12, c.getSlaveSpotPrice()); + + List cbas = c.getBootstrapActions(); + assertNotNull(cbas); + assertTrue(cbas.size() > 0); + ClusterBootstrapAction act = cbas.get(0); + assertNotNull(act); + assertEquals("http://xplenty.s3.amazonaws.com/bootstrap-actions/script_path", act.getScriptPath()); + assertNotNull(act.getArgs()); + List argList = act.getArgs(); + assertTrue(argList.size() > 0); + assertEquals("arg1", argList.get(0)); + assertEquals("some more", argList.get(1)); + + Creator cr = c.getCreator(); + assertNotNull(cr); + assertEquals("xardazz", cr.getDisplayName()); + assertEquals("https://www.xplenty.com/user/3", cr.getHtmlUrl()); + assertEquals("https://www.xplenty.com/api/user/3", cr.getUrl()); + assertEquals("User", cr.getType()); + assertEquals(666, cr.getId().longValue()); } @Test diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ListClusterInstancesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ListClusterInstancesTest.java new file mode 100644 index 0000000..b3d8e9b --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ListClusterInstancesTest.java @@ -0,0 +1,85 @@ +/** + * + */ +package com.xplenty.api.request.cluster; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.ClusterInstance; +import com.xplenty.api.model.ClusterTest; +import junit.framework.TestCase; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * @author xardazz + * + */ +public class ListClusterInstancesTest extends TestCase { + @Test + public void testIntegrity() { + ListClusterInstances lc = new ListClusterInstances(666); + + assertEquals(Xplenty.Resource.ClusterInstances.format("666"), lc.getEndpoint()); + assertEquals(Xplenty.Resource.ClusterInstances.name, lc.getName()); + assertEquals(Http.Method.GET, lc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, lc.getResponseType()); + assertFalse(lc.hasBody()); + assertNull(lc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + ListClusterInstances lc = new ListClusterInstances(666); + List list = new ArrayList<>(); + list.add(ClusterTest.createMockClusterInstance()); + + String json = new ObjectMapper().writeValueAsString(list); + list = lc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertNotNull(list); + assertFalse(list.isEmpty()); + + ClusterInstance c = list.get(0); + assertNotNull(c); + assertEquals("i-4d1b39a7", c.getInstanceId()); + assertEquals("ip-10-124-29-23.ec2.internal", c.getPrivateDns()); + assertEquals("ec2-55-27-210-201.compute-1.amazonaws.com", c.getPublicDns()); + assertEquals("sometype", c.getInstanceType()); + assertEquals("eu-west1", c.getZone()); + assertEquals("https://testapi.xplenty.com/api/clusters/5/instances/i-4d1b39a7", c.getUrl()); + assertEquals(Xplenty.ClusterInstanceStatus.available, c.getStatus()); + assertTrue(c.getMaster()); + assertFalse(c.getSpot()); + assertFalse(c.getVpc()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + ListClusterInstances lc = new ListClusterInstances(666); + List list = new ArrayList<>(); + list.add(ClusterTest.createMockClusterInstance()); + + String json = new ObjectMapper().writeValueAsString(list).replace("[", ""); + try { + list = lc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.ClusterInstances.name + ": error parsing response object", e.getMessage()); + } + } +} From c83ceb4c008310a665ba1f6aab60b289393da477 Mon Sep 17 00:00:00 2001 From: xardas Date: Sun, 10 Jan 2016 19:07:55 +0300 Subject: [PATCH 24/35] add payment and subscription methods add tests --- .../main/java/com/xplenty/api/Xplenty.java | 6 +- .../main/java/com/xplenty/api/XplentyAPI.java | 38 ++++++ .../xplenty/api/http/JsonMapperFactory.java | 2 +- .../com/xplenty/api/model/CreditCardInfo.java | 68 +++++++++++ .../main/java/com/xplenty/api/model/Plan.java | 42 +++---- .../com/xplenty/api/model/Subscription.java | 12 +- .../api/request/subscription/ListPlans.java | 42 +++++++ .../subscription/PaymentMehodInfo.java | 27 +++++ .../subscription/SubscriptionInfo.java | 27 +++++ .../subscription/UpdatePaymentAndPlan.java | 53 +++++++++ .../xplenty/api/model/CreditCardInfoTest.java | 32 +++++ .../java/com/xplenty/api/model/PlanTest.java | 49 ++++++++ .../xplenty/api/model/SubscriptionTest.java | 33 ++++++ .../request/subscription/ListPlansTest.java | 109 ++++++++++++++++++ .../subscription/PaymentMethodInfoTest.java | 82 +++++++++++++ .../subscription/SubscriptionInfotest.java | 84 ++++++++++++++ .../UpdatePaymentAndPlanTest.java | 82 +++++++++++++ 17 files changed, 759 insertions(+), 29 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/CreditCardInfo.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/ListPlans.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/PaymentMehodInfo.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/SubscriptionInfo.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlan.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/CreditCardInfoTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/PlanTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/SubscriptionTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/ListPlansTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/PaymentMethodInfoTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/SubscriptionInfotest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlanTest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index 77274ee..ca7a983 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -364,7 +364,11 @@ public static enum Resource { Connection("connections/%s/%s", "Get Connection information"), Stacks("stacks", "List all supported stacks"), SystemVariables("variables", "List public system variables"), - Timezones("timezones", "List supported Time Zones") + Timezones("timezones", "List supported Time Zones"), + Plans("plans", "List payment plans for account"), + Subscription("subscription", "Get information about current account subscription"), + UpdatePaymentMethodAndPlan("payment_method", "Update payment method and/or plan"), + PaymentMethod("payment_method", "Get payment method information") ; public final String value; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index 7a02377..5471dec 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -29,6 +29,10 @@ import com.xplenty.api.request.public_key.ListPublicKeys; import com.xplenty.api.request.public_key.PublicKeyInfo; import com.xplenty.api.request.schedule.*; +import com.xplenty.api.request.subscription.ListPlans; +import com.xplenty.api.request.subscription.PaymentMehodInfo; +import com.xplenty.api.request.subscription.SubscriptionInfo; +import com.xplenty.api.request.subscription.UpdatePaymentAndPlan; import com.xplenty.api.request.user.CurrentUserInfo; import com.xplenty.api.request.user.ListNotifications; import com.xplenty.api.request.user.MarkNotificationsRead; @@ -958,6 +962,40 @@ public Void markNotificationAsRead() { return client.execute(new MarkNotificationsRead()); } + /** + * List plans that are available for an account. + * @return list of plans + */ + public List listPlans() { + return client.execute(new ListPlans()); + } + + /** + * Information about current account subscription. + * @return subscription object + */ + public Subscription getSubscriptionInfo() { + return client.execute(new SubscriptionInfo()); + } + + /** + * This call updates the payment method or plan or both. + * @param billingPaymentToken The valid payment token created through the billing provider (Paymill). + * @param planId ID of the plan. + * @return credit card object + */ + public CreditCardInfo updatePaymentAndPlan(String billingPaymentToken, String planId) { + return client.execute(new UpdatePaymentAndPlan(billingPaymentToken, planId)); + } + + /** + * Get payment method on file, for an existing account. If there is no payment method on file, returns Resource not found (404). + * @return credit card object + */ + public CreditCardInfo getPaymentMethodInfo() { + return client.execute(new PaymentMehodInfo()); + } + private void checkId(long id) { if (id == 0) { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonMapperFactory.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonMapperFactory.java index c6d805e..ff59eea 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonMapperFactory.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonMapperFactory.java @@ -17,7 +17,7 @@ public class JsonMapperFactory { //mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC); mapper.enable(SerializationFeature.INDENT_OUTPUT); // for pretty print - mapper.enable(SerializationFeature.WRITE_NULL_MAP_VALUES); + mapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/CreditCardInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/CreditCardInfo.java new file mode 100644 index 0000000..3114bd8 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/CreditCardInfo.java @@ -0,0 +1,68 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Data model for credit card info + * Author: Xardas + * Date: 10.01.16 + * Time: 16:53 + */ +public class CreditCardInfo extends XplentyObject { + @JsonProperty("card_last_4") + protected Integer cardLast4; + @JsonProperty("card_number") + protected String cardNumber; + @JsonProperty("expiration_date") + protected String expirationDate; + @JsonProperty("card_type") + protected String cardType; + @JsonProperty + protected String url; + + + protected CreditCardInfo() { + super(CreditCardInfo.class); + } + + /** + * + * @return last four digits of credit card number + * + */ + public Integer getCardLast4() { + return cardLast4; + } + + /** + * + * @return obfuscated credit card number with last 4 digits being visible + */ + public String getCardNumber() { + return cardNumber; + } + + /** + * + * @return credit card expiration date + */ + public String getExpirationDate() { + return expirationDate; + } + + /** + * + * @return credit card type e.g. Visa + */ + public String getCardType() { + return cardType; + } + + /** + * + * @return API resource url + */ + public String getUrl() { + return url; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Plan.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Plan.java index e9c8cf4..fa83d53 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Plan.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Plan.java @@ -14,47 +14,47 @@ */ public class Plan extends XplentyObject { @JsonProperty - private String id; + protected String id; @JsonProperty - private String name; + protected String name; @JsonProperty - private String description; + protected String description; @JsonProperty("price_cents") - private Long priceCents; + protected Long priceCents; @JsonProperty("price_currency") - private String priceCurrency; + protected String priceCurrency; @JsonProperty("price_unit") - private Xplenty.PriceUnit priceUnit; + protected Xplenty.PriceUnit priceUnit; @JsonProperty("cluster_node_hours_included") - private Integer clusterNodeHoursIncluded; + protected Integer clusterNodeHoursIncluded; @JsonProperty("cluster_node_hours_limit") - private Integer clusterNodeHoursLimit; + protected Integer clusterNodeHoursLimit; @JsonProperty("cluster_node_price_cents") - private Long clusterNodePriceCents; + protected Long clusterNodePriceCents; @JsonProperty("cluster_node_price_currency") - private String clusterNodePriceCurrency; + protected String clusterNodePriceCurrency; @JsonProperty("cluster_node_price_unit") - private Xplenty.PriceUnit clusterNodePriceUnit; + protected Xplenty.PriceUnit clusterNodePriceUnit; @JsonProperty("cluster_nodes_limit") - private Integer clusterNodesLimit; + protected Integer clusterNodesLimit; @JsonProperty("cluster_size_limit") - private Integer clusterSizeLimit; + protected Integer clusterSizeLimit; @JsonProperty("clusters_limit") - private Integer clustersLimit; + protected Integer clustersLimit; @JsonProperty("sandbox_clusters_limit") - private Integer sandboxClustersLimit; + protected Integer sandboxClustersLimit; @JsonProperty("sandbox_node_hours_included") - private Integer sandboxNodeHoursIncluded; + protected Integer sandboxNodeHoursIncluded; @JsonProperty("sandbox_node_hours_limit") - private Integer sandboxNodeHoursLimit; + protected Integer sandboxNodeHoursLimit; @JsonProperty("members_limit") - private Integer membersLimit; + protected Integer membersLimit; @JsonProperty - private Integer position; + protected Integer position; @JsonProperty("created_at") - private Date createdAt; + protected Date createdAt; @JsonProperty("updated_at") - private Date updatedAt; + protected Date updatedAt; protected Plan() { super(Plan.class); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java index 24b9ff7..f3d1417 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java @@ -13,17 +13,17 @@ */ public class Subscription extends XplentyObject { @JsonProperty("trial_period_days") - private Integer trialPeriodDays; + protected Integer trialPeriodDays; @JsonProperty("plan_id") - private String planId; + protected String planId; @JsonProperty("trial_start") - private Date trialStart; + protected Date trialStart; @JsonProperty("trial_end") - private Date trialEnd; + protected Date trialEnd; @JsonProperty("trialling") - private Boolean isTrial; + protected Boolean isTrial; @JsonProperty - private String url; + protected String url; protected Subscription() { super(Subscription.class); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/ListPlans.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/ListPlans.java new file mode 100644 index 0000000..d71916b --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/ListPlans.java @@ -0,0 +1,42 @@ +package com.xplenty.api.request.subscription; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Plan; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; + +/** + * List plans that are available for an account. + * Author: Xardas + * Date: 10.01.16 + * Time: 16:35 + */ +public class ListPlans extends AbstractListRequest> { + + public ListPlans() { + super(null, false); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.Plans.value; + } + + @Override + public String getName() { + return Xplenty.Resource.Plans.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/PaymentMehodInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/PaymentMehodInfo.java new file mode 100644 index 0000000..b76a69d --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/PaymentMehodInfo.java @@ -0,0 +1,27 @@ +package com.xplenty.api.request.subscription; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.CreditCardInfo; +import com.xplenty.api.request.AbstractInfoRequest; + +/** + * Get payment method on file, for an existing account. If there is no payment method on file, returns Resource not found (404). + * Author: Xardas + * Date: 10.01.16 + * Time: 17:18 + */ +public class PaymentMehodInfo extends AbstractInfoRequest { + public PaymentMehodInfo() { + super(0); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.PaymentMethod.value; + } + + @Override + public String getName() { + return Xplenty.Resource.PaymentMethod.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/SubscriptionInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/SubscriptionInfo.java new file mode 100644 index 0000000..09414e2 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/SubscriptionInfo.java @@ -0,0 +1,27 @@ +package com.xplenty.api.request.subscription; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.Subscription; +import com.xplenty.api.request.AbstractInfoRequest; + +/** + * Information about current account subscription. + * Author: Xardas + * Date: 10.01.16 + * Time: 16:45 + */ +public class SubscriptionInfo extends AbstractInfoRequest { + public SubscriptionInfo() { + super(0); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.Subscription.value; + } + + @Override + public String getName() { + return Xplenty.Resource.Subscription.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlan.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlan.java new file mode 100644 index 0000000..2d7cb9a --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlan.java @@ -0,0 +1,53 @@ +package com.xplenty.api.request.subscription; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.CreditCardInfo; +import com.xplenty.api.request.AbstractManipulationRequest; + +import java.util.HashMap; + +/** + * This call updates the payment method or plan or both. + * Author: Xardas + * Date: 10.01.16 + * Time: 17:01 + */ +public class UpdatePaymentAndPlan extends AbstractManipulationRequest { + private final String billingPaymentToken; + private final String planId; + + public UpdatePaymentAndPlan(String billingPaymentToken, String planId) { + super(null); + this.billingPaymentToken = billingPaymentToken; + this.planId = planId; + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + public Object getBody() { + HashMap requestEntity = new HashMap<>(); + requestEntity.put("billing_payment_token", billingPaymentToken); + requestEntity.put("plan_id", planId); + return requestEntity; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.UpdatePaymentMethodAndPlan.value; + } + + @Override + public String getName() { + return Xplenty.Resource.UpdatePaymentMethodAndPlan.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.PUT; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/CreditCardInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/CreditCardInfoTest.java new file mode 100644 index 0000000..3a3f4c4 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/CreditCardInfoTest.java @@ -0,0 +1,32 @@ +package com.xplenty.api.model; + +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.Date; + +/** + * Author: Xardas + * Date: 10.01.16 + * Time: 17:27 + */ +public class CreditCardInfoTest extends TestCase { + @Test + public void testBuilder() { + final Date now = new Date(); + CreditCardInfo ccc = createMockCreditCardInfo(); + assertNotNull(ccc); + assertEquals("xxxx-xxxx-xxxx-" + String.valueOf(ccc.getCardLast4()), ccc.getCardNumber()); + } + + + public static CreditCardInfo createMockCreditCardInfo() { + CreditCardInfo ccc = new CreditCardInfo(); + ccc.cardLast4 = 9876; + ccc.cardNumber = "xxxx-xxxx-xxxx-9876"; + ccc.cardType = "MasterCard"; + ccc.expirationDate = "06/66"; + ccc.url = "https://testapi.xplenty.com/api/payment_method"; + return ccc; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/PlanTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/PlanTest.java new file mode 100644 index 0000000..b0e87e9 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/PlanTest.java @@ -0,0 +1,49 @@ +package com.xplenty.api.model; + +import com.xplenty.api.Xplenty; +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.Date; + +/** + * Author: Xardas + * Date: 10.01.16 + * Time: 17:39 + */ +public class PlanTest extends TestCase { + @Test + public void testBuilder() { + final Date now = new Date(); + Plan plan = createMockPlan(now); + assertNotNull(plan); + assertEquals(now.getTime(), plan.getCreatedAt().getTime()); + } + + + public static Plan createMockPlan(Date now) { + Plan plan = new Plan(); + plan.name = "Expert"; + plan.description = "Expert"; + plan.priceCents = 199900L; + plan.priceCurrency = "USD"; + plan.priceUnit = Xplenty.PriceUnit.month; + plan.clusterNodeHoursIncluded = 0; + plan.clusterNodeHoursLimit = -1; + plan.clusterNodePriceCents = 71L; + plan.clusterNodePriceCurrency = "USD"; + plan.clusterNodePriceUnit = Xplenty.PriceUnit.hour; + plan.clusterNodesLimit = 32; + plan.clusterSizeLimit = 0; + plan.clustersLimit = 4096; + plan.sandboxClustersLimit = 1; + plan.sandboxNodeHoursIncluded = 0; + plan.sandboxNodeHoursLimit = -1; + plan.membersLimit = 4096; + plan.position = 7; + plan.createdAt = now; + plan.updatedAt = now; + plan.id = "expert--7"; + return plan; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/SubscriptionTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/SubscriptionTest.java new file mode 100644 index 0000000..f8c1936 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/SubscriptionTest.java @@ -0,0 +1,33 @@ +package com.xplenty.api.model; + +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.Date; + +/** + * Author: Xardas + * Date: 10.01.16 + * Time: 17:34 + */ +public class SubscriptionTest extends TestCase { + @Test + public void testBuilder() { + final Date now = new Date(); + Subscription sub = createMockSubscription(now); + assertNotNull(sub); + assertEquals(now.getTime(), sub.getTrialStart().getTime()); + } + + + public static Subscription createMockSubscription(Date now) { + Subscription sub = new Subscription(); + sub.planId = "free---777"; + sub.trialEnd = now; + sub.trialStart = now; + sub.trialPeriodDays = 666; + sub.isTrial = true; + sub.url = "https://testapi.xplenty.com/api/subscription"; + return sub; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/ListPlansTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/ListPlansTest.java new file mode 100644 index 0000000..931b9d3 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/ListPlansTest.java @@ -0,0 +1,109 @@ +package com.xplenty.api.request.subscription; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Plan; +import com.xplenty.api.model.PlanTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; + +/** + * Author: Xardas + * Date: 10.01.16 + * Time: 17:33 + */ +public class ListPlansTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + ListPlans cc = new ListPlans(); + assertEquals(Xplenty.Resource.Plans.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.Plans.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Plan c = PlanTest.createMockPlan(now); + List planList = new ArrayList<>(); + planList.add(c); + + String json = JsonMapperFactory.getInstance().writeValueAsString(planList); + + ListPlans cc = new ListPlans(); + planList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(planList); + assertTrue(planList.size() > 0); + + c = planList.get(0); + assertNotNull(c); + + assertEquals("Expert", c.getName()); + assertEquals("Expert", c.getDescription()); + assertEquals("USD", c.getPriceCurrency()); + assertEquals("USD", c.getClusterNodePriceCurrency()); + assertEquals(Xplenty.PriceUnit.month, c.getPriceUnit()); + assertEquals(Xplenty.PriceUnit.hour, c.getClusterNodePriceUnit()); + assertEquals(199900, c.getPriceCents().longValue()); + assertEquals(71, c.getClusterNodePriceCents().longValue()); + assertEquals(0, c.getClusterNodeHoursIncluded().intValue()); + assertEquals(-1, c.getClusterNodeHoursLimit().intValue()); + assertEquals(32, c.getClusterNodesLimit().intValue()); + assertEquals(0, c.getClusterSizeLimit().intValue()); + assertEquals(4096, c.getClustersLimit().intValue()); + assertEquals(1, c.getSandboxClustersLimit().intValue()); + assertEquals(0, c.getSandboxNodeHoursIncluded().intValue()); + assertEquals(-1, c.getSandboxNodeHoursLimit().intValue()); + assertEquals(4096, c.getMembersLimit().intValue()); + assertEquals(7, c.getPosition().intValue()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + assertEquals("expert--7", c.getId()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Plan c = PlanTest.createMockPlan(now); + List planList = new ArrayList<>(); + planList.add(c); + String json = JsonMapperFactory.getInstance().writeValueAsString(planList).replace("{", "["); + + try { + ListPlans cc = new ListPlans(); + planList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Plans.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/PaymentMethodInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/PaymentMethodInfoTest.java new file mode 100644 index 0000000..9100bc3 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/PaymentMethodInfoTest.java @@ -0,0 +1,82 @@ +package com.xplenty.api.request.subscription; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.CreditCardInfo; +import com.xplenty.api.model.CreditCardInfoTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * Author: Xardas + * Date: 10.01.16 + * Time: 17:33 + */ +public class PaymentMethodInfoTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + PaymentMehodInfo cc = new PaymentMehodInfo(); + assertEquals(Xplenty.Resource.PaymentMethod.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.PaymentMethod.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + CreditCardInfo c = CreditCardInfoTest.createMockCreditCardInfo(); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + PaymentMehodInfo cc = new PaymentMehodInfo(); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(9876, c.getCardLast4().intValue()); + assertEquals("xxxx-xxxx-xxxx-9876", c.getCardNumber()); + assertEquals("MasterCard", c.getCardType()); + assertEquals("06/66", c.getExpirationDate()); + assertEquals("https://testapi.xplenty.com/api/payment_method", c.getUrl()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + CreditCardInfo c = CreditCardInfoTest.createMockCreditCardInfo(); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + PaymentMehodInfo cc = new PaymentMehodInfo(); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.PaymentMethod.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/SubscriptionInfotest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/SubscriptionInfotest.java new file mode 100644 index 0000000..042aef7 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/SubscriptionInfotest.java @@ -0,0 +1,84 @@ +package com.xplenty.api.request.subscription; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.Subscription; +import com.xplenty.api.model.SubscriptionTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * Author: Xardas + * Date: 10.01.16 + * Time: 17:33 + */ +public class SubscriptionInfoTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + SubscriptionInfo cc = new SubscriptionInfo(); + assertEquals(Xplenty.Resource.Subscription.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.Subscription.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Subscription c = SubscriptionTest.createMockSubscription(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + SubscriptionInfo cc = new SubscriptionInfo(); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(666, c.getTrialPeriodDays().intValue()); + assertEquals("free---777", c.getPlanId()); + assertEquals("https://testapi.xplenty.com/api/subscription", c.getUrl()); + assertTrue(c.isTrial()); + assertTrue(Math.abs(now.getTime() - c.getTrialStart().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getTrialEnd().getTime()) < 1000); //fractions of second are not serialized + + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Subscription c = SubscriptionTest.createMockSubscription(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + SubscriptionInfo cc = new SubscriptionInfo(); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Subscription.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlanTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlanTest.java new file mode 100644 index 0000000..e65b250 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlanTest.java @@ -0,0 +1,82 @@ +package com.xplenty.api.request.subscription; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.CreditCardInfo; +import com.xplenty.api.model.CreditCardInfoTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * Author: Xardas + * Date: 10.01.16 + * Time: 17:33 + */ +public class UpdatePaymentAndPlanTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + UpdatePaymentAndPlan cc = new UpdatePaymentAndPlan("abcd", "free---666"); + assertEquals(Xplenty.Resource.UpdatePaymentMethodAndPlan.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.UpdatePaymentMethodAndPlan.name, cc.getName()); + assertEquals(Http.Method.PUT, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertTrue(cc.hasBody()); + assertNotNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + CreditCardInfo c = CreditCardInfoTest.createMockCreditCardInfo(); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + UpdatePaymentAndPlan cc = new UpdatePaymentAndPlan("abcd", "free---666"); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(9876, c.getCardLast4().intValue()); + assertEquals("xxxx-xxxx-xxxx-9876", c.getCardNumber()); + assertEquals("MasterCard", c.getCardType()); + assertEquals("06/66", c.getExpirationDate()); + assertEquals("https://testapi.xplenty.com/api/payment_method", c.getUrl()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + CreditCardInfo c = CreditCardInfoTest.createMockCreditCardInfo(); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + UpdatePaymentAndPlan cc = new UpdatePaymentAndPlan("abcd", "free---666"); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.UpdatePaymentMethodAndPlan.name + ": error parsing response object", e.getMessage()); + } + + } +} From de10e9fccfd19fadc2bec9f3a9ad2366d1a26574 Mon Sep 17 00:00:00 2001 From: xardas Date: Sun, 10 Jan 2016 21:17:28 +0300 Subject: [PATCH 25/35] changed package entity according to APIv2 added new package methods --- .../main/java/com/xplenty/api/Xplenty.java | 32 +++- .../main/java/com/xplenty/api/XplentyAPI.java | 116 ++++++++++++- .../java/com/xplenty/api/model/Package.java | 158 +++++++++++++----- .../xplenty/api/model/PackageValidation.java | 4 +- .../api/model/PackageValidationError.java | 35 ++++ .../api/request/xpackage/CreatePackage.java | 38 +++++ .../api/request/xpackage/DeletePackage.java | 26 +++ .../xpackage/ListPackageTemplates.java | 43 +++++ .../xpackage/ListPackageValidations.java | 54 ++++++ .../api/request/xpackage/ListPackages.java | 11 +- .../xpackage/PackageValidationInfo.java | 30 ++++ .../xpackage/RunPackageValidation.java | 34 ++++ .../api/request/xpackage/UpdatePackage.java | 39 +++++ 13 files changed, 567 insertions(+), 53 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidationError.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/CreatePackage.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/DeletePackage.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageTemplates.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageValidationInfo.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/RunPackageValidation.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/UpdatePackage.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index ca7a983..2d94821 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -17,11 +17,10 @@ public class Xplenty { public enum Sort { updated("updated"), created("created"), - // todo include all entities that support name sort /** * Note that name sort is available only for {@link com.xplenty.api.model.PublicKey Public Key List}, * {@link com.xplenty.api.model.Member Member List}, {@link com.xplenty.api.model.Account Account List}, - * {@link com.xplenty.api.model.Connection Connection List} + * {@link com.xplenty.api.model.Connection Connection List}, {@link com.xplenty.api.model.Package Package list} */ name("name"), /** @@ -222,7 +221,11 @@ public enum ConnectionType { public enum PackageValidationStatus { running, completed, - failed; + failed, + /** + * used only for filtering meaning all the statuses above + */ + all; } public enum PriceUnit { @@ -259,8 +262,22 @@ public String toString() { } } + public enum PackageStatus { + active, + archived; + } + + public enum PackageFlowType { + dataflow, + workflow, + /** + * used only for filtering meaning all the types above + */ + all; + } + - @JsonFormat(shape= JsonFormat.Shape.SCALAR) + @JsonFormat(shape = JsonFormat.Shape.SCALAR) public static enum ClusterStatus { /** * the cluster is pending creation @@ -310,6 +327,13 @@ public static enum ClusterStatus { public static enum Resource { Package("packages/%s", "Get package info"), Packages("packages", "List packages"), + CreatePackage("packages", "Create new package"), + UpdatePackage("packages/%s", "Update package"), + DeletePackage("packages/%s", "Delete package"), + PackageTemplates("packages/templates", "List package Templates"), + RunPackageValidation("packages/%s/validations", "Run new package validation process"), + PackageValidation("packages/%s/validations/%s", "Get package validation information"), + PackageValidations("packages/%s/validations", "List package validations"), ClusterPlans("cluster_plans", "List cluster plans"), Clusters("clusters", "List clusters"), ClusterInstances("clusters/%s/instances", "List cluster instances"), diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index 5471dec..f8caca7 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -42,8 +42,7 @@ import com.xplenty.api.request.watching.ListWatchers; import com.xplenty.api.request.watching.WatchingStop; import com.xplenty.api.request.webhook.*; -import com.xplenty.api.request.xpackage.ListPackages; -import com.xplenty.api.request.xpackage.PackageInfo; +import com.xplenty.api.request.xpackage.*; import java.util.List; import java.util.Map; @@ -108,6 +107,14 @@ public Package getPackageInfo(long packageId) { return client.execute(new PackageInfo(packageId)); } + /** + * List of packages associated with the account + * @return list of packages + */ + public List listPackages() { + return listPackages(new Properties()); + } + /** * List of packages associated with the account * @param props map of request parameters @@ -118,10 +125,10 @@ public List listPackages(Properties props) { } /** - * List of schedules associated with the account + * List of packages associated with the account * @param offset number of record to start results from * @param limit number of results - * @return list of schedules + * @return list of packages */ public List listPackages(int offset, int limit) { final Properties props = new Properties(); @@ -130,6 +137,107 @@ public List listPackages(int offset, int limit) { return listPackages(props); } + /** + * List package templates that are available for the authenticated user. + * You can use template to create new package with predefined settings. + * @return list of package templates + */ + public List listPackageTemplates() { + return client.execute(new ListPackageTemplates()); + } + + /** + * List validations for specific package. + * Optionally, you can supply the input parameters to filter the validation list so that it contains only validations + * with a specific status, and to determine the order by which the list will be sorted. + * @param packageId id of package to get validation list for + * @return list of package validations + */ + public List listPackageValidations(long packageId) { + return listPackageValidations(packageId, new Properties()); + } + + /** + * List validations for specific package. + * Optionally, you can supply the input parameters to filter the validation list so that it contains only validations + * with a specific status, and to determine the order by which the list will be sorted. + * @param packageId id of package to get validation list for + * @param props map of request parameters + * @return list of package validations + */ + public List listPackageValidations(long packageId, Properties props) { + checkId(packageId); + return client.execute(new ListPackageValidations(props, packageId)); + } + + /** + * List validations for specific package. + * Optionally, you can supply the input parameters to filter the validation list so that it contains only validations + * with a specific status, and to determine the order by which the list will be sorted. + * @param packageId id of package to get validation list for + * @param offset number of record to start results from + * @param limit number of results + * @return list of package validations + */ + public List listPackageValidations(long packageId, int offset, int limit) { + final Properties props = new Properties(); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); + return listPackageValidations(packageId, props); + } + + /** + * Create a new package. + * @param xpackage package object with properties properly set + * @return newly created package object + */ + public Package createPackage(Package xpackage) { + xpackage.withId(null); + return client.execute(new CreatePackage(xpackage)); + } + + /** + * Update an existing package. + * @param xpackage package object with properties properly set + * @return updated package object + */ + public Package updatePackage(Package xpackage) { + checkId(xpackage.getId()); + return client.execute(new UpdatePackage(xpackage)); + } + + /** + * Delete an existing package. + * @param packageId id of package to delete + * @return deleted package object + */ + public Package deletePackage(long packageId) { + checkId(packageId); + return client.execute(new DeletePackage(packageId)); + } + + /** + * Runs new validation process for the package and returns information about status and tracking url. + * @param packageId id of package to validate + * @return package validation object + */ + public PackageValidation runPackageValidation(long packageId) { + checkId(packageId); + return client.execute(new RunPackageValidation(packageId)); + } + + /** + * Returns information about progress of the package validation process. + * @param packageId id of package + * @param validationId id of validation for that package + * @return package validation object + */ + public PackageValidation getPackageValidationInfo(long packageId, long validationId) { + checkId(packageId); + checkId(validationId); + return client.execute(new PackageValidationInfo(validationId, packageId)); + } + /** * List of schedules associated with the account * @return list of schedules diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Package.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Package.java index a5402bb..6153edf 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Package.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Package.java @@ -1,8 +1,7 @@ package com.xplenty.api.model; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; import java.util.Date; import java.util.Map; @@ -13,10 +12,7 @@ * Date: 16.12.15 * Time: 18:08 */ -@JsonInclude(JsonInclude.Include.NON_NULL) -@JsonIgnoreProperties(ignoreUnknown = true) public class Package extends XplentyObject { - @JsonProperty protected Long id; @JsonProperty @@ -33,11 +29,106 @@ public class Package extends XplentyObject { protected Date updatedAt; @JsonProperty protected String url; - - protected Package() { + @JsonProperty("html_url") + protected String htmlUrl; + @JsonProperty + protected Xplenty.PackageStatus status; + @JsonProperty("source_package_id") + protected Long sourcePackageId; + @JsonProperty("package_template_id") + protected Long packageTemplateId; + @JsonProperty("data_flow_json") + protected String dataFlowJson; + @JsonProperty("flow_type") + protected Xplenty.PackageFlowType flowType; + + public Package() { super(Package.class); } + /** + * Set package id. Used for updates. + * @param id id of the package + * @return this instance + */ + public Package withId(Long id) { + this.id = id; + return this; + } + + /** + * If it is provided, the new package will be a copy of package with this ID + * @param sourcePackageId source package id + * @return this instance + */ + public Package withSourcePackageId(Long sourcePackageId) { + this.sourcePackageId = sourcePackageId; + return this; + } + + + /** + * If it is provided, the new package will be created based on the template that is associated with this ID + * @param packageTemplateId template id + * @return this instance + */ + public Package fromTemplate(Long packageTemplateId) { + this.packageTemplateId = packageTemplateId; + return this; + } + + + /** + * Data flow prepared in JSON format + * @param dataFlowJson json string with data flow + * @return this instance + */ + public Package withDataFlow(String dataFlowJson) { + this.dataFlowJson = dataFlowJson; + return this; + } + + /** + * Set package name + * @param name package name + * @return this instance + */ + public Package withName(String name) { + this.name = name; + return this; + } + + /** + * Set package description + * @param description package description + * @return this instance + */ + public Package withDescription(String description) { + this.description = description; + return this; + } + + + /** + * Set package variables + * @param variables variables for this package + * @return this instance + */ + public Package withVariables(Map variables) { + this.variables = variables; + return this; + } + + /** + * Set flow type of the package + * @param flowType flow type of this package + * @return this instance + */ + public Package withFlowType(Xplenty.PackageFlowType flowType) { + this.flowType = flowType; + return this; + } + /** * @@ -97,49 +188,32 @@ public Date getUpdatedAt() { /** * - * @return the package resource URL + * @return the package resource URL (API) */ public String getUrl() { return url; } - @SuppressWarnings("unused") - private void setId(Long id) { - this.id = id; - } - - @SuppressWarnings("unused") - private void setName(String name) { - this.name = name; - } - - @SuppressWarnings("unused") - private void setDescription(String description) { - this.description = description; - } - - @SuppressWarnings("unused") - private void setVariables(Map variables) { - this.variables = variables; - } - - @SuppressWarnings("unused") - private void setOwnerId(Long ownerId) { - this.ownerId = ownerId; - } - - @SuppressWarnings("unused") - private void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; + /** + * + * @return the package resource URL (Web UI) + */ + public String getHtmlUrl() { + return htmlUrl; } - @SuppressWarnings("unused") - private void setUpdatedAt(Date updatedAt) { - this.updatedAt = updatedAt; + /** + * + * @return the package status. + */ + public Xplenty.PackageStatus getStatus() { + return status; } - @SuppressWarnings("unused") - private void setUrl(String url) { - this.url = url; + /** + * Flow type of the package. + */ + public Xplenty.PackageFlowType getFlowType() { + return flowType; } } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java index 15fcd8e..09d9f9d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java @@ -29,7 +29,7 @@ public class PackageValidation extends XplentyObject { @JsonProperty("account_id") private Long accountId; @JsonProperty - private List errors; + private List errors; @JsonProperty("created_at") private Date createdAt; @JsonProperty("updated_at") @@ -93,7 +93,7 @@ public Long getAccountId() { /** * @return the list of the errors which were detected in the validation process */ - public List getErrors() { + public List getErrors() { return errors; } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidationError.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidationError.java new file mode 100644 index 0000000..72cd08e --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidationError.java @@ -0,0 +1,35 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Data model for error, that occured while validating package + * Author: Xardas + * Date: 10.01.16 + * Time: 19:26 + */ +public class PackageValidationError { + @JsonProperty + protected String message; + @JsonProperty("component_id") + protected String componentId; + + protected PackageValidationError() { + } + + /** + * + * @return error message + */ + public String getMessage() { + return message; + } + + /** + * + * @return id of component which this error occured at + */ + public String getComponentId() { + return componentId; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/CreatePackage.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/CreatePackage.java new file mode 100644 index 0000000..cb34942 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/CreatePackage.java @@ -0,0 +1,38 @@ +package com.xplenty.api.request.xpackage; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.Package; +import com.xplenty.api.request.AbstractManipulationRequest; + +/** + * Create a new package. + * Author: Xardas + * Date: 10.01.16 + * Time: 18:32 + */ +public class CreatePackage extends AbstractManipulationRequest { + public CreatePackage(Package entity) { + super(entity); + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.CreatePackage.value; + } + + @Override + public String getName() { + return Xplenty.Resource.CreatePackage.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.POST; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/DeletePackage.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/DeletePackage.java new file mode 100644 index 0000000..6f1b67d --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/DeletePackage.java @@ -0,0 +1,26 @@ +package com.xplenty.api.request.xpackage; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.request.AbstractDeleteRequest; + +/** + * Delete an existing package. + * Author: Xardas + * Date: 10.01.16 + * Time: 20:10 + */ +public class DeletePackage extends AbstractDeleteRequest { + public DeletePackage(long entityId) { + super(entityId); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.DeletePackage.format(String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.DeletePackage.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageTemplates.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageTemplates.java new file mode 100644 index 0000000..b576a71 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageTemplates.java @@ -0,0 +1,43 @@ +package com.xplenty.api.request.xpackage; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.PackageTemplate; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; + +/** + * List package templates that are available for the authenticated user. + * You can use template to create new package with predefined settings. + * Author: Xardas + * Date: 10.01.16 + * Time: 19:58 + */ +public class ListPackageTemplates extends AbstractListRequest> { + + public ListPackageTemplates() { + super(null, false); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.PackageTemplates.value; + } + + @Override + public String getName() { + return Xplenty.Resource.PackageTemplates.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java new file mode 100644 index 0000000..373dbdd --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java @@ -0,0 +1,54 @@ +package com.xplenty.api.request.xpackage; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.PackageValidation; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; +import java.util.Properties; + +/** + * List validations for specific package. + * Optionally, you can supply the input parameters to filter the validation list so that it contains only validations + * with a specific status, and to determine the order by which the list will be sorted. + * Author: Xardas + * Date: 10.01.16 + * Time: 19:41 + */ +public class ListPackageValidations extends AbstractListRequest> { + private final long packageId; + + public ListPackageValidations(Properties parameters, long packageId) { + super(parameters, true); + this.packageId = packageId; + } + + private void validateParameters(Properties params) { + if (params.containsKey(PARAMETER_STATUS) + && !(params.get(PARAMETER_STATUS) instanceof Xplenty.PackageFlowType)) { + throw new XplentyAPIException(String.format("Invalid %s parameter, should be one of PackageValidationStatus values", PARAMETER_STATUS)); + } + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.PackageValidations.format(String.valueOf(packageId)); + } + + @Override + public String getName() { + return Xplenty.Resource.PackageValidations.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java index 9307447..195016f 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java @@ -21,11 +21,20 @@ * Time: 18:08 */ public class ListPackages extends AbstractListRequest> { + public static String PARAMETER_FLOW_TYPE = "flow_type"; public ListPackages(Properties params) { - super(params, false); + super(params, true); + validateParameters(params); } + private void validateParameters(Properties params) { + if (params.containsKey(PARAMETER_FLOW_TYPE) + && !(params.get(PARAMETER_FLOW_TYPE) instanceof Xplenty.PackageFlowType)) { + throw new XplentyAPIException(String.format("Invalid %s parameter, should be one of PackageFlowType values", PARAMETER_FLOW_TYPE)); + } + } + @Override protected String getEndpointRoot() { return Xplenty.Resource.Packages.value; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageValidationInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageValidationInfo.java new file mode 100644 index 0000000..b39cc3e --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageValidationInfo.java @@ -0,0 +1,30 @@ +package com.xplenty.api.request.xpackage; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.PackageValidation; +import com.xplenty.api.request.AbstractInfoRequest; + +/** + * Returns information about progress of the package validation process. + * Author: Xardas + * Date: 10.01.16 + * Time: 19:19 + */ +public class PackageValidationInfo extends AbstractInfoRequest { + private final long packageId; + + public PackageValidationInfo(long entityId, long packageId) { + super(entityId); + this.packageId = packageId; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.PackageValidation.format(String.valueOf(packageId), String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.PackageValidation.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/RunPackageValidation.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/RunPackageValidation.java new file mode 100644 index 0000000..0531fbf --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/RunPackageValidation.java @@ -0,0 +1,34 @@ +package com.xplenty.api.request.xpackage; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.PackageValidation; +import com.xplenty.api.request.AbstractDeleteRequest; + +/** + * Runs new validation process for the package and returns information about status and tracking url. + * Author: Xardas + * Date: 10.01.16 + * Time: 19:11 + */ +public class RunPackageValidation extends AbstractDeleteRequest { + + public RunPackageValidation(long entityId) { + super(entityId); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.RunPackageValidation.format(String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.RunPackageValidation.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.POST; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/UpdatePackage.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/UpdatePackage.java new file mode 100644 index 0000000..811ddfe --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/UpdatePackage.java @@ -0,0 +1,39 @@ +package com.xplenty.api.request.xpackage; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.Package; +import com.xplenty.api.request.AbstractManipulationRequest; + +/** + * Update an existing package. + * Author: Xardas + * Date: 10.01.16 + * Time: 19:03 + */ +public class UpdatePackage extends AbstractManipulationRequest { + + public UpdatePackage(Package entity) { + super(entity); + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.UpdatePackage.format(entity.getId().toString()); + } + + @Override + public String getName() { + return Xplenty.Resource.UpdatePackage.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.PUT; + } +} From c103aeb6f01e6d4521550f907d5c1b4f9d67fc22 Mon Sep 17 00:00:00 2001 From: xardas Date: Mon, 11 Jan 2016 18:46:27 +0300 Subject: [PATCH 26/35] add tests --- .../main/java/com/xplenty/api/XplentyAPI.java | 2 +- .../xplenty/api/model/PackageTemplate.java | 10 +- .../api/model/PackageTemplateAuthor.java | 6 +- .../xplenty/api/model/PackageValidation.java | 22 ++-- .../xpackage/ListPackageValidations.java | 2 +- .../com/xplenty/api/model/PackageTest.java | 108 ++++++++++++++++ .../request/xpackage/CreatePackageTest.java | 101 +++++++++++++++ .../request/xpackage/DeletePackageTest.java | 101 +++++++++++++++ .../xpackage/ListPackageTemplatesTest.java | 101 +++++++++++++++ .../xpackage/ListPackageValidationsTest.java | 118 ++++++++++++++++++ .../request/xpackage/ListPackagesTest.java | 118 ++++++++++++++++++ .../api/request/xpackage/PackageInfoTest.java | 101 +++++++++++++++ .../xpackage/PackageValidationInfoTest.java | 100 +++++++++++++++ .../xpackage/RunPackageValidationTest.java | 103 +++++++++++++++ .../request/xpackage/UpdatePackageTest.java | 101 +++++++++++++++ 15 files changed, 1073 insertions(+), 21 deletions(-) create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/model/PackageTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/CreatePackageTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/DeletePackageTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackageTemplatesTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackageValidationsTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageValidationInfoTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/RunPackageValidationTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/UpdatePackageTest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index f8caca7..f27163a 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -167,7 +167,7 @@ public List listPackageValidations(long packageId) { */ public List listPackageValidations(long packageId, Properties props) { checkId(packageId); - return client.execute(new ListPackageValidations(props, packageId)); + return client.execute(new ListPackageValidations(packageId, props)); } /** diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplate.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplate.java index fa09938..7e3b12d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplate.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplate.java @@ -13,15 +13,15 @@ */ public class PackageTemplate extends XplentyObject { @JsonProperty - private Long id; + protected Long id; @JsonProperty - private String name; + protected String name; @JsonProperty - private String description; + protected String description; @JsonProperty - private Integer position; + protected Integer position; @JsonProperty - private PackageTemplateAuthor author; + protected PackageTemplateAuthor author; protected PackageTemplate() { super(PackageTemplate.class); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java index 9e5fd9b..0a4c34b 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java @@ -9,11 +9,11 @@ */ public class PackageTemplateAuthor { @JsonProperty - private Long id; + protected Long id; @JsonProperty - private String name; + protected String name; @JsonProperty("avatar_url") - private String avatarUrl; + protected String avatarUrl; protected PackageTemplateAuthor() { } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java index 09d9f9d..a7b13e8 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java @@ -15,27 +15,27 @@ */ public class PackageValidation extends XplentyObject { @JsonProperty - private Long id; + protected Long id; @JsonProperty - private Xplenty.PackageValidationStatus status; + protected Xplenty.PackageValidationStatus status; @JsonProperty("status_message") - private String statusMessage; + protected String statusMessage; @JsonProperty - private Long runtime; + protected Long runtime; @JsonProperty("package_id") - private Long packageId; + protected Long packageId; @JsonProperty("owner_id") - private Long ownerId; + protected Long ownerId; @JsonProperty("account_id") - private Long accountId; + protected Long accountId; @JsonProperty - private List errors; + protected List errors; @JsonProperty("created_at") - private Date createdAt; + protected Date createdAt; @JsonProperty("updated_at") - private Date updatedAt; + protected Date updatedAt; @JsonProperty - private String url; + protected String url; protected PackageValidation() { super(PackageValidation.class); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java index 373dbdd..6227d6f 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java @@ -21,7 +21,7 @@ public class ListPackageValidations extends AbstractListRequest> { private final long packageId; - public ListPackageValidations(Properties parameters, long packageId) { + public ListPackageValidations(long packageId, Properties parameters) { super(parameters, true); this.packageId = packageId; } diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/PackageTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/PackageTest.java new file mode 100644 index 0000000..02a62f4 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/PackageTest.java @@ -0,0 +1,108 @@ +package com.xplenty.api.model; + +import com.xplenty.api.Xplenty; +import junit.framework.TestCase; +import org.junit.Test; + +import java.util.*; + +/** + * Author: Xardas + * Date: 11.01.16 + * Time: 16:38 + */ +public class PackageTest extends TestCase { + @Test + public void testBuilder() { + final Date now = new Date(); + Package pack = createMockPackage(now); + assertNotNull(pack); + assertEquals(now.getTime(), pack.getCreatedAt().getTime()); + + pack = createMockPackageForCreation(); + assertNotNull(pack); + assertEquals("name", pack.getName()); + assertEquals("somestr", pack.dataFlowJson); + assertEquals("testdesc", pack.getDescription()); + assertEquals(666, pack.getId().longValue()); + assertEquals(777, pack.packageTemplateId.longValue()); + assertEquals(222, pack.sourcePackageId.longValue()); + assertEquals(Xplenty.PackageFlowType.dataflow, pack.getFlowType()); + assertNotNull(pack.getVariables()); + assertEquals("super$$$\"complex'val\n\t", pack.getVariables().get("var_2")); + + PackageValidation pv = createMockPackageValidation(now); + assertNotNull(pv); + assertEquals(now.getTime(), pv.getCreatedAt().getTime()); + + PackageTemplate pta = createMockPackageTemplate(); + assertNotNull(pta); + assertEquals(666, pta.getId().longValue()); + } + + public static Package createMockPackageForCreation() { + Map packVars = new HashMap<>(); + packVars.put("var_1", "val1"); + packVars.put("var_2", "super$$$\"complex'val\n\t"); + Package pack = new Package().fromTemplate(777L).withDataFlow("somestr").withDescription("testdesc"). + withName("name").withFlowType(Xplenty.PackageFlowType.dataflow).withSourcePackageId(222L). + withId(666L).withVariables(packVars); + return pack; + } + + + public static Package createMockPackage(Date now) { + Package pack = new Package(); + pack.id = 666L; + pack.name = "TestPack"; + pack.description = "TestPack Description"; + pack.flowType = Xplenty.PackageFlowType.workflow; + pack.ownerId = 111L; + pack.url = "https://testapi.xplenty.com/api/package/666"; + pack.htmlUrl = "https://testapi.xplenty.com/package/666"; + pack.status = Xplenty.PackageStatus.active; + Map packVars = new HashMap<>(); + packVars.put("var_1", "val1"); + packVars.put("var_2", "super$$$\"complex'val\n\t"); + pack.variables = packVars; + pack.createdAt = now; + pack.updatedAt = now; + + return pack; + } + + public static PackageValidation createMockPackageValidation(Date now) { + PackageValidation pv = new PackageValidation(); + pv.id = 666L; + pv.accountId = 111L; + pv.ownerId = 222L; + pv.packageId = 777L; + pv.status = Xplenty.PackageValidationStatus.failed; + pv.statusMessage = "Something bad happened"; + pv.url = "https://testapi.xplenty.com/api/packages/777/validations/666"; + pv.runtime = 1234L; + pv.updatedAt = now; + pv.createdAt = now; + List errors = new ArrayList<>(); + PackageValidationError err = new PackageValidationError(); + err.componentId = "12"; + err.message = "couldn't obtain value for var_1"; + errors.add(err); + pv.errors = errors; + return pv; + } + + public static PackageTemplate createMockPackageTemplate() { + PackageTemplate pt = new PackageTemplate(); + pt.id = 666L; + pt.name = "test template"; + pt.description = "really good template"; + pt.position = 1; + PackageTemplateAuthor pta = new PackageTemplateAuthor(); + pta.id = 333L; + pta.name = "best template author"; + pta.avatarUrl = "https://testapi.xplenty.com/api/user/333"; + pt.author = pta; + return pt; + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/CreatePackageTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/CreatePackageTest.java new file mode 100644 index 0000000..dd82313 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/CreatePackageTest.java @@ -0,0 +1,101 @@ +/** + * + */ +package com.xplenty.api.request.xpackage; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MemberTest; +import com.xplenty.api.model.Package; +import com.xplenty.api.model.PackageTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * @author xardas + * + */ +public class CreatePackageTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Date now = new Date(); + CreatePackage cc = new CreatePackage(PackageTest.createMockPackageForCreation()); + assertEquals(Xplenty.Resource.CreatePackage.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.CreatePackage.name, cc.getName()); + assertEquals(Http.Method.POST, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertTrue(cc.hasBody()); + assertNotNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Package c = PackageTest.createMockPackage(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(MemberTest.createMockMemberForCreation()); + assertNotNull(json); + + json = JsonMapperFactory.getInstance().writeValueAsString(c); + + CreatePackage cc = new CreatePackage(PackageTest.createMockPackageForCreation()); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("TestPack", c.getName()); + assertEquals("TestPack Description", c.getDescription()); + assertEquals(Xplenty.PackageFlowType.workflow, c.getFlowType()); + assertEquals("https://testapi.xplenty.com/api/package/666", c.getUrl()); + assertEquals("https://testapi.xplenty.com/package/666", c.getHtmlUrl()); + assertEquals(Xplenty.PackageStatus.active, c.getStatus()); + assertEquals(111, c.getOwnerId().longValue()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + Map packVars = c.getVariables(); + assertNotNull(packVars); + assertEquals("val1", packVars.get("var_1")); + assertEquals("super$$$\"complex'val\n\t", packVars.get("var_2")); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Package c = PackageTest.createMockPackage(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + CreatePackage cc = new CreatePackage(PackageTest.createMockPackageForCreation()); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.CreatePackage.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/DeletePackageTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/DeletePackageTest.java new file mode 100644 index 0000000..d52df29 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/DeletePackageTest.java @@ -0,0 +1,101 @@ +/** + * + */ +package com.xplenty.api.request.xpackage; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MemberTest; +import com.xplenty.api.model.Package; +import com.xplenty.api.model.PackageTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * @author xardas + * + */ +public class DeletePackageTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Date now = new Date(); + DeletePackage cc = new DeletePackage(666); + assertEquals(Xplenty.Resource.DeletePackage.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.DeletePackage.name, cc.getName()); + assertEquals(Http.Method.DELETE, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Package c = PackageTest.createMockPackage(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(MemberTest.createMockMemberForCreation()); + assertNotNull(json); + + json = JsonMapperFactory.getInstance().writeValueAsString(c); + + DeletePackage cc = new DeletePackage(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("TestPack", c.getName()); + assertEquals("TestPack Description", c.getDescription()); + assertEquals(Xplenty.PackageFlowType.workflow, c.getFlowType()); + assertEquals("https://testapi.xplenty.com/api/package/666", c.getUrl()); + assertEquals("https://testapi.xplenty.com/package/666", c.getHtmlUrl()); + assertEquals(Xplenty.PackageStatus.active, c.getStatus()); + assertEquals(111, c.getOwnerId().longValue()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + Map packVars = c.getVariables(); + assertNotNull(packVars); + assertEquals("val1", packVars.get("var_1")); + assertEquals("super$$$\"complex'val\n\t", packVars.get("var_2")); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Package c = PackageTest.createMockPackage(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + DeletePackage cc = new DeletePackage(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.DeletePackage.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackageTemplatesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackageTemplatesTest.java new file mode 100644 index 0000000..a3513a2 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackageTemplatesTest.java @@ -0,0 +1,101 @@ +/** + * + */ +package com.xplenty.api.request.xpackage; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MemberTest; +import com.xplenty.api.model.PackageTemplate; +import com.xplenty.api.model.PackageTemplateAuthor; +import com.xplenty.api.model.PackageTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * @author xardas + * + */ +public class ListPackageTemplatesTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + ListPackageTemplates cc = new ListPackageTemplates(); + assertEquals(Xplenty.Resource.PackageTemplates.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.PackageTemplates.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + PackageTemplate c = PackageTest.createMockPackageTemplate(); + List packList = new ArrayList<>(); + packList.add(c); + + String json = JsonMapperFactory.getInstance().writeValueAsString(MemberTest.createMockMemberForCreation()); + assertNotNull(json); + + json = JsonMapperFactory.getInstance().writeValueAsString(packList); + + ListPackageTemplates cc = new ListPackageTemplates(); + packList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(packList); + assertTrue(packList.size() > 0); + + c = packList.get(0); + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("test template", c.getName()); + assertEquals("really good template", c.getDescription()); + assertEquals(1, c.getPosition().longValue()); + + PackageTemplateAuthor pta = c.getAuthor(); + assertNotNull(pta); + assertEquals(333, pta.getId().longValue()); + assertEquals("best template author", pta.getName()); + assertEquals("https://testapi.xplenty.com/api/user/333", pta.getAvatarUrl()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + PackageTemplate c = PackageTest.createMockPackageTemplate(); + List packList = new ArrayList<>(); + packList.add(c); + String json = JsonMapperFactory.getInstance().writeValueAsString(packList).replace("{", "["); + + try { + ListPackageTemplates cc = new ListPackageTemplates(); + packList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.PackageTemplates.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackageValidationsTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackageValidationsTest.java new file mode 100644 index 0000000..1167b44 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackageValidationsTest.java @@ -0,0 +1,118 @@ +/** + * + */ +package com.xplenty.api.request.xpackage; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MemberTest; +import com.xplenty.api.model.PackageTest; +import com.xplenty.api.model.PackageValidation; +import com.xplenty.api.model.PackageValidationError; +import com.xplenty.api.request.AbstractListRequest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; + +/** + * @author xardas + * + */ +public class ListPackageValidationsTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Properties params = new Properties(); + ListPackageValidations cc = new ListPackageValidations(666, params); + assertEquals(Xplenty.Resource.PackageValidations.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.PackageValidations.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + + params.put(AbstractListRequest.PARAMETER_SORT, Xplenty.Sort.name); + params.put(AbstractListRequest.PARAMETER_DIRECTION, Xplenty.SortDirection.descending); + params.put(ListPackageValidations.PARAMETER_STATUS, Xplenty.PackageValidationStatus.running); + cc = new ListPackageValidations(666, params); + assertEquals(Xplenty.Resource.PackageValidations.format(String.valueOf(666)) + "?" + + ListPackageValidations.PARAMETER_STATUS + "=running&" + + AbstractListRequest.PARAMETER_SORT + "=name&" + + AbstractListRequest.PARAMETER_DIRECTION + "=desc", cc.getEndpoint()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + PackageValidation c = PackageTest.createMockPackageValidation(now); + List packList = new ArrayList<>(); + packList.add(c); + + String json = JsonMapperFactory.getInstance().writeValueAsString(MemberTest.createMockMemberForCreation()); + assertNotNull(json); + + json = JsonMapperFactory.getInstance().writeValueAsString(packList); + + ListPackageValidations cc = new ListPackageValidations(666, new Properties()); + packList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(packList); + assertTrue(packList.size() > 0); + + c = packList.get(0); + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("Something bad happened", c.getStatusMessage()); + assertEquals("https://testapi.xplenty.com/api/packages/777/validations/666", c.getUrl()); + assertEquals(Xplenty.PackageValidationStatus.failed, c.getStatus()); + assertEquals(222, c.getOwnerId().longValue()); + assertEquals(111, c.getAccountId().longValue()); + assertEquals(777, c.getPackageId().longValue()); + assertEquals(1234, c.getRuntime().longValue()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + List errors = c.getErrors(); + assertNotNull(errors); + assertEquals("12", errors.get(0).getComponentId()); + assertEquals("couldn't obtain value for var_1", errors.get(0).getMessage()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + PackageValidation c = PackageTest.createMockPackageValidation(now); + List packList = new ArrayList<>(); + packList.add(c); + String json = JsonMapperFactory.getInstance().writeValueAsString(packList).replace("{", "["); + + try { + ListPackageValidations cc = new ListPackageValidations(666, new Properties()); + packList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.PackageValidations.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java new file mode 100644 index 0000000..bbdaaa8 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java @@ -0,0 +1,118 @@ +/** + * + */ +package com.xplenty.api.request.xpackage; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MemberTest; +import com.xplenty.api.model.Package; +import com.xplenty.api.model.PackageTest; +import com.xplenty.api.request.AbstractListRequest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; + +/** + * @author xardas + * + */ +public class ListPackagesTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Date now = new Date(); + final Properties params = new Properties(); + ListPackages cc = new ListPackages(params); + assertEquals(Xplenty.Resource.Packages.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.Packages.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + + params.put(AbstractListRequest.PARAMETER_SORT, Xplenty.Sort.name); + params.put(AbstractListRequest.PARAMETER_DIRECTION, Xplenty.SortDirection.descending); + params.put(ListPackages.PARAMETER_FLOW_TYPE, Xplenty.PackageFlowType.dataflow); + cc = new ListPackages(params); + assertEquals(Xplenty.Resource.Packages.value + "?" + + ListPackages.PARAMETER_FLOW_TYPE + "=dataflow&" + + AbstractListRequest.PARAMETER_SORT + "=name&" + + AbstractListRequest.PARAMETER_DIRECTION + "=desc", cc.getEndpoint()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Package c = PackageTest.createMockPackage(now); + List packList = new ArrayList<>(); + packList.add(c); + + String json = JsonMapperFactory.getInstance().writeValueAsString(MemberTest.createMockMemberForCreation()); + assertNotNull(json); + + json = JsonMapperFactory.getInstance().writeValueAsString(packList); + + ListPackages cc = new ListPackages(new Properties()); + packList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(packList); + assertTrue(packList.size() > 0); + + c = packList.get(0); + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("TestPack", c.getName()); + assertEquals("TestPack Description", c.getDescription()); + assertEquals(Xplenty.PackageFlowType.workflow, c.getFlowType()); + assertEquals("https://testapi.xplenty.com/api/package/666", c.getUrl()); + assertEquals("https://testapi.xplenty.com/package/666", c.getHtmlUrl()); + assertEquals(Xplenty.PackageStatus.active, c.getStatus()); + assertEquals(111, c.getOwnerId().longValue()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + Map packVars = c.getVariables(); + assertNotNull(packVars); + assertEquals("val1", packVars.get("var_1")); + assertEquals("super$$$\"complex'val\n\t", packVars.get("var_2")); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Package c = PackageTest.createMockPackage(now); + List packList = new ArrayList<>(); + packList.add(c); + String json = JsonMapperFactory.getInstance().writeValueAsString(packList).replace("{", "["); + + try { + ListPackages cc = new ListPackages(new Properties()); + packList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Packages.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java new file mode 100644 index 0000000..b9adfe2 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java @@ -0,0 +1,101 @@ +/** + * + */ +package com.xplenty.api.request.xpackage; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MemberTest; +import com.xplenty.api.model.Package; +import com.xplenty.api.model.PackageTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * @author xardas + * + */ +public class PackageInfoTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Date now = new Date(); + PackageInfo cc = new PackageInfo(666); + assertEquals(Xplenty.Resource.Package.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.Package.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Package c = PackageTest.createMockPackage(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(MemberTest.createMockMemberForCreation()); + assertNotNull(json); + + json = JsonMapperFactory.getInstance().writeValueAsString(c); + + PackageInfo cc = new PackageInfo(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("TestPack", c.getName()); + assertEquals("TestPack Description", c.getDescription()); + assertEquals(Xplenty.PackageFlowType.workflow, c.getFlowType()); + assertEquals("https://testapi.xplenty.com/api/package/666", c.getUrl()); + assertEquals("https://testapi.xplenty.com/package/666", c.getHtmlUrl()); + assertEquals(Xplenty.PackageStatus.active, c.getStatus()); + assertEquals(111, c.getOwnerId().longValue()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + Map packVars = c.getVariables(); + assertNotNull(packVars); + assertEquals("val1", packVars.get("var_1")); + assertEquals("super$$$\"complex'val\n\t", packVars.get("var_2")); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Package c = PackageTest.createMockPackage(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + PackageInfo cc = new PackageInfo(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Package.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageValidationInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageValidationInfoTest.java new file mode 100644 index 0000000..6db3235 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageValidationInfoTest.java @@ -0,0 +1,100 @@ +/** + * + */ +package com.xplenty.api.request.xpackage; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MemberTest; +import com.xplenty.api.model.PackageTest; +import com.xplenty.api.model.PackageValidation; +import com.xplenty.api.model.PackageValidationError; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; + +/** + * @author xardas + * + */ +public class PackageValidationInfoTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Properties params = new Properties(); + PackageValidationInfo cc = new PackageValidationInfo(666, 777); + assertEquals(Xplenty.Resource.PackageValidation.format(String.valueOf(777), String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.PackageValidation.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + PackageValidation c = PackageTest.createMockPackageValidation(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(MemberTest.createMockMemberForCreation()); + assertNotNull(json); + + json = JsonMapperFactory.getInstance().writeValueAsString(c); + + PackageValidationInfo cc = new PackageValidationInfo(666, 777); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("Something bad happened", c.getStatusMessage()); + assertEquals("https://testapi.xplenty.com/api/packages/777/validations/666", c.getUrl()); + assertEquals(Xplenty.PackageValidationStatus.failed, c.getStatus()); + assertEquals(222, c.getOwnerId().longValue()); + assertEquals(111, c.getAccountId().longValue()); + assertEquals(777, c.getPackageId().longValue()); + assertEquals(1234, c.getRuntime().longValue()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + List errors = c.getErrors(); + assertNotNull(errors); + assertEquals("12", errors.get(0).getComponentId()); + assertEquals("couldn't obtain value for var_1", errors.get(0).getMessage()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + PackageValidation c = PackageTest.createMockPackageValidation(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + PackageValidationInfo cc = new PackageValidationInfo(666, 777); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.PackageValidation.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/RunPackageValidationTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/RunPackageValidationTest.java new file mode 100644 index 0000000..c1925f8 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/RunPackageValidationTest.java @@ -0,0 +1,103 @@ +/** + * + */ +package com.xplenty.api.request.xpackage; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MemberTest; +import com.xplenty.api.model.PackageTest; +import com.xplenty.api.model.PackageValidation; +import com.xplenty.api.model.PackageValidationError; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Properties; + +/** + * @author xardas + * + */ +public class RunPackageValidationTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Properties params = new Properties(); + RunPackageValidation cc = new RunPackageValidation(666); + assertEquals(Xplenty.Resource.RunPackageValidation.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.RunPackageValidation.name, cc.getName()); + assertEquals(Http.Method.POST, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + PackageValidation c = PackageTest.createMockPackageValidation(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(MemberTest.createMockMemberForCreation()); + assertNotNull(json); + + json = JsonMapperFactory.getInstance().writeValueAsString(c); + + RunPackageValidation cc = new RunPackageValidation(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("Something bad happened", c.getStatusMessage()); + assertEquals("https://testapi.xplenty.com/api/packages/777/validations/666", c.getUrl()); + assertEquals(Xplenty.PackageValidationStatus.failed, c.getStatus()); + assertEquals(222, c.getOwnerId().longValue()); + assertEquals(111, c.getAccountId().longValue()); + assertEquals(777, c.getPackageId().longValue()); + assertEquals(1234, c.getRuntime().longValue()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + List errors = c.getErrors(); + assertNotNull(errors); + assertEquals("12", errors.get(0).getComponentId()); + assertEquals("couldn't obtain value for var_1", errors.get(0).getMessage()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + PackageValidation c = PackageTest.createMockPackageValidation(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + RunPackageValidation cc = new RunPackageValidation(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.RunPackageValidation.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/UpdatePackageTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/UpdatePackageTest.java new file mode 100644 index 0000000..1502d48 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/UpdatePackageTest.java @@ -0,0 +1,101 @@ +/** + * + */ +package com.xplenty.api.request.xpackage; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MemberTest; +import com.xplenty.api.model.Package; +import com.xplenty.api.model.PackageTest; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * @author xardas + * + */ +public class UpdatePackageTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + final Date now = new Date(); + UpdatePackage cc = new UpdatePackage(PackageTest.createMockPackageForCreation()); + assertEquals(Xplenty.Resource.UpdatePackage.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.UpdatePackage.name, cc.getName()); + assertEquals(Http.Method.PUT, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertTrue(cc.hasBody()); + assertNotNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Package c = PackageTest.createMockPackage(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(MemberTest.createMockMemberForCreation()); + assertNotNull(json); + + json = JsonMapperFactory.getInstance().writeValueAsString(c); + + UpdatePackage cc = new UpdatePackage(PackageTest.createMockPackageForCreation()); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(c); + assertEquals(new Long(666), c.getId()); + assertEquals("TestPack", c.getName()); + assertEquals("TestPack Description", c.getDescription()); + assertEquals(Xplenty.PackageFlowType.workflow, c.getFlowType()); + assertEquals("https://testapi.xplenty.com/api/package/666", c.getUrl()); + assertEquals("https://testapi.xplenty.com/package/666", c.getHtmlUrl()); + assertEquals(Xplenty.PackageStatus.active, c.getStatus()); + assertEquals(111, c.getOwnerId().longValue()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + assertTrue(Math.abs(now.getTime() - c.getUpdatedAt().getTime()) < 1000); //fractions of second are not serialized + + Map packVars = c.getVariables(); + assertNotNull(packVars); + assertEquals("val1", packVars.get("var_1")); + assertEquals("super$$$\"complex'val\n\t", packVars.get("var_2")); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + Package c = PackageTest.createMockPackage(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + UpdatePackage cc = new UpdatePackage(PackageTest.createMockPackageForCreation()); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.UpdatePackage.name + ": error parsing response object", e.getMessage()); + } + + } +} From 164f5032fe10c0073b2dd8d82b7d1fc513d8051c Mon Sep 17 00:00:00 2001 From: xardas Date: Mon, 11 Jan 2016 20:49:40 +0300 Subject: [PATCH 27/35] add product updates methods add job output preview method add tests --- .../main/java/com/xplenty/api/Xplenty.java | 5 +- .../main/java/com/xplenty/api/XplentyAPI.java | 39 ++++++-- .../xplenty/api/model/JobOutputPreview.java | 36 +++++++ .../com/xplenty/api/model/ProductUpdate.java | 98 +++++++++++++++++++ .../api/request/job/JobPreviewOutput.java | 30 ++++++ .../api/request/misc/LikeProductUpdate.java | 39 ++++++++ .../api/request/misc/ListProductUpdates.java | 47 +++++++++ .../java/com/xplenty/api/model/JobTest.java | 7 ++ .../java/com/xplenty/api/model/MiscTest.java | 17 ++++ .../api/request/job/JobPreviewOutputTest.java | 74 ++++++++++++++ .../request/misc/LikeProductUpdateTest.java | 87 ++++++++++++++++ .../request/misc/ListProductUpdatesTest.java | 97 ++++++++++++++++++ 12 files changed, 567 insertions(+), 9 deletions(-) create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/JobOutputPreview.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/model/ProductUpdate.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobPreviewOutput.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/LikeProductUpdate.java create mode 100644 xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListProductUpdates.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobPreviewOutputTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/LikeProductUpdateTest.java create mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListProductUpdatesTest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index 2d94821..6ce021f 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -347,6 +347,7 @@ public static enum Resource { StopJob("jobs/%s", "Stop job"), JobExecVars("jobs/%s/variables", "List job execution variables"), JobLog("jobs/%s/log", "Get job output log"), + JobPreviewOutput("jobs/%s/outputs/%s/preview", "Preview job output"), ClusterWatcher("clusters/%s/watchers", "Adding/removing a cluster watchers"), JobWatcher("jobs/%s/watchers", "Adding/removing a job watchers"), Schedules("schedules", "List schedules"), @@ -392,7 +393,9 @@ public static enum Resource { Plans("plans", "List payment plans for account"), Subscription("subscription", "Get information about current account subscription"), UpdatePaymentMethodAndPlan("payment_method", "Update payment method and/or plan"), - PaymentMethod("payment_method", "Get payment method information") + PaymentMethod("payment_method", "Get payment method information"), + ProductUpdates("product_updates", "List latest product announcements"), + LikeProductUpdate("product_updates/%s/like", "List latest product announcements") ; public final String value; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index f27163a..0198b1a 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -20,10 +20,7 @@ import com.xplenty.api.request.connection.ListConnections; import com.xplenty.api.request.job.*; import com.xplenty.api.request.member.*; -import com.xplenty.api.request.misc.ListRegions; -import com.xplenty.api.request.misc.ListStacks; -import com.xplenty.api.request.misc.ListSystemVariables; -import com.xplenty.api.request.misc.ListTimezones; +import com.xplenty.api.request.misc.*; import com.xplenty.api.request.public_key.CreatePublicKey; import com.xplenty.api.request.public_key.DeletePublicKey; import com.xplenty.api.request.public_key.ListPublicKeys; @@ -33,10 +30,7 @@ import com.xplenty.api.request.subscription.PaymentMehodInfo; import com.xplenty.api.request.subscription.SubscriptionInfo; import com.xplenty.api.request.subscription.UpdatePaymentAndPlan; -import com.xplenty.api.request.user.CurrentUserInfo; -import com.xplenty.api.request.user.ListNotifications; -import com.xplenty.api.request.user.MarkNotificationsRead; -import com.xplenty.api.request.user.UpdateCurrentUser; +import com.xplenty.api.request.user.*; import com.xplenty.api.request.watching.AddClusterWatcher; import com.xplenty.api.request.watching.AddJobWatcher; import com.xplenty.api.request.watching.ListWatchers; @@ -426,6 +420,18 @@ public Map getJobExecutionVariables(long jobId) { public JobLog getJobLog(long jobId) { return client.execute(new JobLogs(jobId)); } + + /** + * The calls returns up to 100 lines raw preview of a job output. + * @param jobId job id + * @param outputId output id + * @return job output preview object + */ + public JobOutputPreview previewJobOutput(long jobId, long outputId) { + checkId(jobId); + checkId(outputId); + return client.execute(new JobPreviewOutput(jobId, outputId)); + } /** * Information about a particular job @@ -1104,6 +1110,23 @@ public CreditCardInfo getPaymentMethodInfo() { return client.execute(new PaymentMehodInfo()); } + /** + * This call returns list of latest product announcements. + * @return list of product updates + */ + public List listProductUpdates() { + return client.execute(new ListProductUpdates()); + } + + /** + * This action allows to like product update by authenticated user. + * @param productUpdateId id of product update to like + * @return liked product update + */ + public ProductUpdate likeProductUpdate(long productUpdateId) { + checkId(productUpdateId); + return client.execute(new LikeProductUpdate(productUpdateId)); + } private void checkId(long id) { if (id == 0) { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobOutputPreview.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobOutputPreview.java new file mode 100644 index 0000000..affc5c5 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobOutputPreview.java @@ -0,0 +1,36 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Data model for Xplenty job output preview + * Author: Xardas + * Date: 11.01.16 + * Time: 18:24 + */ +public class JobOutputPreview extends XplentyObject { + @JsonProperty + protected String preview; + @JsonProperty + protected String url; + + protected JobOutputPreview() { + super(JobOutputPreview.class); + } + + /** + * + * @return output preview + */ + public String getPreview() { + return preview; + } + + /** + * + * @return the job preview URL + */ + public String getUrl() { + return url; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ProductUpdate.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/ProductUpdate.java new file mode 100644 index 0000000..91a1604 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/ProductUpdate.java @@ -0,0 +1,98 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Date; + +/** + * Data model for Xplenty product update + * Author: Xardas + * Date: 11.01.16 + * Time: 17:51 + */ +public class ProductUpdate extends XplentyObject { + @JsonProperty + protected Long id; + @JsonProperty + protected String title; + @JsonProperty("created_at") + protected Date createdAt; + @JsonProperty + protected String body; + @JsonProperty("body_html") + protected String bodyHtml; + @JsonProperty("body_text") + protected String bodyText; + @JsonProperty("likes_count") + protected Long likes; + @JsonProperty("liked") + protected Boolean liked; + + protected ProductUpdate() { + super(ProductUpdate.class); + } + + /** + * + * @return product update id. It can be used to like it. + */ + public Long getId() { + return id; + } + + /** + * + * @return product update title + */ + public String getTitle() { + return title; + } + + /** + * + * @return product update creation date + */ + public Date getCreatedAt() { + return createdAt; + } + + /** + * + * @return product update body + */ + public String getBody() { + return body; + } + + /** + * + * @return product update body in html format + */ + public String getBodyHtml() { + return bodyHtml; + } + + /** + * + * @return product update body in plain text + */ + public String getBodyText() { + return bodyText; + } + + /** + * + * @return current number of likes + */ + public Long getLikes() { + return likes; + } + + /** + * + * @return have you liked it or not + */ + public Boolean getLiked() { + return liked; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobPreviewOutput.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobPreviewOutput.java new file mode 100644 index 0000000..bfb86a3 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobPreviewOutput.java @@ -0,0 +1,30 @@ +package com.xplenty.api.request.job; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.JobOutputPreview; +import com.xplenty.api.request.AbstractInfoRequest; + +/** + * The calls returns up to 100 lines raw preview of a job output. + * Author: Xardas + * Date: 11.01.16 + * Time: 18:24 + */ +public class JobPreviewOutput extends AbstractInfoRequest { + private final long jobId; + + public JobPreviewOutput(long jobId, long outputId) { + super(outputId); + this.jobId = jobId; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.JobPreviewOutput.format(String.valueOf(jobId), String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.JobPreviewOutput.name; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/LikeProductUpdate.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/LikeProductUpdate.java new file mode 100644 index 0000000..4b30931 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/LikeProductUpdate.java @@ -0,0 +1,39 @@ +package com.xplenty.api.request.misc; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.ProductUpdate; +import com.xplenty.api.request.AbstractDeleteRequest; + +/** + * This action allows to like product update by authenticated user. + * Author: Xardas + * Date: 11.01.16 + * Time: 18:09 + */ +public class LikeProductUpdate extends AbstractDeleteRequest { + + public LikeProductUpdate(long entityId) { + super(entityId); + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.LikeProductUpdate.format(String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.LikeProductUpdate.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.POST; + } +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListProductUpdates.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListProductUpdates.java new file mode 100644 index 0000000..3bb4f49 --- /dev/null +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListProductUpdates.java @@ -0,0 +1,47 @@ +package com.xplenty.api.request.misc; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.ProductUpdate; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; + +/** + * This call returns list of latest product announcements. + * Author: Xardas + * Date: 11.01.16 + * Time: 17:48 + */ +public class ListProductUpdates extends AbstractListRequest> { + + public ListProductUpdates() { + super(null, false); + } + + @Override + public String getEndpoint(String apiHost, String accountName) { + return String.format("%s/%s", apiHost, getEndpoint()); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.ProductUpdates.value; + } + + @Override + public String getName() { + return Xplenty.Resource.ProductUpdates.name; + } + + @Override + public List getResponse(Response response) { + try { + return response.getContent(new TypeReference>() {}); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/JobTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/JobTest.java index d2805ac..95ef6c3 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/model/JobTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/JobTest.java @@ -80,4 +80,11 @@ public static Map createMockJobExecutionVariables() { vars.put("_ACCOUNT_ID2", "777"); return vars; } + + public static JobOutputPreview createMockJobOutputPreview() { + JobOutputPreview jop = new JobOutputPreview(); + jop.preview = "out1\tout2\tout3"; + jop.url = "https://testapi.xplenty.com/api/jobs/1/outputs/1/preview"; + return jop; + } } diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/MiscTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/MiscTest.java index 3e35603..5c3d4ed 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/model/MiscTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/MiscTest.java @@ -27,6 +27,10 @@ public void testBuilder() { Map systemVars = createMockSystemVars(); assertNotNull(systemVars); assertTrue(systemVars.size() > 0); + + ProductUpdate pu = createMockProductUpdate(now); + assertNotNull(pu); + assertEquals(now.getTime(), pu.getCreatedAt().getTime()); } public static Timezone createMockTimeZone() { @@ -49,4 +53,17 @@ public static Map createMockSystemVars() { systemVars.put("_BYTES_PER_REDUCER", "666666"); return systemVars; } + + public static ProductUpdate createMockProductUpdate(Date now) { + ProductUpdate pu = new ProductUpdate(); + pu.id = 666L; + pu.title = "Breaking news"; + pu.body = "Now you can cross-join!"; + pu.bodyHtml = "Now you can cross-join"; + pu.bodyText = "now you can cross-join\n"; + pu.createdAt = now; + pu.likes = 15L; + pu.liked = true; + return pu; + } } diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobPreviewOutputTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobPreviewOutputTest.java new file mode 100644 index 0000000..6e71e88 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobPreviewOutputTest.java @@ -0,0 +1,74 @@ +/** + * + */ +package com.xplenty.api.request.job; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.JobOutputPreview; +import com.xplenty.api.model.JobTest; +import junit.framework.TestCase; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardazz + * + */ +public class JobPreviewOutputTest extends TestCase { + + @Test + public void testIntegrity() { + JobPreviewOutput ji = new JobPreviewOutput(666, 777); + + assertEquals(Xplenty.Resource.JobPreviewOutput.format(String.valueOf(666), String.valueOf(777)), ji.getEndpoint()); + assertEquals(Xplenty.Resource.JobPreviewOutput.name, ji.getName()); + assertEquals(Http.Method.GET, ji.getHttpMethod()); + assertEquals(Http.MediaType.JSON, ji.getResponseType()); + assertFalse(ji.hasBody()); + assertNull(ji.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + JobPreviewOutput ji = new JobPreviewOutput(666, 777); + JobOutputPreview j = JobTest.createMockJobOutputPreview(); + String json = new ObjectMapper().writeValueAsString(j); + + j = ji.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(j); + assertEquals("out1\tout2\tout3", j.getPreview()); + assertEquals("https://testapi.xplenty.com/api/jobs/1/outputs/1/preview", j.getUrl()); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + JobPreviewOutput ji = new JobPreviewOutput(666, 777); + JobOutputPreview j = JobTest.createMockJobOutputPreview(); + String json = new ObjectMapper().writeValueAsString(j).replace("{", "["); + + try { + j = ji.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.JobPreviewOutput.name + ": error parsing response object", e.getMessage()); + } + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/LikeProductUpdateTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/LikeProductUpdateTest.java new file mode 100644 index 0000000..8e8a3b9 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/LikeProductUpdateTest.java @@ -0,0 +1,87 @@ +/** + * + */ +package com.xplenty.api.request.misc; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MiscTest; +import com.xplenty.api.model.ProductUpdate; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; + +/** + * @author xardas + * + */ +public class LikeProductUpdateTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + LikeProductUpdate cc = new LikeProductUpdate(666); + assertEquals(Xplenty.Resource.LikeProductUpdate.format(String.valueOf(666)), cc.getEndpoint()); + assertEquals(Xplenty.Resource.LikeProductUpdate.name, cc.getName()); + assertEquals(Http.Method.POST, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + + ProductUpdate c = MiscTest.createMockProductUpdate(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + LikeProductUpdate cc = new LikeProductUpdate(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertEquals("Breaking news", c.getTitle()); + assertEquals("Now you can cross-join!", c.getBody()); + assertEquals("Now you can cross-join", c.getBodyHtml()); + assertEquals("now you can cross-join\n", c.getBodyText()); + assertEquals(666, c.getId().longValue()); + assertEquals(15, c.getLikes().longValue()); + assertTrue(c.getLiked()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + ProductUpdate c = MiscTest.createMockProductUpdate(now); + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + + try { + LikeProductUpdate cc = new LikeProductUpdate(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.LikeProductUpdate.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListProductUpdatesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListProductUpdatesTest.java new file mode 100644 index 0000000..f66db40 --- /dev/null +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListProductUpdatesTest.java @@ -0,0 +1,97 @@ +/** + * + */ +package com.xplenty.api.request.misc; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.sun.jersey.api.client.ClientResponse.Status; +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.JsonMapperFactory; +import com.xplenty.api.http.Response; +import com.xplenty.api.model.MiscTest; +import com.xplenty.api.model.ProductUpdate; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; + +/** + * @author xardas + * + */ +public class ListProductUpdatesTest extends TestCase { + + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + ListProductUpdates cc = new ListProductUpdates(); + assertEquals(Xplenty.Resource.ProductUpdates.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.ProductUpdates.name, cc.getName()); + assertEquals(Http.Method.GET, cc.getHttpMethod()); + assertEquals(Http.MediaType.JSON, cc.getResponseType()); + assertFalse(cc.hasBody()); + assertNull(cc.getBody()); + } + + @Test + public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + + List productList = new ArrayList<>(); + ProductUpdate c = MiscTest.createMockProductUpdate(now); + productList.add(c); + + String json = JsonMapperFactory.getInstance().writeValueAsString(productList); + + ListProductUpdates cc = new ListProductUpdates(); + productList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + + assertNotNull(productList); + assertTrue(productList.size() > 0); + + c = productList.get(0); + assertEquals("Breaking news", c.getTitle()); + assertEquals("Now you can cross-join!", c.getBody()); + assertEquals("Now you can cross-join", c.getBodyHtml()); + assertEquals("now you can cross-join\n", c.getBodyText()); + assertEquals(666, c.getId().longValue()); + assertEquals(15, c.getLikes().longValue()); + assertTrue(c.getLiked()); + assertTrue(Math.abs(now.getTime() - c.getCreatedAt().getTime()) < 1000); //fractions of second are not serialized + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + Date now = new Date(); + List productList = new ArrayList<>(); + ProductUpdate c = MiscTest.createMockProductUpdate(now); + productList.add(c); + String json = JsonMapperFactory.getInstance().writeValueAsString(productList).replace("{", "["); + + try { + ListProductUpdates cc = new ListProductUpdates(); + productList = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.ProductUpdates.name + ": error parsing response object", e.getMessage()); + } + + } +} From fc8abdbccfafffea657ba2bfc3cbc60586ebf80c Mon Sep 17 00:00:00 2001 From: xardas Date: Mon, 11 Jan 2016 21:12:57 +0300 Subject: [PATCH 28/35] add cluster and package object inclusion possibility --- .../main/java/com/xplenty/api/XplentyAPI.java | 27 +++++++++++++------ .../com/xplenty/api/request/job/JobInfo.java | 16 +++++++++-- .../xplenty/api/request/job/JobInfoTest.java | 14 +++++++--- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index 0198b1a..62c0984 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -432,14 +432,25 @@ public JobOutputPreview previewJobOutput(long jobId, long outputId) { checkId(outputId); return client.execute(new JobPreviewOutput(jobId, outputId)); } - - /** - * Information about a particular job - * @param jobId id of the job, see {@link #listJobs()} to get a list of jobs with id's - * @return - */ - public Job jobInformation(long jobId) { - return client.execute(new JobInfo(jobId)).withParentApiInstance(this); + + /** + * Information about a particular job + * @param jobId id of the job, see {@link #listJobs()} to get a list of jobs with id's + * @return job object + */ + public Job jobInformation(long jobId) { + return jobInformation(jobId, false, false); + } + + /** + * Information about a particular job + * @param jobId id of the job, see {@link #listJobs()} to get a list of jobs with id's + * @param includeCluster include cluster object + * @param includePackage include package object + * @return job object + */ + public Job jobInformation(long jobId, boolean includeCluster, boolean includePackage) { + return client.execute(new JobInfo(jobId, includeCluster, includePackage)).withParentApiInstance(this); } /** diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobInfo.java index dd0434d..fdc6187 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobInfo.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobInfo.java @@ -12,9 +12,13 @@ * */ public class JobInfo extends AbstractInfoRequest { + private final boolean includeCluster; + private final boolean includePackage; - public JobInfo(long entityId) { + public JobInfo(long entityId, boolean includeCluster, boolean includePackage) { super(entityId); + this.includeCluster = includeCluster; + this.includePackage = includePackage; } @Override @@ -24,7 +28,15 @@ public String getName() { @Override public String getEndpoint() { - return Xplenty.Resource.Job.format(Long.toString(entityId)); + final String include; + if (!includeCluster && !includePackage) { + include = ""; + } else if (includeCluster && includePackage) { + include = "?include=cluster,package"; + } else { + include = String.format("?include=%s", includePackage ? "package" : "cluster"); + } + return Xplenty.Resource.Job.format(Long.toString(entityId)) + include; } } diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobInfoTest.java index 9e3f669..adbcc62 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobInfoTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobInfoTest.java @@ -13,7 +13,6 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; import com.xplenty.api.model.JobTest; -import com.xplenty.api.request.job.JobInfo; import junit.framework.TestCase; import org.junit.Test; @@ -29,7 +28,7 @@ public class JobInfoTest extends TestCase { @Test public void testIntegrity() { - JobInfo ji = new JobInfo(111L); + JobInfo ji = new JobInfo(111L, false, false); assertEquals(Xplenty.Resource.Job.format(Long.toString(111)), ji.getEndpoint()); assertEquals(Xplenty.Resource.Job.name, ji.getName()); @@ -37,12 +36,19 @@ public void testIntegrity() { assertEquals(Http.MediaType.JSON, ji.getResponseType()); assertFalse(ji.hasBody()); assertNull(ji.getBody()); + + ji = new JobInfo(111L, true, false); + assertEquals(Xplenty.Resource.Job.format(Long.toString(111)) + "?include=cluster", ji.getEndpoint()); + ji = new JobInfo(111L, false, true); + assertEquals(Xplenty.Resource.Job.format(Long.toString(111)) + "?include=package", ji.getEndpoint()); + ji = new JobInfo(111L, true, true); + assertEquals(Xplenty.Resource.Job.format(Long.toString(111)) + "?include=cluster,package", ji.getEndpoint()); } @Test public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { Date now = new Date(); - JobInfo ji = new JobInfo(111L); + JobInfo ji = new JobInfo(111L, false, false); Job j = JobTest.createMockJob(now); String json = new ObjectMapper().writeValueAsString(j); @@ -73,7 +79,7 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor @Test public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { Date now = new Date(); - JobInfo ji = new JobInfo(111L); + JobInfo ji = new JobInfo(111L, false, false); Job j = JobTest.createMockJob(now); String json = new ObjectMapper().writeValueAsString(j).replace("running", "success"); From a5fc803af9c634cf9bec4dfae9e80fbba5315a6d Mon Sep 17 00:00:00 2001 From: xardas Date: Tue, 12 Jan 2016 20:16:01 +0300 Subject: [PATCH 29/35] add cluster and package object inclusion possibility to job list fix 2 little bugs --- .../main/java/com/xplenty/api/Xplenty.java | 27 ++++++++++++++++++- .../main/java/com/xplenty/api/model/Job.java | 2 +- .../com/xplenty/api/request/job/ListJobs.java | 17 +++++++----- .../xplenty/api/request/job/ListJobsTest.java | 14 ++++++++-- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java index 6ce021f..7ec7678 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java @@ -97,7 +97,11 @@ public enum JobStatus { /** * the job has stopped */ - stopped("stopped") + stopped("stopped"), + /** + * used only for filtering meaning all the statuses above + */ + all("all") ; @SuppressWarnings("unused") @@ -321,6 +325,27 @@ public static enum ClusterStatus { } } + public enum ListJobInclude { + cluster("cluster"), + xpackage("package"), + xpackage_and_cluster("cluster,package"); + + private final String entity; + + ListJobInclude(String entity) { + this.entity = entity; + } + + public String getEntity() { + return entity; + } + + @Override + public String toString() { + return entity; + } + } + /** * Endpoints and short descriptions for REST resources */ diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java index 28777e9..483c551 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java @@ -52,7 +52,7 @@ public class Job extends XplentyObject { protected Cluster cluster; @JsonProperty("package_id") protected Long packageId; - @JsonProperty + @JsonProperty("package") protected Package xpackage; @JsonProperty protected String errors; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/ListJobs.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/ListJobs.java index 49bafca..0d945bd 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/ListJobs.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/ListJobs.java @@ -5,7 +5,6 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.xplenty.api.Xplenty; -import com.xplenty.api.Xplenty.ClusterStatus; import com.xplenty.api.exceptions.XplentyAPIException; import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; @@ -19,6 +18,7 @@ * */ public class ListJobs extends AbstractListRequest> { + public static final String PARAMETER_INCLUDE = "include"; public ListJobs(Properties params) { super(params, true); @@ -26,11 +26,16 @@ public ListJobs(Properties params) { } private void validateParameters(Properties params) { - if ( params.containsKey(PARAMETER_STATUS) - && !(params.get(PARAMETER_STATUS) instanceof ClusterStatus) - && !(params.get(PARAMETER_STATUS) instanceof String && "all".equals(params.get(PARAMETER_STATUS))) - ) - throw new XplentyAPIException("Invalid 'status' parameter"); + if (params.containsKey(PARAMETER_STATUS) + && !(params.get(PARAMETER_STATUS) instanceof Xplenty.JobStatus) + ) { + throw new XplentyAPIException(String.format("Invalid %s parameter, should be JobStatus", PARAMETER_STATUS)); + } + if (params.containsKey(PARAMETER_INCLUDE) + && !(params.get(PARAMETER_INCLUDE) instanceof Xplenty.ListJobInclude) + ) { + throw new XplentyAPIException(String.format("Invalid %s parameter, should be ListJobInclude", PARAMETER_INCLUDE)); + } } @Override diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/ListJobsTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/ListJobsTest.java index 51c69c5..7c0c0cd 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/ListJobsTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/ListJobsTest.java @@ -12,7 +12,7 @@ import com.xplenty.api.http.Response; import com.xplenty.api.model.Job; import com.xplenty.api.model.JobTest; -import com.xplenty.api.request.job.ListJobs; +import com.xplenty.api.request.AbstractListRequest; import junit.framework.TestCase; import org.junit.Test; @@ -26,7 +26,8 @@ public class ListJobsTest extends TestCase { @Test public void testIntegrity() { - ListJobs lj = new ListJobs(new Properties()); + final Properties props = new Properties(); + ListJobs lj = new ListJobs(props); assertEquals(Xplenty.Resource.Jobs.value, lj.getEndpoint()); assertEquals(Xplenty.Resource.Jobs.name, lj.getName()); @@ -34,6 +35,15 @@ public void testIntegrity() { assertEquals(Http.MediaType.JSON, lj.getResponseType()); assertFalse(lj.hasBody()); assertNull(lj.getBody()); + + props.put(AbstractListRequest.PARAMETER_SORT, Xplenty.Sort.name); + props.put(AbstractListRequest.PARAMETER_DIRECTION, Xplenty.SortDirection.descending); + props.put(ListJobs.PARAMETER_INCLUDE, Xplenty.ListJobInclude.xpackage_and_cluster); + lj = new ListJobs(props); + assertEquals(Xplenty.Resource.Jobs.value + "?" + + ListJobs.PARAMETER_INCLUDE + "=cluster,package&" + + AbstractListRequest.PARAMETER_SORT + "=name&" + + AbstractListRequest.PARAMETER_DIRECTION + "=desc", lj.getEndpoint()); } @Test From 80a35d9568bb943520816cc2e82c04d08e28f66a Mon Sep 17 00:00:00 2001 From: xardas Date: Tue, 12 Jan 2016 20:38:13 +0300 Subject: [PATCH 30/35] add package info and list flags to return json data flow --- .../src/main/java/com/xplenty/api/XplentyAPI.java | 13 ++++++++++++- .../xplenty/api/request/xpackage/ListPackages.java | 8 ++++++++ .../xplenty/api/request/xpackage/PackageInfo.java | 8 +++++--- .../api/request/xpackage/ListPackagesTest.java | 2 ++ .../api/request/xpackage/PackageInfoTest.java | 9 ++++++--- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index 62c0984..3b706dd 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -98,7 +98,18 @@ public XplentyAPI(HttpClient client){ * @return package object */ public Package getPackageInfo(long packageId) { - return client.execute(new PackageInfo(packageId)); + return getPackageInfo(packageId, false); + } + + + /** + * Get package information + * @param packageId of the package to get + * @param includeDataFlow if set to true, package object will contain data flow json + * @return package object + */ + public Package getPackageInfo(long packageId, boolean includeDataFlow) { + return client.execute(new PackageInfo(packageId, includeDataFlow)); } /** diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java index 195016f..9f2d534 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java @@ -21,7 +21,9 @@ * Time: 18:08 */ public class ListPackages extends AbstractListRequest> { + protected static final String ACTUAL_INCLUDE_VALUE = "flow"; public static String PARAMETER_FLOW_TYPE = "flow_type"; + public static final String PARAMETER_INCLUDE_DATA_FLOW = "include"; public ListPackages(Properties params) { super(params, true); @@ -33,6 +35,12 @@ private void validateParameters(Properties params) { && !(params.get(PARAMETER_FLOW_TYPE) instanceof Xplenty.PackageFlowType)) { throw new XplentyAPIException(String.format("Invalid %s parameter, should be one of PackageFlowType values", PARAMETER_FLOW_TYPE)); } + if (params.containsKey(PARAMETER_INCLUDE_DATA_FLOW) + && !(params.get(PARAMETER_INCLUDE_DATA_FLOW) instanceof Boolean)) { + throw new XplentyAPIException(String.format("Invalid %s parameter, should be Boolean", PARAMETER_INCLUDE_DATA_FLOW)); + } else if (params.containsKey(PARAMETER_INCLUDE_DATA_FLOW)) { + params.put(PARAMETER_INCLUDE_DATA_FLOW, ACTUAL_INCLUDE_VALUE); + } } @Override diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java index 5322078..20f6342 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java @@ -1,7 +1,6 @@ package com.xplenty.api.request.xpackage; import com.xplenty.api.Xplenty; -import com.xplenty.api.model.*; import com.xplenty.api.request.AbstractInfoRequest; /** @@ -10,8 +9,11 @@ * Time: 20:07 */ public class PackageInfo extends AbstractInfoRequest { - public PackageInfo(long entityId) { + private final boolean includeDataFlow; + + public PackageInfo(long entityId, boolean includeDataFlow) { super(entityId); + this.includeDataFlow = includeDataFlow; } @Override @@ -21,6 +23,6 @@ public String getName() { @Override public String getEndpoint() { - return Xplenty.Resource.Package.format(String.valueOf(entityId)); + return Xplenty.Resource.Package.format(String.valueOf(entityId) + (includeDataFlow ? "?include=flow" : "")); } } diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java index bbdaaa8..502cdc3 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java @@ -48,8 +48,10 @@ public void testIntegrity() { params.put(AbstractListRequest.PARAMETER_SORT, Xplenty.Sort.name); params.put(AbstractListRequest.PARAMETER_DIRECTION, Xplenty.SortDirection.descending); params.put(ListPackages.PARAMETER_FLOW_TYPE, Xplenty.PackageFlowType.dataflow); + params.put(ListPackages.PARAMETER_INCLUDE_DATA_FLOW, true); cc = new ListPackages(params); assertEquals(Xplenty.Resource.Packages.value + "?" + + ListPackages.PARAMETER_INCLUDE_DATA_FLOW + "=flow&" + ListPackages.PARAMETER_FLOW_TYPE + "=dataflow&" + AbstractListRequest.PARAMETER_SORT + "=name&" + AbstractListRequest.PARAMETER_DIRECTION + "=desc", cc.getEndpoint()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java index b9adfe2..0c9c000 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java @@ -37,13 +37,16 @@ public void setUp() { public void testIntegrity() { final Date now = new Date(); - PackageInfo cc = new PackageInfo(666); + PackageInfo cc = new PackageInfo(666, false); assertEquals(Xplenty.Resource.Package.format(String.valueOf(666)), cc.getEndpoint()); assertEquals(Xplenty.Resource.Package.name, cc.getName()); assertEquals(Http.Method.GET, cc.getHttpMethod()); assertEquals(Http.MediaType.JSON, cc.getResponseType()); assertFalse(cc.hasBody()); assertNull(cc.getBody()); + + cc = new PackageInfo(666, true); + assertEquals(Xplenty.Resource.Package.format(String.valueOf(666)) + "?include=flow", cc.getEndpoint()); } @Test @@ -56,7 +59,7 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor json = JsonMapperFactory.getInstance().writeValueAsString(c); - PackageInfo cc = new PackageInfo(666); + PackageInfo cc = new PackageInfo(666, false); c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, json, Status.OK.getStatusCode(), @@ -87,7 +90,7 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); try { - PackageInfo cc = new PackageInfo(666); + PackageInfo cc = new PackageInfo(666, false); c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, json, Status.OK.getStatusCode(), From 40137df5b6aa97939dde564ce906bca480d2e0d9 Mon Sep 17 00:00:00 2001 From: xardas Date: Tue, 12 Jan 2016 21:17:54 +0300 Subject: [PATCH 31/35] some fixes --- .../src/main/java/com/xplenty/api/XplentyAPI.java | 5 +++-- .../src/main/java/com/xplenty/api/http/Http.java | 5 +++-- .../src/main/java/com/xplenty/api/http/JerseyClient.java | 2 +- .../src/main/java/com/xplenty/api/http/Response.java | 1 + .../src/main/java/com/xplenty/api/http/SyncNettyClient.java | 2 +- .../src/main/java/com/xplenty/api/model/Member.java | 3 ++- .../java/com/xplenty/api/request/member/CreateMember.java | 4 ++-- .../src/test/java/com/xplenty/api/model/MemberTest.java | 2 +- .../com/xplenty/api/request/member/CreateMemberTest.java | 6 +++--- 9 files changed, 17 insertions(+), 13 deletions(-) diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java index 3b706dd..d61c6ee 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java @@ -803,10 +803,11 @@ public PublicKey getPublicKeyInfo(long publicKeyId) { * Creates a new member on an account. The call sends an invitation to join Xplenty in case the user is not yet a user of Xplenty. * @param email email of the member to add * @param role role of the member + * @param name name of the member * @return newly created member object */ - public Member createMember(String email, Xplenty.AccountRole role) { - return client.execute(new CreateMember(email, role)); + public Member createMember(String email, Xplenty.AccountRole role, String name) { + return client.execute(new CreateMember(email, role, name)); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java index 12667ec..b747d98 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java @@ -15,8 +15,9 @@ public class Http { * Media types supported by Xplenty API */ public static enum MediaType { - JSON("application/vnd.xplenty+json"); - + JSON("application/vnd.xplenty+json"), + PURE_JSON("application/json"); + public final String value; MediaType(String type) { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java index 91b5483..5c61aba 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java @@ -111,7 +111,7 @@ private WebResource.Builder getConfiguredResource(Request request) { } catch (Exception e) { throw new XplentyAPIException(e); } - b.entity(sw.toString()).type(Http.MediaType.JSON.value); + b.entity(sw.toString()).type(Http.MediaType.PURE_JSON.value); } return b; diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java index 88ef5d2..c04ea61 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java @@ -45,6 +45,7 @@ public void validate(String requestName) { public static Response forContentType(Http.MediaType type, String content, int status, Map headers) { switch (type) { case JSON: + case PURE_JSON: return new JsonResponse(content, status, headers); default: throw new UnsupportedOperationException(String.format("This media type [%s] not supported!", type)); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java index 2cb3fb9..2692507 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java @@ -141,7 +141,7 @@ public T execute(Request xplentyRequest) throws XplentyAPIException { final byte[] contentBytes = bos.toByteArray(); cb.writeBytes(contentBytes); request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, contentBytes.length); - request.headers().set(HttpHeaders.Names.CONTENT_TYPE, Http.MediaType.JSON.value); + request.headers().set(HttpHeaders.Names.CONTENT_TYPE, Http.MediaType.PURE_JSON.value); request.setContent(cb); } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java index 045d87d..2d473ac 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java @@ -46,10 +46,11 @@ protected Member() { super(Member.class); } - public Member(String email, Xplenty.AccountRole role) { + public Member(String email, Xplenty.AccountRole role, String name) { super(Member.class); this.email = email; this.role = role; + this.name = name; } public Member(Long id, Xplenty.AccountRole role) { diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/CreateMember.java b/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/CreateMember.java index 51922c9..807508e 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/CreateMember.java +++ b/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/CreateMember.java @@ -14,8 +14,8 @@ public class CreateMember extends AbstractManipulationRequest { - public CreateMember(String email, Xplenty.AccountRole role) { - super(new Member(email, role)); + public CreateMember(String email, Xplenty.AccountRole role, String name) { + super(new Member(email, role, name)); } @Override diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/MemberTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/MemberTest.java index ffa806a..996214b 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/model/MemberTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/model/MemberTest.java @@ -23,7 +23,7 @@ public void testBuilder() { } public static Member createMockMemberForCreation() { - Member mem = new Member("xardazz@github.com", Xplenty.AccountRole.admin); + Member mem = new Member("xardazz@github.com", Xplenty.AccountRole.admin, "testuser"); return mem; } diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/CreateMemberTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/CreateMemberTest.java index dc6eb78..b5c8eb4 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/CreateMemberTest.java +++ b/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/CreateMemberTest.java @@ -34,7 +34,7 @@ public void setUp() { @Test public void testIntegrity() { - CreateMember cc = new CreateMember("xardazz@github.com", Xplenty.AccountRole.admin); + CreateMember cc = new CreateMember("xardazz@github.com", Xplenty.AccountRole.admin, "testuser"); assertEquals(Xplenty.Resource.CreateMember.value, cc.getEndpoint()); assertEquals(Xplenty.Resource.CreateMember.name, cc.getName()); assertEquals(Http.Method.POST, cc.getHttpMethod()); @@ -53,7 +53,7 @@ public void testValidResponseHandling() throws JsonProcessingException, Unsuppor json = JsonMapperFactory.getInstance().writeValueAsString(c); - CreateMember cc = new CreateMember("xardazz@github.com", Xplenty.AccountRole.admin); + CreateMember cc = new CreateMember("xardazz@github.com", Xplenty.AccountRole.admin, "testuser"); c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, json, Status.CREATED.getStatusCode(), @@ -82,7 +82,7 @@ public void testInvalidResponseHandling() throws JsonProcessingException, Unsupp String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); try { - CreateMember cc = new CreateMember("xardazz@github.com", Xplenty.AccountRole.admin); + CreateMember cc = new CreateMember("xardazz@github.com", Xplenty.AccountRole.admin, "testuser"); c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, json, Status.CREATED.getStatusCode(), From 3c13f2d7781e08638c56b13a64159cfd205e478b Mon Sep 17 00:00:00 2001 From: xardas Date: Wed, 13 Jan 2016 19:37:05 +0300 Subject: [PATCH 32/35] merge modules renew dependencies some bug fixes including: -correct handling of 204 status by jersey client -add watchers sending null as body --- .gitignore | 2 +- pom.xml | 209 +++++++++++++++++- .../main/java/com/xplenty/api/Xplenty.java | 0 .../main/java/com/xplenty/api/XplentyAPI.java | 0 .../api/exceptions/AuthFailedException.java | 0 .../exceptions/RequestFailedException.java | 0 .../api/exceptions/XplentyAPIException.java | 0 .../com/xplenty/api/http/ClientBuilder.java | 0 .../xplenty/api/http/ConsoleNettyLogger.java | 0 .../main/java/com/xplenty/api/http/Http.java | 0 .../java/com/xplenty/api/http/HttpClient.java | 0 .../com/xplenty/api/http/JerseyClient.java | 4 +- .../xplenty/api/http/JsonMapperFactory.java | 0 .../com/xplenty/api/http/JsonResponse.java | 0 .../java/com/xplenty/api/http/Response.java | 0 .../api/http/SSLEngineDefaultImpl.java | 0 .../com/xplenty/api/http/SyncNettyClient.java | 0 .../java/com/xplenty/api/model/Account.java | 0 .../java/com/xplenty/api/model/Cluster.java | 0 .../api/model/ClusterBootstrapAction.java | 0 .../xplenty/api/model/ClusterInstance.java | 0 .../api/model/ClusterWatchingLogEntry.java | 0 .../com/xplenty/api/model/Connection.java | 0 .../com/xplenty/api/model/ConnectionType.java | 0 .../api/model/ConnectionTypeGroup.java | 0 .../java/com/xplenty/api/model/Creator.java | 0 .../com/xplenty/api/model/CreditCardInfo.java | 0 .../java/com/xplenty/api/model/HookEvent.java | 0 .../main/java/com/xplenty/api/model/Job.java | 0 .../java/com/xplenty/api/model/JobLog.java | 0 .../java/com/xplenty/api/model/JobOutput.java | 0 .../xplenty/api/model/JobOutputPreview.java | 0 .../api/model/JobWatchingLogEntry.java | 0 .../java/com/xplenty/api/model/Member.java | 0 .../com/xplenty/api/model/Notification.java | 0 .../java/com/xplenty/api/model/Package.java | 0 .../xplenty/api/model/PackageTemplate.java | 0 .../api/model/PackageTemplateAuthor.java | 0 .../xplenty/api/model/PackageValidation.java | 0 .../api/model/PackageValidationError.java | 0 .../main/java/com/xplenty/api/model/Plan.java | 0 .../com/xplenty/api/model/ProductUpdate.java | 0 .../java/com/xplenty/api/model/PublicKey.java | 0 .../java/com/xplenty/api/model/Region.java | 0 .../java/com/xplenty/api/model/Schedule.java | 0 .../com/xplenty/api/model/ScheduleTask.java | 0 .../api/model/ScheduleTaskPackage.java | 0 .../java/com/xplenty/api/model/Stack.java | 0 .../com/xplenty/api/model/Subscription.java | 0 .../java/com/xplenty/api/model/Timezone.java | 0 .../main/java/com/xplenty/api/model/User.java | 0 .../api/model/UserNotificationSettings.java | 0 .../java/com/xplenty/api/model/Watcher.java | 0 .../java/com/xplenty/api/model/WebHook.java | 0 .../com/xplenty/api/model/WebHookEvent.java | 0 .../xplenty/api/model/WebHookSettings.java | 0 .../com/xplenty/api/model/XplentyObject.java | 0 .../api/request/AbstractDeleteRequest.java | 0 .../api/request/AbstractInfoRequest.java | 0 .../api/request/AbstractListRequest.java | 0 .../request/AbstractManipulationRequest.java | 0 .../xplenty/api/request/AbstractRequest.java | 0 .../xplenty/api/request/RawGetRequest.java | 0 .../java/com/xplenty/api/request/Request.java | 0 .../api/request/account/AccountInfo.java | 0 .../api/request/account/CreateAccount.java | 0 .../api/request/account/DeleteAccount.java | 0 .../request/account/ListAccountRegions.java | 0 .../api/request/account/ListAccounts.java | 0 .../api/request/account/UpdateAccount.java | 0 .../api/request/cluster/ClusterInfo.java | 0 .../api/request/cluster/CreateCluster.java | 0 .../request/cluster/ListClusterInstances.java | 0 .../api/request/cluster/ListClusters.java | 0 .../api/request/cluster/TerminateCluster.java | 0 .../api/request/cluster/UpdateCluster.java | 0 .../request/connection/ConnectionInfo.java | 0 .../request/connection/DeleteConnection.java | 0 .../connection/ListConnectionTypes.java | 0 .../request/connection/ListConnections.java | 0 .../request/job/JobExecutionVariables.java | 0 .../com/xplenty/api/request/job/JobInfo.java | 0 .../com/xplenty/api/request/job/JobLogs.java | 0 .../api/request/job/JobPreviewOutput.java | 0 .../com/xplenty/api/request/job/ListJobs.java | 0 .../com/xplenty/api/request/job/RunJob.java | 0 .../com/xplenty/api/request/job/StopJob.java | 0 .../api/request/member/CreateMember.java | 0 .../api/request/member/DeleteMember.java | 0 .../api/request/member/ListMembers.java | 0 .../api/request/member/MemberInfo.java | 0 .../api/request/member/SetMemberRole.java | 0 .../api/request/misc/LikeProductUpdate.java | 0 .../api/request/misc/ListProductUpdates.java | 0 .../xplenty/api/request/misc/ListRegions.java | 0 .../xplenty/api/request/misc/ListStacks.java | 0 .../api/request/misc/ListSystemVariables.java | 0 .../api/request/misc/ListTimezones.java | 0 .../request/public_key/CreatePublicKey.java | 0 .../request/public_key/DeletePublicKey.java | 0 .../request/public_key/ListPublicKeys.java | 0 .../api/request/public_key/PublicKeyInfo.java | 0 .../api/request/schedule/CloneSchedule.java | 0 .../api/request/schedule/CreateSchedule.java | 0 .../api/request/schedule/DeleteSchedule.java | 0 .../api/request/schedule/ListSchedules.java | 0 .../api/request/schedule/ScheduleInfo.java | 0 .../api/request/schedule/UpdateSchedule.java | 0 .../api/request/subscription/ListPlans.java | 0 .../subscription/PaymentMehodInfo.java | 0 .../subscription/SubscriptionInfo.java | 0 .../subscription/UpdatePaymentAndPlan.java | 0 .../api/request/user/CurrentUserInfo.java | 0 .../api/request/user/ListNotifications.java | 0 .../request/user/MarkNotificationsRead.java | 0 .../api/request/user/UpdateCurrentUser.java | 0 .../request/watching/AddClusterWatcher.java | 2 +- .../api/request/watching/AddJobWatcher.java | 2 +- .../api/request/watching/ListWatchers.java | 0 .../api/request/watching/WatchingStop.java | 0 .../api/request/webhook/CreateWebHook.java | 0 .../api/request/webhook/DeleteWebHook.java | 0 .../api/request/webhook/ListHookEvents.java | 0 .../api/request/webhook/ListWebHooks.java | 0 .../api/request/webhook/PingWebHook.java | 0 .../api/request/webhook/ToggleWebHook.java | 0 .../api/request/webhook/UpdateWebHook.java | 0 .../api/request/webhook/WebHookInfo.java | 0 .../api/request/webhook/WebHookResetSalt.java | 0 .../api/request/xpackage/CreatePackage.java | 0 .../api/request/xpackage/DeletePackage.java | 0 .../xpackage/ListPackageTemplates.java | 0 .../xpackage/ListPackageValidations.java | 0 .../api/request/xpackage/ListPackages.java | 0 .../api/request/xpackage/PackageInfo.java | 0 .../xpackage/PackageValidationInfo.java | 0 .../xpackage/RunPackageValidation.java | 0 .../api/request/xpackage/UpdatePackage.java | 0 .../api/WatchersTestAgainstMockServer.java | 0 .../api/WatchersTestAgainstStaging.java | 0 .../ITWatchersTestAgainstMockServer.java | 25 ++- .../com/xplenty/api/model/AccountTest.java | 0 .../com/xplenty/api/model/ClusterTest.java | 0 .../com/xplenty/api/model/ConnectionTest.java | 0 .../xplenty/api/model/ConnectionTypeTest.java | 0 .../xplenty/api/model/CreditCardInfoTest.java | 0 .../java/com/xplenty/api/model/JobTest.java | 0 .../com/xplenty/api/model/MemberTest.java | 0 .../java/com/xplenty/api/model/MiscTest.java | 0 .../xplenty/api/model/NotificationTest.java | 0 .../com/xplenty/api/model/PackageTest.java | 0 .../java/com/xplenty/api/model/PlanTest.java | 0 .../com/xplenty/api/model/PublicKeyTest.java | 0 .../com/xplenty/api/model/RegionTest.java | 0 .../com/xplenty/api/model/ScheduleTest.java | 0 .../xplenty/api/model/SubscriptionTest.java | 0 .../java/com/xplenty/api/model/UserTest.java | 0 .../com/xplenty/api/model/WebHookTest.java | 0 .../xplenty/api/model/XplentyObjectTest.java | 0 .../api/request/account/AccountInfoTest.java | 0 .../request/account/CreateAccountTest.java | 0 .../request/account/DeleteAccountTest.java | 0 .../account/ListAccountRegionsTest.java | 0 .../api/request/account/ListAccountsTest.java | 0 .../request/account/UpdateAccountTest.java | 0 .../api/request/cluster/ClusterInfoTest.java | 0 .../request/cluster/CreateClusterTest.java | 0 .../cluster/ListClusterInstancesTest.java | 0 .../api/request/cluster/ListClustersTest.java | 0 .../request/cluster/TerminateClusterTest.java | 0 .../request/cluster/UpdateClusterTest.java | 0 .../connection/ConnectionInfoTest.java | 0 .../connection/DeleteConnectionTest.java | 0 .../connection/ListConnectionTypesTest.java | 0 .../connection/ListConnectionsTest.java | 0 .../job/JobExecutionVariablesTest.java | 0 .../xplenty/api/request/job/JobInfoTest.java | 0 .../xplenty/api/request/job/JobLogsTest.java | 0 .../api/request/job/JobPreviewOutputTest.java | 0 .../xplenty/api/request/job/ListJobsTest.java | 0 .../xplenty/api/request/job/RunJobTest.java | 0 .../xplenty/api/request/job/StopJobTest.java | 0 .../api/request/member/CreateMemberTest.java | 0 .../api/request/member/DeleteMemberTest.java | 0 .../api/request/member/MemberInfoTest.java | 0 .../api/request/member/MemberListTest.java | 0 .../api/request/member/SetMemberRoleTest.java | 0 .../request/misc/LikeProductUpdateTest.java | 0 .../request/misc/ListProductUpdatesTest.java | 0 .../api/request/misc/ListRegionsTest.java | 0 .../api/request/misc/ListStacksTest.java | 0 .../request/misc/ListSystemVariablesTest.java | 0 .../api/request/misc/ListTimezonesTest.java | 0 .../public_key/CreatePublicKeyTest.java | 0 .../public_key/DeletePublicKeyTest.java | 0 .../public_key/ListPublicKeysTest.java | 0 .../request/public_key/PublicKeyInfoTest.java | 0 .../request/schedule/CloneScheduleTest.java | 0 .../request/schedule/CreateScheduleTest.java | 0 .../request/schedule/DeleteScheduleTest.java | 0 .../request/schedule/ListSchedulesTest.java | 0 .../request/schedule/ScheduleInfoTest.java | 0 .../request/schedule/UpdateScheduleTest.java | 0 .../request/subscription/ListPlansTest.java | 0 .../subscription/PaymentMethodInfoTest.java | 0 .../subscription/SubscriptionInfoTest.java | 0 .../UpdatePaymentAndPlanTest.java | 0 .../api/request/user/CurrentUserInfoTest.java | 0 .../request/user/ListNotificationsTest.java | 0 .../user/MarkNotificationsReadTest.java | 0 .../request/user/UpdateCurrentUserTest.java | 0 .../request/webhook/CreateWebHookTest.java | 0 .../request/webhook/DeleteWebHookTest.java | 0 .../api/request/webhook/ListWebHookTest.java | 0 .../api/request/webhook/PingWebHookTest.java | 0 .../request/webhook/ToggleWebHookTest.java | 0 .../request/webhook/UpdateWebHookTest.java | 0 .../api/request/webhook/WebHookInfoTest.java | 0 .../request/webhook/WebHookResetSaltTest.java | 0 .../request/xpackage/CreatePackageTest.java | 0 .../request/xpackage/DeletePackageTest.java | 0 .../xpackage/ListPackageTemplatesTest.java | 0 .../xpackage/ListPackageValidationsTest.java | 0 .../request/xpackage/ListPackagesTest.java | 0 .../api/request/xpackage/PackageInfoTest.java | 0 .../xpackage/PackageValidationInfoTest.java | 0 .../xpackage/RunPackageValidationTest.java | 0 .../request/xpackage/UpdatePackageTest.java | 0 .../com/xplenty/api}/ITProviderRegion.scala | 2 + .../com/xplenty/api/router}/Router.scala | 6 +- .../com/xplenty/api}/webapp/WEB-INF/web.xml | 2 +- xplenty.jar-core/pom.xml | 99 --------- .../java/com/xplenty/api/XplentyAPITest.java | 164 -------------- xplenty.jar-integ-test/pom.xml | 161 -------------- 234 files changed, 227 insertions(+), 451 deletions(-) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/Xplenty.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/XplentyAPI.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/exceptions/AuthFailedException.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/exceptions/RequestFailedException.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/exceptions/XplentyAPIException.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/http/ClientBuilder.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/http/ConsoleNettyLogger.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/http/Http.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/http/HttpClient.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/http/JerseyClient.java (97%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/http/JsonMapperFactory.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/http/JsonResponse.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/http/Response.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/http/SSLEngineDefaultImpl.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/http/SyncNettyClient.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Account.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Cluster.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/ClusterBootstrapAction.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/ClusterInstance.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/ClusterWatchingLogEntry.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Connection.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/ConnectionType.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/ConnectionTypeGroup.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Creator.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/CreditCardInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/HookEvent.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Job.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/JobLog.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/JobOutput.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/JobOutputPreview.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/JobWatchingLogEntry.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Member.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Notification.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Package.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/PackageTemplate.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/PackageTemplateAuthor.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/PackageValidation.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/PackageValidationError.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Plan.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/ProductUpdate.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/PublicKey.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Region.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Schedule.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/ScheduleTask.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/ScheduleTaskPackage.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Stack.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Subscription.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Timezone.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/User.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/UserNotificationSettings.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/Watcher.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/WebHook.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/WebHookEvent.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/WebHookSettings.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/model/XplentyObject.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/AbstractDeleteRequest.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/AbstractInfoRequest.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/AbstractListRequest.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/AbstractManipulationRequest.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/AbstractRequest.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/RawGetRequest.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/Request.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/account/AccountInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/account/CreateAccount.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/account/DeleteAccount.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/account/ListAccountRegions.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/account/ListAccounts.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/account/UpdateAccount.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/cluster/ClusterInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/cluster/CreateCluster.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/cluster/ListClusterInstances.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/cluster/ListClusters.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/cluster/TerminateCluster.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/cluster/UpdateCluster.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/connection/ConnectionInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/connection/DeleteConnection.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/connection/ListConnectionTypes.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/connection/ListConnections.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/job/JobExecutionVariables.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/job/JobInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/job/JobLogs.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/job/JobPreviewOutput.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/job/ListJobs.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/job/RunJob.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/job/StopJob.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/member/CreateMember.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/member/DeleteMember.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/member/ListMembers.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/member/MemberInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/member/SetMemberRole.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/misc/LikeProductUpdate.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/misc/ListProductUpdates.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/misc/ListRegions.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/misc/ListStacks.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/misc/ListSystemVariables.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/misc/ListTimezones.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/public_key/CreatePublicKey.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/public_key/DeletePublicKey.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/public_key/ListPublicKeys.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/public_key/PublicKeyInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/schedule/CloneSchedule.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/schedule/CreateSchedule.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/schedule/DeleteSchedule.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/schedule/ListSchedules.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/schedule/ScheduleInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/schedule/UpdateSchedule.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/subscription/ListPlans.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/subscription/PaymentMehodInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/subscription/SubscriptionInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlan.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/user/CurrentUserInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/user/ListNotifications.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/user/MarkNotificationsRead.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/user/UpdateCurrentUser.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java (96%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/watching/AddJobWatcher.java (96%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/watching/ListWatchers.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/watching/WatchingStop.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/webhook/CreateWebHook.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/webhook/DeleteWebHook.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/webhook/ListHookEvents.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/webhook/ListWebHooks.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/webhook/PingWebHook.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/webhook/ToggleWebHook.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/webhook/UpdateWebHook.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/webhook/WebHookInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/webhook/WebHookResetSalt.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/xpackage/CreatePackage.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/xpackage/DeletePackage.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/xpackage/ListPackageTemplates.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/xpackage/ListPackages.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/xpackage/PackageInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/xpackage/PackageValidationInfo.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/xpackage/RunPackageValidation.java (100%) rename {xplenty.jar-core/src => src}/main/java/com/xplenty/api/request/xpackage/UpdatePackage.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/WatchersTestAgainstStaging.java (100%) rename {xplenty.jar-integ-test/src/test/com/xplenty => src/test/java/com/xplenty/api/integration}/ITWatchersTestAgainstMockServer.java (61%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/AccountTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/ClusterTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/ConnectionTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/ConnectionTypeTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/CreditCardInfoTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/JobTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/MemberTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/MiscTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/NotificationTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/PackageTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/PlanTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/PublicKeyTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/RegionTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/ScheduleTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/SubscriptionTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/UserTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/WebHookTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/model/XplentyObjectTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/account/AccountInfoTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/account/CreateAccountTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/account/DeleteAccountTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/account/ListAccountRegionsTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/account/ListAccountsTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/account/UpdateAccountTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/cluster/CreateClusterTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/cluster/ListClusterInstancesTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/cluster/ListClustersTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/cluster/TerminateClusterTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/cluster/UpdateClusterTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/connection/ConnectionInfoTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/connection/DeleteConnectionTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/connection/ListConnectionTypesTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/connection/ListConnectionsTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/job/JobExecutionVariablesTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/job/JobInfoTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/job/JobLogsTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/job/JobPreviewOutputTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/job/ListJobsTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/job/RunJobTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/job/StopJobTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/member/CreateMemberTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/member/DeleteMemberTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/member/MemberInfoTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/member/MemberListTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/member/SetMemberRoleTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/misc/LikeProductUpdateTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/misc/ListProductUpdatesTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/misc/ListRegionsTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/misc/ListStacksTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/misc/ListSystemVariablesTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/misc/ListTimezonesTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/public_key/CreatePublicKeyTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/public_key/DeletePublicKeyTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/public_key/ListPublicKeysTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/public_key/PublicKeyInfoTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/schedule/CloneScheduleTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/schedule/CreateScheduleTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/schedule/DeleteScheduleTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/schedule/ListSchedulesTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/schedule/ScheduleInfoTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/schedule/UpdateScheduleTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/subscription/ListPlansTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/subscription/PaymentMethodInfoTest.java (100%) rename xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/SubscriptionInfotest.java => src/test/java/com/xplenty/api/request/subscription/SubscriptionInfoTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlanTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/user/CurrentUserInfoTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/user/ListNotificationsTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/user/MarkNotificationsReadTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/user/UpdateCurrentUserTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/webhook/CreateWebHookTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/webhook/DeleteWebHookTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/webhook/ListWebHookTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/webhook/PingWebHookTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/webhook/ToggleWebHookTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/webhook/UpdateWebHookTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/webhook/WebHookInfoTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/webhook/WebHookResetSaltTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/xpackage/CreatePackageTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/xpackage/DeletePackageTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/xpackage/ListPackageTemplatesTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/xpackage/ListPackageValidationsTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/xpackage/PackageValidationInfoTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/xpackage/RunPackageValidationTest.java (100%) rename {xplenty.jar-core/src => src}/test/java/com/xplenty/api/request/xpackage/UpdatePackageTest.java (100%) rename {xplenty.jar-integ-test/src/test/com/xplenty => src/test/scala/com/xplenty/api}/ITProviderRegion.scala (94%) rename {xplenty.jar-integ-test/src/main/com/xplenty => src/test/scala/com/xplenty/api/router}/Router.scala (75%) rename {xplenty.jar-integ-test/src/main => src/test/scala/com/xplenty/api}/webapp/WEB-INF/web.xml (88%) delete mode 100644 xplenty.jar-core/pom.xml delete mode 100644 xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java delete mode 100644 xplenty.jar-integ-test/pom.xml diff --git a/.gitignore b/.gitignore index 101eb93..ec2b3a9 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,5 @@ target/ .classpath .project .settings/ -testpackage/* +*/testpackage XplentyApiTest.java diff --git a/pom.xml b/pom.xml index b77fe58..ab2d2b0 100644 --- a/pom.xml +++ b/pom.xml @@ -1,20 +1,207 @@ - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.xplenty - root - 1.0.0 - pom + Xplenty.jar + 0.2.1-SNAPSHOT + jar - + Xplenty.jar + http://xplenty.com + + UTF-8 - UTF-8 + 9.3.6.v20151106 + 2.10.4 + 2.4.0 + _2.10 + 1.7 - - xplenty.jar-core - xplenty.jar-integ-test - + + + com.sun.jersey + jersey-client + 1.9.1 + true + + + + com.sun.jersey + jersey-core + 1.9.1 + true + + + + com.sun.jersey + jersey-json + 1.9.1 + true + + + + com.fasterxml.jackson.core + jackson-core + 2.1.1 + + + + com.fasterxml.jackson.core + jackson-databind + 2.1.1 + + + + com.fasterxml.jackson.core + jackson-annotations + 2.1.1 + + + + io.netty + netty + 3.10.5.Final + true + + + + + org.scala-lang + scala-library + ${scala.version} + true + + + org.scalatest + scalatest_2.10 + 1.9.1 + test + + + org.scalatra + scalatra${scalatra.suffix} + ${scalatra.version} + + + org.scala-lang + scala-library + + + true + + + javax.servlet + javax.servlet-api + 3.1.0 + test + + + + junit + junit + 4.6 + test + + + + org.mockito + mockito-all + 1.8.4 + test + + + + + Xplenty + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + ${jdk.version} + ${jdk.version} + + + + org.eclipse.jetty + jetty-maven-plugin + 9.3.1.v20150714 + + 10 + 8005 + STOP + + / + ${project.basedir}/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml + + ${project.basedir}/src/test/scala/com/xplenty/api/webapp + ${project.build.testOutputDirectory} + + + + start-jetty + pre-integration-test + + start + + + 0 + + + + stop-jetty + post-integration-test + + stop + + + + + + net.alchim31.maven + scala-maven-plugin + 3.1.0 + + + scala-compile-first + process-resources + + add-source + compile + + + + scala-test-compile + process-test-resources + + + + + testCompile + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.14.1 + + false + + + + verify + + integration-test + verify + + + + + + diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java b/src/main/java/com/xplenty/api/Xplenty.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java rename to src/main/java/com/xplenty/api/Xplenty.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java b/src/main/java/com/xplenty/api/XplentyAPI.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java rename to src/main/java/com/xplenty/api/XplentyAPI.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/AuthFailedException.java b/src/main/java/com/xplenty/api/exceptions/AuthFailedException.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/AuthFailedException.java rename to src/main/java/com/xplenty/api/exceptions/AuthFailedException.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/RequestFailedException.java b/src/main/java/com/xplenty/api/exceptions/RequestFailedException.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/RequestFailedException.java rename to src/main/java/com/xplenty/api/exceptions/RequestFailedException.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/XplentyAPIException.java b/src/main/java/com/xplenty/api/exceptions/XplentyAPIException.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/XplentyAPIException.java rename to src/main/java/com/xplenty/api/exceptions/XplentyAPIException.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/ClientBuilder.java b/src/main/java/com/xplenty/api/http/ClientBuilder.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/http/ClientBuilder.java rename to src/main/java/com/xplenty/api/http/ClientBuilder.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/ConsoleNettyLogger.java b/src/main/java/com/xplenty/api/http/ConsoleNettyLogger.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/http/ConsoleNettyLogger.java rename to src/main/java/com/xplenty/api/http/ConsoleNettyLogger.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java b/src/main/java/com/xplenty/api/http/Http.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/http/Http.java rename to src/main/java/com/xplenty/api/http/Http.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java b/src/main/java/com/xplenty/api/http/HttpClient.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/http/HttpClient.java rename to src/main/java/com/xplenty/api/http/HttpClient.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java b/src/main/java/com/xplenty/api/http/JerseyClient.java similarity index 97% rename from xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java rename to src/main/java/com/xplenty/api/http/JerseyClient.java index 5c61aba..d07b396 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JerseyClient.java +++ b/src/main/java/com/xplenty/api/http/JerseyClient.java @@ -79,7 +79,9 @@ public T execute(Request request) { case PUT: response = builder.put(ClientResponse.class); break; case DELETE: response = builder.delete(ClientResponse.class); break; } - Response processedResponse = Response.forContentType(request.getResponseType(), response.getEntity(String.class), + final String entity = response.hasEntity() && response.getStatus() != 204 ? response.getEntity(String.class) : null; + + Response processedResponse = Response.forContentType(request.getResponseType(), entity, response.getStatus(), convertJerseyHeaders(response.getHeaders())); processedResponse.validate(request.getName()); return request.getResponse(processedResponse); diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonMapperFactory.java b/src/main/java/com/xplenty/api/http/JsonMapperFactory.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonMapperFactory.java rename to src/main/java/com/xplenty/api/http/JsonMapperFactory.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonResponse.java b/src/main/java/com/xplenty/api/http/JsonResponse.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/http/JsonResponse.java rename to src/main/java/com/xplenty/api/http/JsonResponse.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java b/src/main/java/com/xplenty/api/http/Response.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/http/Response.java rename to src/main/java/com/xplenty/api/http/Response.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SSLEngineDefaultImpl.java b/src/main/java/com/xplenty/api/http/SSLEngineDefaultImpl.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/http/SSLEngineDefaultImpl.java rename to src/main/java/com/xplenty/api/http/SSLEngineDefaultImpl.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java b/src/main/java/com/xplenty/api/http/SyncNettyClient.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/http/SyncNettyClient.java rename to src/main/java/com/xplenty/api/http/SyncNettyClient.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Account.java b/src/main/java/com/xplenty/api/model/Account.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Account.java rename to src/main/java/com/xplenty/api/model/Account.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Cluster.java b/src/main/java/com/xplenty/api/model/Cluster.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Cluster.java rename to src/main/java/com/xplenty/api/model/Cluster.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterBootstrapAction.java b/src/main/java/com/xplenty/api/model/ClusterBootstrapAction.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterBootstrapAction.java rename to src/main/java/com/xplenty/api/model/ClusterBootstrapAction.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterInstance.java b/src/main/java/com/xplenty/api/model/ClusterInstance.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterInstance.java rename to src/main/java/com/xplenty/api/model/ClusterInstance.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterWatchingLogEntry.java b/src/main/java/com/xplenty/api/model/ClusterWatchingLogEntry.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/ClusterWatchingLogEntry.java rename to src/main/java/com/xplenty/api/model/ClusterWatchingLogEntry.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Connection.java b/src/main/java/com/xplenty/api/model/Connection.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Connection.java rename to src/main/java/com/xplenty/api/model/Connection.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ConnectionType.java b/src/main/java/com/xplenty/api/model/ConnectionType.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/ConnectionType.java rename to src/main/java/com/xplenty/api/model/ConnectionType.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ConnectionTypeGroup.java b/src/main/java/com/xplenty/api/model/ConnectionTypeGroup.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/ConnectionTypeGroup.java rename to src/main/java/com/xplenty/api/model/ConnectionTypeGroup.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Creator.java b/src/main/java/com/xplenty/api/model/Creator.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Creator.java rename to src/main/java/com/xplenty/api/model/Creator.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/CreditCardInfo.java b/src/main/java/com/xplenty/api/model/CreditCardInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/CreditCardInfo.java rename to src/main/java/com/xplenty/api/model/CreditCardInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/HookEvent.java b/src/main/java/com/xplenty/api/model/HookEvent.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/HookEvent.java rename to src/main/java/com/xplenty/api/model/HookEvent.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java b/src/main/java/com/xplenty/api/model/Job.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java rename to src/main/java/com/xplenty/api/model/Job.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobLog.java b/src/main/java/com/xplenty/api/model/JobLog.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/JobLog.java rename to src/main/java/com/xplenty/api/model/JobLog.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobOutput.java b/src/main/java/com/xplenty/api/model/JobOutput.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/JobOutput.java rename to src/main/java/com/xplenty/api/model/JobOutput.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobOutputPreview.java b/src/main/java/com/xplenty/api/model/JobOutputPreview.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/JobOutputPreview.java rename to src/main/java/com/xplenty/api/model/JobOutputPreview.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobWatchingLogEntry.java b/src/main/java/com/xplenty/api/model/JobWatchingLogEntry.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/JobWatchingLogEntry.java rename to src/main/java/com/xplenty/api/model/JobWatchingLogEntry.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java b/src/main/java/com/xplenty/api/model/Member.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Member.java rename to src/main/java/com/xplenty/api/model/Member.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Notification.java b/src/main/java/com/xplenty/api/model/Notification.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Notification.java rename to src/main/java/com/xplenty/api/model/Notification.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Package.java b/src/main/java/com/xplenty/api/model/Package.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Package.java rename to src/main/java/com/xplenty/api/model/Package.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplate.java b/src/main/java/com/xplenty/api/model/PackageTemplate.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplate.java rename to src/main/java/com/xplenty/api/model/PackageTemplate.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java b/src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java rename to src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java b/src/main/java/com/xplenty/api/model/PackageValidation.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidation.java rename to src/main/java/com/xplenty/api/model/PackageValidation.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidationError.java b/src/main/java/com/xplenty/api/model/PackageValidationError.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/PackageValidationError.java rename to src/main/java/com/xplenty/api/model/PackageValidationError.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Plan.java b/src/main/java/com/xplenty/api/model/Plan.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Plan.java rename to src/main/java/com/xplenty/api/model/Plan.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ProductUpdate.java b/src/main/java/com/xplenty/api/model/ProductUpdate.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/ProductUpdate.java rename to src/main/java/com/xplenty/api/model/ProductUpdate.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/PublicKey.java b/src/main/java/com/xplenty/api/model/PublicKey.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/PublicKey.java rename to src/main/java/com/xplenty/api/model/PublicKey.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Region.java b/src/main/java/com/xplenty/api/model/Region.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Region.java rename to src/main/java/com/xplenty/api/model/Region.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Schedule.java b/src/main/java/com/xplenty/api/model/Schedule.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Schedule.java rename to src/main/java/com/xplenty/api/model/Schedule.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ScheduleTask.java b/src/main/java/com/xplenty/api/model/ScheduleTask.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/ScheduleTask.java rename to src/main/java/com/xplenty/api/model/ScheduleTask.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ScheduleTaskPackage.java b/src/main/java/com/xplenty/api/model/ScheduleTaskPackage.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/ScheduleTaskPackage.java rename to src/main/java/com/xplenty/api/model/ScheduleTaskPackage.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Stack.java b/src/main/java/com/xplenty/api/model/Stack.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Stack.java rename to src/main/java/com/xplenty/api/model/Stack.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java b/src/main/java/com/xplenty/api/model/Subscription.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Subscription.java rename to src/main/java/com/xplenty/api/model/Subscription.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Timezone.java b/src/main/java/com/xplenty/api/model/Timezone.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Timezone.java rename to src/main/java/com/xplenty/api/model/Timezone.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java b/src/main/java/com/xplenty/api/model/User.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/User.java rename to src/main/java/com/xplenty/api/model/User.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/UserNotificationSettings.java b/src/main/java/com/xplenty/api/model/UserNotificationSettings.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/UserNotificationSettings.java rename to src/main/java/com/xplenty/api/model/UserNotificationSettings.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Watcher.java b/src/main/java/com/xplenty/api/model/Watcher.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/Watcher.java rename to src/main/java/com/xplenty/api/model/Watcher.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java b/src/main/java/com/xplenty/api/model/WebHook.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHook.java rename to src/main/java/com/xplenty/api/model/WebHook.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java b/src/main/java/com/xplenty/api/model/WebHookEvent.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookEvent.java rename to src/main/java/com/xplenty/api/model/WebHookEvent.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookSettings.java b/src/main/java/com/xplenty/api/model/WebHookSettings.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/WebHookSettings.java rename to src/main/java/com/xplenty/api/model/WebHookSettings.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/model/XplentyObject.java b/src/main/java/com/xplenty/api/model/XplentyObject.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/model/XplentyObject.java rename to src/main/java/com/xplenty/api/model/XplentyObject.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java b/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java rename to src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractInfoRequest.java b/src/main/java/com/xplenty/api/request/AbstractInfoRequest.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractInfoRequest.java rename to src/main/java/com/xplenty/api/request/AbstractInfoRequest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractListRequest.java b/src/main/java/com/xplenty/api/request/AbstractListRequest.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractListRequest.java rename to src/main/java/com/xplenty/api/request/AbstractListRequest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java b/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java rename to src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractRequest.java b/src/main/java/com/xplenty/api/request/AbstractRequest.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractRequest.java rename to src/main/java/com/xplenty/api/request/AbstractRequest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/RawGetRequest.java b/src/main/java/com/xplenty/api/request/RawGetRequest.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/RawGetRequest.java rename to src/main/java/com/xplenty/api/request/RawGetRequest.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/Request.java b/src/main/java/com/xplenty/api/request/Request.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/Request.java rename to src/main/java/com/xplenty/api/request/Request.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/AccountInfo.java b/src/main/java/com/xplenty/api/request/account/AccountInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/account/AccountInfo.java rename to src/main/java/com/xplenty/api/request/account/AccountInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/CreateAccount.java b/src/main/java/com/xplenty/api/request/account/CreateAccount.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/account/CreateAccount.java rename to src/main/java/com/xplenty/api/request/account/CreateAccount.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/DeleteAccount.java b/src/main/java/com/xplenty/api/request/account/DeleteAccount.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/account/DeleteAccount.java rename to src/main/java/com/xplenty/api/request/account/DeleteAccount.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/ListAccountRegions.java b/src/main/java/com/xplenty/api/request/account/ListAccountRegions.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/account/ListAccountRegions.java rename to src/main/java/com/xplenty/api/request/account/ListAccountRegions.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/ListAccounts.java b/src/main/java/com/xplenty/api/request/account/ListAccounts.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/account/ListAccounts.java rename to src/main/java/com/xplenty/api/request/account/ListAccounts.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/account/UpdateAccount.java b/src/main/java/com/xplenty/api/request/account/UpdateAccount.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/account/UpdateAccount.java rename to src/main/java/com/xplenty/api/request/account/UpdateAccount.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ClusterInfo.java b/src/main/java/com/xplenty/api/request/cluster/ClusterInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ClusterInfo.java rename to src/main/java/com/xplenty/api/request/cluster/ClusterInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/CreateCluster.java b/src/main/java/com/xplenty/api/request/cluster/CreateCluster.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/CreateCluster.java rename to src/main/java/com/xplenty/api/request/cluster/CreateCluster.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ListClusterInstances.java b/src/main/java/com/xplenty/api/request/cluster/ListClusterInstances.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ListClusterInstances.java rename to src/main/java/com/xplenty/api/request/cluster/ListClusterInstances.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ListClusters.java b/src/main/java/com/xplenty/api/request/cluster/ListClusters.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/ListClusters.java rename to src/main/java/com/xplenty/api/request/cluster/ListClusters.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/TerminateCluster.java b/src/main/java/com/xplenty/api/request/cluster/TerminateCluster.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/TerminateCluster.java rename to src/main/java/com/xplenty/api/request/cluster/TerminateCluster.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/UpdateCluster.java b/src/main/java/com/xplenty/api/request/cluster/UpdateCluster.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/cluster/UpdateCluster.java rename to src/main/java/com/xplenty/api/request/cluster/UpdateCluster.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ConnectionInfo.java b/src/main/java/com/xplenty/api/request/connection/ConnectionInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ConnectionInfo.java rename to src/main/java/com/xplenty/api/request/connection/ConnectionInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/DeleteConnection.java b/src/main/java/com/xplenty/api/request/connection/DeleteConnection.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/DeleteConnection.java rename to src/main/java/com/xplenty/api/request/connection/DeleteConnection.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ListConnectionTypes.java b/src/main/java/com/xplenty/api/request/connection/ListConnectionTypes.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ListConnectionTypes.java rename to src/main/java/com/xplenty/api/request/connection/ListConnectionTypes.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ListConnections.java b/src/main/java/com/xplenty/api/request/connection/ListConnections.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/connection/ListConnections.java rename to src/main/java/com/xplenty/api/request/connection/ListConnections.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobExecutionVariables.java b/src/main/java/com/xplenty/api/request/job/JobExecutionVariables.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobExecutionVariables.java rename to src/main/java/com/xplenty/api/request/job/JobExecutionVariables.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobInfo.java b/src/main/java/com/xplenty/api/request/job/JobInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobInfo.java rename to src/main/java/com/xplenty/api/request/job/JobInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobLogs.java b/src/main/java/com/xplenty/api/request/job/JobLogs.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobLogs.java rename to src/main/java/com/xplenty/api/request/job/JobLogs.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobPreviewOutput.java b/src/main/java/com/xplenty/api/request/job/JobPreviewOutput.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/job/JobPreviewOutput.java rename to src/main/java/com/xplenty/api/request/job/JobPreviewOutput.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/ListJobs.java b/src/main/java/com/xplenty/api/request/job/ListJobs.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/job/ListJobs.java rename to src/main/java/com/xplenty/api/request/job/ListJobs.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/RunJob.java b/src/main/java/com/xplenty/api/request/job/RunJob.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/job/RunJob.java rename to src/main/java/com/xplenty/api/request/job/RunJob.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/job/StopJob.java b/src/main/java/com/xplenty/api/request/job/StopJob.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/job/StopJob.java rename to src/main/java/com/xplenty/api/request/job/StopJob.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/CreateMember.java b/src/main/java/com/xplenty/api/request/member/CreateMember.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/member/CreateMember.java rename to src/main/java/com/xplenty/api/request/member/CreateMember.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/DeleteMember.java b/src/main/java/com/xplenty/api/request/member/DeleteMember.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/member/DeleteMember.java rename to src/main/java/com/xplenty/api/request/member/DeleteMember.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/ListMembers.java b/src/main/java/com/xplenty/api/request/member/ListMembers.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/member/ListMembers.java rename to src/main/java/com/xplenty/api/request/member/ListMembers.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/MemberInfo.java b/src/main/java/com/xplenty/api/request/member/MemberInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/member/MemberInfo.java rename to src/main/java/com/xplenty/api/request/member/MemberInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/member/SetMemberRole.java b/src/main/java/com/xplenty/api/request/member/SetMemberRole.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/member/SetMemberRole.java rename to src/main/java/com/xplenty/api/request/member/SetMemberRole.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/LikeProductUpdate.java b/src/main/java/com/xplenty/api/request/misc/LikeProductUpdate.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/LikeProductUpdate.java rename to src/main/java/com/xplenty/api/request/misc/LikeProductUpdate.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListProductUpdates.java b/src/main/java/com/xplenty/api/request/misc/ListProductUpdates.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListProductUpdates.java rename to src/main/java/com/xplenty/api/request/misc/ListProductUpdates.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListRegions.java b/src/main/java/com/xplenty/api/request/misc/ListRegions.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListRegions.java rename to src/main/java/com/xplenty/api/request/misc/ListRegions.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListStacks.java b/src/main/java/com/xplenty/api/request/misc/ListStacks.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListStacks.java rename to src/main/java/com/xplenty/api/request/misc/ListStacks.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListSystemVariables.java b/src/main/java/com/xplenty/api/request/misc/ListSystemVariables.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListSystemVariables.java rename to src/main/java/com/xplenty/api/request/misc/ListSystemVariables.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListTimezones.java b/src/main/java/com/xplenty/api/request/misc/ListTimezones.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/misc/ListTimezones.java rename to src/main/java/com/xplenty/api/request/misc/ListTimezones.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/CreatePublicKey.java b/src/main/java/com/xplenty/api/request/public_key/CreatePublicKey.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/CreatePublicKey.java rename to src/main/java/com/xplenty/api/request/public_key/CreatePublicKey.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/DeletePublicKey.java b/src/main/java/com/xplenty/api/request/public_key/DeletePublicKey.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/DeletePublicKey.java rename to src/main/java/com/xplenty/api/request/public_key/DeletePublicKey.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/ListPublicKeys.java b/src/main/java/com/xplenty/api/request/public_key/ListPublicKeys.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/ListPublicKeys.java rename to src/main/java/com/xplenty/api/request/public_key/ListPublicKeys.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/PublicKeyInfo.java b/src/main/java/com/xplenty/api/request/public_key/PublicKeyInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/public_key/PublicKeyInfo.java rename to src/main/java/com/xplenty/api/request/public_key/PublicKeyInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/CloneSchedule.java b/src/main/java/com/xplenty/api/request/schedule/CloneSchedule.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/CloneSchedule.java rename to src/main/java/com/xplenty/api/request/schedule/CloneSchedule.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/CreateSchedule.java b/src/main/java/com/xplenty/api/request/schedule/CreateSchedule.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/CreateSchedule.java rename to src/main/java/com/xplenty/api/request/schedule/CreateSchedule.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/DeleteSchedule.java b/src/main/java/com/xplenty/api/request/schedule/DeleteSchedule.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/DeleteSchedule.java rename to src/main/java/com/xplenty/api/request/schedule/DeleteSchedule.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/ListSchedules.java b/src/main/java/com/xplenty/api/request/schedule/ListSchedules.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/ListSchedules.java rename to src/main/java/com/xplenty/api/request/schedule/ListSchedules.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/ScheduleInfo.java b/src/main/java/com/xplenty/api/request/schedule/ScheduleInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/ScheduleInfo.java rename to src/main/java/com/xplenty/api/request/schedule/ScheduleInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/UpdateSchedule.java b/src/main/java/com/xplenty/api/request/schedule/UpdateSchedule.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/schedule/UpdateSchedule.java rename to src/main/java/com/xplenty/api/request/schedule/UpdateSchedule.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/ListPlans.java b/src/main/java/com/xplenty/api/request/subscription/ListPlans.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/ListPlans.java rename to src/main/java/com/xplenty/api/request/subscription/ListPlans.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/PaymentMehodInfo.java b/src/main/java/com/xplenty/api/request/subscription/PaymentMehodInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/PaymentMehodInfo.java rename to src/main/java/com/xplenty/api/request/subscription/PaymentMehodInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/SubscriptionInfo.java b/src/main/java/com/xplenty/api/request/subscription/SubscriptionInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/SubscriptionInfo.java rename to src/main/java/com/xplenty/api/request/subscription/SubscriptionInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlan.java b/src/main/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlan.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlan.java rename to src/main/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlan.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/CurrentUserInfo.java b/src/main/java/com/xplenty/api/request/user/CurrentUserInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/user/CurrentUserInfo.java rename to src/main/java/com/xplenty/api/request/user/CurrentUserInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/ListNotifications.java b/src/main/java/com/xplenty/api/request/user/ListNotifications.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/user/ListNotifications.java rename to src/main/java/com/xplenty/api/request/user/ListNotifications.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/MarkNotificationsRead.java b/src/main/java/com/xplenty/api/request/user/MarkNotificationsRead.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/user/MarkNotificationsRead.java rename to src/main/java/com/xplenty/api/request/user/MarkNotificationsRead.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/user/UpdateCurrentUser.java b/src/main/java/com/xplenty/api/request/user/UpdateCurrentUser.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/user/UpdateCurrentUser.java rename to src/main/java/com/xplenty/api/request/user/UpdateCurrentUser.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java b/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java similarity index 96% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java rename to src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java index 2c0fdd8..ef44a05 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java +++ b/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java @@ -32,7 +32,7 @@ public String getName() { public String getEndpoint() { return Resource.ClusterWatcher.format(Long.toString(_clusterId)); } @Override - public boolean hasBody() { return true; } + public boolean hasBody() { return false; } @Override public Object getBody() { return null; } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java b/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java similarity index 96% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java rename to src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java index c448200..348be62 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java +++ b/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java @@ -34,7 +34,7 @@ public String getEndpoint() { } @Override - public boolean hasBody() { return true; } + public boolean hasBody() { return false; } @Override public Object getBody() { return null; } diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/ListWatchers.java b/src/main/java/com/xplenty/api/request/watching/ListWatchers.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/ListWatchers.java rename to src/main/java/com/xplenty/api/request/watching/ListWatchers.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java b/src/main/java/com/xplenty/api/request/watching/WatchingStop.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/watching/WatchingStop.java rename to src/main/java/com/xplenty/api/request/watching/WatchingStop.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/CreateWebHook.java b/src/main/java/com/xplenty/api/request/webhook/CreateWebHook.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/CreateWebHook.java rename to src/main/java/com/xplenty/api/request/webhook/CreateWebHook.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/DeleteWebHook.java b/src/main/java/com/xplenty/api/request/webhook/DeleteWebHook.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/DeleteWebHook.java rename to src/main/java/com/xplenty/api/request/webhook/DeleteWebHook.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ListHookEvents.java b/src/main/java/com/xplenty/api/request/webhook/ListHookEvents.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ListHookEvents.java rename to src/main/java/com/xplenty/api/request/webhook/ListHookEvents.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ListWebHooks.java b/src/main/java/com/xplenty/api/request/webhook/ListWebHooks.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ListWebHooks.java rename to src/main/java/com/xplenty/api/request/webhook/ListWebHooks.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/PingWebHook.java b/src/main/java/com/xplenty/api/request/webhook/PingWebHook.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/PingWebHook.java rename to src/main/java/com/xplenty/api/request/webhook/PingWebHook.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ToggleWebHook.java b/src/main/java/com/xplenty/api/request/webhook/ToggleWebHook.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/ToggleWebHook.java rename to src/main/java/com/xplenty/api/request/webhook/ToggleWebHook.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/UpdateWebHook.java b/src/main/java/com/xplenty/api/request/webhook/UpdateWebHook.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/UpdateWebHook.java rename to src/main/java/com/xplenty/api/request/webhook/UpdateWebHook.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookInfo.java b/src/main/java/com/xplenty/api/request/webhook/WebHookInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookInfo.java rename to src/main/java/com/xplenty/api/request/webhook/WebHookInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookResetSalt.java b/src/main/java/com/xplenty/api/request/webhook/WebHookResetSalt.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/webhook/WebHookResetSalt.java rename to src/main/java/com/xplenty/api/request/webhook/WebHookResetSalt.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/CreatePackage.java b/src/main/java/com/xplenty/api/request/xpackage/CreatePackage.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/CreatePackage.java rename to src/main/java/com/xplenty/api/request/xpackage/CreatePackage.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/DeletePackage.java b/src/main/java/com/xplenty/api/request/xpackage/DeletePackage.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/DeletePackage.java rename to src/main/java/com/xplenty/api/request/xpackage/DeletePackage.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageTemplates.java b/src/main/java/com/xplenty/api/request/xpackage/ListPackageTemplates.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageTemplates.java rename to src/main/java/com/xplenty/api/request/xpackage/ListPackageTemplates.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java b/src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java rename to src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java b/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java rename to src/main/java/com/xplenty/api/request/xpackage/ListPackages.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java b/src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java rename to src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageValidationInfo.java b/src/main/java/com/xplenty/api/request/xpackage/PackageValidationInfo.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/PackageValidationInfo.java rename to src/main/java/com/xplenty/api/request/xpackage/PackageValidationInfo.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/RunPackageValidation.java b/src/main/java/com/xplenty/api/request/xpackage/RunPackageValidation.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/RunPackageValidation.java rename to src/main/java/com/xplenty/api/request/xpackage/RunPackageValidation.java diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/UpdatePackage.java b/src/main/java/com/xplenty/api/request/xpackage/UpdatePackage.java similarity index 100% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/xpackage/UpdatePackage.java rename to src/main/java/com/xplenty/api/request/xpackage/UpdatePackage.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java b/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java rename to src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstStaging.java b/src/test/java/com/xplenty/api/WatchersTestAgainstStaging.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstStaging.java rename to src/test/java/com/xplenty/api/WatchersTestAgainstStaging.java diff --git a/xplenty.jar-integ-test/src/test/com/xplenty/ITWatchersTestAgainstMockServer.java b/src/test/java/com/xplenty/api/integration/ITWatchersTestAgainstMockServer.java similarity index 61% rename from xplenty.jar-integ-test/src/test/com/xplenty/ITWatchersTestAgainstMockServer.java rename to src/test/java/com/xplenty/api/integration/ITWatchersTestAgainstMockServer.java index eba3086..d8734d3 100644 --- a/xplenty.jar-integ-test/src/test/com/xplenty/ITWatchersTestAgainstMockServer.java +++ b/src/test/java/com/xplenty/api/integration/ITWatchersTestAgainstMockServer.java @@ -1,15 +1,21 @@ -package com.xplenty; +package com.xplenty.api.integration; +import com.xplenty.api.Xplenty; import com.xplenty.api.XplentyAPI; +import com.xplenty.api.http.ClientBuilder; +import com.xplenty.api.http.Http; import com.xplenty.api.model.ClusterWatchingLogEntry; import com.xplenty.api.model.JobWatchingLogEntry; import com.xplenty.api.model.Watcher; -import com.xplenty.api.util.Http; import junit.framework.Assert; import junit.framework.TestCase; -import org.joda.time.DateTime; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; public class ITWatchersTestAgainstMockServer extends TestCase { + private final DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); private XplentyAPI api; private String host = "localhost:8080/mock"; @@ -19,7 +25,10 @@ public class ITWatchersTestAgainstMockServer extends TestCase { @Override public void setUp(){ - api = new XplentyAPI(accountID, apiKey, host, Http.Protocol.Http); + ClientBuilder builder = new ClientBuilder().withAccount(accountID).withApiKey(apiKey). + withHost(host).withProtocol(Http.Protocol.Http).withVersion(Xplenty.Version.V2). + withClientImpl(Http.HttpClientImpl.SyncNetty).withLogHttpCommunication(true).withTimeout(10); + api = new XplentyAPI(builder); } public void testListClusterWatchers() { @@ -34,15 +43,15 @@ public void testJobWatchers() { Assert.assertEquals("Xplenty - Job watcher", res[0].getName()); } - public void testAddWatchers4Cluster() { + public void testAddWatchers4Cluster() throws ParseException { ClusterWatchingLogEntry res = api.addClusterWatchers(5); - Assert.assertEquals(new DateTime("2013-04-09T11:19:20+03:00").toDate(), res.getCreationTimeStamp()); + Assert.assertEquals(dFormat.parse("2013-04-09T11:19:20Z"), res.getCreationTimeStamp()); Assert.assertEquals( "https://api.xplenty.com/xplenation/api/clusters/370", res.getUrl()); } - public void testAddWatchers4Job() { + public void testAddWatchers4Job() throws ParseException { JobWatchingLogEntry res = api.addJobWatchers(7); - Assert.assertEquals(new DateTime("2013-04-09T11:19:20+03:00").toDate(), res.getCreationTimeStamp()); + Assert.assertEquals(dFormat.parse("2013-04-09T11:19:20Z"), res.getCreationTimeStamp()); Assert.assertEquals("https://api.xplenty.com/xplenation/api/jobs/370", res.getUrl() ); } diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/AccountTest.java b/src/test/java/com/xplenty/api/model/AccountTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/AccountTest.java rename to src/test/java/com/xplenty/api/model/AccountTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/ClusterTest.java b/src/test/java/com/xplenty/api/model/ClusterTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/ClusterTest.java rename to src/test/java/com/xplenty/api/model/ClusterTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/ConnectionTest.java b/src/test/java/com/xplenty/api/model/ConnectionTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/ConnectionTest.java rename to src/test/java/com/xplenty/api/model/ConnectionTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/ConnectionTypeTest.java b/src/test/java/com/xplenty/api/model/ConnectionTypeTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/ConnectionTypeTest.java rename to src/test/java/com/xplenty/api/model/ConnectionTypeTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/CreditCardInfoTest.java b/src/test/java/com/xplenty/api/model/CreditCardInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/CreditCardInfoTest.java rename to src/test/java/com/xplenty/api/model/CreditCardInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/JobTest.java b/src/test/java/com/xplenty/api/model/JobTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/JobTest.java rename to src/test/java/com/xplenty/api/model/JobTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/MemberTest.java b/src/test/java/com/xplenty/api/model/MemberTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/MemberTest.java rename to src/test/java/com/xplenty/api/model/MemberTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/MiscTest.java b/src/test/java/com/xplenty/api/model/MiscTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/MiscTest.java rename to src/test/java/com/xplenty/api/model/MiscTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/NotificationTest.java b/src/test/java/com/xplenty/api/model/NotificationTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/NotificationTest.java rename to src/test/java/com/xplenty/api/model/NotificationTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/PackageTest.java b/src/test/java/com/xplenty/api/model/PackageTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/PackageTest.java rename to src/test/java/com/xplenty/api/model/PackageTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/PlanTest.java b/src/test/java/com/xplenty/api/model/PlanTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/PlanTest.java rename to src/test/java/com/xplenty/api/model/PlanTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/PublicKeyTest.java b/src/test/java/com/xplenty/api/model/PublicKeyTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/PublicKeyTest.java rename to src/test/java/com/xplenty/api/model/PublicKeyTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/RegionTest.java b/src/test/java/com/xplenty/api/model/RegionTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/RegionTest.java rename to src/test/java/com/xplenty/api/model/RegionTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/ScheduleTest.java b/src/test/java/com/xplenty/api/model/ScheduleTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/ScheduleTest.java rename to src/test/java/com/xplenty/api/model/ScheduleTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/SubscriptionTest.java b/src/test/java/com/xplenty/api/model/SubscriptionTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/SubscriptionTest.java rename to src/test/java/com/xplenty/api/model/SubscriptionTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/UserTest.java b/src/test/java/com/xplenty/api/model/UserTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/UserTest.java rename to src/test/java/com/xplenty/api/model/UserTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/WebHookTest.java b/src/test/java/com/xplenty/api/model/WebHookTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/WebHookTest.java rename to src/test/java/com/xplenty/api/model/WebHookTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/XplentyObjectTest.java b/src/test/java/com/xplenty/api/model/XplentyObjectTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/model/XplentyObjectTest.java rename to src/test/java/com/xplenty/api/model/XplentyObjectTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/AccountInfoTest.java b/src/test/java/com/xplenty/api/request/account/AccountInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/account/AccountInfoTest.java rename to src/test/java/com/xplenty/api/request/account/AccountInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/CreateAccountTest.java b/src/test/java/com/xplenty/api/request/account/CreateAccountTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/account/CreateAccountTest.java rename to src/test/java/com/xplenty/api/request/account/CreateAccountTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/DeleteAccountTest.java b/src/test/java/com/xplenty/api/request/account/DeleteAccountTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/account/DeleteAccountTest.java rename to src/test/java/com/xplenty/api/request/account/DeleteAccountTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/ListAccountRegionsTest.java b/src/test/java/com/xplenty/api/request/account/ListAccountRegionsTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/account/ListAccountRegionsTest.java rename to src/test/java/com/xplenty/api/request/account/ListAccountRegionsTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/ListAccountsTest.java b/src/test/java/com/xplenty/api/request/account/ListAccountsTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/account/ListAccountsTest.java rename to src/test/java/com/xplenty/api/request/account/ListAccountsTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/account/UpdateAccountTest.java b/src/test/java/com/xplenty/api/request/account/UpdateAccountTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/account/UpdateAccountTest.java rename to src/test/java/com/xplenty/api/request/account/UpdateAccountTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java b/src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java rename to src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/CreateClusterTest.java b/src/test/java/com/xplenty/api/request/cluster/CreateClusterTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/CreateClusterTest.java rename to src/test/java/com/xplenty/api/request/cluster/CreateClusterTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ListClusterInstancesTest.java b/src/test/java/com/xplenty/api/request/cluster/ListClusterInstancesTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ListClusterInstancesTest.java rename to src/test/java/com/xplenty/api/request/cluster/ListClusterInstancesTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ListClustersTest.java b/src/test/java/com/xplenty/api/request/cluster/ListClustersTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/ListClustersTest.java rename to src/test/java/com/xplenty/api/request/cluster/ListClustersTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/TerminateClusterTest.java b/src/test/java/com/xplenty/api/request/cluster/TerminateClusterTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/TerminateClusterTest.java rename to src/test/java/com/xplenty/api/request/cluster/TerminateClusterTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/UpdateClusterTest.java b/src/test/java/com/xplenty/api/request/cluster/UpdateClusterTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/cluster/UpdateClusterTest.java rename to src/test/java/com/xplenty/api/request/cluster/UpdateClusterTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ConnectionInfoTest.java b/src/test/java/com/xplenty/api/request/connection/ConnectionInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ConnectionInfoTest.java rename to src/test/java/com/xplenty/api/request/connection/ConnectionInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/DeleteConnectionTest.java b/src/test/java/com/xplenty/api/request/connection/DeleteConnectionTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/DeleteConnectionTest.java rename to src/test/java/com/xplenty/api/request/connection/DeleteConnectionTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ListConnectionTypesTest.java b/src/test/java/com/xplenty/api/request/connection/ListConnectionTypesTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ListConnectionTypesTest.java rename to src/test/java/com/xplenty/api/request/connection/ListConnectionTypesTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ListConnectionsTest.java b/src/test/java/com/xplenty/api/request/connection/ListConnectionsTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/connection/ListConnectionsTest.java rename to src/test/java/com/xplenty/api/request/connection/ListConnectionsTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobExecutionVariablesTest.java b/src/test/java/com/xplenty/api/request/job/JobExecutionVariablesTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobExecutionVariablesTest.java rename to src/test/java/com/xplenty/api/request/job/JobExecutionVariablesTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobInfoTest.java b/src/test/java/com/xplenty/api/request/job/JobInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobInfoTest.java rename to src/test/java/com/xplenty/api/request/job/JobInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobLogsTest.java b/src/test/java/com/xplenty/api/request/job/JobLogsTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobLogsTest.java rename to src/test/java/com/xplenty/api/request/job/JobLogsTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobPreviewOutputTest.java b/src/test/java/com/xplenty/api/request/job/JobPreviewOutputTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/job/JobPreviewOutputTest.java rename to src/test/java/com/xplenty/api/request/job/JobPreviewOutputTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/ListJobsTest.java b/src/test/java/com/xplenty/api/request/job/ListJobsTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/job/ListJobsTest.java rename to src/test/java/com/xplenty/api/request/job/ListJobsTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/RunJobTest.java b/src/test/java/com/xplenty/api/request/job/RunJobTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/job/RunJobTest.java rename to src/test/java/com/xplenty/api/request/job/RunJobTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/job/StopJobTest.java b/src/test/java/com/xplenty/api/request/job/StopJobTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/job/StopJobTest.java rename to src/test/java/com/xplenty/api/request/job/StopJobTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/CreateMemberTest.java b/src/test/java/com/xplenty/api/request/member/CreateMemberTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/member/CreateMemberTest.java rename to src/test/java/com/xplenty/api/request/member/CreateMemberTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/DeleteMemberTest.java b/src/test/java/com/xplenty/api/request/member/DeleteMemberTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/member/DeleteMemberTest.java rename to src/test/java/com/xplenty/api/request/member/DeleteMemberTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/MemberInfoTest.java b/src/test/java/com/xplenty/api/request/member/MemberInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/member/MemberInfoTest.java rename to src/test/java/com/xplenty/api/request/member/MemberInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/MemberListTest.java b/src/test/java/com/xplenty/api/request/member/MemberListTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/member/MemberListTest.java rename to src/test/java/com/xplenty/api/request/member/MemberListTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/member/SetMemberRoleTest.java b/src/test/java/com/xplenty/api/request/member/SetMemberRoleTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/member/SetMemberRoleTest.java rename to src/test/java/com/xplenty/api/request/member/SetMemberRoleTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/LikeProductUpdateTest.java b/src/test/java/com/xplenty/api/request/misc/LikeProductUpdateTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/LikeProductUpdateTest.java rename to src/test/java/com/xplenty/api/request/misc/LikeProductUpdateTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListProductUpdatesTest.java b/src/test/java/com/xplenty/api/request/misc/ListProductUpdatesTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListProductUpdatesTest.java rename to src/test/java/com/xplenty/api/request/misc/ListProductUpdatesTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListRegionsTest.java b/src/test/java/com/xplenty/api/request/misc/ListRegionsTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListRegionsTest.java rename to src/test/java/com/xplenty/api/request/misc/ListRegionsTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListStacksTest.java b/src/test/java/com/xplenty/api/request/misc/ListStacksTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListStacksTest.java rename to src/test/java/com/xplenty/api/request/misc/ListStacksTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListSystemVariablesTest.java b/src/test/java/com/xplenty/api/request/misc/ListSystemVariablesTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListSystemVariablesTest.java rename to src/test/java/com/xplenty/api/request/misc/ListSystemVariablesTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListTimezonesTest.java b/src/test/java/com/xplenty/api/request/misc/ListTimezonesTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/misc/ListTimezonesTest.java rename to src/test/java/com/xplenty/api/request/misc/ListTimezonesTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/CreatePublicKeyTest.java b/src/test/java/com/xplenty/api/request/public_key/CreatePublicKeyTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/CreatePublicKeyTest.java rename to src/test/java/com/xplenty/api/request/public_key/CreatePublicKeyTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/DeletePublicKeyTest.java b/src/test/java/com/xplenty/api/request/public_key/DeletePublicKeyTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/DeletePublicKeyTest.java rename to src/test/java/com/xplenty/api/request/public_key/DeletePublicKeyTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/ListPublicKeysTest.java b/src/test/java/com/xplenty/api/request/public_key/ListPublicKeysTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/ListPublicKeysTest.java rename to src/test/java/com/xplenty/api/request/public_key/ListPublicKeysTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/PublicKeyInfoTest.java b/src/test/java/com/xplenty/api/request/public_key/PublicKeyInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/public_key/PublicKeyInfoTest.java rename to src/test/java/com/xplenty/api/request/public_key/PublicKeyInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/CloneScheduleTest.java b/src/test/java/com/xplenty/api/request/schedule/CloneScheduleTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/CloneScheduleTest.java rename to src/test/java/com/xplenty/api/request/schedule/CloneScheduleTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/CreateScheduleTest.java b/src/test/java/com/xplenty/api/request/schedule/CreateScheduleTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/CreateScheduleTest.java rename to src/test/java/com/xplenty/api/request/schedule/CreateScheduleTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/DeleteScheduleTest.java b/src/test/java/com/xplenty/api/request/schedule/DeleteScheduleTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/DeleteScheduleTest.java rename to src/test/java/com/xplenty/api/request/schedule/DeleteScheduleTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/ListSchedulesTest.java b/src/test/java/com/xplenty/api/request/schedule/ListSchedulesTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/ListSchedulesTest.java rename to src/test/java/com/xplenty/api/request/schedule/ListSchedulesTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/ScheduleInfoTest.java b/src/test/java/com/xplenty/api/request/schedule/ScheduleInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/ScheduleInfoTest.java rename to src/test/java/com/xplenty/api/request/schedule/ScheduleInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/UpdateScheduleTest.java b/src/test/java/com/xplenty/api/request/schedule/UpdateScheduleTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/schedule/UpdateScheduleTest.java rename to src/test/java/com/xplenty/api/request/schedule/UpdateScheduleTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/ListPlansTest.java b/src/test/java/com/xplenty/api/request/subscription/ListPlansTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/ListPlansTest.java rename to src/test/java/com/xplenty/api/request/subscription/ListPlansTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/PaymentMethodInfoTest.java b/src/test/java/com/xplenty/api/request/subscription/PaymentMethodInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/PaymentMethodInfoTest.java rename to src/test/java/com/xplenty/api/request/subscription/PaymentMethodInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/SubscriptionInfotest.java b/src/test/java/com/xplenty/api/request/subscription/SubscriptionInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/SubscriptionInfotest.java rename to src/test/java/com/xplenty/api/request/subscription/SubscriptionInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlanTest.java b/src/test/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlanTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlanTest.java rename to src/test/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlanTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/CurrentUserInfoTest.java b/src/test/java/com/xplenty/api/request/user/CurrentUserInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/user/CurrentUserInfoTest.java rename to src/test/java/com/xplenty/api/request/user/CurrentUserInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/ListNotificationsTest.java b/src/test/java/com/xplenty/api/request/user/ListNotificationsTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/user/ListNotificationsTest.java rename to src/test/java/com/xplenty/api/request/user/ListNotificationsTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/MarkNotificationsReadTest.java b/src/test/java/com/xplenty/api/request/user/MarkNotificationsReadTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/user/MarkNotificationsReadTest.java rename to src/test/java/com/xplenty/api/request/user/MarkNotificationsReadTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/user/UpdateCurrentUserTest.java b/src/test/java/com/xplenty/api/request/user/UpdateCurrentUserTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/user/UpdateCurrentUserTest.java rename to src/test/java/com/xplenty/api/request/user/UpdateCurrentUserTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/CreateWebHookTest.java b/src/test/java/com/xplenty/api/request/webhook/CreateWebHookTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/CreateWebHookTest.java rename to src/test/java/com/xplenty/api/request/webhook/CreateWebHookTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/DeleteWebHookTest.java b/src/test/java/com/xplenty/api/request/webhook/DeleteWebHookTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/DeleteWebHookTest.java rename to src/test/java/com/xplenty/api/request/webhook/DeleteWebHookTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/ListWebHookTest.java b/src/test/java/com/xplenty/api/request/webhook/ListWebHookTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/ListWebHookTest.java rename to src/test/java/com/xplenty/api/request/webhook/ListWebHookTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/PingWebHookTest.java b/src/test/java/com/xplenty/api/request/webhook/PingWebHookTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/PingWebHookTest.java rename to src/test/java/com/xplenty/api/request/webhook/PingWebHookTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/ToggleWebHookTest.java b/src/test/java/com/xplenty/api/request/webhook/ToggleWebHookTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/ToggleWebHookTest.java rename to src/test/java/com/xplenty/api/request/webhook/ToggleWebHookTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/UpdateWebHookTest.java b/src/test/java/com/xplenty/api/request/webhook/UpdateWebHookTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/UpdateWebHookTest.java rename to src/test/java/com/xplenty/api/request/webhook/UpdateWebHookTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookInfoTest.java b/src/test/java/com/xplenty/api/request/webhook/WebHookInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookInfoTest.java rename to src/test/java/com/xplenty/api/request/webhook/WebHookInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookResetSaltTest.java b/src/test/java/com/xplenty/api/request/webhook/WebHookResetSaltTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/webhook/WebHookResetSaltTest.java rename to src/test/java/com/xplenty/api/request/webhook/WebHookResetSaltTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/CreatePackageTest.java b/src/test/java/com/xplenty/api/request/xpackage/CreatePackageTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/CreatePackageTest.java rename to src/test/java/com/xplenty/api/request/xpackage/CreatePackageTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/DeletePackageTest.java b/src/test/java/com/xplenty/api/request/xpackage/DeletePackageTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/DeletePackageTest.java rename to src/test/java/com/xplenty/api/request/xpackage/DeletePackageTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackageTemplatesTest.java b/src/test/java/com/xplenty/api/request/xpackage/ListPackageTemplatesTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackageTemplatesTest.java rename to src/test/java/com/xplenty/api/request/xpackage/ListPackageTemplatesTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackageValidationsTest.java b/src/test/java/com/xplenty/api/request/xpackage/ListPackageValidationsTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackageValidationsTest.java rename to src/test/java/com/xplenty/api/request/xpackage/ListPackageValidationsTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java b/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java rename to src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java b/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java rename to src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageValidationInfoTest.java b/src/test/java/com/xplenty/api/request/xpackage/PackageValidationInfoTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/PackageValidationInfoTest.java rename to src/test/java/com/xplenty/api/request/xpackage/PackageValidationInfoTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/RunPackageValidationTest.java b/src/test/java/com/xplenty/api/request/xpackage/RunPackageValidationTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/RunPackageValidationTest.java rename to src/test/java/com/xplenty/api/request/xpackage/RunPackageValidationTest.java diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/UpdatePackageTest.java b/src/test/java/com/xplenty/api/request/xpackage/UpdatePackageTest.java similarity index 100% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/xpackage/UpdatePackageTest.java rename to src/test/java/com/xplenty/api/request/xpackage/UpdatePackageTest.java diff --git a/xplenty.jar-integ-test/src/test/com/xplenty/ITProviderRegion.scala b/src/test/scala/com/xplenty/api/ITProviderRegion.scala similarity index 94% rename from xplenty.jar-integ-test/src/test/com/xplenty/ITProviderRegion.scala rename to src/test/scala/com/xplenty/api/ITProviderRegion.scala index 644d908..3225073 100644 --- a/xplenty.jar-integ-test/src/test/com/xplenty/ITProviderRegion.scala +++ b/src/test/scala/com/xplenty/api/ITProviderRegion.scala @@ -1,3 +1,5 @@ +package com.xplenty.api + import org.scalatest.FlatSpec import org.scalatest.matchers.ShouldMatchers diff --git a/xplenty.jar-integ-test/src/main/com/xplenty/Router.scala b/src/test/scala/com/xplenty/api/router/Router.scala similarity index 75% rename from xplenty.jar-integ-test/src/main/com/xplenty/Router.scala rename to src/test/scala/com/xplenty/api/router/Router.scala index e441b27..893e71c 100644 --- a/xplenty.jar-integ-test/src/main/com/xplenty/Router.scala +++ b/src/test/scala/com/xplenty/api/router/Router.scala @@ -1,4 +1,4 @@ -package com.xplenty +package com.xplenty.api.router import org.scalatra.ScalatraServlet @@ -16,7 +16,7 @@ class Router extends ScalatraServlet{ } post("/:accountID/api/clusters/:cluster_id/watchers"){ - """{"created_at":"2013-04-09T11:19:20+03:00","cluster_url":"https://api.xplenty.com/xplenation/api/clusters/370"}""" + """{"created_at":"2013-04-09T11:19:20Z","cluster_url":"https://api.xplenty.com/xplenation/api/clusters/370"}""" } delete("/:accountID/api/clusters/:cluster_id/watchers"){ @@ -28,7 +28,7 @@ class Router extends ScalatraServlet{ } post("/:accountID/api/jobs/:cluster_id/watchers"){ - """{"created_at":"2013-04-09T11:19:20+03:00","job_url":"https://api.xplenty.com/xplenation/api/jobs/370"}""" + """{"created_at":"2013-04-09T11:19:20Z","job_url":"https://api.xplenty.com/xplenation/api/jobs/370"}""" } delete("/:accountID/api/jobs/:cluster_id/watchers"){ diff --git a/xplenty.jar-integ-test/src/main/webapp/WEB-INF/web.xml b/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml similarity index 88% rename from xplenty.jar-integ-test/src/main/webapp/WEB-INF/web.xml rename to src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml index cb00261..2bde565 100644 --- a/xplenty.jar-integ-test/src/main/webapp/WEB-INF/web.xml +++ b/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml @@ -6,7 +6,7 @@ version="2.5"> Hello - com.xplenty.Router + com.xplenty.api.router.Router Hello diff --git a/xplenty.jar-core/pom.xml b/xplenty.jar-core/pom.xml deleted file mode 100644 index 7540e0f..0000000 --- a/xplenty.jar-core/pom.xml +++ /dev/null @@ -1,99 +0,0 @@ - - 4.0.0 - - com.xplenty - Xplenty.jar - 0.1.3-SNAPSHOT - jar - - Xplenty.jar - http://xplenty.com - - com.xplenty - root - 1.0.0 - - - - UTF-8 - - - - - com.sun.jersey - jersey-client - 1.9.1 - true - - - - com.sun.jersey - jersey-core - 1.9.1 - true - - - - com.sun.jersey - jersey-json - 1.9.1 - true - - - - com.fasterxml.jackson.core - jackson-core - 2.1.1 - - - - com.fasterxml.jackson.core - jackson-databind - 2.1.1 - - - - com.fasterxml.jackson.core - jackson-annotations - 2.1.1 - - - - io.netty - netty - 3.10.5.Final - true - - - - junit - junit - 4.6 - test - - - - org.mockito - mockito-all - 1.8.4 - test - - - - joda-time - joda-time - 2.2 - test - - - - org.joda - joda-convert - 1.3.1 - test - - - - - diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java deleted file mode 100644 index e48c836..0000000 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java +++ /dev/null @@ -1,164 +0,0 @@ -/** - * - */ -package com.xplenty.api; - -import com.xplenty.api.Xplenty.ClusterType; -import com.xplenty.api.exceptions.XplentyAPIException; -import com.xplenty.api.http.Http; -import com.xplenty.api.http.HttpClient; -import com.xplenty.api.http.ClientBuilder; -import com.xplenty.api.http.Response; -import com.xplenty.api.model.Cluster; -import com.xplenty.api.model.Job; -import com.xplenty.api.request.Request; -import junit.framework.Assert; -import junit.framework.TestCase; -import org.joda.time.DateTime; - -import java.util.HashMap; - -/** - * @author Yuriy Kovalek - * - */ -public class XplentyAPITest extends TestCase { - - public void testConstructor() { - XplentyAPI api = new XplentyAPI("testAcc", "testKey"); - - assertNotNull(api); - assertEquals("testAcc", api.getAccountName()); - assertEquals("testKey", api.getApiKey()); - } - - public void testBuilder() { - ClientBuilder builder = new ClientBuilder().withAccount("testAcc").withApiKey("testKey"). - withHost("www.example.com").withProtocol(Http.Protocol.Http).withVersion(Xplenty.Version.V1). - withClientImpl(Http.HttpClientImpl.SyncNetty).withLogHttpCommunication(true).withTimeout(10); - XplentyAPI api = new XplentyAPI(builder); - - assertNotNull(api); - assertEquals("www.example.com", api.getHost()); - assertEquals(Http.Protocol.Http, api.getProtocol()); - assertEquals(Xplenty.Version.V1, api.getVersion()); - assertEquals("testAcc", api.getAccountName()); - assertEquals(10, api.getTimeout()); - - - } - - public void testListClusters() { - HttpClient client = new HttpClient() { - @Override - public T execute(Request xplentyRequest) throws XplentyAPIException { - Response mockedResponse = Response.forContentType(Http.MediaType.JSON, - "[{\"nodes\":\"1\", " + - "\"type\": \"sandbox\", " + - "\"available_since\":\"2000-01-01T00:00:00Z\"," + - "\"terminated_at\":\"2000-01-01T00:00:00Z\", " + - "\"nodes\":\"1\","+ - "\"type\":\"sandbox\"," + - "\"available_since\":\"2000-01-01T02:00:00Z\"," + - "\"terminated_at\":\"2000-01-01T02:00:00Z\"}]", - 200, new HashMap()); - - return xplentyRequest.getResponse(mockedResponse); - } - - @Override - public void shutdown() { - - } - - @Override - public String getAccountName() { - return null; - } - - @Override - public String getApiKey() { - return null; - } - - @Override - public String getHost() { - return null; - } - - @Override - public Http.Protocol getProtocol() { - return null; - } - - @Override - public Xplenty.Version getVersion() { - return null; - } - - @Override - public int getTimeout() { - return 0; - } - - }; - XplentyAPI api = new XplentyAPI(client); - Cluster res = api.listClusters().get(0); - Assert.assertEquals(res.getNodes(), new Integer(1)); - Assert.assertEquals(res.getType(), ClusterType.sandbox); - Assert.assertEquals(res.getAvailableSince(), new DateTime(2000, 1, 1, 2, 0, 0).toDate()); - Assert.assertEquals(res.getTerminatedAt(), new DateTime(2000, 1, 1, 2, 0, 0).toDate()); - } - - public void testListJobs() { - HttpClient client = new HttpClient() { - @Override - public T execute(Request xplentyRequest) throws XplentyAPIException { - Response mockedResponse = Response.forContentType(Http.MediaType.JSON, - "[{\"failed_at\":\"2000-01-01T02:00:00Z\"," + - "\"completed_at\":\"2000-01-01T02:00:00Z\"}]", - 200, new HashMap()); - return xplentyRequest.getResponse(mockedResponse); - } - - @Override - public void shutdown() { - - } - - @Override - public String getAccountName() { - return null; - } - - @Override - public String getApiKey() { - return null; - } - - @Override - public String getHost() { - return null; - } - - @Override - public Http.Protocol getProtocol() { - return null; - } - - @Override - public Xplenty.Version getVersion() { - return null; - } - - @Override - public int getTimeout() { - return 0; - } - }; - XplentyAPI api = new XplentyAPI(client); - Job res = api.listJobs().get(0); - Assert.assertEquals(res.getFailedAt(), new DateTime(2000, 1, 1, 2, 0, 0).toDate()); - Assert.assertEquals(res.getCompletedAt(), new DateTime(2000, 1, 1, 2, 0, 0).toDate()); - } -} \ No newline at end of file diff --git a/xplenty.jar-integ-test/pom.xml b/xplenty.jar-integ-test/pom.xml deleted file mode 100644 index d8f5ca9..0000000 --- a/xplenty.jar-integ-test/pom.xml +++ /dev/null @@ -1,161 +0,0 @@ - - - - root - com.xplenty - 1.0.0 - - 4.0.0 - - xplenty.jar-integ-test - - - 6.1.16 - 2.9.2 - 2.0.4 - _2.9.1 - - - - - - com.xplenty - Xplenty.jar - 0.1.3-SNAPSHOT - - - org.scala-lang - scala-library - ${scala.version} - - - org.scalatest - scalatest_${scala.version} - 1.8 - test - - - org.scalatra - scalatra${scalatra.suffix} - ${scalatra.version} - - - org.scala-lang - scala-library - - - - - org.mortbay.jetty - servlet-api-2.5 - 6.1.14 - - - junit - junit - 4.6 - test - - - org.mockito - mockito-all - 1.8.4 - test - - - - joda-time - joda-time - 2.2 - test - - - - org.joda - joda-convert - 1.3.1 - test - - - - - - src/main - src/test - - - org.mortbay.jetty - maven-jetty-plugin - ${jettyVersion} - - 10 - 8005 - STOP - / - - - - start-jetty - pre-integration-test - - run - - - 0 - true - - - - stop-jetty - post-integration-test - - stop - - - - - - net.alchim31.maven - scala-maven-plugin - 3.1.0 - - - scala-compile-first - process-resources - - add-source - compile - - - - scala-test-compile - process-test-resources - - testCompile - - - - - - org.apache.maven.plugins - maven-failsafe-plugin - 2.14.1 - - false - - - - verify - - integration-test - verify - - - - - - - - \ No newline at end of file From bde94623da744169d723a37c13bbc14c1d9cf5f9 Mon Sep 17 00:00:00 2001 From: xardas Date: Wed, 13 Jan 2016 20:36:39 +0300 Subject: [PATCH 33/35] add member integration tests --- .../ITMembersTestAgainstMockServer.java | 133 ++++++++++++++++++ .../ITWatchersTestAgainstMockServer.java | 2 +- .../com/xplenty/api/router/MemberRouter.scala | 39 +++++ .../xplenty/api/router/WatchersRouter.scala | 35 +++++ .../com/xplenty/api/webapp/WEB-INF/web.xml | 16 ++- 5 files changed, 220 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/xplenty/api/integration/ITMembersTestAgainstMockServer.java create mode 100644 src/test/scala/com/xplenty/api/router/MemberRouter.scala create mode 100644 src/test/scala/com/xplenty/api/router/WatchersRouter.scala diff --git a/src/test/java/com/xplenty/api/integration/ITMembersTestAgainstMockServer.java b/src/test/java/com/xplenty/api/integration/ITMembersTestAgainstMockServer.java new file mode 100644 index 0000000..815410c --- /dev/null +++ b/src/test/java/com/xplenty/api/integration/ITMembersTestAgainstMockServer.java @@ -0,0 +1,133 @@ +package com.xplenty.api.integration; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.XplentyAPI; +import com.xplenty.api.http.ClientBuilder; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.Member; +import junit.framework.TestCase; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.List; + +public class ITMembersTestAgainstMockServer extends TestCase { + private final DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + + private XplentyAPI api; + private String host = "localhost:8080/mock/members"; + private String apiKey = "dsfgsdfh"; + private String accountID = "testerAccount"; + + + @Override + public void setUp(){ + ClientBuilder builder = new ClientBuilder().withAccount(accountID).withApiKey(apiKey). + withHost(host).withProtocol(Http.Protocol.Http).withVersion(Xplenty.Version.V2). + withClientImpl(Http.HttpClientImpl.SyncNetty).withLogHttpCommunication(true).withTimeout(10); + api = new XplentyAPI(builder); + } + + public void testCreateMember() throws ParseException { + Member c = api.createMember("test@xplenty.com", Xplenty.AccountRole.admin, "member1"); + assertNotNull(c); + assertEquals(new Long(387), c.getId()); + assertEquals("member1", c.getName()); + assertEquals("test@xplenty.com", c.getEmail()); + assertEquals("test@xplenty.com", c.getGravatarEmail()); + assertEquals(String.format("https://localhost/%s/settings/members/387", accountID), c.getHtmlUrl()); + assertEquals("https://secure.gravatar.com/", c.getAvatarUrl()); + assertFalse(c.getConfirmed()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals(String.format("https://localhost/%s/api/members/387", accountID), c.getUrl()); + assertNull(c.getLocation()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getCreatedAt()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getUpdatedAt()); + assertNull(c.getConfirmedAt()); + assertFalse(c.getOwner()); + } + + public void testMemberInfo() throws ParseException { + final int memberId = 666; + Member c = api.getMemberInfo(memberId); + assertNotNull(c); + assertEquals(new Long(memberId), c.getId()); + assertEquals("member1", c.getName()); + assertEquals("test@xplenty.com", c.getEmail()); + assertEquals("test@xplenty.com", c.getGravatarEmail()); + assertEquals(String.format("https://localhost/%s/settings/members/%s", accountID, memberId), c.getHtmlUrl()); + assertEquals("https://secure.gravatar.com/", c.getAvatarUrl()); + assertTrue(c.getConfirmed()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals(String.format("https://localhost/%s/api/members/%s", accountID, memberId), c.getUrl()); + assertEquals("Moscow", c.getLocation()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getCreatedAt()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getUpdatedAt()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getConfirmedAt()); + assertFalse(c.getOwner()); + } + + public void testDeleteMember() throws ParseException { + final int memberId = 666; + Member c = api.deleteMember(memberId); + assertNotNull(c); + assertEquals(new Long(memberId), c.getId()); + assertEquals("member1", c.getName()); + assertEquals("test@xplenty.com", c.getEmail()); + assertEquals("test@xplenty.com", c.getGravatarEmail()); + assertEquals(String.format("https://localhost/%s/settings/members/%s", accountID, memberId), c.getHtmlUrl()); + assertEquals("https://secure.gravatar.com/", c.getAvatarUrl()); + assertTrue(c.getConfirmed()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals(String.format("https://localhost/%s/api/members/%s", accountID, memberId), c.getUrl()); + assertEquals("Moscow", c.getLocation()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getCreatedAt()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getUpdatedAt()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getConfirmedAt()); + assertFalse(c.getOwner()); + } + + public void testSetMemberRole() throws ParseException { + final int memberId = 666; + Member c = api.setMemberRole(memberId, Xplenty.AccountRole.member); + assertNotNull(c); + assertEquals(new Long(memberId), c.getId()); + assertEquals("member1", c.getName()); + assertEquals("test@xplenty.com", c.getEmail()); + assertEquals("test@xplenty.com", c.getGravatarEmail()); + assertEquals(String.format("https://localhost/%s/settings/members/%s", accountID, memberId), c.getHtmlUrl()); + assertEquals("https://secure.gravatar.com/", c.getAvatarUrl()); + assertTrue(c.getConfirmed()); + assertEquals(Xplenty.AccountRole.member, c.getRole()); + assertEquals(String.format("https://localhost/%s/api/members/%s", accountID, memberId), c.getUrl()); + assertEquals("Moscow", c.getLocation()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getCreatedAt()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getUpdatedAt()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getConfirmedAt()); + assertFalse(c.getOwner()); + } + + public void testListMembers() throws ParseException { + final int memberId = 666; + List list = api.listMembers(); + assertNotNull(list); + assertTrue(list.size() > 0); + Member c = list.get(0); + assertNotNull(c); + assertEquals(new Long(memberId), c.getId()); + assertEquals("member1", c.getName()); + assertEquals("test@xplenty.com", c.getEmail()); + assertEquals("test@xplenty.com", c.getGravatarEmail()); + assertEquals(String.format("https://localhost/%s/settings/members/%s", accountID, memberId), c.getHtmlUrl()); + assertEquals("https://secure.gravatar.com/", c.getAvatarUrl()); + assertTrue(c.getConfirmed()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals(String.format("https://localhost/%s/api/members/%s", accountID, memberId), c.getUrl()); + assertEquals("Moscow", c.getLocation()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getCreatedAt()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getUpdatedAt()); + assertEquals(dFormat.parse("2016-01-13T16:44:20Z"), c.getConfirmedAt()); + assertFalse(c.getOwner()); + } +} diff --git a/src/test/java/com/xplenty/api/integration/ITWatchersTestAgainstMockServer.java b/src/test/java/com/xplenty/api/integration/ITWatchersTestAgainstMockServer.java index d8734d3..bcd975f 100644 --- a/src/test/java/com/xplenty/api/integration/ITWatchersTestAgainstMockServer.java +++ b/src/test/java/com/xplenty/api/integration/ITWatchersTestAgainstMockServer.java @@ -18,7 +18,7 @@ public class ITWatchersTestAgainstMockServer extends TestCase { private final DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); private XplentyAPI api; - private String host = "localhost:8080/mock"; + private String host = "localhost:8080/mock/watchers"; private String apiKey = "dsfgsdfh"; private String accountID = "testerAccount"; diff --git a/src/test/scala/com/xplenty/api/router/MemberRouter.scala b/src/test/scala/com/xplenty/api/router/MemberRouter.scala new file mode 100644 index 0000000..b407bf9 --- /dev/null +++ b/src/test/scala/com/xplenty/api/router/MemberRouter.scala @@ -0,0 +1,39 @@ +package com.xplenty.api.router + +import org.scalatra.ScalatraServlet + +/** + * Mock server returning fixed values + * @author xardazz + */ +class MemberRouter extends ScalatraServlet { + + post("/:accountId/api/members") { + val accId = params("accountId") + s"""{"id":387,"name":"member1","email":"test@xplenty.com","gravatar_email":"test@xplenty.com","created_at":"2016-01-13T16:44:20Z","updated_at":"2016-01-13T16:44:20Z","confirmed_at":null,"location":null,"avatar_url":"https://secure.gravatar.com/","role":"admin","owner":false,"url":"https://localhost/$accId/api/members/387","html_url":"https://localhost/$accId/settings/members/387","confirmed":false}""" + } + + put("/:accountId/api/members/:memberId") { + val accId = params("accountId") + val memberId = params("memberId") + s"""{"id":$memberId,"name":"member1","email":"test@xplenty.com","gravatar_email":"test@xplenty.com","created_at":"2016-01-13T16:44:20Z","updated_at":"2016-01-13T16:44:20Z","confirmed_at":"2016-01-13T16:44:20Z","location":"Moscow","avatar_url":"https://secure.gravatar.com/","role":"member","owner":false,"url":"https://localhost/$accId/api/members/$memberId","html_url":"https://localhost/$accId/settings/members/$memberId","confirmed":true}""" + } + + get("/:accountId/api/members/:memberId") { + val accId = params("accountId") + val memberId = params("memberId") + s"""{"id":$memberId,"name":"member1","email":"test@xplenty.com","gravatar_email":"test@xplenty.com","created_at":"2016-01-13T16:44:20Z","updated_at":"2016-01-13T16:44:20Z","confirmed_at":"2016-01-13T16:44:20Z","location":"Moscow","avatar_url":"https://secure.gravatar.com/","role":"admin","owner":false,"url":"https://localhost/$accId/api/members/$memberId","html_url":"https://localhost/$accId/settings/members/$memberId","confirmed":true}""" + } + + get("/:accountId/api/members") { + val accId = params("accountId") + val memberId = 666 + s"""[{"id":$memberId,"name":"member1","email":"test@xplenty.com","gravatar_email":"test@xplenty.com","created_at":"2016-01-13T16:44:20Z","updated_at":"2016-01-13T16:44:20Z","confirmed_at":"2016-01-13T16:44:20Z","location":"Moscow","avatar_url":"https://secure.gravatar.com/","role":"admin","owner":false,"url":"https://localhost/$accId/api/members/$memberId","html_url":"https://localhost/$accId/settings/members/$memberId","confirmed":true}]""" + } + + delete("/:accountId/api/members/:memberId") { + val accId = params("accountId") + val memberId = params("memberId") + s"""{"id":$memberId,"name":"member1","email":"test@xplenty.com","gravatar_email":"test@xplenty.com","created_at":"2016-01-13T16:44:20Z","updated_at":"2016-01-13T16:44:20Z","confirmed_at":"2016-01-13T16:44:20Z","location":"Moscow","avatar_url":"https://secure.gravatar.com/","role":"admin","owner":false,"url":"https://localhost/$accId/api/members/$memberId","html_url":"https://localhost/$accId/settings/members/$memberId","confirmed":true}""" + } +} diff --git a/src/test/scala/com/xplenty/api/router/WatchersRouter.scala b/src/test/scala/com/xplenty/api/router/WatchersRouter.scala new file mode 100644 index 0000000..73525fe --- /dev/null +++ b/src/test/scala/com/xplenty/api/router/WatchersRouter.scala @@ -0,0 +1,35 @@ +package com.xplenty.api.router + +import org.scalatra.ScalatraServlet + +/** + * Mock server returning fixed values + * @author alexanderdomeshek + */ +class WatchersRouter extends ScalatraServlet{ + + get("/:accountID/api/clusters/:cluster_id/watchers"){ + """[{"id":1,"display_name":"Xplenty"}]""" + } + + post("/:accountID/api/clusters/:cluster_id/watchers"){ + """{"created_at":"2013-04-09T11:19:20Z","cluster_url":"https://api.xplenty.com/xplenation/api/clusters/370"}""" + } + + delete("/:accountID/api/clusters/:cluster_id/watchers"){ + response.setStatus(204) + } + + get("/:accountID/api/jobs/:cluster_id/watchers"){ + """[{"id":1,"display_name":"Xplenty - Job watcher"}]""" + } + + post("/:accountID/api/jobs/:cluster_id/watchers"){ + """{"created_at":"2013-04-09T11:19:20Z","job_url":"https://api.xplenty.com/xplenation/api/jobs/370"}""" + } + + delete("/:accountID/api/jobs/:cluster_id/watchers"){ + response.setStatus(204) + } + +} diff --git a/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml b/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml index 2bde565..2716c66 100644 --- a/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml +++ b/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml @@ -5,11 +5,19 @@ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> - Hello - com.xplenty.api.router.Router + Watchers + com.xplenty.api.router.WatchersRouter + + Members + com.xplenty.api.router.MemberRouter + + + Watchers + /mock/watchers/* + - Hello - /mock/* + Members + /mock/members/* \ No newline at end of file From 10a05d6d3266fe8e1cd18245106e2e2f901fb258 Mon Sep 17 00:00:00 2001 From: xardas Date: Wed, 13 Jan 2016 21:01:39 +0300 Subject: [PATCH 34/35] add public key integration tests --- .../ITPublicKeyTestAgainstMockServer.java | 66 +++++++++++++++++++ .../xplenty/api/router/PublicKeyRouter.scala | 28 ++++++++ .../scala/com/xplenty/api/router/Router.scala | 37 ----------- .../com/xplenty/api/webapp/WEB-INF/web.xml | 9 +++ 4 files changed, 103 insertions(+), 37 deletions(-) create mode 100644 src/test/java/com/xplenty/api/integration/ITPublicKeyTestAgainstMockServer.java create mode 100644 src/test/scala/com/xplenty/api/router/PublicKeyRouter.scala delete mode 100644 src/test/scala/com/xplenty/api/router/Router.scala diff --git a/src/test/java/com/xplenty/api/integration/ITPublicKeyTestAgainstMockServer.java b/src/test/java/com/xplenty/api/integration/ITPublicKeyTestAgainstMockServer.java new file mode 100644 index 0000000..cbcc092 --- /dev/null +++ b/src/test/java/com/xplenty/api/integration/ITPublicKeyTestAgainstMockServer.java @@ -0,0 +1,66 @@ +package com.xplenty.api.integration; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.XplentyAPI; +import com.xplenty.api.http.ClientBuilder; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.PublicKey; +import junit.framework.TestCase; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.List; + +public class ITPublicKeyTestAgainstMockServer extends TestCase { + private final DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + + private XplentyAPI api; + private String host = "localhost:8080/mock/pk"; + private String apiKey = "dsfgsdfh"; + private String accountID = "testerAccount"; + private Long entityId = 33L; + + + @Override + public void setUp(){ + ClientBuilder builder = new ClientBuilder().withAccount(accountID).withApiKey(apiKey). + withHost(host).withProtocol(Http.Protocol.Http).withVersion(Xplenty.Version.V2). + withClientImpl(Http.HttpClientImpl.SyncNetty).withLogHttpCommunication(true).withTimeout(10); + api = new XplentyAPI(builder); + } + + public void testDeletePublicKey() throws Exception { + PublicKey c = api.deletePublicKey(entityId); + checkEntity(c); + } + + public void testPublicKeyInfo() throws Exception { + PublicKey c = api.getPublicKeyInfo(entityId); + checkEntity(c); + } + + public void testListPublicKeys() throws Exception { + List list = api.listPublicKeys(); + assertNotNull(list); + assertTrue(list.size() > 0); + PublicKey c = list.get(0); + checkEntity(c); + } + + public void testCreatePublicKey() throws Exception { + PublicKey c = api.createPublicKey("Test", "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAA...AAA xardazz@github.com"); + checkEntity(c); + } + + private void checkEntity(PublicKey c) throws ParseException { + assertNotNull(c); + assertEquals(new Long(33), c.getId()); + assertEquals("xardazz@github.com", c.getComment()); + assertEquals("ff:ff:ff:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:ff:ff:ff", c.getFingerprint()); + assertEquals("Test", c.getName()); + assertEquals(String.format("https://localhost/user/keys/%s", entityId), c.getUrl()); + assertEquals(dFormat.parse("2016-01-06T20:05:21Z"), c.getCreatedAt()); + assertEquals(dFormat.parse("2016-01-06T20:05:21Z"), c.getUpdatedAt()); + } +} diff --git a/src/test/scala/com/xplenty/api/router/PublicKeyRouter.scala b/src/test/scala/com/xplenty/api/router/PublicKeyRouter.scala new file mode 100644 index 0000000..5283e2a --- /dev/null +++ b/src/test/scala/com/xplenty/api/router/PublicKeyRouter.scala @@ -0,0 +1,28 @@ +package com.xplenty.api.router + +import org.scalatra.ScalatraServlet + +/** + * Mock server returning fixed values + * @author xardazz + */ +class PublicKeyRouter extends ScalatraServlet { + + post("/user/keys") { + s"""{"id":33,"comment":"xardazz@github.com","name":"Test","fingerprint":"ff:ff:ff:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:ff:ff:ff","created_at":"2016-01-06T20:05:21Z","updated_at":"2016-01-06T20:05:21Z","url":"https://localhost/user/keys/33"}""" + } + + get("/user/keys/:keyId") { + val keyId = params("keyId") + s"""{"id":$keyId,"comment":"xardazz@github.com","name":"Test","fingerprint":"ff:ff:ff:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:ff:ff:ff","created_at":"2016-01-06T20:05:21Z","updated_at":"2016-01-06T20:05:21Z","url":"https://localhost/user/keys/$keyId"}""" + } + + get("/user/keys") { + s"""[{"id":33,"comment":"xardazz@github.com","name":"Test","fingerprint":"ff:ff:ff:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:ff:ff:ff","created_at":"2016-01-06T20:05:21Z","updated_at":"2016-01-06T20:05:21Z","url":"https://localhost/user/keys/33"}]""" + } + + delete("/user/keys/:keyId") { + val keyId = params("keyId") + s"""{"id":$keyId,"comment":"xardazz@github.com","name":"Test","fingerprint":"ff:ff:ff:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:ff:ff:ff","created_at":"2016-01-06T20:05:21Z","updated_at":"2016-01-06T20:05:21Z","url":"https://localhost/user/keys/$keyId"}""" + } +} diff --git a/src/test/scala/com/xplenty/api/router/Router.scala b/src/test/scala/com/xplenty/api/router/Router.scala deleted file mode 100644 index 893e71c..0000000 --- a/src/test/scala/com/xplenty/api/router/Router.scala +++ /dev/null @@ -1,37 +0,0 @@ -package com.xplenty.api.router - -import org.scalatra.ScalatraServlet - -/** - * Created with IntelliJ IDEA. - * User: alexanderdomeshek - * Date: 5/16/13 - * Time: 11:57 AM - * To change this template use File | Settings | File Templates. - */ -class Router extends ScalatraServlet{ - - get("/:accountID/api/clusters/:cluster_id/watchers"){ - """[{"id":1,"display_name":"Xplenty"}]""" - } - - post("/:accountID/api/clusters/:cluster_id/watchers"){ - """{"created_at":"2013-04-09T11:19:20Z","cluster_url":"https://api.xplenty.com/xplenation/api/clusters/370"}""" - } - - delete("/:accountID/api/clusters/:cluster_id/watchers"){ - response.setStatus(204) - } - - get("/:accountID/api/jobs/:cluster_id/watchers"){ - """[{"id":1,"display_name":"Xplenty - Job watcher"}]""" - } - - post("/:accountID/api/jobs/:cluster_id/watchers"){ - """{"created_at":"2013-04-09T11:19:20Z","job_url":"https://api.xplenty.com/xplenation/api/jobs/370"}""" - } - - delete("/:accountID/api/jobs/:cluster_id/watchers"){ - response.setStatus(204) - } -} diff --git a/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml b/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml index 2716c66..759262b 100644 --- a/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml +++ b/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml @@ -12,6 +12,11 @@ Members com.xplenty.api.router.MemberRouter + + PK + com.xplenty.api.router.PublicKeyRouter + + Watchers /mock/watchers/* @@ -20,4 +25,8 @@ Members /mock/members/* + + PK + /mock/pk/* + \ No newline at end of file From 51e30692456a05eec9ae01307670e42215ea31f4 Mon Sep 17 00:00:00 2001 From: xardas Date: Wed, 13 Jan 2016 21:41:40 +0300 Subject: [PATCH 35/35] Add account integration tests --- .../xplenty/api/http/JsonMapperFactory.java | 2 +- .../ITAccountTestAgainstMockServer.java | 106 ++++++++++++++++++ .../ITPublicKeyTestAgainstMockServer.java | 2 +- .../xplenty/api/router/AccountRouter.scala | 37 ++++++ .../com/xplenty/api/webapp/WEB-INF/web.xml | 8 ++ 5 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/xplenty/api/integration/ITAccountTestAgainstMockServer.java create mode 100644 src/test/scala/com/xplenty/api/router/AccountRouter.scala diff --git a/src/main/java/com/xplenty/api/http/JsonMapperFactory.java b/src/main/java/com/xplenty/api/http/JsonMapperFactory.java index ff59eea..2bd1ed2 100644 --- a/src/main/java/com/xplenty/api/http/JsonMapperFactory.java +++ b/src/main/java/com/xplenty/api/http/JsonMapperFactory.java @@ -16,7 +16,7 @@ public class JsonMapperFactory { mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); //mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC); - mapper.enable(SerializationFeature.INDENT_OUTPUT); // for pretty print + mapper.disable(SerializationFeature.INDENT_OUTPUT); // for pretty print mapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")); diff --git a/src/test/java/com/xplenty/api/integration/ITAccountTestAgainstMockServer.java b/src/test/java/com/xplenty/api/integration/ITAccountTestAgainstMockServer.java new file mode 100644 index 0000000..2dc2148 --- /dev/null +++ b/src/test/java/com/xplenty/api/integration/ITAccountTestAgainstMockServer.java @@ -0,0 +1,106 @@ +package com.xplenty.api.integration; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.XplentyAPI; +import com.xplenty.api.http.ClientBuilder; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.Account; +import com.xplenty.api.model.Region; +import junit.framework.TestCase; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.List; + +public class ITAccountTestAgainstMockServer extends TestCase { + private final DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + + private XplentyAPI api; + private String host = "localhost:8080/mock/account"; + private String apiKey = "dsfgsdfh"; + private String accountID = "testerAccount"; + private Long entityId = 666L; + private String uniqueId = "superunique"; + + + @Override + public void setUp(){ + ClientBuilder builder = new ClientBuilder().withAccount(accountID).withApiKey(apiKey). + withHost(host).withProtocol(Http.Protocol.Http).withVersion(Xplenty.Version.V2). + withClientImpl(Http.HttpClientImpl.SyncNetty).withLogHttpCommunication(true).withTimeout(10); + api = new XplentyAPI(builder); + } + + public void testListaccountRegions() { + List list = api.listAvailableRegions(); + assertNotNull(list); + assertTrue(list.size() > 0); + + Region c = list.get(0); + assertEquals("AWS - US East (N. Virginia)", c.getName()); + assertEquals("Amazon Web Services", c.getGroupName()); + assertEquals("amazon-web-services::us-east-1", c.getId()); + } + + public void testDeleteAccount() throws Exception { + Account c = api.deleteAccount(uniqueId); + checkEntity(c); + } + + public void testAccountInfo() throws Exception { + Account c = api.getAccountInfo(uniqueId); + checkEntity(c); + } + + public void testListAccounts() throws Exception { + List list = api.listAccounts(); + assertNotNull(list); + assertTrue(list.size() > 0); + Account c = list.get(0); + checkEntity(c); + } + + public void testCreateAccount() throws Exception { + Account c = api.createAccount("test", "gcloud::europe-west", uniqueId); + checkEntity(c); + } + + public void testUpdateAccount() throws Exception { + Account c = new Account(uniqueId); + c.setAccountId(uniqueId); + c.setName("test"); + c.setBillingEmail("xardazz@github.com"); + c.setGravatarEmail("xardazz@github.com"); + c.setLocation("Private Drive"); + c.setRegion("gcloud::europe-west"); + c = api.updateAccount(c); + checkEntity(c); + } + + private void checkEntity(Account c) throws ParseException { + assertNotNull(c); + + assertEquals(new Long(entityId), c.getId()); + assertEquals("test", c.getName()); + assertEquals(uniqueId, c.getAccountId()); + assertEquals("gcloud::europe-west", c.getRegion()); + assertEquals("https://secure.gravatar.com", c.getAvatarUrl()); + assertEquals("xardazz@github.com", c.getBillingEmail()); + assertEquals("gravatar@gravatar.com", c.getGravatarEmail()); + assertEquals(Xplenty.AccountRole.admin, c.getRole()); + assertEquals(123, c.getConnectionsCount().intValue()); + assertEquals(1234, c.getJobsCount().intValue()); + assertEquals(12345, c.getMembersCount().intValue()); + assertEquals(123456, c.getPackagesCount().intValue()); + assertEquals(1234567, c.getRunningJobsCount().intValue()); + assertEquals(12345678, c.getSchedulesCount().intValue()); + assertEquals(String.format("ssh-rsa AAAAAAA....AAAAAA Xplenty/%s", uniqueId), c.getPublicKey()); + assertEquals(111, c.getOwnerId().longValue()); + assertEquals("Private Drive", c.getLocation()); + assertEquals(String.format("u_%s", entityId), c.getUname()); + assertEquals(String.format("https://localhost/accounts/%s", uniqueId), c.getUrl()); + assertEquals(dFormat.parse("2016-01-13T20:07:21Z"), c.getCreatedAt()); + assertEquals(dFormat.parse("2016-01-13T20:07:21Z"), c.getUpdatedAt()); + } +} diff --git a/src/test/java/com/xplenty/api/integration/ITPublicKeyTestAgainstMockServer.java b/src/test/java/com/xplenty/api/integration/ITPublicKeyTestAgainstMockServer.java index cbcc092..63ce8da 100644 --- a/src/test/java/com/xplenty/api/integration/ITPublicKeyTestAgainstMockServer.java +++ b/src/test/java/com/xplenty/api/integration/ITPublicKeyTestAgainstMockServer.java @@ -55,7 +55,7 @@ public void testCreatePublicKey() throws Exception { private void checkEntity(PublicKey c) throws ParseException { assertNotNull(c); - assertEquals(new Long(33), c.getId()); + assertEquals(new Long(entityId), c.getId()); assertEquals("xardazz@github.com", c.getComment()); assertEquals("ff:ff:ff:aa:aa:aa:aa:aa:aa:aa:aa:aa:aa:ff:ff:ff", c.getFingerprint()); assertEquals("Test", c.getName()); diff --git a/src/test/scala/com/xplenty/api/router/AccountRouter.scala b/src/test/scala/com/xplenty/api/router/AccountRouter.scala new file mode 100644 index 0000000..8a56ced --- /dev/null +++ b/src/test/scala/com/xplenty/api/router/AccountRouter.scala @@ -0,0 +1,37 @@ +package com.xplenty.api.router + +import org.scalatra.ScalatraServlet + +/** + * Mock server returning fixed values + * @author xardazz + */ +class AccountRouter extends ScalatraServlet { + + post("/accounts") { + s"""{"id":666,"name":"test","uname":"u_666","region":"gcloud::europe-west","location":"Private Drive","role":"admin","url":"https://localhost/accounts/superunique","account_id":"superunique","billing_email":"xardazz@github.com","gravatar_email":"gravatar@gravatar.com","avatar_url":"https://secure.gravatar.com","created_at":"2016-01-13T20:07:21Z","updated_at":"2016-01-13T20:07:21Z","schedules_count":12345678,"connections_count":123,"owner_id":111,"members_count":12345,"packages_count":123456,"jobs_count":1234,"running_jobs_count":1234567,"public_key":"ssh-rsa AAAAAAA....AAAAAA Xplenty/superunique"}""" + } + + put("/accounts/:entityId") { + val entityId = params("entityId") + s"""{"id":666,"name":"test","uname":"u_666","region":"gcloud::europe-west","location":"Private Drive","role":"admin","url":"https://localhost/accounts/$entityId","account_id":"$entityId","billing_email":"xardazz@github.com","gravatar_email":"gravatar@gravatar.com","avatar_url":"https://secure.gravatar.com","created_at":"2016-01-13T20:07:21Z","updated_at":"2016-01-13T20:07:21Z","schedules_count":12345678,"connections_count":123,"owner_id":111,"members_count":12345,"packages_count":123456,"jobs_count":1234,"running_jobs_count":1234567,"public_key":"ssh-rsa AAAAAAA....AAAAAA Xplenty/$entityId"}""" + } + + get("/accounts/:entityId") { + val entityId = params("entityId") + s"""{"id":666,"name":"test","uname":"u_666","region":"gcloud::europe-west","location":"Private Drive","role":"admin","url":"https://localhost/accounts/$entityId","account_id":"$entityId","billing_email":"xardazz@github.com","gravatar_email":"gravatar@gravatar.com","avatar_url":"https://secure.gravatar.com","created_at":"2016-01-13T20:07:21Z","updated_at":"2016-01-13T20:07:21Z","schedules_count":12345678,"connections_count":123,"owner_id":111,"members_count":12345,"packages_count":123456,"jobs_count":1234,"running_jobs_count":1234567,"public_key":"ssh-rsa AAAAAAA....AAAAAA Xplenty/$entityId"}""" + } + + get("/accounts") { + s"""[{"id":666,"name":"test","uname":"u_666","region":"gcloud::europe-west","location":"Private Drive","role":"admin","url":"https://localhost/accounts/superunique","account_id":"superunique","billing_email":"xardazz@github.com","gravatar_email":"gravatar@gravatar.com","avatar_url":"https://secure.gravatar.com","created_at":"2016-01-13T20:07:21Z","updated_at":"2016-01-13T20:07:21Z","schedules_count":12345678,"connections_count":123,"owner_id":111,"members_count":12345,"packages_count":123456,"jobs_count":1234,"running_jobs_count":1234567,"public_key":"ssh-rsa AAAAAAA....AAAAAA Xplenty/superunique"}]""" + } + + delete("/accounts/:entityId") { + val entityId = params("entityId") + s"""{"id":666,"name":"test","uname":"u_666","region":"gcloud::europe-west","location":"Private Drive","role":"admin","url":"https://localhost/accounts/$entityId","account_id":"$entityId","billing_email":"xardazz@github.com","gravatar_email":"gravatar@gravatar.com","avatar_url":"https://secure.gravatar.com","created_at":"2016-01-13T20:07:21Z","updated_at":"2016-01-13T20:07:21Z","schedules_count":12345678,"connections_count":123,"owner_id":111,"members_count":12345,"packages_count":123456,"jobs_count":1234,"running_jobs_count":1234567,"public_key":"ssh-rsa AAAAAAA....AAAAAA Xplenty/$entityId"}""" + } + + get("/:accountId/api/regions") { + s"""[{"name":"AWS - US East (N. Virginia)","group_name":"Amazon Web Services","id":"amazon-web-services::us-east-1"},{"name":"AWS - US West (N. California)","group_name":"Amazon Web Services","id":"amazon-web-services::us-west-1"},{"name":"AWS - US West (Oregon)","group_name":"Amazon Web Services","id":"amazon-web-services::us-west-2"},{"name":"AWS - EU (Ireland)","group_name":"Amazon Web Services","id":"amazon-web-services::eu-west-1"},{"name":"SoftLayer - Dallas 5 (DAL05)","group_name":"SoftLayer CloudLayer","id":"soft-layer::dal05"},{"name":"SoftLayer - Amsterdam 1 (AMS01)","group_name":"SoftLayer CloudLayer","id":"soft-layer::ams01"},{"name":"SoftLayer - Singapore 1 (SNG01)","group_name":"SoftLayer CloudLayer","id":"soft-layer::sng01"},{"name":"AWS - Asia Pacific (Singapore)","group_name":"Amazon Web Services","id":"amazon-web-services::ap-southeast-1"},{"name":"AWS - Asia Pacific (Sydney)","group_name":"Amazon Web Services","id":"amazon-web-services::ap-southeast-2"},{"name":"AWS - Asia Pacific (Tokyo)","group_name":"Amazon Web Services","id":"amazon-web-services::ap-northeast-1"},{"name":"AWS - South America (S\u00e3o Paulo)","group_name":"Amazon Web Services","id":"amazon-web-services::sa-east-1"},{"name":"Google Cloud - East Asia","group_name":"Google Cloud","id":"gcloud::asia-east1"},{"name":"Google Cloud - Western Europe","group_name":"Google Cloud","id":"gcloud::europe-west1"},{"name":"Google Cloud - Central US","group_name":"Google Cloud","id":"gcloud::us-central1"}]""" + } +} diff --git a/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml b/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml index 759262b..0cbdffd 100644 --- a/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml +++ b/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml @@ -16,6 +16,10 @@ PK com.xplenty.api.router.PublicKeyRouter + + Account + com.xplenty.api.router.AccountRouter + Watchers @@ -29,4 +33,8 @@ PK /mock/pk/* + + Account + /mock/account/* + \ No newline at end of file