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/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/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/src/main/java/com/xplenty/api/Xplenty.java b/src/main/java/com/xplenty/api/Xplenty.java new file mode 100644 index 0000000..c370381 --- /dev/null +++ b/src/main/java/com/xplenty/api/Xplenty.java @@ -0,0 +1,493 @@ +/** + * + */ +package com.xplenty.api; + +import com.fasterxml.jackson.annotation.JsonFormat; + +/** + * A bunch of convenience structures + * @author Yuriy Kovalek + * + */ +@SuppressWarnings("unused") +public class Xplenty { + public static final int MAX_LIMIT = 100; + + public enum Sort { + updated("updated"), + created("created"), + /** + * 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.Package Package list} + */ + name("name"), + /** + * Note that email sort is available only for {@link com.xplenty.api.model.Member Member List} + */ + email("email"), + /** + * 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"), + /** + * Note that type sort is available only for {@link com.xplenty.api.model.Connection Connection List} + */ + type("type") + ; + + public final String value; + + Sort(String value) { + this.value = value; + } + + public String toString() { + return value; + } + } + + + public enum SortDirection { + ascending("asc"), + descending("desc"); + + public final String value; + + SortDirection(String value) { + this.value = value; + } + + public String toString() { + return value; + } + } + + public enum JobStatus { + /** + * the user sent a request to run the job + */ + idle("idle"), + /** + * 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"), + /** + * used only for filtering meaning all the statuses above + */ + all("all") + ; + + @SuppressWarnings("unused") + private final String status; + + JobStatus(String status) { + this.status = status; + } + + } + + public enum ScheduleStatus { + enabled("enabled"), + disabled("disabled"), + /** + * used only for filtering meaning all the statuses above + */ + all("all") + ; + @SuppressWarnings("unused") + private final String status; + + ScheduleStatus(String status) { + this.status = status; + } + } + + public enum ScheduleIntervalUnit { + + minutes("minutes"), + hours("hours"), + days("days"), + weeks("weeks"), + months("months"); + + @SuppressWarnings("unused") + private final String unit; + + ScheduleIntervalUnit(String unit) { + this.unit = unit; + } + } + + public enum ClusterType { + production("production"), + sandbox("sandbox"); + + @SuppressWarnings("unused") + private final String type; + + ClusterType(String type) { + this.type = type; + } + + } + + public enum AccountRole { + /** + * only current owner can transfer ownership to another member + */ + owner("owner"), + admin("admin"), + member("member"), + /** + * used only for filtering meaning all the roles above + */ + all("all"); + + @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"), + salesforce("salesforce"), + /** + * used only for filtering meaning all the types above + */ + all("all"); + + @SuppressWarnings("unused") + private final String type; + + ConnectionType(String type) { + this.type = type; + } + } + + public enum PackageValidationStatus { + running, + completed, + failed, + /** + * used only for filtering meaning all the statuses above + */ + all; + } + + public enum PriceUnit { + year, + month, + day, + hour; + } + + public enum HookEvent { + 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; + + HookEvent(String event) { + this.event = event; + } + + public static HookEvent fromString(String name) { + for (HookEvent event : HookEvent.values()) { + if (event.getEvent().equals(name)) { + return event; + } + } + throw new IllegalArgumentException(String.format("No enum value found for string %s", name)); + } + + public String getEvent() { + return event; + } + + @Override + public String toString() { + return event; + } + } + + public enum PackageStatus { + active, + archived; + } + + public enum PackageFlowType { + dataflow, + workflow, + /** + * used only for filtering meaning all the types above + */ + all; + } + + public enum ReuseClusterStrategy { + /** + * do not re-use. It means a new cluster will always be created + */ + none, + /** + * re-use cluster created by this schedule or create a new one if none was found + */ + self, + /** + * re-use any cluster with minimal size settings defined in task[nodes] attribute + */ + any; + } + + public enum HookType { + email, + hipchat, + pagerduty, + slack, + web; + } + + + @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") + private final String status; + + ClusterStatus(String status) { + this.status = status; + } + } + + 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 + */ + 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"), + Cluster("clusters/%s", "Get cluster information"), + CreateCluster("clusters", "Create cluster"), + UpdateCluster("clusters/%s", "Update cluster"), + TerminateCluster("clusters/%s", "Terminate cluster"), + Jobs("jobs", "List jobs"), + 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"), + 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"), + 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"), + ResetUserPassword("user_password", "Send user password reset instructions"), + UserNotifications("user/notifications", "List user notifications"), + MarkUserNotificationRead("user/notifications/mark", "Mark user notification as read"), + HookEvents("hook_events", "List supported Hook Events"), + CreateHook("hooks", "Create new hook"), + UpdateHook("hooks/%s", "Update existing hook"), + DeleteHook("hooks/%s", "Delete hook"), + PingHook("hooks/%s/ping", "Ping(fire test notification) for hook"), + HookResetSalt("hooks/%s/reset_salt", "Reset hook's salt"), + Hook("hooks/%s", "Get hook information"), + Hooks("hooks", "List 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"), + 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"), + 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"), + Connections("connections", "List Connections"), + ConnectionTypes("connections/types", "List Connection Types"), + DeleteConnection("connections/%s/%s", "Delete Connection"), + 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"), + 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"), + ProductUpdates("product_updates", "List latest product announcements"), + LikeProductUpdate("product_updates/%s/like", "List latest product announcements") + ; + + public final String value; + public final String name; + + Resource(String val, String name) { + this.value = val; + this.name = name; + } + + public String format(String... values) { + return String.format(value, (Object[]) values); + } + } + + public static enum Version { + V1(1), V2(2); + + private final int value; + + Version(int ver) { + this.value = ver; + } + + public String format() { + return "version=" + Integer.toString(value); + } + } + + public static enum SubjectType { + CLUSTER, JOB + } +} diff --git a/src/main/java/com/xplenty/api/XplentyAPI.java b/src/main/java/com/xplenty/api/XplentyAPI.java new file mode 100644 index 0000000..929ebc9 --- /dev/null +++ b/src/main/java/com/xplenty/api/XplentyAPI.java @@ -0,0 +1,1221 @@ +/** + * + */ +package com.xplenty.api; + +import com.xplenty.api.Xplenty.ClusterType; +import com.xplenty.api.Xplenty.Version; +import com.xplenty.api.exceptions.XplentyAPIException; +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.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.*; +import com.xplenty.api.request.member.*; +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; +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.*; +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.request.hook.*; +import com.xplenty.api.request.xpackage.*; + +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 com.xplenty.api.http.HttpClient} is created + * for each instance of XplentyAPI. + *

+ * Example usage: + *

{@code
+ *		XplentyAPI api = new XplentyAPI("accountName", "apiKey");
+ *List plans = api.listClusterPlans();
+ * }
+ * 
+ * + * {@link RuntimeException} will be thrown for any request failures. + * + * @author Yuriy Kovalek and Xardas + */ +public class XplentyAPI { + private final HttpClient client; + + /** + * 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) { + client = new ClientBuilder().withAccount(accountName).withApiKey(apiKey).build(); + } + + /** + * Constructs a XplentyAPI with a {@link XplentyWebConnectivity} 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 + * @param logHttpCommunication enables logging of requests and responses + */ + /** + * 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(ClientBuilder clientBuilder) { + client = clientBuilder.build(); + } + + /** + * Constructs a XplentyAPI with the {@link com.xplenty.api.http.HttpClient} given + * @param client Configured http client + */ + public XplentyAPI(HttpClient client){ + this.client = client; + } + + /** + * Get package information + * @param packageId of the package to get + * @return package object + */ + public Package getPackageInfo(long 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)); + } + + /** + * 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 + * @return list of packages + */ + public List listPackages(Properties props) { + return client.execute(new ListPackages(props)); + } + + /** + * List of packages associated with the account + * @param offset number of record to start results from + * @param limit number of results + * @return list of packages + */ + public List listPackages(int offset, int limit) { + final Properties props = new Properties(); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); + 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(packageId, props)); + } + + /** + * 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 + */ + public List listSchedules() { + return listSchedules(new Properties()); + } + + /** + * List of schedules associated with the account + * @param offset number of record to start results from + * @param limit number of results + * @return list of schedules + */ + public List listSchedules(int offset, int limit) { + final Properties props = new Properties(); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); + return listSchedules(props); + } + + /** + * 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.schedule.ListSchedules} + * @return list of schedules + */ + public List listSchedules(Properties props) { + return client.execute(new ListSchedules(props)); + } + + /** + * List of clusters associated with the account + * @return list of clusters + */ + public List listClusters() { + return listClusters(new Properties()); + } + + /** + * List of clusters associated with the account + * @param offset number of record to start results from + * @param limit number of results + * @return list of clusters + */ + public List listClusters(int offset, int limit) { + final Properties props = new Properties(); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); + return listClusters(props); + } + + + /** + * 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 com.xplenty.api.request.cluster.ListClusters} + * @return list of clusters + */ + 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 + * @return + */ + public Cluster clusterInformation(long clusterId) { + return client.execute(new ClusterInfo(clusterId)).withParentApiInstance(this); + } + + /** + * Create cluster with specified properties + * @param nodes number of nodes + * @param type of cluster - 'sandbox' if sandbox + * @param name cluster name + * @param description cluster description + * @param terminateOnIdle should the cluster terminate on idle status + * @param timeToIdle time in seconds before cluster is considered idle + * @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) + .ofType(type) + .named(name) + .withDescription(description) + .withTerminateOnIdle(terminateOnIdle) + .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 + * @param id id of cluster + * @param nodes number of nodes + * @param name cluster name + * @param description cluster description + * @param terminateOnIdle should the cluster terminate on idle status + * @param timeToIdle time in seconds before cluster is considered idle + * @return + */ + public Cluster updateCluster(long id, Integer nodes, String name, String description, Boolean terminateOnIdle, Long timeToIdle) { + return client.execute(new UpdateCluster( + new Cluster().withId(id).withNodes(nodes).named(name).withDescription(description) + .withTerminateOnIdle(terminateOnIdle).withTimeToIdle(timeToIdle) + )).withParentApiInstance(this); + } + + /** + * Terminate cluster with given id + * @param clusterId is clusterId + * @return + */ + public Cluster terminateCluster(long clusterId) { + return client.execute(new TerminateCluster(clusterId)).withParentApiInstance(this); + } + + /** + * List of jobs associated with the account + * @return list of jobs + */ + public List listJobs() { + return listJobs(new Properties()); + } + + /** + * List of jobs associated with the account + * @param offset number of record to start results from + * @param limit number of results + * @return list of jobs + */ + public List listJobs(int offset, int limit) { + final Properties props = new Properties(); + props.put(AbstractListRequest.PARAMETER_LIMIT, limit); + props.put(AbstractListRequest.PARAMETER_OFFSET, offset); + return listJobs(props); + } + + /** + * 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 com.xplenty.api.request.job.ListJobs} + * @return list of jobs + */ + 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)); + } + + /** + * 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 + * @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); + } + + /** + * Execute a job on a cluster + * @param clusterId cluster to execute on, see {@link #listClusters()} to get a list of available clusters + * @param packageId package id, obtained from account web page + * @param variables map of variables to be passed to the job + * @return + */ + public Job runJob(long clusterId, long packageId, Map variables) { + return runJob(clusterId, packageId, variables, false); + } + + /** + * Execute a job on a cluster + * @param clusterId cluster to execute on, see {@link #listClusters()} to get a list of available clusters + * @param packageId package id, obtained from account web page + * @param variables map of variables to be passed to the job + * @param dynamicVariables indication whether the variables should be treated as dynamic + * @return + */ + public Job runJob(long clusterId, long packageId, Map variables, boolean dynamicVariables) { + Job job = new Job().onCluster(clusterId).withPackage(packageId); + if (dynamicVariables) + job = job.withDynamicVariables(variables); + else + job = job.withVariables(variables); + return client.execute(new RunJob(job)).withParentApiInstance(this); + } + + /** + * Stop a particular job + * @param jobId id of job to stop, see {@link #listJobs()} to get a list of jobs + * @return + */ + public Job stopJob(long jobId) { + return client.execute(new StopJob(jobId)).withParentApiInstance(this); + } + + public List listClusterWatchers(long clusterId) { + return client.execute(new ListWatchers(Xplenty.SubjectType.CLUSTER, clusterId)); + } + + public List listJobWatchers(long clusterId) { + return client.execute(new ListWatchers(Xplenty.SubjectType.JOB, clusterId)); + } + + public ClusterWatchingLogEntry addClusterWatchers(long clusterId) { + return client.execute(new AddClusterWatcher(clusterId)).withParentApiInstance(this); + } + + public JobWatchingLogEntry addJobWatchers(long jobId) { + return client.execute(new AddJobWatcher(jobId)).withParentApiInstance(this); + } + + + public Boolean removeClusterWatchers(long clusterId) { + return client.execute(new WatchingStop(Xplenty.SubjectType.CLUSTER, clusterId)); + } + + public Boolean removeJobWatchers(long jobId) { + return client.execute(new WatchingStop(Xplenty.SubjectType.JOB, jobId)); + } + + /** + * Creates new schedule + * @param schedule Schedule to create. All fields that have public setters (except for id) should be set + * @return newly created schedule object + */ + public Schedule createSchedule(Schedule schedule) { + if (schedule.getId() != null) { + schedule.withId(null); + } + return client.execute(new CreateSchedule(schedule)); + } + + /** + * Update schedule + * @param schedule Schedule to update. Any fields that have public setters can be set. Id must be set! + * @return updated schedule object + */ + public Schedule updateSchedule(Schedule schedule) { + checkId(schedule.getId()); + return client.execute(new UpdateSchedule(schedule)); + } + + /** + * Delete schedule + * @param scheduleId Id of schedule to delete + * @return deleted schedule object + */ + public Schedule deleteSchedule(long scheduleId) { + checkId(scheduleId); + return client.execute(new DeleteSchedule(scheduleId)); + } + + /** + * Clone schedule + * @param scheduleId Id of schedule to clone + * @return cloned schedule object + */ + public Schedule cloneSchedule(long scheduleId) { + checkId(scheduleId); + return client.execute(new CloneSchedule(scheduleId)); + } + + /** + * Get schedule info + * @param scheduleId Id of schedule to get + * @return schedule object + */ + public Schedule getScheduleInfo(long scheduleId) { + 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)); + } + + /** + * Sends user password reset instructions + * @param email email of the user + */ + public void resetUserPassword(String email) { + checkStringId(email); + client.execute(new ResetUserPassword(email)); + } + + /** + * 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 hook to recieve notifications for events subscribed + * @param name name of the hook. Leave null for default + * @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 Hook createHook(String name, HookSettings settings, List events) { + return client.execute(new CreateHook(name, settings, events)); + } + + /** + * Create new hook to recieve notifications for events subscribed + * @param name name of the hook. Leave null for default + * @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 Hook createHook(String name, List events, HookSettings settings) { + return client.execute(new CreateHook(name, events, settings)); + } + + /** + * Update hook + * @param hookId id of the hook + * @param name name of the hook + * @param settings settings used to connect to your server, pass null if no change required + * @param events subscribe to these events. All existing events will be replaced. Leave null if no change required + * @return updated web hook object + */ + public Hook updateHook(long hookId, String name, HookSettings settings, List events) { + checkId(hookId); + return client.execute(new UpdateHook(hookId, name, settings, events)); + } + + /** + * Update hook + * @param hookId id of the hook + * @param name name of the hook + * @param events subscribe to these events. All existing events will be replaced. 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 Hook updateHook(long hookId, String name, List events, HookSettings settings) { + checkId(hookId); + return client.execute(new UpdateHook(hookId, name, events, 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 Hook toggleWebHook(long webHookId, boolean active) { + checkId(webHookId); + return client.execute(new ToggleHook(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 ListHooks(params)); + } + + /** + * Delete webhook with specified id + * @param webHookId id of the webhook to delete + * @return deleted web hook object + */ + public Hook deleteWebHook(long webHookId) { + checkId(webHookId); + return client.execute(new DeleteHook(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 HookResetSalt(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 Hook pingWebHook(long webHookId) { + checkId(webHookId); + return client.execute(new PingHook(webHookId)); + } + + /** + * Get web hook information + * @param webHookId id of the webhook to get info for + * @return web hook object + */ + public Hook getWebHookInfo(long webHookId) { + checkId(webHookId); + return client.execute(new HookInfo(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) { + checkId(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) { + 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 + * @param name name of the member + * @return newly created member object + */ + public Member createMember(String email, Xplenty.AccountRole role, String name) { + return client.execute(new CreateMember(email, role, name)); + } + + + /** + * 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}, {@link com.xplenty.api.request.member.ListMembers}. + * @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)); + } + + /** + * 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 regions, available for account + */ + 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)); + } + + /** + * 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)); + } + + /** + * 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() { + 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()); + } + + /** + * 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) { + throw new XplentyAPIException("No Id specified!"); + } + } + + 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 + * @return Account name + */ + public String getAccountName() { + return client.getAccountName(); + } + + /** + * API key used by this XplentyAPI instance + * @return API Key + */ + public String getApiKey() { + return client.getApiKey(); + } + + /** + * API host this instance uses + * @return API host + */ + public String getHost() { + return client.getHost(); + } + + /** + * Protocol this API instance uses + * @return protocol + */ + public Http.Protocol getProtocol() { + return client.getProtocol(); + } + + /** + * API version + * @return version + */ + 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/src/main/java/com/xplenty/api/exceptions/AuthFailedException.java similarity index 72% 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 index ff557d4..51ab188 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/AuthFailedException.java +++ b/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/src/main/java/com/xplenty/api/exceptions/RequestFailedException.java b/src/main/java/com/xplenty/api/exceptions/RequestFailedException.java new file mode 100644 index 0000000..cfbd334 --- /dev/null +++ b/src/main/java/com/xplenty/api/exceptions/RequestFailedException.java @@ -0,0 +1,37 @@ +/** + * + */ +package com.xplenty.api.exceptions; + +import com.xplenty.api.http.Http; + +/** + * @author Yuriy Kovalek + * + */ +public class RequestFailedException extends XplentyAPIException { + private static final long serialVersionUID = -456749863406425145L; + + private final int status; + private final String response; + private final String statusDescription; + + 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() { + return status; + } + + public String getResponse() { + return response; + } + + public String getStatusDescription() { + return statusDescription; + } +} 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/src/main/java/com/xplenty/api/http/ClientBuilder.java b/src/main/java/com/xplenty/api/http/ClientBuilder.java new file mode 100644 index 0000000..8c84e08 --- /dev/null +++ b/src/main/java/com/xplenty/api/http/ClientBuilder.java @@ -0,0 +1,123 @@ +package com.xplenty.api.http; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; + +/** + * Builder for Http client + * Author: Xardas + * Date: 02.01.16 + * Time: 15:32 + */ +public class ClientBuilder { + + 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; + + /** + * + * @param timeout timeout for connection/reading + * @return builder + */ + public ClientBuilder 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 ClientBuilder withApiKey(String apiKey) { + this.apiKey = apiKey; + return this; + } + + /** + * + * @param protocol protocol to use for connection + * @return builder + */ + public ClientBuilder withProtocol(Http.Protocol protocol) { + this.protocol = protocol; + return this; + } + + /** + * + * @param logHttpCommunication Log raw packets transfered? (before encryption / after decryption) + * @return builder + */ + public ClientBuilder withLogHttpCommunication(boolean logHttpCommunication) { + this.logHttpCommunication = logHttpCommunication; + return this; + } + + /** + * + * @param accountName account name used for Xplenty sign-up + * @return builder + */ + public ClientBuilder withAccount(String accountName) { + this.accountName = accountName; + return this; + } + + /** + * + * @param host API hostname + * @return builder + */ + public ClientBuilder withHost(String host) { + this.host = host; + return this; + } + + /** + * + * @param impl client implementation to use + * @return builder + */ + public ClientBuilder withClientImpl(Http.HttpClientImpl impl) { + this.clientImpl = impl; + return this; + } + + /** + * + * @param version API version to use + * @return builder + */ + public ClientBuilder 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!"); + } + if (accountName == null) { + throw new XplentyAPIException("Account name not set!"); + } + switch (clientImpl) { + case SyncNetty: + return new SyncNettyClient(accountName, apiKey, host, protocol, version, timeout, logHttpCommunication); + default: + return new JerseyClient(accountName, apiKey, host, protocol, version, timeout, logHttpCommunication); + } + } + +} diff --git a/src/main/java/com/xplenty/api/http/ConsoleNettyLogger.java b/src/main/java/com/xplenty/api/http/ConsoleNettyLogger.java new file mode 100644 index 0000000..faec9a4 --- /dev/null +++ b/src/main/java/com/xplenty/api/http/ConsoleNettyLogger.java @@ -0,0 +1,85 @@ +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); + if (ctx != null) { + ctx.sendDownstream(channelEvent); + } + } + + @Override + public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent channelEvent) throws Exception { + log(channelEvent, true); + if (ctx != null) { + ctx.sendUpstream(channelEvent); + } + } + + protected 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()); + } + } + + protected void logStateMessage(String str) { + System.err.println(String.format("%s %s", getDateFormat().format(System.currentTimeMillis()), str)); + } + + protected 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 ? "<<<" : ">>>"); + } + + protected 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]>>>"); + } + + protected 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/src/main/java/com/xplenty/api/http/Http.java b/src/main/java/com/xplenty/api/http/Http.java new file mode 100644 index 0000000..787bc7c --- /dev/null +++ b/src/main/java/com/xplenty/api/http/Http.java @@ -0,0 +1,101 @@ +/** + * + */ +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"), + PURE_JSON("application/json"); + + public final String value; + + MediaType(String type) { + value = type; + } + } + + /** + * HTTP methods supported by Xplenty API + */ + public enum Method { + GET, POST, PUT, DELETE + } + + /** + * Protocol used by Xplenty API Server + */ + public static enum Protocol { + Http("http"), + Https("https"); + + public final String value; + + Protocol(String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } + } + + /** + * Http Client Implementation used to connect + */ + public static enum HttpClientImpl { + SyncNetty, /* for future impl 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."), + HTTP_505(505, "Version not supported"); + + 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/src/main/java/com/xplenty/api/http/HttpClient.java b/src/main/java/com/xplenty/api/http/HttpClient.java new file mode 100644 index 0000000..ba66265 --- /dev/null +++ b/src/main/java/com/xplenty/api/http/HttpClient.java @@ -0,0 +1,68 @@ +package com.xplenty.api.http; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.request.Request; + +/** + * Http Client interface for Request processing + * 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; + + /** + * 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/src/main/java/com/xplenty/api/http/JerseyClient.java b/src/main/java/com/xplenty/api/http/JerseyClient.java new file mode 100644 index 0000000..9983ea8 --- /dev/null +++ b/src/main/java/com/xplenty/api/http/JerseyClient.java @@ -0,0 +1,165 @@ +/** + * + */ +package com.xplenty.api.http; + +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.WebResource; +import com.sun.jersey.api.client.config.ClientConfig; +import com.sun.jersey.api.client.config.DefaultClientConfig; +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.XplentyAPIException; +import com.xplenty.api.request.Request; + +import javax.ws.rs.core.MultivaluedMap; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; + +/** + * Proxy for connecting to the XplentyAPI over HTTP + * + * @author Yuriy Kovalek and Xardas + * + */ +public class JerseyClient implements HttpClient { + + private final String host; + 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; + + + /** + * 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 + */ + 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 * 1000); + config.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, timeout * 1000); + client = Client.create(config); + client.addFilter(new HTTPBasicAuthFilter(apiKey, "")); + if (logHttpCommunication) { + client.addFilter(new LoggingFilter()); + } + } + + /** + * Synchronously execute given request + * @param request request to execute + * @return respective response type + */ + public T execute(Request request) { + WebResource.Builder builder = getConfiguredResource(request); + ClientResponse response = null; + switch (request.getHttpMethod()) { + case GET: response = builder.get(ClientResponse.class); break; + case POST: response = builder.post(ClientResponse.class); break; + case PUT: response = builder.put(ClientResponse.class); break; + case DELETE: response = builder.delete(ClientResponse.class); break; + } + 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); + } + + protected 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 com.sun.jersey.api.client.WebResource.Builder} for given request + * @param request that would be submitted to the XPlenty Server + * @return builder + */ + protected WebResource.Builder getConfiguredResource(Request request) { + WebResource.Builder b = client.resource(getMethodURL(request.getEndpoint(host, accountName))) + .accept(request.getResponseType().value + + (version == null ? "" : "; " + version.format()) + ); + if (request.hasBody()) { + StringWriter sw = new StringWriter(); + try { + ObjectMapper objectMapper = JsonMapperFactory.getInstance(); + objectMapper.writeValue(sw, request.getBody()); + } catch (Exception e) { + throw new XplentyAPIException(e); + } + b.entity(sw.toString()).type(Http.MediaType.PURE_JSON.value); + } + + return b; + } + + /** + * Constructs the actual URL + * @param methodEndpoint - describes the action type + * @return filly qualified URL + */ + protected String getMethodURL(String methodEndpoint) { + if (methodEndpoint.startsWith("http")) { + return methodEndpoint; + } + return String.format("%s://%s", protocol, methodEndpoint); + } + + public String getAccountName() { + return accountName; + } + + public String getApiKey() { + return apiKey; + } + + public Http.Protocol getProtocol() { + return protocol; + } + + public String getHost() { + return host; + } + + public Version getVersion() { + return version; + } + + @Override + public int getTimeout() { + return timeout; + } + + @Override + public void shutdown() { + if (client != null) { + client.destroy(); + } + } +} diff --git a/src/main/java/com/xplenty/api/http/JsonMapperFactory.java b/src/main/java/com/xplenty/api/http/JsonMapperFactory.java new file mode 100644 index 0000000..2bd1ed2 --- /dev/null +++ b/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.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'")); + } + + public static ObjectMapper getInstance() { + + return mapper; + } +} diff --git a/src/main/java/com/xplenty/api/http/JsonResponse.java b/src/main/java/com/xplenty/api/http/JsonResponse.java new file mode 100644 index 0000000..1adeb5a --- /dev/null +++ b/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/src/main/java/com/xplenty/api/http/Response.java b/src/main/java/com/xplenty/api/http/Response.java new file mode 100644 index 0000000..70b3313 --- /dev/null +++ b/src/main/java/com/xplenty/api/http/Response.java @@ -0,0 +1,105 @@ +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; + +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; + } + + /** + * 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) { + case JSON: + case PURE_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) && (actualTypeArguments[0] instanceof Class)) { + + cls = (Class) actualTypeArguments[0]; + if (XplentyObject.class.isAssignableFrom(cls)) { + return; + } + } else if (Map.class.isAssignableFrom(rawType) && (actualTypeArguments[1] instanceof Class)) { + cls = (Class) actualTypeArguments[1]; + if (XplentyObject.class.isAssignableFrom(cls) || String.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/src/main/java/com/xplenty/api/http/SSLEngineDefaultImpl.java b/src/main/java/com/xplenty/api/http/SSLEngineDefaultImpl.java new file mode 100644 index 0000000..c193b20 --- /dev/null +++ b/src/main/java/com/xplenty/api/http/SSLEngineDefaultImpl.java @@ -0,0 +1,31 @@ +/* + * CopyRight Alexey Gromov 2012-... + * You can freely improve this code + */ + +package com.xplenty.api.http; + +import javax.net.ssl.SSLContext; +import java.security.SecureRandom; + + +/** + * + * @author xardas + */ +public class SSLEngineDefaultImpl { + private static SSLContext wc; + static { + try { + wc = SSLContext.getInstance("TLS"); + // use default implementations + wc.init(null, null, new SecureRandom()); + } catch (Exception ex) { } + + } + + public static SSLContext getSSLContext() { + return wc; + } + +} diff --git a/src/main/java/com/xplenty/api/http/SyncNettyClient.java b/src/main/java/com/xplenty/api/http/SyncNettyClient.java new file mode 100644 index 0000000..d5b934b --- /dev/null +++ b/src/main/java/com/xplenty/api/http/SyncNettyClient.java @@ -0,0 +1,334 @@ +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.ssl.SslHandler; + +import javax.net.ssl.SSLEngine; +import java.io.ByteArrayOutputStream; +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"; + 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; + private final String host; + 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, 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) { + throw new XplentyAPIException("Error encoding API key", e); + } + + chanfac = new NioClientSocketChannelFactory( + 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", 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 + 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); + } + + protected 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(host, accountName))); + 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.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 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); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectMapper objectMapper = JsonMapperFactory.getInstance(); + 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.PURE_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(); + if (httpBody == null) { + throw new XplentyAPIException("Request timed out!"); + } + 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); + } 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()); + } + + protected String getAcceptHeaderValue(String baseValue) { + if (this.version == null) { + return baseValue; + } + return String.format("%s; %s", baseValue, this.version.format()); + } + + protected Map convertNettyHeaders(HttpHeaders headers) { + final Map convertedHeaders = new HashMap<>(); + for (Map.Entry header : headers.entries()) { + convertedHeaders.put(header.getKey(), header.getValue()); + } + return convertedHeaders; + } + + /** + * Constructs the actual URL + * @param methodEndpoint - describes the action type + * @return filly qualified URL + */ + protected String getMethodURL(String methodEndpoint) { + if (methodEndpoint.startsWith("http")) { + return methodEndpoint; + } + return String.format("%s://%s", protocol, methodEndpoint); + } + + + protected 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.shutdown(); // does the same + client.releaseExternalResources(); + } + + + protected static class PipelineFactory implements ChannelPipelineFactory { + + private final boolean ssl; + private final boolean logCommunication; + private final ChannelUpstreamHandler handler; + + public PipelineFactory(ChannelUpstreamHandler handler, boolean ssl, boolean logCommunication) { + this.handler = handler; + this.ssl = ssl; + this.logCommunication = logCommunication; + } + + public ChannelPipeline getPipeline() throws Exception { + // Create a default pipeline implementation. + ChannelPipeline pipeline = pipeline(); + + // Enable HTTPS if necessary. + if (ssl) { + // 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)); + } + + if (logCommunication) { + 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)); + + if (logCommunication) { + pipeline.addLast("httpconsolelogger", new ConsoleNettyLogger(true)); + } + + pipeline.addLast("handler", handler); + return pipeline; + } + } + + + + protected 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; + } + } + + @Override + public int getTimeout() { + return timeout; + } + + @Override + public String getHost() { + return host; + } + + @Override + public Http.Protocol getProtocol() { + return protocol; + } + + @Override + public Xplenty.Version getVersion() { + return version; + } + + @Override + public String getAccountName() { + return accountName; + } + + @Override + public String getApiKey() { + return apiKey; + } +} diff --git a/src/main/java/com/xplenty/api/model/Account.java b/src/main/java/com/xplenty/api/model/Account.java new file mode 100644 index 0000000..674b58a --- /dev/null +++ b/src/main/java/com/xplenty/api/model/Account.java @@ -0,0 +1,302 @@ +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 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 + public Long id; + @JsonProperty("account_id") + public String accountId; + @JsonProperty + public String name; + @JsonProperty + public String uname; + @JsonProperty + public String region; + @JsonProperty + public String location; + @JsonProperty("billing_email") + public String billingEmail; + @JsonProperty("gravatar_email") + public String gravatarEmail; + @JsonProperty("avatar_url") + public String avatarUrl; + @JsonProperty("created_at") + public Date createdAt; + @JsonProperty("updated_at") + public Date updatedAt; + @JsonProperty("schedules_count") + public Integer schedulesCount; + @JsonProperty("connections_count") + public Integer connectionsCount; + @JsonProperty + public Xplenty.AccountRole role; + @JsonProperty("owner_id") + public Long ownerId; + @JsonProperty("members_count") + public Integer membersCount; + @JsonProperty("packages_count") + public Integer packagesCount; + @JsonProperty("jobs_count") + public Integer jobsCount; + @JsonProperty("running_jobs_count") + public Integer runningJobsCount; + @JsonProperty + public String url; + @JsonProperty("public_key") + public String publicKey; + @JsonIgnore + public String currentAccountId; + + + protected Account() { + super(Account.class); + } + + 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; + } + + /** + * + * @return the account's numeric identifier + */ + public Long getId() { + return id; + } + + /** + * + * @return the account's unique identifier + */ + 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 + */ + 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; + } + + /** + * + * @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/src/main/java/com/xplenty/api/model/AvailableHookEvent.java b/src/main/java/com/xplenty/api/model/AvailableHookEvent.java new file mode 100644 index 0000000..df5d6d9 --- /dev/null +++ b/src/main/java/com/xplenty/api/model/AvailableHookEvent.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 AvailableHookEvent extends XplentyObject { + @JsonProperty + private String id; + @JsonProperty("group_name") + private String groupName; + @JsonProperty("name") + private String description; + + protected AvailableHookEvent() { + super(AvailableHookEvent.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/src/main/java/com/xplenty/api/model/Cluster.java b/src/main/java/com/xplenty/api/model/Cluster.java new file mode 100644 index 0000000..3388f2e --- /dev/null +++ b/src/main/java/com/xplenty/api/model/Cluster.java @@ -0,0 +1,470 @@ +/** + * + */ +package com.xplenty.api.model; + +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 + * + */ +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; + @JsonProperty("updated_at") + protected Date updatedAt; + @JsonProperty("available_since") + protected Date availableSince; + @JsonProperty("terminated_at") + protected Date terminatedAt; + @JsonProperty("launched_at") + protected Date launchedAt; + @JsonProperty("running_jobs_count") + protected Long runningJobsCount; + @JsonProperty + protected String url; + @JsonProperty("terminate_on_idle") + protected Boolean terminateOnIdle; + @JsonProperty("time_to_idle") + 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); + } + + public Cluster withNodes(Integer nodes) { + this.nodes = nodes; + return this; + } + + public Cluster withId(Long id) { + this.id = id; + return this; + } + + public Cluster named(String name) { + this.name = name; + return this; + } + + public Cluster withDescription(String description) { + this.description = description; + return this; + } + + public Cluster ofType(ClusterType type) { + this.type = type; + return this; + } + + public Cluster withTerminateOnIdle(Boolean terminateOnIdle) { + this.terminateOnIdle = terminateOnIdle; + return this; + } + + 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...)} + */ + public void waitForStatus(ClusterStatus... statuses) { + waitForStatus(null, statuses); + } + + /** + * Blocks execution until required status is received from the Xplenty server, or until timeout occurs. + * @param timeout time in seconds before terminating the wait, {@code null} to wait forever + * @param statuses list of statuses to wait for, see {@link ClusterStatus} for the list of supported statuses + */ + public void waitForStatus(Long timeout, ClusterStatus... statuses) { + if (getParentApiInstance() == null) + throw new XplentyAPIException("The parent API instance is not set"); + long start = System.currentTimeMillis(); + statusWait: + while (true) { + try { + Thread.sleep(XplentyObject.StatusRefreshInterval); + } catch (InterruptedException e) { + throw new XplentyAPIException("Error sleeping", e); + } + Cluster c = getParentApiInstance().clusterInformation(id); + for (ClusterStatus status: statuses) { + if (c.getStatus() == status) + break statusWait; + } + if (timeout != null && System.currentTimeMillis() - timeout*1000 > start) + 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 date and time the cluster was launched at + */ + public Date getLaunchedAt() { + return launchedAt; + } + + /** + * + * @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; + } + + /** + * + * @return the ID of the cluster's plan + */ + public String getPlanId() { + return planId; + } + + /** + * + * @return the time since cluster changed status to idle + */ + public Date getIdleSince() { + return idleSince; + } + + /** + * + * @return the region in which the cluster was created + */ + public String getRegion() { + return region; + } + + /** + * + * @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; + } +} diff --git a/src/main/java/com/xplenty/api/model/ClusterBootstrapAction.java b/src/main/java/com/xplenty/api/model/ClusterBootstrapAction.java new file mode 100644 index 0000000..0adb695 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/model/ClusterInstance.java b/src/main/java/com/xplenty/api/model/ClusterInstance.java new file mode 100644 index 0000000..9c2535b --- /dev/null +++ b/src/main/java/com/xplenty/api/model/ClusterInstance.java @@ -0,0 +1,112 @@ +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") + protected String instanceId; + @JsonProperty("private_dns") + protected String privateDns; + @JsonProperty("public_dns") + protected String publicDns; + @JsonProperty + protected Xplenty.ClusterInstanceStatus status; + @JsonProperty + protected Boolean master; + @JsonProperty + protected Boolean spot; + @JsonProperty + protected Boolean vpc; + @JsonProperty + protected String zone; + @JsonProperty("instance_type") + protected String instanceType; + @JsonProperty + protected String url; + + protected 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; + } + + /** + * + * @return API resource url + */ + public String getUrl() { + return url; + } +} 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/src/main/java/com/xplenty/api/model/Connection.java b/src/main/java/com/xplenty/api/model/Connection.java new file mode 100644 index 0000000..0d845be --- /dev/null +++ b/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 + protected Long id; + @JsonProperty + protected String name; + @JsonProperty("created_at") + protected Date createdAt; + @JsonProperty("updated_at") + protected Date updatedAt; + @JsonProperty + protected Xplenty.ConnectionType type; + @JsonProperty + protected String url; + + protected 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/src/main/java/com/xplenty/api/model/ConnectionType.java b/src/main/java/com/xplenty/api/model/ConnectionType.java new file mode 100644 index 0000000..b96b5ef --- /dev/null +++ b/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/src/main/java/com/xplenty/api/model/ConnectionTypeGroup.java b/src/main/java/com/xplenty/api/model/ConnectionTypeGroup.java new file mode 100644 index 0000000..30f00f5 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/model/Creator.java b/src/main/java/com/xplenty/api/model/Creator.java new file mode 100644 index 0000000..2dc4df7 --- /dev/null +++ b/src/main/java/com/xplenty/api/model/Creator.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 Creator { + @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 Creator() { + } + + /** + * + * @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/src/main/java/com/xplenty/api/model/CreditCardInfo.java b/src/main/java/com/xplenty/api/model/CreditCardInfo.java new file mode 100644 index 0000000..3114bd8 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/model/EmailHookSettings.java b/src/main/java/com/xplenty/api/model/EmailHookSettings.java new file mode 100644 index 0000000..32cc283 --- /dev/null +++ b/src/main/java/com/xplenty/api/model/EmailHookSettings.java @@ -0,0 +1,81 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.xplenty.api.Xplenty; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * Settings specific for the email hook + * Author: Xardas + * Date: 24.01.16 + * Time: 19:32 + */ +public class EmailHookSettings implements HookSettings { + @JsonProperty + @JsonSerialize(using = EmailsSerializer.class) + @JsonDeserialize(using = EmailsDeserializer.class) + protected List emails; + + protected EmailHookSettings() { + } + + public EmailHookSettings(List emails) { + this.emails = emails; + } + + /** + * + * @return list of emails + */ + public List getEmails() { + return emails; + } + + @Override + public Xplenty.HookType getType() { + return Xplenty.HookType.email; + } + + protected static class EmailsSerializer extends JsonSerializer> { + @Override + public void serialize(List emails, JsonGenerator jgen, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { + if (emails == null || emails.size() == 0) { + return; + } + // For more flexible solution, right now don't see anything against direct field name: + // http://stackoverflow.com/questions/33519354/how-to-get-property-or-field-name-in-a-custom-json-serializer + StringBuilder emailsBuilder = new StringBuilder(); + for (String email : emails) { + emailsBuilder.append(email).append(","); + } + emailsBuilder.setLength(emailsBuilder.length() - 1); + jgen.writeString(emailsBuilder.toString()); + } + } + + protected static class EmailsDeserializer extends JsonDeserializer> { + + @Override + public List deserialize(JsonParser json, DeserializationContext deserializeCtx) throws IOException, JsonProcessingException { + JsonNode rawEmails = json.getCodec().readTree(json); + if (rawEmails.isNull()) { + return null; + } + String[] emailArray = rawEmails.asText().split(","); + List emails = new ArrayList<>(emailArray.length); + for (String email :emailArray) { + emails.add(email); + } + return emails; + } + } +} diff --git a/src/main/java/com/xplenty/api/model/HipChatHookSettings.java b/src/main/java/com/xplenty/api/model/HipChatHookSettings.java new file mode 100644 index 0000000..304700a --- /dev/null +++ b/src/main/java/com/xplenty/api/model/HipChatHookSettings.java @@ -0,0 +1,66 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; + +/** + * Settings specific for the Hip Chat hook + * Author: Xardas + * Date: 24.01.16 + * Time: 20:00 + */ +public class HipChatHookSettings implements HookSettings { + @JsonProperty + private String room; + @JsonProperty("auth_token") + private String authToken; + + public HipChatHookSettings() { + } + + public HipChatHookSettings(String room, String authToken) { + this.room = room; + this.authToken = authToken; + } + + /** + * + * @param room ID of the Hip Chat room + * @return this instance + */ + public HipChatHookSettings withRoom(String room) { + this.room = room; + return this; + } + + /** + * + * @param authToken Hip Chat API token + * @return this instance + */ + public HipChatHookSettings withAuthToken(String authToken) { + this.authToken = authToken; + return this; + } + + /** + * + * @return ID of the Hip Chat room + */ + public String getRoom() { + return room; + } + + /** + * + * @return Hip Chat API token + */ + public String getAuthToken() { + return authToken; + } + + @Override + public Xplenty.HookType getType() { + return Xplenty.HookType.hipchat; + } +} diff --git a/src/main/java/com/xplenty/api/model/Hook.java b/src/main/java/com/xplenty/api/model/Hook.java new file mode 100644 index 0000000..31b464d --- /dev/null +++ b/src/main/java/com/xplenty/api/model/Hook.java @@ -0,0 +1,197 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.xplenty.api.Xplenty; + +import java.io.IOException; +import java.util.List; + +/** + * Data model for Xplenty hook + * An Xplenty hook is kind of notification that will be fired 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: + *
    + *
  • id
  • + *
  • event name
  • + *
  • url
  • + *
  • salt
  • + *
+ * Notified application can verify with salt if request was sent by Xplenty. + * Author: Xardas + * Date: 04.01.16 + * Time: 17:42 + */ +@JsonDeserialize(using = Hook.HookDeserializer.class) +public class Hook extends XplentyObject { + @JsonProperty + protected Long id; + @JsonProperty + protected String name; + @JsonProperty + protected Boolean active; + @JsonProperty + protected HookSettings settings; + @JsonProperty + protected String salt; + @JsonProperty + protected List events; + @JsonProperty + protected Xplenty.HookType type; + + protected Hook() { + super(Hook.class); + } + + public Hook(String name, HookSettings settings, List events) { + super(Hook.class); + this.settings = settings; + this.events = events; + this.name = name; + } + + public Hook(Long id, String name, HookSettings settings, List events) { + super(Hook.class); + this.id = id; + this.settings = settings; + this.events = events; + this.name = name; + } + + public Hook(Long id, Boolean active) { + super(Hook.class); + this.id = id; + this.active = active; + } + + /** + * + * @return the numeric hook ID + */ + public Long getId() { + return id; + } + + /** + * + * @return Name of the hook + */ + public String getName() { + return name; + } + + /** + * + * @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 HookSettings getSettings() { + return settings; + } + + /** + * + * @return list of notification events. + */ + public List getEvents() { + return events; + } + + /** + * + * @return salt needed for verification + */ + public String getSalt() { + return salt; + } + + /** + * + * @return the type of this hook. Based on it {@link #settings} can be safely cast to one of the implementations + */ + public Xplenty.HookType getType() { + if (type != null) { + return type; + } else if (settings != null) { + return settings.getType(); + } + return null; + } + + protected static class HookDeserializer extends JsonDeserializer { + + @Override + public Hook deserialize(JsonParser jp, DeserializationContext desCtx) throws IOException, JsonProcessingException { + JsonNode root = jp.readValueAsTree(); + Hook hook; + if (!root.has("id") && root.has("salt")) { + hook = new Hook(); + hook.salt = root.get("salt").asText(); + return hook; + } + Long id = root.get("id").asLong(); + Boolean active = root.get("active").asBoolean(); + Xplenty.HookType hookType; + try { + hookType = Xplenty.HookType.valueOf(root.get("type").asText()); + } catch (IllegalArgumentException ex) { + throw new IOException("This hook type not supported yet", ex); + } + + hook = new Hook(id, active); + HookSettings settings; + JsonParser settingsParser = root.get("settings").traverse(); + // as it's created with empty codec, we need to explicitly set it (or objects won't be deserialized correctly + settingsParser.setCodec(jp.getCodec()); + switch (hookType) { + case email: + settings = settingsParser.readValueAs(EmailHookSettings.class); + break; + case hipchat: + settings = settingsParser.readValueAs(HipChatHookSettings.class); + break; + case pagerduty: + settings = settingsParser.readValueAs(PagerDutyHookSettings.class); + break; + case slack: + settings = settingsParser.readValueAs(SlackHookSettings.class); + break; + case web: + settings = settingsParser.readValueAs(WebHookSettings.class); + break; + default: + throw new IOException("This hook type not supported yet"); + } + hook.settings = settings; + if (root.has("events")) { + JsonParser eventParser = root.get("events").traverse(); + eventParser.setCodec(jp.getCodec()); + hook.events = eventParser.readValueAs(new TypeReference>() {}); + } + + if (root.has("salt")) { + hook.salt = root.get("salt").asText(); + } + + if (root.has("name")) { + hook.name = root.get("name").asText(); + } + + return hook; + } + } +} diff --git a/src/main/java/com/xplenty/api/model/HookEvent.java b/src/main/java/com/xplenty/api/model/HookEvent.java new file mode 100644 index 0000000..3612c2c --- /dev/null +++ b/src/main/java/com/xplenty/api/model/HookEvent.java @@ -0,0 +1,94 @@ +package com.xplenty.api.model; + +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; + +/** + * Data Model for Xplenty Web Hook event + * Author: Xardas + * Date: 04.01.16 + * Time: 18:01 + */ +@JsonSerialize(using = HookEvent.WebHookEventSerializer.class) +public class HookEvent { + @JsonProperty + protected Long id; + @JsonProperty + protected String name; + @JsonProperty("last_response") + protected WebHookEventResponse lastResponse; + @JsonProperty("last_trigger_status") + protected String lastTriggerStatus; + @JsonProperty("last_trigger_time") + protected Date lastTriggerTime; + + public HookEvent() {} + + public HookEvent(String name) { + this.name = name; + } + + /** + * + * @return id of the event + */ + public Long getId() { + return id; + } + + /** + * + * @return name constant of the event + */ + public String getName() { + return name; + } + + public Xplenty.HookEvent getEvent() { + try { + return Xplenty.HookEvent.fromString(name); + } catch (IllegalArgumentException ex) { + return null; + } + } + + /** + * + * @return last response + */ + public WebHookEventResponse getLastResponse() { + return lastResponse; + } + + /** + * + * @return last trigger status + */ + public String getLastTriggerStatus() { + return lastTriggerStatus; + } + + /** + * + * @return time web hook was last triggered + */ + public Date getLastTriggerTime() { + return lastTriggerTime; + } + + protected static class WebHookEventSerializer extends JsonSerializer { + + @Override + public void serialize(HookEvent hookEvent, JsonGenerator jGen, SerializerProvider sp) throws IOException, JsonProcessingException { + jGen.writeString(hookEvent.getName()); + } + } +} diff --git a/src/main/java/com/xplenty/api/model/HookSettings.java b/src/main/java/com/xplenty/api/model/HookSettings.java new file mode 100644 index 0000000..87abacd --- /dev/null +++ b/src/main/java/com/xplenty/api/model/HookSettings.java @@ -0,0 +1,15 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.xplenty.api.Xplenty; + +/** + * Interface representing specific hook settings + * Author: Xardas + * Date: 24.01.16 + * Time: 19:22 + */ +public interface HookSettings { + @JsonIgnore + Xplenty.HookType getType(); +} 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 64% 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 index 9ecc5e0..483c551 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Job.java +++ b/src/main/java/com/xplenty/api/model/Job.java @@ -3,43 +3,38 @@ */ 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.JsonIgnore; 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 +48,30 @@ public Job() { protected Date updatedAt; @JsonProperty("cluster_id") protected Long clusterId; + @JsonProperty + protected Cluster cluster; @JsonProperty("package_id") protected Long packageId; + @JsonProperty("package") + 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 Creator 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 +100,20 @@ 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 + */ + @JsonIgnore + public JobLog getJobLog() { + return getParentApiInstance().getJobLog(id); + } public Job withId(long id) { this.id = id; @@ -121,60 +139,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 Creator getCreator() { + return creator; + } + + @SuppressWarnings("unused") private void setId(long id) { this.id = id; } diff --git a/src/main/java/com/xplenty/api/model/JobLog.java b/src/main/java/com/xplenty/api/model/JobLog.java new file mode 100644 index 0000000..e46e357 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/model/JobOutput.java similarity index 87% 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 index e928a63..4c03958 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/JobOutput.java +++ b/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/src/main/java/com/xplenty/api/model/JobOutputPreview.java b/src/main/java/com/xplenty/api/model/JobOutputPreview.java new file mode 100644 index 0000000..affc5c5 --- /dev/null +++ b/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/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/src/main/java/com/xplenty/api/model/Member.java b/src/main/java/com/xplenty/api/model/Member.java new file mode 100644 index 0000000..2d473ac --- /dev/null +++ b/src/main/java/com/xplenty/api/model/Member.java @@ -0,0 +1,174 @@ +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 + protected Long id; + @JsonProperty + protected String name; + @JsonProperty + protected String email; + @JsonProperty("gravatar_email") + protected String gravatarEmail; + @JsonProperty("avatar_url") + protected String avatarUrl; + @JsonProperty("created_at") + protected Date createdAt; + @JsonProperty("updated_at") + protected Date updatedAt; + @JsonProperty + protected String location; + @JsonProperty + protected Boolean confirmed; + @JsonProperty("confirmed_at") + protected Date confirmedAt; + @JsonProperty + protected Xplenty.AccountRole role; + @JsonProperty + protected Boolean owner; + @JsonProperty + protected String url; + @JsonProperty("html_url") + protected String htmlUrl; + + protected Member() { + super(Member.class); + } + + 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) { + super(Member.class); + this.id = id; + this.role = role; + } + + /** + * + * @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/src/main/java/com/xplenty/api/model/Notification.java b/src/main/java/com/xplenty/api/model/Notification.java new file mode 100644 index 0000000..4ce0f51 --- /dev/null +++ b/src/main/java/com/xplenty/api/model/Notification.java @@ -0,0 +1,79 @@ +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 + protected Long id; + @JsonProperty + protected String title; + @JsonProperty + protected String message; + @JsonProperty("last_read_at") + protected Date lastReadAt; + @JsonProperty("created_at") + protected Date createdAt; + @JsonProperty("updated_at") + protected Date updatedAt; + + 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; + } + + /** + * + * @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/src/main/java/com/xplenty/api/model/Package.java b/src/main/java/com/xplenty/api/model/Package.java new file mode 100644 index 0000000..6153edf --- /dev/null +++ b/src/main/java/com/xplenty/api/model/Package.java @@ -0,0 +1,219 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; + +import java.util.Date; +import java.util.Map; + +/** + * Data model for Xplenty package + * Author: Xardas + * Date: 16.12.15 + * Time: 18:08 + */ +public class Package extends XplentyObject { + @JsonProperty + protected Long id; + @JsonProperty + protected String name; + @JsonProperty + protected String description; + @JsonProperty + protected Map variables; + @JsonProperty("owner_id") + protected Long ownerId; + @JsonProperty("created_at") + protected Date createdAt; + @JsonProperty("updated_at") + protected Date updatedAt; + @JsonProperty + protected String url; + @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; + } + + + /** + * + * @return the numeric package ID + */ + public Long getId() { + return id; + } + + /** + * + * @return the name given to the package upon creation + */ + public String getName() { + return name; + } + + /** + * + * @return the description given to the package upon creation + */ + public String getDescription() { + return description; + } + + /** + * + * @return the list of package variables + */ + public Map getVariables() { + return variables; + } + + /** + * + * @return the numeric user id of the package owner + */ + public Long getOwnerId() { + return ownerId; + } + + /** + * + * @return the date and time the package was created + */ + public Date getCreatedAt() { + return createdAt; + } + + /** + * + * @return the date and time the package was last updated + */ + public Date getUpdatedAt() { + return updatedAt; + } + + /** + * + * @return the package resource URL (API) + */ + public String getUrl() { + return url; + } + + /** + * + * @return the package resource URL (Web UI) + */ + public String getHtmlUrl() { + return htmlUrl; + } + + /** + * + * @return the package status. + */ + public Xplenty.PackageStatus getStatus() { + return status; + } + + /** + * Flow type of the package. + */ + public Xplenty.PackageFlowType getFlowType() { + return flowType; + } +} diff --git a/src/main/java/com/xplenty/api/model/PackageTemplate.java b/src/main/java/com/xplenty/api/model/PackageTemplate.java new file mode 100644 index 0000000..7e3b12d --- /dev/null +++ b/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 + protected Long id; + @JsonProperty + protected String name; + @JsonProperty + protected String description; + @JsonProperty + protected Integer position; + @JsonProperty + protected 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/src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java b/src/main/java/com/xplenty/api/model/PackageTemplateAuthor.java new file mode 100644 index 0000000..0a4c34b --- /dev/null +++ b/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 + protected Long id; + @JsonProperty + protected String name; + @JsonProperty("avatar_url") + protected 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/src/main/java/com/xplenty/api/model/PackageValidation.java b/src/main/java/com/xplenty/api/model/PackageValidation.java new file mode 100644 index 0000000..a7b13e8 --- /dev/null +++ b/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 + protected Long id; + @JsonProperty + protected Xplenty.PackageValidationStatus status; + @JsonProperty("status_message") + protected String statusMessage; + @JsonProperty + protected Long runtime; + @JsonProperty("package_id") + protected Long packageId; + @JsonProperty("owner_id") + protected Long ownerId; + @JsonProperty("account_id") + protected Long accountId; + @JsonProperty + protected List errors; + @JsonProperty("created_at") + protected Date createdAt; + @JsonProperty("updated_at") + protected Date updatedAt; + @JsonProperty + protected 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; + } +} diff --git a/src/main/java/com/xplenty/api/model/PackageValidationError.java b/src/main/java/com/xplenty/api/model/PackageValidationError.java new file mode 100644 index 0000000..72cd08e --- /dev/null +++ b/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/src/main/java/com/xplenty/api/model/PagerDutyHookSettings.java b/src/main/java/com/xplenty/api/model/PagerDutyHookSettings.java new file mode 100644 index 0000000..e54cef6 --- /dev/null +++ b/src/main/java/com/xplenty/api/model/PagerDutyHookSettings.java @@ -0,0 +1,87 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; + +/** + * Settings specific for the PagerDuty hook. + * Author: Xardas + * Date: 24.01.16 + * Time: 20:05 + */ +public class PagerDutyHookSettings implements HookSettings { + @JsonProperty("pd_account") + private String pdAccount; + @JsonProperty("service_name") + private String serviceName; + @JsonProperty("service_key") + private String serviceKey; + + public PagerDutyHookSettings() { + } + + public PagerDutyHookSettings(String pdAccount, String serviceName, String serviceKey) { + this.pdAccount = pdAccount; + this.serviceName = serviceName; + this.serviceKey = serviceKey; + } + + /** + * + * @param pdAccount PagerDuty account + * @return this instance + */ + public PagerDutyHookSettings withPdAccount(String pdAccount) { + this.pdAccount = pdAccount; + return this; + } + + /** + * + * @param serviceName name of the PagerDuty service + * @return this instance + */ + public PagerDutyHookSettings withServiceName(String serviceName) { + this.serviceName = serviceName; + return this; + } + + /** + * + * @param serviceKey key of the PagerDuty service + * @return this instance + */ + public PagerDutyHookSettings withServiceKey(String serviceKey) { + this.serviceKey = serviceKey; + return this; + } + + /** + * + * @return PagerDuty account + */ + public String getPdAccount() { + return pdAccount; + } + + /** + * + * @return name of the PagerDuty service + */ + public String getServiceName() { + return serviceName; + } + + /** + * + * @return key of the PagerDuty service + */ + public String getServiceKey() { + return serviceKey; + } + + @Override + public Xplenty.HookType getType() { + return Xplenty.HookType.pagerduty; + } +} diff --git a/src/main/java/com/xplenty/api/model/Plan.java b/src/main/java/com/xplenty/api/model/Plan.java new file mode 100644 index 0000000..fa83d53 --- /dev/null +++ b/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 + protected String id; + @JsonProperty + protected String name; + @JsonProperty + protected String description; + @JsonProperty("price_cents") + protected Long priceCents; + @JsonProperty("price_currency") + protected String priceCurrency; + @JsonProperty("price_unit") + protected Xplenty.PriceUnit priceUnit; + @JsonProperty("cluster_node_hours_included") + protected Integer clusterNodeHoursIncluded; + @JsonProperty("cluster_node_hours_limit") + protected Integer clusterNodeHoursLimit; + @JsonProperty("cluster_node_price_cents") + protected Long clusterNodePriceCents; + @JsonProperty("cluster_node_price_currency") + protected String clusterNodePriceCurrency; + @JsonProperty("cluster_node_price_unit") + protected Xplenty.PriceUnit clusterNodePriceUnit; + @JsonProperty("cluster_nodes_limit") + protected Integer clusterNodesLimit; + @JsonProperty("cluster_size_limit") + protected Integer clusterSizeLimit; + @JsonProperty("clusters_limit") + protected Integer clustersLimit; + @JsonProperty("sandbox_clusters_limit") + protected Integer sandboxClustersLimit; + @JsonProperty("sandbox_node_hours_included") + protected Integer sandboxNodeHoursIncluded; + @JsonProperty("sandbox_node_hours_limit") + protected Integer sandboxNodeHoursLimit; + @JsonProperty("members_limit") + protected Integer membersLimit; + @JsonProperty + protected Integer position; + @JsonProperty("created_at") + protected Date createdAt; + @JsonProperty("updated_at") + protected 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/src/main/java/com/xplenty/api/model/ProductUpdate.java b/src/main/java/com/xplenty/api/model/ProductUpdate.java new file mode 100644 index 0000000..91a1604 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/model/PublicKey.java b/src/main/java/com/xplenty/api/model/PublicKey.java new file mode 100644 index 0000000..b0e7a6e --- /dev/null +++ b/src/main/java/com/xplenty/api/model/PublicKey.java @@ -0,0 +1,98 @@ +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 + protected Long id; + @JsonProperty + protected String name; + @JsonProperty + protected String comment; + @JsonProperty + protected String fingerprint; + @JsonProperty("created_at") + protected Date createdAt; + @JsonProperty("updated_at") + protected Date updatedAt; + @JsonProperty + 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 + */ + 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/src/main/java/com/xplenty/api/model/Region.java b/src/main/java/com/xplenty/api/model/Region.java new file mode 100644 index 0000000..5e419c5 --- /dev/null +++ b/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/model/Schedule.java b/src/main/java/com/xplenty/api/model/Schedule.java similarity index 72% 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 index 994b7c0..88145fb 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Schedule.java +++ b/src/main/java/com/xplenty/api/model/Schedule.java @@ -50,8 +50,14 @@ public Schedule() { protected Date updatedAt; @JsonProperty protected String url; + @JsonProperty("html_url") + protected String htmlUrl; @JsonProperty protected ScheduleTask task; + @JsonProperty + protected Boolean overlap; + @JsonProperty("reuse_cluster_strategy") + protected Xplenty.ReuseClusterStrategy reuseClusterStrategy; /** * @@ -167,12 +173,20 @@ public Date getUpdatedAt() { /** * - * @return the schedule resource ID + * @return the schedule resource URL (API) */ public String getUrl() { return url; } + /** + * + * @return the schedule resource URL (Web UI) + */ + public String getHtmlUrl() { + return htmlUrl; + } + /** * * @return task that will be executed on schedule @@ -181,84 +195,75 @@ public ScheduleTask getTask() { return task; } + /** + * + * @return if execution overlapping is allowed + */ + public Boolean getOverlap() { + return overlap; + } - @SuppressWarnings("unused") - public void setId(Long id) { + /** + * + * @return the strategy of re-using cluster + */ + public Xplenty.ReuseClusterStrategy getReuseClusterStrategy() { + return reuseClusterStrategy; + } + + public Schedule withId(Long id) { this.id = id; + return this; } - - @SuppressWarnings("unused") - public void setName(String name) { + + public Schedule withName(String name) { this.name = name; + return this; } - @SuppressWarnings("unused") - public void setDescription(String description) { + + public Schedule withDescription(String description) { this.description = description; + return this; } - @SuppressWarnings("unused") - private void setOwnerId(Long ownerId) { - this.ownerId = ownerId; - } - @SuppressWarnings("unused") - public void setStatus(Xplenty.ScheduleStatus status) { + public Schedule withStatus(Xplenty.ScheduleStatus status) { this.status = status; + return this; } - @SuppressWarnings("unused") - public void setStartAt(Date startAt) { + + public Schedule withStartAt(Date startAt) { this.startAt = startAt; + return this; } - @SuppressWarnings("unused") - private void setNextRunAt(Date nextRunAt) { - this.nextRunAt = nextRunAt; - } - @SuppressWarnings("unused") - public void setIntervalAmount(Long intervalAmount) { + public Schedule withIntervalAmount(Long intervalAmount) { this.intervalAmount = intervalAmount; + return this; } - @SuppressWarnings("unused") - public void setIntervalUnit(Xplenty.ScheduleIntervalUnit intervalUnit) { - this.intervalUnit = intervalUnit; - } - @SuppressWarnings("unused") - private void setLastRunAt(Date lastRunAt) { - this.lastRunAt = lastRunAt; - } - - @SuppressWarnings("unused") - private void setLastRunStatus(String lastRunStatus) { - this.lastRunStatus = lastRunStatus; + public Schedule withIntervalUnit(Xplenty.ScheduleIntervalUnit intervalUnit) { + this.intervalUnit = intervalUnit; + return this; } - @SuppressWarnings("unused") - private void setExecutionCount(Long executionCount) { - this.executionCount = executionCount; - } - @SuppressWarnings("unused") - private void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - } - - @SuppressWarnings("unused") - private void setUpdatedAt(Date updatedAt) { - this.updatedAt = updatedAt; + public Schedule withTask(ScheduleTask task) { + this.task = task; + return this; } - @SuppressWarnings("unused") - private void setUrl(String url) { - this.url = url; + public Schedule withOverlap(Boolean overlap) { + this.overlap = overlap; + return this; } - @SuppressWarnings("unused") - public void setTask(ScheduleTask task) { - this.task = task; + public Schedule withReuseClusterStrategy(Xplenty.ReuseClusterStrategy reuseClusterStrategy) { + this.reuseClusterStrategy = reuseClusterStrategy; + return this; } } 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 80% 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 index c35e19f..e639eaf 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ScheduleTask.java +++ b/src/main/java/com/xplenty/api/model/ScheduleTask.java @@ -1,12 +1,13 @@ 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.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; @@ -19,8 +20,6 @@ * Date: 16.12.15 * Time: 19:56 */ -@JsonInclude(JsonInclude.Include.NON_NULL) -@JsonIgnoreProperties(ignoreUnknown = true) public class ScheduleTask { @JsonProperty @@ -50,25 +49,24 @@ public List getPackages() { return packages; } - - @SuppressWarnings("unused") - public void setNodes(Integer nodes) { + public ScheduleTask withNodes(Integer nodes) { this.nodes = nodes; + return this; } - @SuppressWarnings("unused") - public void setTerminateOnIdle(Boolean terminateOnIdle) { + public ScheduleTask withTerminateOnIdle(Boolean terminateOnIdle) { this.terminateOnIdle = terminateOnIdle; + return this; } - @SuppressWarnings("unused") - public void setTimeToIdle(Integer timeToIdle) { + public ScheduleTask withTimeToIdle(Integer timeToIdle) { this.timeToIdle = timeToIdle; + return this; } - @SuppressWarnings("unused") - public void setPackages(List packages) { + public ScheduleTask withPackages(List packages) { this.packages = packages; + return this; } protected static class Number2BooleanJsonConverter extends JsonDeserializer { 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 61% 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 index 6a4faac..6891f8d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/ScheduleTaskPackage.java +++ b/src/main/java/com/xplenty/api/model/ScheduleTaskPackage.java @@ -1,7 +1,5 @@ package com.xplenty.api.model; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Map; @@ -12,8 +10,6 @@ * Date: 16.12.15 * Time: 20:21 */ -@JsonInclude(JsonInclude.Include.NON_NULL) -@JsonIgnoreProperties(ignoreUnknown = true) public class ScheduleTaskPackage { @JsonProperty("package_id") protected Long packageId; @@ -28,14 +24,13 @@ public Map getVariables() { return variables; } - - @SuppressWarnings("unused") - public void setPackageId(Long packageId) { + public ScheduleTaskPackage withPackageId(Long packageId) { this.packageId = packageId; + return this; } - @SuppressWarnings("unused") - public void setVariables(Map variables) { + public ScheduleTaskPackage withVariables(Map variables) { this.variables = variables; + return this; } } diff --git a/src/main/java/com/xplenty/api/model/SlackHookSettings.java b/src/main/java/com/xplenty/api/model/SlackHookSettings.java new file mode 100644 index 0000000..93ac072 --- /dev/null +++ b/src/main/java/com/xplenty/api/model/SlackHookSettings.java @@ -0,0 +1,107 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; + +/** + * Settings specific for the Slack hook. + * Author: Xardas + * Date: 24.01.16 + * Time: 19:07 + */ +public class SlackHookSettings implements HookSettings { + @JsonProperty + private String url; + @JsonProperty + private String team; + @JsonProperty + private String channel; + @JsonProperty + private String username; + + public SlackHookSettings() {} + + public SlackHookSettings(String url, String team, String channel, String username) { + this.url = url; + this.team = team; + this.channel = channel; + this.username = username; + } + + /** + * + * @param url URL containing unique token which allows to send messages on the Slack channel. + * @return this instance + */ + public SlackHookSettings withUrl(String url) { + this.url = url; + return this; + } + + /** + * + * @param team Slack team + * @return this instance + */ + public SlackHookSettings withTeam(String team) { + this.team = team; + return this; + } + + /** + * + * @param channel Slack channel + * @return this instance + */ + public SlackHookSettings withChannel(String channel) { + this.channel = channel; + return this; + } + + /** + * + * @param username Slack username + * @return this instance + */ + public SlackHookSettings withUsername(String username) { + this.username = username; + return this; + } + + /** + * + * @return URL containing unique token which allows to send messages on the Slack channel. + */ + public String getUrl() { + return url; + } + + /** + * + * @return Slack team + */ + public String getTeam() { + return team; + } + + /** + * + * @return Slack channel + */ + public String getChannel() { + return channel; + } + + /** + * + * @return Slack username + */ + public String getUsername() { + return username; + } + + @Override + public Xplenty.HookType getType() { + return Xplenty.HookType.slack; + } +} diff --git a/src/main/java/com/xplenty/api/model/Stack.java b/src/main/java/com/xplenty/api/model/Stack.java new file mode 100644 index 0000000..97175c9 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/model/Subscription.java b/src/main/java/com/xplenty/api/model/Subscription.java new file mode 100644 index 0000000..f3d1417 --- /dev/null +++ b/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") + protected Integer trialPeriodDays; + @JsonProperty("plan_id") + protected String planId; + @JsonProperty("trial_start") + protected Date trialStart; + @JsonProperty("trial_end") + protected Date trialEnd; + @JsonProperty("trialling") + protected Boolean isTrial; + @JsonProperty + protected 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 String 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/src/main/java/com/xplenty/api/model/Timezone.java b/src/main/java/com/xplenty/api/model/Timezone.java new file mode 100644 index 0000000..eb6c9f1 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/model/User.java b/src/main/java/com/xplenty/api/model/User.java new file mode 100644 index 0000000..9b03900 --- /dev/null +++ b/src/main/java/com/xplenty/api/model/User.java @@ -0,0 +1,258 @@ +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 + protected Long id; + @JsonProperty + protected String name; + @JsonProperty + protected String email; + @JsonProperty("gravatar_email") + protected String gravatarEmail; + @JsonProperty("avatar_url") + protected String avatarUrl; + @JsonProperty("time_zone") + protected String timeZone; + @JsonProperty + protected String location; + @JsonProperty + protected Boolean confirmed; + @JsonProperty("confirmed_at") + protected Date confirmedAt; + @JsonProperty("notifications_count") + protected Integer notificationsCount; + @JsonProperty("unread_notifications_count") + protected Integer unreadNotificationsCount; + @JsonProperty("notification_settings") + protected UserNotificationSettings notificationSettings; + @JsonProperty("receive_newsletter") + protected Boolean receiveNewsLetter; + @JsonProperty("created_at") + protected Date createdAt; + @JsonProperty("updated_at") + protected Date updatedAt; + @JsonProperty("api_key") + protected String apiKey; + @JsonProperty + protected String url; + @JsonProperty("last_login") + protected Date lastLogin; + + @JsonProperty("current_password") + private String currentPassword; + @JsonProperty("new_password") + private String newPassword; + + public User() { + super(User.class); + } + + public User(String currentPassword) { + super(User.class); + this.currentPassword = currentPassword; + } + + + /** + * @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 isConfirmed() { + 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 isReceiveNewsLetter() { + 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; + } + + /** + * + * @return date and time of the last user login + */ + public Date getLastLogin() { + return lastLogin; + } + + /** + * + * @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/src/main/java/com/xplenty/api/model/UserNotificationSettings.java b/src/main/java/com/xplenty/api/model/UserNotificationSettings.java new file mode 100644 index 0000000..5d0d542 --- /dev/null +++ b/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/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/src/main/java/com/xplenty/api/model/WebHookEventResponse.java b/src/main/java/com/xplenty/api/model/WebHookEventResponse.java new file mode 100644 index 0000000..2397cee --- /dev/null +++ b/src/main/java/com/xplenty/api/model/WebHookEventResponse.java @@ -0,0 +1,25 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Author: Xardas + * Date: 19.01.16 + * Time: 20:31 + */ +public class WebHookEventResponse { + @JsonProperty + private String code; + @JsonProperty + private String body; + + protected WebHookEventResponse() {} + + public String getCode() { + return code; + } + + public String getBody() { + return body; + } +} diff --git a/src/main/java/com/xplenty/api/model/WebHookSettings.java b/src/main/java/com/xplenty/api/model/WebHookSettings.java new file mode 100644 index 0000000..19b3671 --- /dev/null +++ b/src/main/java/com/xplenty/api/model/WebHookSettings.java @@ -0,0 +1,114 @@ +package com.xplenty.api.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.xplenty.api.Xplenty; + +/** + * Data model for Xplenty Web Hook settings + * Author: Xardas + * Date: 04.01.16 + * Time: 17:52 + */ +public class WebHookSettings implements HookSettings { + @JsonProperty + private String url; + @JsonProperty("insecure_ssl") + private Boolean insecureSSL; + @JsonProperty("basic_auth") + private Boolean basicAuth; + @JsonProperty("basic_auth_data") + private String basicAuthData; + @JsonProperty("encrypted_basic_auth_data") + protected String encryptedBasicAuthData; + + 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; + } + + /** + * + * @return server encrypted data needed for basic authentication + */ + public String getEncryptedBasicAuthData() { + return encryptedBasicAuthData; + } + + /** + * + * @param url URL of the target server + */ + public WebHookSettings withUrl(String url) { + this.url = url; + return this; + } + + /** + * + * @param insecureSSL indicates whether SSL certificate is verified + */ + public WebHookSettings withInsecureSSL(Boolean insecureSSL) { + this.insecureSSL = insecureSSL; + return this; + } + + /** + * + * @param basicAuth indicates whether the basic authentication is required + */ + public WebHookSettings withBasicAuth(Boolean basicAuth) { + this.basicAuth = basicAuth; + return this; + } + + /** + * + * @param basicAuthData data needed for basic authentication (user:password encoded with base64) + */ + public WebHookSettings withBasicAuthData(String basicAuthData) { + this.basicAuthData = basicAuthData; + return this; + } + + @Override + public Xplenty.HookType getType() { + return Xplenty.HookType.web; + } +} 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 73% 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 index 175cca5..b29bad3 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractDeleteRequest.java +++ b/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; @@ -13,12 +12,12 @@ * 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; @SuppressWarnings("unchecked") - protected AbstractDeleteRequest(Long entityId) { + protected AbstractDeleteRequest(long entityId) { this.entityId = entityId; final Type superclass = this.getClass().getGenericSuperclass(); if (superclass instanceof Class) { @@ -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/src/main/java/com/xplenty/api/request/AbstractInfoRequest.java similarity index 76% 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 index cb9d0e0..01144b7 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractInfoRequest.java +++ b/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; @@ -13,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; @@ -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/AbstractParametrizedRequest.java b/src/main/java/com/xplenty/api/request/AbstractListRequest.java similarity index 51% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractParametrizedRequest.java rename to src/main/java/com/xplenty/api/request/AbstractListRequest.java index e3956ff..89c8c64 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractParametrizedRequest.java +++ b/src/main/java/com/xplenty/api/request/AbstractListRequest.java @@ -2,7 +2,11 @@ import com.xplenty.api.Xplenty; 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; /** @@ -10,64 +14,82 @@ * Date: 17.12.15 * Time: 20:46 */ -public abstract class AbstractParametrizedRequest implements Request { +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 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) && !(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)); } } @Override public String getEndpoint() { - if (parameters.isEmpty()) { + if (parameters == null || parameters.isEmpty()) { return getEndpointRoot(); } 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); @@ -77,6 +99,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/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java similarity index 65% 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 index 73ab94d..76a9f0e 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/AbstractManipulationRequest.java +++ b/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; @@ -15,8 +14,8 @@ * Date: 18.12.15 * Time: 18:49 */ -public abstract class AbstractManipulationRequest implements Request { - protected final T entity; +public abstract class AbstractManipulationRequest extends AbstractRequest { + protected T entity; private final Class clazz; @SuppressWarnings("unchecked") @@ -29,11 +28,14 @@ protected AbstractManipulationRequest(T entity) { this.clazz = (Class) ((ParameterizedType) superclass).getActualTypeArguments()[0]; } + protected void setEntity(T entity) { + this.entity = 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); @@ -53,9 +55,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/src/main/java/com/xplenty/api/request/AbstractRequest.java b/src/main/java/com/xplenty/api/request/AbstractRequest.java new file mode 100644 index 0000000..f5414da --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/RawGetRequest.java b/src/main/java/com/xplenty/api/request/RawGetRequest.java new file mode 100644 index 0000000..8774a50 --- /dev/null +++ b/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/Request.java b/src/main/java/com/xplenty/api/request/Request.java similarity index 73% 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 index 4c48841..ce3d747 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/Request.java +++ b/src/main/java/com/xplenty/api/request/Request.java @@ -3,15 +3,15 @@ */ 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 * */ 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(ClientResponse response); + + T getResponse(Response response); } diff --git a/src/main/java/com/xplenty/api/request/account/AccountInfo.java b/src/main/java/com/xplenty/api/request/account/AccountInfo.java new file mode 100644 index 0000000..84dfbe8 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/account/CreateAccount.java b/src/main/java/com/xplenty/api/request/account/CreateAccount.java new file mode 100644 index 0000000..df6118e --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/account/DeleteAccount.java b/src/main/java/com/xplenty/api/request/account/DeleteAccount.java new file mode 100644 index 0000000..15842a0 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/account/ListAccountRegions.java b/src/main/java/com/xplenty/api/request/account/ListAccountRegions.java new file mode 100644 index 0000000..f422d60 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/account/ListAccounts.java b/src/main/java/com/xplenty/api/request/account/ListAccounts.java new file mode 100644 index 0000000..a6ec900 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/account/UpdateAccount.java b/src/main/java/com/xplenty/api/request/account/UpdateAccount.java new file mode 100644 index 0000000..7b4b1ff --- /dev/null +++ b/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/main/java/com/xplenty/api/request/ClusterInfo.java b/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 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/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/src/main/java/com/xplenty/api/request/cluster/CreateCluster.java similarity index 76% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateCluster.java rename to src/main/java/com/xplenty/api/request/cluster/CreateCluster.java index 6e0669e..1d7c722 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateCluster.java +++ b/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.util.Http; -import com.xplenty.api.util.Http.Method; +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/src/main/java/com/xplenty/api/request/cluster/ListClusterInstances.java b/src/main/java/com/xplenty/api/request/cluster/ListClusterInstances.java new file mode 100644 index 0000000..63b28ed --- /dev/null +++ b/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; + } + +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java b/src/main/java/com/xplenty/api/request/cluster/ListClusters.java similarity index 57% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java rename to src/main/java/com/xplenty/api/request/cluster/ListClusters.java index d489396..22349f0 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListClusters.java +++ b/src/main/java/com/xplenty/api/request/cluster/ListClusters.java @@ -1,18 +1,15 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.cluster; 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.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 com.xplenty.api.request.AbstractListRequest; import java.util.List; import java.util.Properties; @@ -21,7 +18,7 @@ * @author Yuriy Kovalek * */ -public class ListClusters extends AbstractParametrizedRequest> { +public class ListClusters extends AbstractListRequest> { public ListClusters(Properties params) { super(params, true); @@ -36,26 +33,15 @@ 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; } @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); } @@ -66,14 +52,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/TerminateCluster.java b/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 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/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/src/main/java/com/xplenty/api/request/cluster/UpdateCluster.java similarity index 77% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCluster.java rename to src/main/java/com/xplenty/api/request/cluster/UpdateCluster.java index 0aeb5d2..2a74ddf 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateCluster.java +++ b/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.util.Http; -import com.xplenty.api.util.Http.Method; +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/src/main/java/com/xplenty/api/request/connection/ConnectionInfo.java b/src/main/java/com/xplenty/api/request/connection/ConnectionInfo.java new file mode 100644 index 0000000..a796aa7 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/connection/DeleteConnection.java b/src/main/java/com/xplenty/api/request/connection/DeleteConnection.java new file mode 100644 index 0000000..a904dcf --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/connection/ListConnectionTypes.java b/src/main/java/com/xplenty/api/request/connection/ListConnectionTypes.java new file mode 100644 index 0000000..c41c533 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/connection/ListConnections.java b/src/main/java/com/xplenty/api/request/connection/ListConnections.java new file mode 100644 index 0000000..e37e131 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/hook/CreateHook.java b/src/main/java/com/xplenty/api/request/hook/CreateHook.java new file mode 100644 index 0000000..7a7a94d --- /dev/null +++ b/src/main/java/com/xplenty/api/request/hook/CreateHook.java @@ -0,0 +1,68 @@ +package com.xplenty.api.request.hook; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.Hook; +import com.xplenty.api.model.HookSettings; +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 CreateHook extends AbstractManipulationRequest { + + public CreateHook(String name, HookSettings 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) { + com.xplenty.api.model.HookEvent hookEvent = new com.xplenty.api.model.HookEvent(event); + convEvents.add(hookEvent); + } + Hook entity = new Hook(name, settings, convEvents); + setEntity(entity); + } + + public CreateHook(String name, List events, HookSettings 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.HookEvent event : events) { + com.xplenty.api.model.HookEvent hookEvent = new com.xplenty.api.model.HookEvent(event.toString()); + convEvents.add(hookEvent); + } + Hook entity = new Hook(name, settings, convEvents); + setEntity(entity); + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.CreateHook.value; + } + + @Override + public String getName() { + return Xplenty.Resource.CreateHook.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.POST; + } +} diff --git a/src/main/java/com/xplenty/api/request/hook/DeleteHook.java b/src/main/java/com/xplenty/api/request/hook/DeleteHook.java new file mode 100644 index 0000000..fc9b6c8 --- /dev/null +++ b/src/main/java/com/xplenty/api/request/hook/DeleteHook.java @@ -0,0 +1,28 @@ +package com.xplenty.api.request.hook; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.Hook; +import com.xplenty.api.request.AbstractDeleteRequest; + +/** + * Delete web hook + * Author: Xardas + * Date: 18.12.15 + * Time: 20:25 + */ +public class DeleteHook extends AbstractDeleteRequest { + + public DeleteHook(long entityId) { + super(entityId); + } + + @Override + public String getName() { + return Xplenty.Resource.DeleteHook.name; + } + + @Override + public String getEndpoint() { + return Xplenty.Resource.DeleteHook.format(String.valueOf(entityId)); + } +} diff --git a/src/main/java/com/xplenty/api/request/hook/HookInfo.java b/src/main/java/com/xplenty/api/request/hook/HookInfo.java new file mode 100644 index 0000000..cf6c471 --- /dev/null +++ b/src/main/java/com/xplenty/api/request/hook/HookInfo.java @@ -0,0 +1,28 @@ +package com.xplenty.api.request.hook; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.Hook; +import com.xplenty.api.request.AbstractInfoRequest; + +/** + * Get web hook information + * Author: Xardas + * Date: 05.01.16 + * Time: 18:26 + */ +public class HookInfo extends AbstractInfoRequest { + + public HookInfo(long entityId) { + super(entityId); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.Hook.format(String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.Hook.name; + } +} diff --git a/src/main/java/com/xplenty/api/request/hook/HookResetSalt.java b/src/main/java/com/xplenty/api/request/hook/HookResetSalt.java new file mode 100644 index 0000000..051fe6e --- /dev/null +++ b/src/main/java/com/xplenty/api/request/hook/HookResetSalt.java @@ -0,0 +1,62 @@ +package com.xplenty.api.request.hook; + +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.Hook; +import com.xplenty.api.request.AbstractRequest; + +/** + * Reset salt for web hook + * Author: Xardas + * Date: 05.01.16 + * Time: 18:07 + */ +public class HookResetSalt extends AbstractRequest { + private final long webHookId; + + public HookResetSalt(long webHookId) { + this.webHookId = webHookId; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.HookResetSalt.format(String.valueOf(webHookId)); + } + + @Override + public String getName() { + return Xplenty.Resource.HookResetSalt.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 { + Hook webHook = response.getContent(Hook.class); + return webHook.getSalt(); + } catch (Exception e) { + throw new XplentyAPIException(getName() + ": error parsing response object", e); + } + } +} diff --git a/src/main/java/com/xplenty/api/request/hook/ListHookEvents.java b/src/main/java/com/xplenty/api/request/hook/ListHookEvents.java new file mode 100644 index 0000000..f693cdb --- /dev/null +++ b/src/main/java/com/xplenty/api/request/hook/ListHookEvents.java @@ -0,0 +1,47 @@ +package com.xplenty.api.request.hook; + +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.AvailableHookEvent; +import com.xplenty.api.request.AbstractListRequest; + +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/src/main/java/com/xplenty/api/request/hook/ListHooks.java b/src/main/java/com/xplenty/api/request/hook/ListHooks.java new file mode 100644 index 0000000..0b340e2 --- /dev/null +++ b/src/main/java/com/xplenty/api/request/hook/ListHooks.java @@ -0,0 +1,44 @@ +package com.xplenty.api.request.hook; + +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.Hook; +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 ListHooks extends AbstractListRequest> { + + + public ListHooks(Properties parameters) { + super(parameters, true); + } + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.Hooks.value; + } + + @Override + public String getName() { + return Xplenty.Resource.Hooks.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/src/main/java/com/xplenty/api/request/hook/PingHook.java b/src/main/java/com/xplenty/api/request/hook/PingHook.java new file mode 100644 index 0000000..0da7d57 --- /dev/null +++ b/src/main/java/com/xplenty/api/request/hook/PingHook.java @@ -0,0 +1,27 @@ +package com.xplenty.api.request.hook; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.model.Hook; +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 PingHook extends AbstractInfoRequest { + public PingHook(long entityId) { + super(entityId); + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.PingHook.format(String.valueOf(entityId)); + } + + @Override + public String getName() { + return Xplenty.Resource.PingHook.name; + } +} diff --git a/src/main/java/com/xplenty/api/request/hook/ToggleHook.java b/src/main/java/com/xplenty/api/request/hook/ToggleHook.java new file mode 100644 index 0000000..860f94f --- /dev/null +++ b/src/main/java/com/xplenty/api/request/hook/ToggleHook.java @@ -0,0 +1,39 @@ +package com.xplenty.api.request.hook; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.Hook; +import com.xplenty.api.request.AbstractManipulationRequest; + +/** + * Enable/Disable Web hook + * Author: Xardas + * Date: 05.01.16 + * Time: 17:28 + */ +public class ToggleHook extends AbstractManipulationRequest { + + public ToggleHook(Long id, Boolean active) { + super(new Hook(id, active)); + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.UpdateHook.format(entity.getId().toString()); + } + + @Override + public String getName() { + return Xplenty.Resource.UpdateHook.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.PUT; + } +} diff --git a/src/main/java/com/xplenty/api/request/hook/UpdateHook.java b/src/main/java/com/xplenty/api/request/hook/UpdateHook.java new file mode 100644 index 0000000..26dc396 --- /dev/null +++ b/src/main/java/com/xplenty/api/request/hook/UpdateHook.java @@ -0,0 +1,63 @@ +package com.xplenty.api.request.hook; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.Http; +import com.xplenty.api.model.Hook; +import com.xplenty.api.model.HookEvent; +import com.xplenty.api.model.HookSettings; +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 UpdateHook extends AbstractManipulationRequest { + + public UpdateHook(Long id, String name, HookSettings settings, List events) { + super(null); + List convEvents = events.size() > 0 ? new ArrayList(events.size()) : null; + for (String event : events) { + com.xplenty.api.model.HookEvent hookEvent = new com.xplenty.api.model.HookEvent(event); + convEvents.add(hookEvent); + } + Hook entity = new Hook(id, name, settings, convEvents); + setEntity(entity); + } + + public UpdateHook(Long id, String name, List events, HookSettings settings) { + super(null); + List convEvents = events.size() > 0 ? new ArrayList(events.size()) : null; + for (Xplenty.HookEvent event : events) { + com.xplenty.api.model.HookEvent hookEvent = new com.xplenty.api.model.HookEvent(event.toString()); + convEvents.add(hookEvent); + } + + Hook entity = new Hook(id, name, settings, convEvents); + setEntity(entity); + } + + @Override + protected String getPackKey() { + return null; + } + + @Override + protected String getEndpoint() { + return Xplenty.Resource.UpdateHook.format(entity.getId().toString()); + } + + @Override + public String getName() { + return Xplenty.Resource.UpdateHook.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.PUT; + } +} diff --git a/src/main/java/com/xplenty/api/request/job/JobExecutionVariables.java b/src/main/java/com/xplenty/api/request/job/JobExecutionVariables.java new file mode 100644 index 0000000..e2ea35e --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/job/JobInfo.java b/src/main/java/com/xplenty/api/request/job/JobInfo.java new file mode 100644 index 0000000..fdc6187 --- /dev/null +++ b/src/main/java/com/xplenty/api/request/job/JobInfo.java @@ -0,0 +1,42 @@ +/** + * + */ +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 + * + */ +public class JobInfo extends AbstractInfoRequest { + private final boolean includeCluster; + private final boolean includePackage; + + public JobInfo(long entityId, boolean includeCluster, boolean includePackage) { + super(entityId); + this.includeCluster = includeCluster; + this.includePackage = includePackage; + } + + @Override + public String getName() { + return Xplenty.Resource.Job.name; + } + + @Override + public String getEndpoint() { + 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/src/main/java/com/xplenty/api/request/job/JobLogs.java b/src/main/java/com/xplenty/api/request/job/JobLogs.java new file mode 100644 index 0000000..6124ace --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/job/JobPreviewOutput.java b/src/main/java/com/xplenty/api/request/job/JobPreviewOutput.java new file mode 100644 index 0000000..bfb86a3 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/job/ListJobs.java b/src/main/java/com/xplenty/api/request/job/ListJobs.java new file mode 100644 index 0000000..0d945bd --- /dev/null +++ b/src/main/java/com/xplenty/api/request/job/ListJobs.java @@ -0,0 +1,61 @@ +/** + * + */ +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.model.Job; +import com.xplenty.api.request.AbstractListRequest; + +import java.util.List; +import java.util.Properties; + +/** + * @author Yuriy Kovalek + * + */ +public class ListJobs extends AbstractListRequest> { + public static final String PARAMETER_INCLUDE = "include"; + + public ListJobs(Properties params) { + super(params, true); + validateParameters(params); + } + + private void validateParameters(Properties params) { + 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 + public String getName() { + return Xplenty.Resource.Jobs.name; + } + + + @Override + protected String getEndpointRoot() { + return Xplenty.Resource.Jobs.value; + } + + @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/RunJob.java b/src/main/java/com/xplenty/api/request/job/RunJob.java similarity index 76% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/RunJob.java rename to src/main/java/com/xplenty/api/request/job/RunJob.java index d498b5c..ed05a7d 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/RunJob.java +++ b/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.util.Http; -import com.xplenty.api.util.Http.Method; +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/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 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/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/src/main/java/com/xplenty/api/request/member/CreateMember.java b/src/main/java/com/xplenty/api/request/member/CreateMember.java new file mode 100644 index 0000000..807508e --- /dev/null +++ b/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, String name) { + super(new Member(email, role, name)); + } + + @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/src/main/java/com/xplenty/api/request/member/DeleteMember.java b/src/main/java/com/xplenty/api/request/member/DeleteMember.java new file mode 100644 index 0000000..c632ad2 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/member/ListMembers.java b/src/main/java/com/xplenty/api/request/member/ListMembers.java new file mode 100644 index 0000000..b841571 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/member/MemberInfo.java b/src/main/java/com/xplenty/api/request/member/MemberInfo.java new file mode 100644 index 0000000..707bc86 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/member/SetMemberRole.java b/src/main/java/com/xplenty/api/request/member/SetMemberRole.java new file mode 100644 index 0000000..12f69fe --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/misc/LikeProductUpdate.java b/src/main/java/com/xplenty/api/request/misc/LikeProductUpdate.java new file mode 100644 index 0000000..4b30931 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/misc/ListProductUpdates.java b/src/main/java/com/xplenty/api/request/misc/ListProductUpdates.java new file mode 100644 index 0000000..3bb4f49 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/misc/ListRegions.java b/src/main/java/com/xplenty/api/request/misc/ListRegions.java new file mode 100644 index 0000000..857482b --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/misc/ListStacks.java b/src/main/java/com/xplenty/api/request/misc/ListStacks.java new file mode 100644 index 0000000..4cac5a1 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/misc/ListSystemVariables.java b/src/main/java/com/xplenty/api/request/misc/ListSystemVariables.java new file mode 100644 index 0000000..f4cbe62 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/misc/ListTimezones.java b/src/main/java/com/xplenty/api/request/misc/ListTimezones.java new file mode 100644 index 0000000..68e2fa9 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/public_key/CreatePublicKey.java b/src/main/java/com/xplenty/api/request/public_key/CreatePublicKey.java new file mode 100644 index 0000000..b08a28a --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/public_key/DeletePublicKey.java b/src/main/java/com/xplenty/api/request/public_key/DeletePublicKey.java new file mode 100644 index 0000000..5841201 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/public_key/ListPublicKeys.java b/src/main/java/com/xplenty/api/request/public_key/ListPublicKeys.java new file mode 100644 index 0000000..e634df8 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/public_key/PublicKeyInfo.java b/src/main/java/com/xplenty/api/request/public_key/PublicKeyInfo.java new file mode 100644 index 0000000..1e75cb0 --- /dev/null +++ b/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/main/java/com/xplenty/api/request/CloneSchedule.java b/src/main/java/com/xplenty/api/request/schedule/CloneSchedule.java similarity index 71% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/CloneSchedule.java rename to src/main/java/com/xplenty/api/request/schedule/CloneSchedule.java index 49708f2..17c5491 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CloneSchedule.java +++ b/src/main/java/com/xplenty/api/request/schedule/CloneSchedule.java @@ -1,18 +1,18 @@ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; -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; +import com.xplenty.api.request.AbstractRequest; /** * Author: Xardas * Date: 18.12.15 * Time: 20:25 */ -public class CloneSchedule implements Request { +public class CloneSchedule extends AbstractRequest { private final Long entityId; @@ -21,10 +21,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/CreateSchedule.java b/src/main/java/com/xplenty/api/request/schedule/CreateSchedule.java similarity index 83% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateSchedule.java rename to src/main/java/com/xplenty/api/request/schedule/CreateSchedule.java index c117cbb..499c8a3 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/CreateSchedule.java +++ b/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.util.Http; +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/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 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/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/src/main/java/com/xplenty/api/request/schedule/ListSchedules.java similarity index 50% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/ListSchedules.java rename to src/main/java/com/xplenty/api/request/schedule/ListSchedules.java index 10216fa..0e23597 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListSchedules.java +++ b/src/main/java/com/xplenty/api/request/schedule/ListSchedules.java @@ -1,17 +1,14 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; 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.model.Cluster; +import com.xplenty.api.http.Response; import com.xplenty.api.model.Schedule; -import com.xplenty.api.util.Http.MediaType; -import com.xplenty.api.util.Http.Method; +import com.xplenty.api.request.AbstractListRequest; import java.util.List; import java.util.Properties; @@ -23,7 +20,7 @@ * Date: 16.12.15 * Time: 18:08 */ -public class ListSchedules extends AbstractParametrizedRequest> { +public class ListSchedules extends AbstractListRequest> { public ListSchedules(Properties params) { @@ -40,25 +37,12 @@ private void validateParameters(Properties params) { } @Override - public Method getHttpMethod() { - return Method.GET; - } - - @Override - public MediaType getResponseType() { - return MediaType.JSON; - } - - - - @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 @@ -66,16 +50,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/ScheduleInfo.java b/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 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/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/src/main/java/com/xplenty/api/request/schedule/UpdateSchedule.java similarity index 84% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateSchedule.java rename to src/main/java/com/xplenty/api/request/schedule/UpdateSchedule.java index 0e825b0..5f7f7bc 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/UpdateSchedule.java +++ b/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.util.Http; +import com.xplenty.api.http.Http; +import com.xplenty.api.request.AbstractManipulationRequest; /** * Author: Xardas diff --git a/src/main/java/com/xplenty/api/request/subscription/ListPlans.java b/src/main/java/com/xplenty/api/request/subscription/ListPlans.java new file mode 100644 index 0000000..d71916b --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/subscription/PaymentMehodInfo.java b/src/main/java/com/xplenty/api/request/subscription/PaymentMehodInfo.java new file mode 100644 index 0000000..b76a69d --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/subscription/SubscriptionInfo.java b/src/main/java/com/xplenty/api/request/subscription/SubscriptionInfo.java new file mode 100644 index 0000000..09414e2 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlan.java b/src/main/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlan.java new file mode 100644 index 0000000..2d7cb9a --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/user/CurrentUserInfo.java b/src/main/java/com/xplenty/api/request/user/CurrentUserInfo.java new file mode 100644 index 0000000..754ee3d --- /dev/null +++ b/src/main/java/com/xplenty/api/request/user/CurrentUserInfo.java @@ -0,0 +1,48 @@ +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. + * 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/src/main/java/com/xplenty/api/request/user/ListNotifications.java b/src/main/java/com/xplenty/api/request/user/ListNotifications.java new file mode 100644 index 0000000..7fb6fbb --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/user/MarkNotificationsRead.java b/src/main/java/com/xplenty/api/request/user/MarkNotificationsRead.java new file mode 100644 index 0000000..18331fb --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/user/ResetUserPassword.java b/src/main/java/com/xplenty/api/request/user/ResetUserPassword.java new file mode 100644 index 0000000..ad36f02 --- /dev/null +++ b/src/main/java/com/xplenty/api/request/user/ResetUserPassword.java @@ -0,0 +1,54 @@ +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.model.User; +import com.xplenty.api.request.AbstractManipulationRequest; + +/** + * Sends user password reset instructions + * + * Author: Xardas + * Date: 23.02.16 + * Time: 18:40 + */ +public class ResetUserPassword extends AbstractManipulationRequest { + + public ResetUserPassword(String email) { + super(null); + User user = new User(); + user.setEmail(email); + setEntity(user); + } + + @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.ResetUserPassword.value; + } + + @Override + public String getName() { + return Xplenty.Resource.ResetUserPassword.name; + } + + @Override + public Http.Method getHttpMethod() { + return Http.Method.POST; + } + + @Override + public User getResponse(Response response) { + return null; + } +} diff --git a/src/main/java/com/xplenty/api/request/user/UpdateCurrentUser.java b/src/main/java/com/xplenty/api/request/user/UpdateCurrentUser.java new file mode 100644 index 0000000..343616d --- /dev/null +++ b/src/main/java/com/xplenty/api/request/user/UpdateCurrentUser.java @@ -0,0 +1,43 @@ +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 + * 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/src/main/java/com/xplenty/api/request/watching/AddClusterWatcher.java similarity index 60% 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 93c277a..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 @@ -1,17 +1,16 @@ 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 com.xplenty.api.request.AbstractRequest; -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 { +public class AddClusterWatcher extends AbstractRequest { private Long _clusterId; public AddClusterWatcher(Long subjectId) { @@ -33,16 +32,15 @@ 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; } @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/src/main/java/com/xplenty/api/request/watching/AddJobWatcher.java similarity index 63% 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 16ae594..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 @@ -1,17 +1,16 @@ 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 com.xplenty.api.request.AbstractRequest; -import static com.xplenty.api.util.Http.Method.POST; +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) { @@ -35,16 +34,15 @@ public String getEndpoint() { } @Override - public boolean hasBody() { return true; } + public boolean hasBody() { return false; } @Override 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/src/main/java/com/xplenty/api/request/watching/ListWatchers.java similarity index 76% 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 index 58c380a..a136a00 100644 --- 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 @@ -1,21 +1,20 @@ 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 com.xplenty.api.request.AbstractRequest; 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> { +public class ListWatchers extends AbstractRequest> { private SubjectType _kind = null; private Long _subjectId = 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/src/main/java/com/xplenty/api/request/watching/WatchingStop.java similarity index 70% 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 index 3c920dd..a31bd2e 100644 --- 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 @@ -1,15 +1,16 @@ 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.request.Request; -import com.xplenty.api.util.Http; +import com.xplenty.api.http.Http; +import com.xplenty.api.http.Response; +import com.xplenty.api.request.AbstractRequest; -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 { +public class WatchingStop extends AbstractRequest { private Long _subjectId; private SubjectType _kind; @@ -45,11 +46,11 @@ 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, ""); + throw new RequestFailedException("204 expected, but something went wrong", response.getStatus(), ""); } } diff --git a/src/main/java/com/xplenty/api/request/xpackage/CreatePackage.java b/src/main/java/com/xplenty/api/request/xpackage/CreatePackage.java new file mode 100644 index 0000000..cb34942 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/xpackage/DeletePackage.java b/src/main/java/com/xplenty/api/request/xpackage/DeletePackage.java new file mode 100644 index 0000000..6f1b67d --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/xpackage/ListPackageTemplates.java b/src/main/java/com/xplenty/api/request/xpackage/ListPackageTemplates.java new file mode 100644 index 0000000..b576a71 --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java b/src/main/java/com/xplenty/api/request/xpackage/ListPackageValidations.java new file mode 100644 index 0000000..6227d6f --- /dev/null +++ b/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(long packageId, Properties parameters) { + 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/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java b/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java new file mode 100644 index 0000000..9f2d534 --- /dev/null +++ b/src/main/java/com/xplenty/api/request/xpackage/ListPackages.java @@ -0,0 +1,65 @@ +/** + * + */ +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; + + +/** + * Request for retrieval of all available packages + * Author: Xardas + * Date: 16.12.15 + * 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); + 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)); + } + 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 + protected String getEndpointRoot() { + return Xplenty.Resource.Packages.value; + } + + @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.Packages.name; + } + +} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/request/PackageInfo.java b/src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java similarity index 58% rename from xplenty.jar-core/src/main/java/com/xplenty/api/request/PackageInfo.java rename to src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java index 6c4a722..20f6342 100644 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/PackageInfo.java +++ b/src/main/java/com/xplenty/api/request/xpackage/PackageInfo.java @@ -1,6 +1,7 @@ -package com.xplenty.api.request; +package com.xplenty.api.request.xpackage; import com.xplenty.api.Xplenty; +import com.xplenty.api.request.AbstractInfoRequest; /** * Author: Xardas @@ -8,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 @@ -19,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/src/main/java/com/xplenty/api/request/xpackage/PackageValidationInfo.java b/src/main/java/com/xplenty/api/request/xpackage/PackageValidationInfo.java new file mode 100644 index 0000000..b39cc3e --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/xpackage/RunPackageValidation.java b/src/main/java/com/xplenty/api/request/xpackage/RunPackageValidation.java new file mode 100644 index 0000000..0531fbf --- /dev/null +++ b/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/src/main/java/com/xplenty/api/request/xpackage/UpdatePackage.java b/src/main/java/com/xplenty/api/request/xpackage/UpdatePackage.java new file mode 100644 index 0000000..811ddfe --- /dev/null +++ b/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; + } +} 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 87% rename from xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java rename to src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java index 2023270..4e5f438 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java +++ b/src/test/java/com/xplenty/api/WatchersTestAgainstMockServer.java @@ -1,12 +1,13 @@ package com.xplenty.api; +import com.xplenty.api.http.ClientBuilder; 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); + ClientBuilder builder = new ClientBuilder().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/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/src/test/java/com/xplenty/api/exception/ExceptionTest.java b/src/test/java/com/xplenty/api/exception/ExceptionTest.java new file mode 100644 index 0000000..122344a --- /dev/null +++ b/src/test/java/com/xplenty/api/exception/ExceptionTest.java @@ -0,0 +1,28 @@ +package com.xplenty.api.exception; + +import com.xplenty.api.exceptions.AuthFailedException; +import com.xplenty.api.exceptions.RequestFailedException; +import com.xplenty.api.http.Http; +import junit.framework.TestCase; + +/** + * Author: Xardas + * Date: 24.01.16 + * Time: 6:44 + */ +public class ExceptionTest extends TestCase { + public void testAuthFailedException() { + AuthFailedException afe = new AuthFailedException(Http.ResponseStatus.HTTP_401, "bad auth"); + assertEquals("bad auth", afe.getResponse()); + assertEquals(Http.ResponseStatus.HTTP_401.getCode(), afe.getStatus()); + assertEquals(Http.ResponseStatus.HTTP_401.getDescription(), afe.getStatusDescription()); + } + + public void testRequestFailedException() { + RequestFailedException afe = new RequestFailedException("Bad things really happen", Http.ResponseStatus.HTTP_406, "can't accept your request"); + assertEquals("can't accept your request", afe.getResponse()); + assertEquals(Http.ResponseStatus.HTTP_406.getCode(), afe.getStatus()); + assertEquals(Http.ResponseStatus.HTTP_406.getDescription(), afe.getStatusDescription()); + } + +} diff --git a/src/test/java/com/xplenty/api/http/ConsoleNettyLoggerTest.java b/src/test/java/com/xplenty/api/http/ConsoleNettyLoggerTest.java new file mode 100644 index 0000000..d8aca8a --- /dev/null +++ b/src/test/java/com/xplenty/api/http/ConsoleNettyLoggerTest.java @@ -0,0 +1,160 @@ +package com.xplenty.api.http; + +import junit.framework.TestCase; +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.*; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.net.InetSocketAddress; +import java.net.SocketAddress; + + +/** + * Author: Xardas + * Date: 24.01.16 + * Time: 7:42 + */ +public class ConsoleNettyLoggerTest extends TestCase { + public void testLogger() throws Exception { + + String lineSeparator = System.getProperty("line.separator"); + ConsoleNettyLogger cne = new ConsoleNettyLogger(false); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + PrintStream myps = new PrintStream(bos); + System.setErr(myps); + final DefaultChannelPipeline pipeline = new DefaultChannelPipeline(); + final Channel chan = new AbstractChannel(null, null, pipeline, new ChannelSink() { + @Override + public void eventSunk(ChannelPipeline pipeline, ChannelEvent e) throws Exception { + + } + + @Override + public void exceptionCaught(ChannelPipeline pipeline, ChannelEvent e, ChannelPipelineException cause) throws Exception { + + } + + @Override + public ChannelFuture execute(ChannelPipeline pipeline, Runnable task) { + return null; + } + }) { + @Override + public ChannelConfig getConfig() { + return null; + } + + @Override + public boolean isBound() { + return false; + } + + @Override + public boolean isConnected() { + return false; + } + + @Override + public SocketAddress getLocalAddress() { + return new InetSocketAddress("localhost", 888); + } + + @Override + public SocketAddress getRemoteAddress() { + return new InetSocketAddress("localhost", 666); + } + }; + ChannelStateEvent cse = new ChannelStateEvent() { + @Override + public ChannelState getState() { + return ChannelState.OPEN; + } + + @Override + public Object getValue() { + return "[id 666]"; + } + + @Override + public Channel getChannel() { + return chan; + } + + @Override + public ChannelFuture getFuture() { + return new DefaultChannelFuture(chan, true); + } + + @Override + public String toString() { + return String.format("[id: 0x%s]: state %s", Integer.toHexString(chan.getId()), getState()); + } + }; + cne.handleDownstream(pipeline.getContext(cne), cse); + String res = bos.toString(); + assertTrue(res.endsWith(String.format("[id: 0x%s]: state OPEN%s", Integer.toHexString(chan.getId()), lineSeparator))); + + bos = new ByteArrayOutputStream(); + myps = new PrintStream(bos); + System.setErr(myps); + cne.handleUpstream(pipeline.getContext(cne), cse); + res = bos.toString(); + assertTrue(res.endsWith(String.format("[id: 0x%s]: state OPEN%s", Integer.toHexString(chan.getId()), lineSeparator))); + + bos = new ByteArrayOutputStream(); + myps = new PrintStream(bos); + System.setErr(myps); + cne.log(cse, true); + res = bos.toString(); + assertTrue(res.endsWith(String.format("[id: 0x%s]: state OPEN%s", Integer.toHexString(chan.getId()), lineSeparator))); + + bos = new ByteArrayOutputStream(); + myps = new PrintStream(bos); + System.setErr(myps); + cne.logStateMessage("some important log msg"); + res = bos.toString(); + assertTrue(res.endsWith("some important log msg" + lineSeparator)); + + final ChannelBuffer cb = ChannelBuffers.dynamicBuffer(8000); + cb.writeBytes("some important log msg".getBytes()); + cb.resetReaderIndex(); + + bos = new ByteArrayOutputStream(); + myps = new PrintStream(bos); + System.setErr(myps); + cne.logMessage(cb, false); + res = bos.toString(); + assertTrue(res.endsWith(String.format(">>>%ssome important log msg%s>>>%s", lineSeparator, lineSeparator, lineSeparator))); + + bos = new ByteArrayOutputStream(); + myps = new PrintStream(bos); + System.setErr(myps); + cne.logMessage(cb, true); + res = bos.toString(); + assertTrue(res.endsWith(String.format("<<<%ssome important log msg%s<<<%s", lineSeparator, lineSeparator, lineSeparator))); + + bos = new ByteArrayOutputStream(); + myps = new PrintStream(bos); + System.setErr(myps); + cne.logHttpMessage(cb, true); + res = bos.toString(); + assertTrue(res.endsWith(String.format("<<<%ssome important log msg%s[DECODED]<<<%s", lineSeparator, lineSeparator, lineSeparator))); + + bos = new ByteArrayOutputStream(); + myps = new PrintStream(bos); + System.setErr(myps); + cne.logHttpMessage(cb, false); + res = bos.toString(); + assertTrue(res.endsWith(String.format(">>>%ssome important log msg%s[DECODED]>>>%s", lineSeparator, lineSeparator, lineSeparator))); + + + bos = new ByteArrayOutputStream(); + myps = new PrintStream(bos); + System.setErr(myps); + cne.logException(new Exception("bad things")); + res = bos.toString(); + assertTrue(res.endsWith(String.format("bad things%s", lineSeparator))); + } +} diff --git a/src/test/java/com/xplenty/api/http/JerseyClientTest.java b/src/test/java/com/xplenty/api/http/JerseyClientTest.java new file mode 100644 index 0000000..7d7c3aa --- /dev/null +++ b/src/test/java/com/xplenty/api/http/JerseyClientTest.java @@ -0,0 +1,50 @@ +package com.xplenty.api.http; + +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.core.header.InBoundHeaders; +import com.xplenty.api.Xplenty; +import com.xplenty.api.request.user.CurrentUserInfo; +import junit.framework.TestCase; +import org.jboss.netty.handler.codec.http.HttpHeaders; + +import javax.ws.rs.core.MultivaluedMap; +import java.util.Map; + +/** + * Author: Xardas + * Date: 24.01.16 + * Time: 9:36 + */ +public class JerseyClientTest extends TestCase { + public void testBuilder() { + JerseyClient client = new JerseyClient("a", "b", "c", Http.Protocol.Https, Xplenty.Version.V2, 10, true); + assertEquals("a", client.getAccountName()); + assertEquals("b", client.getApiKey()); + assertEquals("c", client.getHost()); + assertEquals(Http.Protocol.Https, client.getProtocol()); + assertEquals(Xplenty.Version.V2, client.getVersion()); + assertEquals(10, client.getTimeout()); + } + + public void testJerseyClientInnerMethods() throws Exception { + JerseyClient client = new JerseyClient("a", "b", "c", Http.Protocol.Https, Xplenty.Version.V2, 10, true); + MultivaluedMap jerseyHeaders = new InBoundHeaders(); + jerseyHeaders.add(HttpHeaders.Names.ACCEPT_CHARSET, "utf8"); + jerseyHeaders.add(HttpHeaders.Names.CONTENT_TYPE, "json"); + Map headers = client.convertJerseyHeaders(jerseyHeaders); + assertNotNull(headers); + assertEquals(2, headers.size()); + assertEquals("utf8", headers.get(HttpHeaders.Names.ACCEPT_CHARSET)); + assertEquals("json", headers.get(HttpHeaders.Names.CONTENT_TYPE)); + + assertEquals("https://a/b", client.getMethodURL("a/b")); + assertEquals("http://a/b", client.getMethodURL("http://a/b")); + + CurrentUserInfo userinfo = new CurrentUserInfo(null); + WebResource.Builder jerseyBuilder = client.getConfiguredResource(userinfo); + assertNotNull(jerseyBuilder); + + client.shutdown(); + } + +} diff --git a/src/test/java/com/xplenty/api/http/ResponseTest.java b/src/test/java/com/xplenty/api/http/ResponseTest.java new file mode 100644 index 0000000..6ea25e9 --- /dev/null +++ b/src/test/java/com/xplenty/api/http/ResponseTest.java @@ -0,0 +1,158 @@ +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.Cluster; +import com.xplenty.api.model.PublicKey; +import junit.framework.TestCase; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Author: Xardas + * Date: 24.01.16 + * Time: 6:54 + */ +public class ResponseTest extends TestCase { + public void testValidate() { + Response resp = new Response("abc", 403, new HashMap()) { + @Override + public T getContent(TypeReference typeReference) throws XplentyAPIException { + return null; + } + + @Override + public T getContent(Class typeReference) throws XplentyAPIException { + return null; + } + }; + + try { + resp.validate(""); + } catch (Exception ex) { + assertEquals(RequestFailedException.class, ex.getClass()); + RequestFailedException rfex = (RequestFailedException) ex; + assertEquals(Http.ResponseStatus.HTTP_403.getCode(), rfex.getStatus()); + assertEquals(Http.ResponseStatus.HTTP_403.getDescription(), rfex.getStatusDescription()); + } + + resp = new Response("abc", 401, new HashMap()) { + @Override + public T getContent(TypeReference typeReference) throws XplentyAPIException { + return null; + } + + @Override + public T getContent(Class typeReference) throws XplentyAPIException { + return null; + } + }; + + try { + resp.validate(""); + } catch (Exception ex) { + assertEquals(AuthFailedException.class, ex.getClass()); + AuthFailedException rfex = (AuthFailedException) ex; + assertEquals(Http.ResponseStatus.HTTP_401.getCode(), rfex.getStatus()); + assertEquals(Http.ResponseStatus.HTTP_401.getDescription(), rfex.getStatusDescription()); + } + + resp = new Response("abc", 200, new HashMap()) { + @Override + public T getContent(TypeReference typeReference) throws XplentyAPIException { + return null; + } + + @Override + public T getContent(Class typeReference) throws XplentyAPIException { + return null; + } + }; + + resp.validate(""); + assertTrue(resp.isValid()); + } + + public void testForContentType() { + Response resp = Response.forContentType(Http.MediaType.JSON, "dd", 200, new HashMap()); + assertNotNull(resp); + assertEquals(JsonResponse.class, resp.getClass()); + assertTrue(resp.isValid()); + assertEquals("dd", resp.getRawContent()); + assertNotNull(resp.getHeaders()); + assertEquals(Http.ResponseStatus.HTTP_200, resp.getStatus()); + + resp = Response.forContentType(Http.MediaType.JSON, "dd", 404, new HashMap()); + assertFalse(resp.isValid()); + + } + + public void testCheckTypedInfo() { + Response resp = Response.forContentType(Http.MediaType.JSON, "dd", 200, new HashMap()); + try { + resp.checkTypedInfo(new TypeReference>>() {}); + } catch (Exception ex) { + assertEquals(XplentyAPIException.class, ex.getClass()); + assertEquals(Response.INVALID_TYPE_INFORMATION, ex.getMessage()); + } + + try { + resp.checkTypedInfo(new TypeReference>() {}); + } catch (Exception ex) { + assertEquals(XplentyAPIException.class, ex.getClass()); + assertEquals(Response.INVALID_TYPE_INFORMATION, ex.getMessage()); + } + + try { + resp.checkTypedInfo(new TypeReference() {}); + } catch (Exception ex) { + assertEquals(XplentyAPIException.class, ex.getClass()); + assertEquals(Response.INVALID_TYPE_INFORMATION, ex.getMessage()); + } + + resp.checkTypedInfo(new TypeReference>() {}); + resp.checkTypedInfo(new TypeReference>() {}); + resp.checkTypedInfo(new TypeReference>() {}); + + } + + public void testJsonResponse() { + JsonResponse resp = new JsonResponse("{\"a\" : 9, \"b\": \"omg\"}", 200, new HashMap()); + assertEquals("{\"a\" : 9, \"b\": \"omg\"}", resp.getRawContent()); + assertNotNull(resp.getHeaders()); + assertEquals(Http.ResponseStatus.HTTP_200, resp.getStatus()); + assertTrue(resp.isValid()); + resp.validate("test"); + Map map = resp.getContent(new TypeReference>() {}); + assertNotNull(map); + assertEquals("9", map.get("a")); + assertEquals("omg", map.get("b")); + try { + resp.getContent(new TypeReference>() {}); + } catch (Exception ex) { + // json doesn't contain cluster object + assertEquals(IllegalArgumentException.class, ex.getClass()); + } + + try { + resp.getContent(new TypeReference>() {}); + } catch (Exception ex) { + assertEquals(XplentyAPIException.class, ex.getClass()); + } + + + resp = new JsonResponse("{\"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\"}", 200, new HashMap()); + assertNotNull(resp.getHeaders()); + assertEquals(Http.ResponseStatus.HTTP_200, resp.getStatus()); + assertTrue(resp.isValid()); + resp.validate("test"); + PublicKey pk = resp.getContent(PublicKey.class); + assertNotNull(pk); + assertEquals(33, pk.getId().longValue()); + + } +} diff --git a/src/test/java/com/xplenty/api/http/SSLEngineDefaultImplTest.java b/src/test/java/com/xplenty/api/http/SSLEngineDefaultImplTest.java new file mode 100644 index 0000000..929453a --- /dev/null +++ b/src/test/java/com/xplenty/api/http/SSLEngineDefaultImplTest.java @@ -0,0 +1,20 @@ +package com.xplenty.api.http; + +import junit.framework.TestCase; + +import javax.net.ssl.SSLContext; + +/** + * Author: Xardas + * Date: 24.01.16 + * Time: 9:53 + */ +public class SSLEngineDefaultImplTest extends TestCase { + public void testEngine() { + SSLContext context = SSLEngineDefaultImpl.getSSLContext(); + assertNotNull(context); + assertEquals("TLS", context.getProtocol()); + assertTrue(context.getProvider().entrySet().size() > 0); + } + +} diff --git a/src/test/java/com/xplenty/api/http/SyncNettyClientTest.java b/src/test/java/com/xplenty/api/http/SyncNettyClientTest.java new file mode 100644 index 0000000..bfbb573 --- /dev/null +++ b/src/test/java/com/xplenty/api/http/SyncNettyClientTest.java @@ -0,0 +1,185 @@ +package com.xplenty.api.http; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.exceptions.XplentyAPIException; +import junit.framework.TestCase; +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.*; +import org.jboss.netty.handler.codec.http.*; + +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.net.URL; +import java.util.Map; + +/** + * Author: Xardas + * Date: 24.01.16 + * Time: 8:52 + */ +public class SyncNettyClientTest extends TestCase { + + public void testBuilder() { + SyncNettyClient client = new SyncNettyClient("a", "b", "c", Http.Protocol.Https, Xplenty.Version.V2, 10, true); + assertEquals("a", client.getAccountName()); + assertEquals("b", client.getApiKey()); + assertEquals("c", client.getHost()); + assertEquals(Http.Protocol.Https, client.getProtocol()); + assertEquals(Xplenty.Version.V2, client.getVersion()); + assertEquals(10, client.getTimeout()); + } + + public void testMessageRecieved() throws Exception { + final DefaultChannelPipeline pipeline = new DefaultChannelPipeline(); + final Channel chan = new AbstractChannel(null, null, pipeline, new ChannelSink() { + + + @Override + public void eventSunk(ChannelPipeline pipeline, ChannelEvent e) throws Exception { + + } + + @Override + public void exceptionCaught(ChannelPipeline pipeline, ChannelEvent e, ChannelPipelineException cause) throws Exception { + + } + + @Override + public ChannelFuture execute(ChannelPipeline pipeline, Runnable task) { + return null; + } + }) { + + @Override + public ChannelConfig getConfig() { + return null; + } + + @Override + public boolean isBound() { + return false; + } + + @Override + public boolean isConnected() { + return false; + } + + @Override + public SocketAddress getLocalAddress() { + return new InetSocketAddress("localhost", 888); + } + + @Override + public SocketAddress getRemoteAddress() { + return new InetSocketAddress("localhost", 666); + } + }; + + ChannelHandlerContext ctx = new ChannelHandlerContext() { + final SyncNettyClient.NettyResponse nettyResponse = new SyncNettyClient.NettyResponse(); + + @Override + public Channel getChannel() { + chan.setAttachment(nettyResponse); + return chan; + } + + @Override + public ChannelPipeline getPipeline() { + return pipeline; + } + + @Override + public String getName() { + return null; + } + + @Override + public ChannelHandler getHandler() { + return null; + } + + @Override + public boolean canHandleUpstream() { + return false; + } + + @Override + public boolean canHandleDownstream() { + return false; + } + + @Override + public void sendUpstream(ChannelEvent e) { + + } + + @Override + public void sendDownstream(ChannelEvent e) { + + } + + @Override + public Object getAttachment() { + + return nettyResponse; + } + + @Override + public void setAttachment(Object attachment) { + + } + }; + + SyncNettyClient client = new SyncNettyClient("a", "b", "c", Http.Protocol.Https, Xplenty.Version.V2, 10, true); + + DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); + final ChannelBuffer cb = ChannelBuffers.dynamicBuffer(8000); + cb.writeBytes("{\"a\" : 9}".getBytes()); + cb.resetReaderIndex(); + response.setContent(cb); + MessageEvent me = new DownstreamMessageEvent(chan, new DefaultChannelFuture(chan, true), response, null); + client.messageReceived(ctx, me); + + SyncNettyClient.NettyResponse processedResp = (SyncNettyClient.NettyResponse) ctx.getAttachment(); + assertEquals(200, processedResp.getResponse().getStatus().getCode()); + assertEquals("{\"a\" : 9}", client.channelBuffer2String(processedResp.getResponse().getContent())); + + try { + client.exceptionCaught(ctx, new DefaultExceptionEvent(chan, new Exception("some exception"))); + } catch (Exception ex) { + assertEquals(XplentyAPIException.class, ex.getClass()); + assertEquals("Exception while communicating with remote Xplenty server", ex.getMessage()); + } + + client.shutdown(); + + } + + public void testInnerMethods() throws Exception { + SyncNettyClient client = new SyncNettyClient("a", "b", "c", Http.Protocol.Https, Xplenty.Version.V2, 10, true); + + assertEquals("ogg; version=2", client.getAcceptHeaderValue("ogg")); + assertEquals("https://a/b", client.getMethodURL("a/b")); + assertEquals("http://a/b", client.getMethodURL("http://a/b")); + assertEquals(SyncNettyClient.DEFAULT_HTTP_PORT, client.getPort(new URL("http://localhost/"))); + assertEquals(SyncNettyClient.DEFAULT_HTTPS_PORT, client.getPort(new URL("https://localhost/"))); + assertEquals(867, client.getPort(new URL("http://localhost:867/"))); + + assertEquals(HttpMethod.POST, client.convertRequestMethod(Http.Method.POST)); + assertEquals(HttpMethod.PUT, client.convertRequestMethod(Http.Method.PUT)); + assertEquals(HttpMethod.GET, client.convertRequestMethod(Http.Method.GET)); + assertEquals(HttpMethod.DELETE, client.convertRequestMethod(Http.Method.DELETE)); + + HttpHeaders nettyHeaders = new DefaultHttpHeaders(); + nettyHeaders.add(HttpHeaders.Names.ACCEPT_CHARSET, "utf8"); + nettyHeaders.add(HttpHeaders.Names.CONTENT_TYPE, "json"); + Map headers = client.convertNettyHeaders(nettyHeaders); + assertNotNull(headers); + assertEquals(2, headers.size()); + assertEquals("utf8", headers.get(HttpHeaders.Names.ACCEPT_CHARSET)); + assertEquals("json", headers.get(HttpHeaders.Names.CONTENT_TYPE)); + } +} 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/ITClusterTestAgainstMockServer.java b/src/test/java/com/xplenty/api/integration/ITClusterTestAgainstMockServer.java new file mode 100644 index 0000000..d255caf --- /dev/null +++ b/src/test/java/com/xplenty/api/integration/ITClusterTestAgainstMockServer.java @@ -0,0 +1,140 @@ +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.Cluster; +import com.xplenty.api.model.ClusterBootstrapAction; +import com.xplenty.api.model.ClusterInstance; +import com.xplenty.api.model.Creator; +import junit.framework.TestCase; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.List; + +public class ITClusterTestAgainstMockServer 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/cluster"; + private String apiKey = "dsfgsdfh"; + private String accountID = "testerAccount"; + private Long entityId = 666L; + + + @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.Jersey).withLogHttpCommunication(true).withTimeout(10); + api = new XplentyAPI(builder); + } + + public void testCreateCluster() throws Exception { + Cluster c = new Cluster().ofType(Xplenty.ClusterType.production).withAllowFallback(true).withDescription("somedesc"). + withMasterInstanceType("mastertype").withMasterSpotPercentage(0.7).withMasterSpotPrice(1.3).withNodes(7). + withRegion("Hogwarts").withSlaveInstanceType("slavetype").withSlaveSpotPercentage(0.3).withSlaveSpotPrice(1.1). + withStack("pinky-everest").withTerminateOnIdle(true).withTimeToIdle(2000L).withZone("GB").named("clustername"); + c = api.createCluster(c); + checkEntity(c); + } + + public void testUpdateCluster() throws Exception { + Cluster c = api.updateCluster(entityId, 15, "new name", "some new description", false, 12345L); + checkEntity(c); + } + + + public void testTerminateCluster() throws Exception { + Cluster c = api.terminateCluster(entityId); + checkEntity(c); + } + + public void testGetClusterInfo() throws Exception { + Cluster c = api.clusterInformation(entityId); + checkEntity(c); + } + + public void testListClusters() throws Exception { + List list = api.listClusters(); + assertNotNull(list); + assertTrue(list.size() > 0); + Cluster c = list.get(0); + checkEntity(c); + } + + public void testListClusterInstances() throws Exception { + List list = api.listClusterInstances(entityId); + assertNotNull(list); + assertTrue(list.size() > 0); + ClusterInstance c = list.get(0); + checkClusterInstance(c); + } + + private void checkEntity(Cluster c) throws ParseException { + assertNotNull(c); + assertEquals(entityId, c.getId()); + + assertEquals("brown-sea-277", c.getName()); + assertEquals("sea is brown in this time of the year", c.getDescription()); + assertEquals(Xplenty.ClusterStatus.terminated, c.getStatus()); + assertEquals(new Long(352), c.getOwnerId()); + assertEquals(new Integer(7), c.getNodes()); + assertEquals(new Long(3600), c.getTimeToIdle()); + assertEquals(Xplenty.ClusterType.production, c.getType()); + assertEquals(new Long(0), c.getRunningJobsCount()); + assertTrue(c.getTerminateOnIdle()); + assertTrue(c.getTerminatedOnIdle()); + assertEquals(dFormat.parse("2016-01-22T13:22:20Z"), c.getCreatedAt()); + assertEquals(dFormat.parse("2016-01-22T14:33:03Z"), c.getUpdatedAt()); + assertEquals(dFormat.parse("2016-01-22T13:31:07Z"), c.getAvailableSince()); + assertEquals(dFormat.parse("2016-01-22T14:33:02Z"), c.getTerminatedAt()); + assertEquals(dFormat.parse("2016-01-22T14:33:02Z"), c.getIdleSince()); + assertEquals(dFormat.parse("2016-01-22T13:31:06Z"), c.getLaunchedAt()); + assertEquals(String.format("https://localhost/%s/api/clusters/%s", accountID, entityId), c.getUrl()); + assertEquals(String.format("https://localhost/%s/clusters/%s", accountID, entityId), c.getHtmlUrl()); + assertTrue(c.getAllowFallback()); + assertNull(c.getMasterInstanceType()); + assertNull(c.getSlaveInstanceType()); + assertNull(c.getZone()); + assertEquals("amazon-web-services::eu-west-1", c.getRegion()); + assertEquals("pro", c.getPlanId()); + assertEquals("white-everest", c.getStack()); + assertNull(c.getMasterSpotPercentage()); + assertNull(c.getMasterSpotPrice()); + assertNull(c.getSlaveSpotPercentage()); + assertNull(c.getSlaveSpotPrice()); + Creator creator = c.getCreator(); + assertEquals("User", creator.getType()); + assertEquals(352, creator.getId().longValue()); + assertEquals("Alexey Gromov", creator.getDisplayName()); + assertEquals(String.format("https://localhost/%s/api/members/352", accountID), creator.getUrl()); + assertEquals(String.format("https://localhost/%s/settings/members/352", accountID), creator.getHtmlUrl()); + List bactions = c.getBootstrapActions(); + assertNotNull(bactions); + assertTrue(bactions.size() > 0); + ClusterBootstrapAction baction = bactions.get(0); + assertEquals("my/super/script.sh", baction.getScriptPath()); + List args = baction.getArgs(); + assertNotNull(args); + assertTrue(args.size() > 0); + assertEquals("arg1", args.get(0)); + } + + private void checkClusterInstance(ClusterInstance ci) throws ParseException { + assertNotNull(ci); + assertEquals("i-4d1b39a7", ci.getInstanceId()); + assertEquals("ip-10-124-29-23.ec2.internal", ci.getPrivateDns()); + assertEquals("ec2-55-27-210-201.compute-1.amazonaws.com", ci.getPublicDns()); + assertEquals("sometype", ci.getInstanceType()); + assertEquals("eu-west1", ci.getZone()); + assertEquals(String.format("https://localhost/%s/api/clusters/%s/instances/i-4d1b39a7", accountID, entityId), ci.getUrl()); + assertEquals(Xplenty.ClusterInstanceStatus.available, ci.getStatus()); + assertTrue(ci.getMaster()); + assertFalse(ci.getSpot()); + assertFalse(ci.getVpc()); + } +} 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/ITPackageTestAgainstMockServer.java b/src/test/java/com/xplenty/api/integration/ITPackageTestAgainstMockServer.java new file mode 100644 index 0000000..e6b522f --- /dev/null +++ b/src/test/java/com/xplenty/api/integration/ITPackageTestAgainstMockServer.java @@ -0,0 +1,159 @@ +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.*; +import com.xplenty.api.model.Package; +import junit.framework.TestCase; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ITPackageTestAgainstMockServer 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/package"; + private String apiKey = "dsfgsdfh"; + private String accountID = "testerAccount"; + private Long packageId = 666L; + private Long packageTemplateId = 333L; + private Long packageValidationId = 666L; + + + @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 testCreatePackage() throws Exception { + Map packVars = new HashMap<>(); + packVars.put("var_1", "val1"); + packVars.put("var_2", "super$$$\"complex'val\n\t"); + Package c = new Package().fromTemplate(3L).withDataFlow("someflow").withDescription("test description"). + withFlowType(Xplenty.PackageFlowType.workflow).withName("Test Pack").withSourcePackageId(111L). + withVariables(packVars); + c = api.createPackage(c); + checkPackage(c); + } + + public void testUpdatePackage() throws Exception { + Map packVars = new HashMap<>(); + packVars.put("var_1", "val1"); + packVars.put("var_2", "super$$$\"complex'val\n\t"); + Package c = new Package().withDataFlow("someflow").withDescription("test description"). + withFlowType(Xplenty.PackageFlowType.workflow).withName("Test Pack").withId(packageId). + withVariables(packVars); + c = api.updatePackage(c); + checkPackage(c); + } + + public void testDeletePackage() throws Exception { + Package c = api.deletePackage(packageId); + checkPackage(c); + } + + public void testPackageInfo() throws Exception { + Package c = api.getPackageInfo(packageId); + checkPackage(c); + } + + public void testListPackages() throws Exception { + List list = api.listPackages(); + assertNotNull(list); + assertTrue(list.size() > 0); + Package c = list.get(0); + checkPackage(c); + } + + public void testListPackageTemplates() throws Exception { + List list = api.listPackageTemplates(); + assertNotNull(list); + assertTrue(list.size() > 0); + PackageTemplate c = list.get(0); + checkPackageTemplate(c); + } + + public void testListPackageValidations() throws Exception { + List list = api.listPackageValidations(packageId); + assertNotNull(list); + assertTrue(list.size() > 0); + PackageValidation c = list.get(0); + checkPackageValidation(c); + } + + public void testPackageValidationInfo() throws Exception { + PackageValidation c = api.getPackageValidationInfo(packageId, packageValidationId); + checkPackageValidation(c); + } + + public void testRunPackageValidation() throws Exception { + PackageValidation c = api.runPackageValidation(packageId); + checkPackageValidation(c); + } + + + private void checkPackage(Package c) throws ParseException { + assertNotNull(c); + assertEquals(packageId, c.getId()); + + assertEquals("TestPack", c.getName()); + assertEquals("TestPack Description", c.getDescription()); + assertEquals(Xplenty.PackageFlowType.workflow, c.getFlowType()); + assertEquals(String.format("https://localhost/%s/api/package/%s", accountID, packageId), c.getUrl()); + assertEquals(String.format("https://localhost/%s/package/%s", accountID, packageId), c.getHtmlUrl()); + assertEquals(Xplenty.PackageStatus.active, c.getStatus()); + assertEquals(111, c.getOwnerId().longValue()); + assertEquals(dFormat.parse("2016-01-14T20:25:12Z"), c.getCreatedAt()); + assertEquals(dFormat.parse("2016-01-14T20:25:12Z"), c.getUpdatedAt()); + + Map packVars = c.getVariables(); + assertNotNull(packVars); + assertEquals("val1", packVars.get("var_1")); + assertEquals("supercomplex", packVars.get("var_2")); + + } + + private void checkPackageValidation(PackageValidation c) throws ParseException { + assertNotNull(c); + assertEquals(packageValidationId, c.getId()); + + assertEquals("Something bad happened", c.getStatusMessage()); + assertEquals(String.format("https://localhost/%s/api/packages/%s/validations/%s", accountID, packageId, packageValidationId), c.getUrl()); + assertEquals(Xplenty.PackageValidationStatus.failed, c.getStatus()); + assertEquals(222, c.getOwnerId().longValue()); + assertEquals(111, c.getAccountId().longValue()); + assertEquals(packageId, c.getPackageId()); + assertEquals(1234, c.getRuntime().longValue()); + assertEquals(dFormat.parse("2016-01-14T20:34:27Z"), c.getCreatedAt()); + assertEquals(dFormat.parse("2016-01-14T20:34:27Z"), c.getUpdatedAt()); + + List errors = c.getErrors(); + assertNotNull(errors); + assertEquals("12", errors.get(0).getComponentId()); + assertEquals("couldn't obtain value for var_1", errors.get(0).getMessage()); + } + + private void checkPackageTemplate(PackageTemplate c) throws ParseException { + assertNotNull(c); + assertEquals(packageTemplateId, 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(String.format("https://localhost/%s/api/user/333", accountID), pta.getAvatarUrl()); + } +} 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..63ce8da --- /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(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()); + 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/java/com/xplenty/api/integration/ITScheduleTestAgainstMockServer.java b/src/test/java/com/xplenty/api/integration/ITScheduleTestAgainstMockServer.java new file mode 100644 index 0000000..ef40d74 --- /dev/null +++ b/src/test/java/com/xplenty/api/integration/ITScheduleTestAgainstMockServer.java @@ -0,0 +1,129 @@ +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.Schedule; +import com.xplenty.api.model.ScheduleTask; +import com.xplenty.api.model.ScheduleTaskPackage; +import junit.framework.TestCase; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; + +public class ITScheduleTestAgainstMockServer 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/schedule"; + private String apiKey = "dsfgsdfh"; + private String accountID = "testerAccount"; + private Long entityId = 666L; + + + @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 testCreateSchedule() throws Exception { + Schedule c = new Schedule(); + c.withName("1111SUPER SCHEDULE LAUNCH IT NOW MEGA TEST").withDescription("1111LAUNCH YOUR SCHEDULE NO SMS NO PAYMENT"); + c.withIntervalAmount(100L).withIntervalUnit(Xplenty.ScheduleIntervalUnit.hours).withStartAt(new Date()); + c.withStatus(Xplenty.ScheduleStatus.disabled); + ScheduleTask task = new ScheduleTask().withNodes(10).withTerminateOnIdle(true).withTimeToIdle(1000); + List packages = new ArrayList<>(); + ScheduleTaskPackage pack = new ScheduleTaskPackage().withPackageId(4991L); + Map vars = new HashMap<>(); + vars.put("testvar", "testval"); + pack.withVariables(vars); + packages.add(pack); + task.withPackages(packages); + c.withTask(task); + c = api.createSchedule(c); + checkEntity(c); + } + + public void testUpdateSchedule() throws Exception { + Schedule c = new Schedule(); + c.withId(entityId).withName("1111SUPER SCHEDULE LAUNCH IT NOW MEGA TEST").withDescription("1111LAUNCH YOUR SCHEDULE NO SMS NO PAYMENT"); + c.withIntervalAmount(100L).withIntervalUnit(Xplenty.ScheduleIntervalUnit.hours).withStartAt(new Date()); + c.withStatus(Xplenty.ScheduleStatus.disabled); + ScheduleTask task = new ScheduleTask().withNodes(10).withTerminateOnIdle(true).withTimeToIdle(1000); + List packages = new ArrayList<>(); + ScheduleTaskPackage pack = new ScheduleTaskPackage().withPackageId(4991L); + Map vars = new HashMap<>(); + vars.put("testvar", "testval"); + pack.withVariables(vars); + packages.add(pack); + task.withPackages(packages); + c.withTask(task); + c = api.updateSchedule(c); + checkEntity(c); + } + + public void testGetScheduleInfo() throws Exception { + Schedule c = api.getScheduleInfo(entityId); + checkEntity(c); + } + + public void testDeleteSchedule() throws Exception { + Schedule c = api.deleteSchedule(entityId); + checkEntity(c); + } + + public void testCloneSchedule() throws Exception { + Schedule c = api.cloneSchedule(entityId); + checkEntity(c); + } + + public void testListSchedules() throws Exception { + List list = api.listSchedules(); + assertNotNull(list); + assertTrue(list.size() > 0); + + Schedule c = list.get(0); + checkEntity(c); + } + + private void checkEntity(Schedule c) throws ParseException { + assertNotNull(c); + assertEquals(entityId, c.getId()); + assertEquals("testSchedule", c.getName()); + assertEquals("some neat description", c.getDescription()); + assertEquals("Successfully stored zillion records", c.getLastRunStatus()); + assertEquals(Xplenty.ScheduleStatus.enabled, c.getStatus()); + assertEquals(String.format("https://localhost/%s/api/schedules/%s", accountID, entityId), c.getUrl()); + assertEquals(String.format("https://localhost/%s/schedules/%s", accountID, entityId), c.getHtmlUrl()); + assertEquals(1, c.getExecutionCount().longValue()); + assertEquals(10, c.getIntervalAmount().longValue()); + assertEquals(1, c.getOwnerId().longValue()); + assertEquals(Xplenty.ScheduleIntervalUnit.days, c.getIntervalUnit()); + assertEquals(dFormat.parse("2016-01-24T10:07:42Z"), c.getCreatedAt()); + assertEquals(dFormat.parse("2016-01-24T10:07:42Z"), c.getUpdatedAt()); + assertEquals(dFormat.parse("2016-01-24T10:07:42Z"), c.getLastRunAt()); + assertEquals(dFormat.parse("2016-01-24T10:07:42Z"), c.getNextRunAt()); + assertEquals(dFormat.parse("2016-01-24T10:07:42Z"), c.getStartAt()); + assertTrue(c.getOverlap()); + assertEquals(Xplenty.ReuseClusterStrategy.any, c.getReuseClusterStrategy()); + + ScheduleTask task = c.getTask(); + assertNotNull(task); + assertTrue(task.getTerminateOnIdle()); + assertEquals(3, task.getNodes().intValue()); + assertEquals(1800, task.getTimeToIdle().intValue()); + List list = task.getPackages(); + assertNotNull(list); + assertTrue(list.size() > 0); + ScheduleTaskPackage xpackage = list.get(0); + assertEquals(1, xpackage.getPackageId().intValue()); + assertNotNull(xpackage.getVariables()); + assertEquals("varvalue", xpackage.getVariables().get("var1")); + } +} diff --git a/src/test/java/com/xplenty/api/integration/ITUserTestAgainstMockServer.java b/src/test/java/com/xplenty/api/integration/ITUserTestAgainstMockServer.java new file mode 100644 index 0000000..e6f435e --- /dev/null +++ b/src/test/java/com/xplenty/api/integration/ITUserTestAgainstMockServer.java @@ -0,0 +1,110 @@ +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.Notification; +import com.xplenty.api.model.User; +import junit.framework.TestCase; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.List; + +public class ITUserTestAgainstMockServer 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/user"; + private String apiKey = "dsfgsdfh"; + private String accountID = "testerAccount"; + private Long userId = 666L; + private Long notificationId = 777L; + + + @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 testGetUserInfo() throws Exception { + User c = api.getCurrentUserInfo(); + checkUser(c, false); + c = api.getCurrentUserInfo("supersecretpassword"); + checkUser(c, true); + } + + public void testUpdateCurrentUser() throws Exception { + User c = new User(); + c.setEmail("omg@omg.com"); + c.setLocation("Colorado"); + c.setCurrentPassword("supersecretpassword"); + c.setNewPassword("123"); + c.setGravatarEmail("grav@grav.com"); + c.setName("Xardazz"); + c.setReceiveNewsLetter(true); + c.setTimeZone("UTC"); + c = api.updateCurrentUser(c); + checkUser(c, false); + } + + public void testListNotifications() throws Exception { + List list = api.listUserNotifications(); + assertNotNull(list); + assertTrue(list.size() > 0); + Notification c = list.get(0); + checkNotification(c); + } + + public void testMarkNotificationsRead() throws Exception { + api.markNotificationAsRead(); + } + + public void testResetUserPassword() throws Exception { + api.resetUserPassword("test@test.com"); + } + + private void checkUser(User c, boolean withApiKey) throws ParseException { + assertNotNull(c); + assertEquals(userId, c.getId()); + + if (withApiKey) { + assertEquals("yepitsapikey", c.getApiKey()); + } else { + assertNull(c.getApiKey()); + } + assertEquals("Vasya Pupkin", c.getName()); + assertEquals(dFormat.parse("2016-01-17T19:41:12Z"), c.getCreatedAt()); + assertEquals(dFormat.parse("2016-01-17T19:41:12Z"), c.getUpdatedAt()); + assertEquals(dFormat.parse("2016-01-17T19:41:12Z"), c.getLastLogin()); + assertEquals(dFormat.parse("2016-01-17T19:41:12Z"), c.getConfirmedAt()); + 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()); + + } + + private void checkNotification(Notification c) throws ParseException { + assertNotNull(c); + assertEquals(notificationId, c.getId()); + assertEquals("Cluster available", c.getTitle()); + assertEquals("Cluster is available", c.getMessage()); + assertEquals(dFormat.parse("2016-01-17T19:42:18Z"), c.getCreatedAt()); + assertEquals(dFormat.parse("2016-01-17T19:42:18Z"), c.getUpdatedAt()); + assertEquals(dFormat.parse("2016-01-17T19:42:18Z"), c.getLastReadAt()); + } +} 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 59% 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..bcd975f 100644 --- a/xplenty.jar-integ-test/src/test/com/xplenty/ITWatchersTestAgainstMockServer.java +++ b/src/test/java/com/xplenty/api/integration/ITWatchersTestAgainstMockServer.java @@ -1,25 +1,34 @@ -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"; + private String host = "localhost:8080/mock/watchers"; private String apiKey = "dsfgsdfh"; private String accountID = "testerAccount"; @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/src/test/java/com/xplenty/api/integration/ITWebHookTestAgainstMockServer.java b/src/test/java/com/xplenty/api/integration/ITWebHookTestAgainstMockServer.java new file mode 100644 index 0000000..96fc2ff --- /dev/null +++ b/src/test/java/com/xplenty/api/integration/ITWebHookTestAgainstMockServer.java @@ -0,0 +1,164 @@ +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.*; +import junit.framework.TestCase; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; + +public class ITWebHookTestAgainstMockServer 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/hook"; + private String apiKey = "dsfgsdfh"; + private String accountID = "testerAccount"; + private Long entityId = 666L; + + + @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 testCreateWebHook() throws Exception { + List events = new ArrayList<>(); + events.add(Xplenty.HookEvent.cluster_available); + events.add(Xplenty.HookEvent.cluster_error); + HookSettings settings = new WebHookSettings("http://localhost", true, true, "md5encodedcredientials"); + Hook c = api.createHook(null, events, settings); + assertEquals(Xplenty.HookType.web, c.getType()); + checkEntity(c); + + settings = new SlackHookSettings("http://localhost", "xplenty", "xplenty", "xardazz"); + c = api.createHook(null, events, settings); + assertEquals(Xplenty.HookType.slack, c.getType()); + checkEntity(c); + + List emails = new ArrayList<>(); + emails.add("test@tesr.com"); + settings = new EmailHookSettings(emails); + c = api.createHook("omg", events, settings); + assertEquals(Xplenty.HookType.email, c.getType()); + checkEntity(c); + + settings = new PagerDutyHookSettings("xplenty", "xplenty", "aaa"); + c = api.createHook(null, events, settings); + assertEquals(Xplenty.HookType.pagerduty, c.getType()); + checkEntity(c); + + settings = new HipChatHookSettings("xplenty", "aaaaa"); + c = api.createHook(null, events, settings); + assertEquals(Xplenty.HookType.hipchat, c.getType()); + checkEntity(c); + } + + public void testUpdateWebHook() throws Exception { + List replaceevents = new ArrayList<>(); + replaceevents.add(Xplenty.HookEvent.job_completed); + replaceevents.add(Xplenty.HookEvent.job_started); + WebHookSettings settings = new WebHookSettings("http://localhost", true, true, "md5encodedcredientials"); + Hook c = api.updateHook(entityId, null, replaceevents, settings); + assertEquals(Xplenty.HookType.web, c.getType()); + checkEntity(c); + c = api.updateHook(entityId, null, replaceevents, null); + checkEntity(c); + } + + + public void testDeleteWebHook() throws Exception { + Hook c = api.deleteWebHook(entityId); + checkEntity(c); + } + + public void testGetWebHookInfo() throws Exception { + Hook c = api.getWebHookInfo(entityId); + checkEntity(c); + } + + public void testPingWebHook() throws Exception { + Hook c = api.pingWebHook(entityId); + checkEntity(c); + } + + public void testListWebHooks() throws Exception { + List list = api.listWebHooks(); + assertNotNull(list); + assertTrue(list.size() > 0); + + Hook c = list.get(0); + checkEntity(c); + } + + public void testToggleWebHook() throws Exception { + Hook c = api.toggleWebHook(entityId, false); + checkEntity(c); + } + + public void testWebHookResetSalt() throws Exception { + String newsalt = api.webHookResetSalt(entityId); + assertEquals("newsalt", newsalt); + } + + + private void checkEntity(Hook c) throws ParseException { + assertNotNull(c); + assertEquals(entityId, c.getId()); + assertEquals(true, c.getActive().booleanValue()); + assertEquals("000abcdead", c.getSalt()); + switch (c.getType()) { + case email: + EmailHookSettings ehs = (EmailHookSettings) c.getSettings(); + assertNotNull(ehs.getEmails()); + assertEquals(2, ehs.getEmails().size()); + assertEquals("a@a.com", ehs.getEmails().get(0)); + assertEquals("b@b.com", ehs.getEmails().get(1)); + break; + case hipchat: + HipChatHookSettings hchs = (HipChatHookSettings) c.getSettings(); + assertEquals("aaaa", hchs.getAuthToken()); + assertEquals("xplenty", hchs.getRoom()); + break; + case pagerduty: + PagerDutyHookSettings pdhs = (PagerDutyHookSettings) c.getSettings(); + assertEquals("xplenty1", pdhs.getPdAccount()); + assertEquals("xplenty", pdhs.getServiceName()); + assertEquals("aaaaaa", pdhs.getServiceKey()); + break; + case slack: + SlackHookSettings shs = (SlackHookSettings) c.getSettings(); + assertEquals("xplenty", shs.getTeam()); + assertEquals("xplentych", shs.getChannel()); + assertEquals("http://localhost", shs.getUrl()); + assertEquals("xardazz", shs.getUsername()); + break; + default: + final WebHookSettings whSettings = (WebHookSettings) c.getSettings(); + assertEquals("http://localhost/test", whSettings.getUrl()); + assertEquals(false, whSettings.getBasicAuth().booleanValue()); + assertEquals(true, whSettings.getInsecureSSL().booleanValue()); + } + + final HookEvent event = c.getEvents().get(0); + // we've got custom json serializer that removes everything except name + assertEquals(333, event.getId().longValue()); + assertEquals("success", event.getLastTriggerStatus()); + assertNotNull(event.getLastResponse()); + WebHookEventResponse wher = event.getLastResponse(); + assertEquals("200", wher.getCode()); + assertEquals("nice event", wher.getBody()); + assertEquals(dFormat.parse("2016-01-18T11:19:20Z"), event.getLastTriggerTime()); + assertEquals(Xplenty.HookEvent.job_all, event.getEvent()); + assertEquals("job", event.getName()); + } +} diff --git a/src/test/java/com/xplenty/api/model/AccountTest.java b/src/test/java/com/xplenty/api/model/AccountTest.java new file mode 100644 index 0000000..2f9ea7c --- /dev/null +++ b/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/src/test/java/com/xplenty/api/model/ClusterTest.java b/src/test/java/com/xplenty/api/model/ClusterTest.java new file mode 100644 index 0000000..eb4cdfb --- /dev/null +++ b/src/test/java/com/xplenty/api/model/ClusterTest.java @@ -0,0 +1,99 @@ +/** + * + */ +package com.xplenty.api.model; + +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 + * + */ +public class ClusterTest extends TestCase { + @Test + public void testBuilder() { + Cluster c = new Cluster().withDescription("description") + .named("my cluster") + .withNodes(2) + .withAllowFallback(true); + + assertEquals("description", c.getDescription()); + assertEquals("my cluster", c.getName()); + assertEquals(new Integer(2), c.getNodes()); + assertTrue(c.getAllowFallback()); + ClusterInstance ci = createMockClusterInstance(); + assertNotNull(ci); + } + + public static Cluster createMockCluster(Date now) { + Cluster c = new Cluster(); + c.createdAt = now; + c.updatedAt = now; + c.availableSince = now; + c.terminatedAt = now; + c.name = "my cluster"; + c.description = "description"; + c.ownerId = 1L; + c.nodes = 2; + c.type = ClusterType.production; + c.id = 3L; + c.runningJobsCount = 0L; + c.status = ClusterStatus.available; + c.url = "https://www.xplenty.com/api/" + Xplenty.Resource.Cluster.format(Long.toString(3)); + 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/src/test/java/com/xplenty/api/model/ConnectionTest.java b/src/test/java/com/xplenty/api/model/ConnectionTest.java new file mode 100644 index 0000000..07e489c --- /dev/null +++ b/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/src/test/java/com/xplenty/api/model/ConnectionTypeTest.java b/src/test/java/com/xplenty/api/model/ConnectionTypeTest.java new file mode 100644 index 0000000..63beb05 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/model/CreditCardInfoTest.java b/src/test/java/com/xplenty/api/model/CreditCardInfoTest.java new file mode 100644 index 0000000..3a3f4c4 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/model/HookTest.java b/src/test/java/com/xplenty/api/model/HookTest.java new file mode 100644 index 0000000..c5b0458 --- /dev/null +++ b/src/test/java/com/xplenty/api/model/HookTest.java @@ -0,0 +1,51 @@ +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 HookTest extends TestCase { + @Test + public void testBuilder() { + final Date now = new Date(); + Hook webhook = createMockHook(now); + assertNotNull(webhook); + assertEquals(now.getTime(), webhook.getEvents().get(0).getLastTriggerTime().getTime()); + + } + + + public static Hook createMockHook(Date now) { + Hook hook = new Hook(); + hook.id = 666L; + hook.name = "test"; + hook.active = true; + hook.salt = "000abcdead"; + WebHookSettings whs = new WebHookSettings("http://localhost/test", true, false, "somedata"); + whs.encryptedBasicAuthData = "wtjnIcvVp1fLC2fy9rAsSQ==\\n"; + hook.settings = whs; + List events = new ArrayList<>(); + HookEvent whe = new HookEvent(); + whe.id = 111L; + whe.lastTriggerStatus = "omg!"; + whe.lastTriggerTime = now; + whe.name = "job"; + events.add(whe); + whe = new HookEvent(); + whe.id = 222L; + whe.lastTriggerStatus = "omg2!"; + whe.lastTriggerTime = now; + whe.name = "cluster"; + events.add(whe); + hook.events = events; + return hook; + } +} 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 67% 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 index 4b15230..95ef6c3 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/model/JobTest.java +++ b/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,25 @@ 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; + } + + 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/src/test/java/com/xplenty/api/model/MemberTest.java b/src/test/java/com/xplenty/api/model/MemberTest.java new file mode 100644 index 0000000..996214b --- /dev/null +++ b/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, "testuser"); + 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/src/test/java/com/xplenty/api/model/MiscTest.java b/src/test/java/com/xplenty/api/model/MiscTest.java new file mode 100644 index 0000000..5c3d4ed --- /dev/null +++ b/src/test/java/com/xplenty/api/model/MiscTest.java @@ -0,0 +1,69 @@ +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); + + ProductUpdate pu = createMockProductUpdate(now); + assertNotNull(pu); + assertEquals(now.getTime(), pu.getCreatedAt().getTime()); + } + + 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; + } + + 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/src/test/java/com/xplenty/api/model/NotificationTest.java b/src/test/java/com/xplenty/api/model/NotificationTest.java new file mode 100644 index 0000000..903449f --- /dev/null +++ b/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/src/test/java/com/xplenty/api/model/PackageTest.java b/src/test/java/com/xplenty/api/model/PackageTest.java new file mode 100644 index 0000000..02a62f4 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/model/PlanTest.java b/src/test/java/com/xplenty/api/model/PlanTest.java new file mode 100644 index 0000000..b0e87e9 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/model/PublicKeyTest.java b/src/test/java/com/xplenty/api/model/PublicKeyTest.java new file mode 100644 index 0000000..ef69981 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/model/RegionTest.java b/src/test/java/com/xplenty/api/model/RegionTest.java new file mode 100644 index 0000000..210e40a --- /dev/null +++ b/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/src/test/java/com/xplenty/api/model/ScheduleTest.java b/src/test/java/com/xplenty/api/model/ScheduleTest.java new file mode 100644 index 0000000..f4dc9e5 --- /dev/null +++ b/src/test/java/com/xplenty/api/model/ScheduleTest.java @@ -0,0 +1,81 @@ +package com.xplenty.api.model; + +import com.xplenty.api.Xplenty; +import com.xplenty.api.http.JsonMapperFactory; +import junit.framework.TestCase; +import org.junit.Test; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.*; + +/** + * Author: Xardas + * Date: 17.12.15 + * Time: 21:18 + */ +public class ScheduleTest extends TestCase { + + + @Test + public void testBuilder() throws Exception { + Date now = new Date(); + Schedule sched = createMockSchedule(now); + assertNotNull(sched); + + DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + + sched = new Schedule(); + sched.withId(189L).withName("1111SUPER SCHEDULE LAUNCH IT NOW MEGA TEST").withDescription("1111LAUNCH YOUR SCHEDULE NO SMS NO PAYMENT"); + sched.withIntervalAmount(100L).withIntervalUnit(Xplenty.ScheduleIntervalUnit.hours).withStartAt(now); + sched.withStatus(Xplenty.ScheduleStatus.disabled); + ScheduleTask task = new ScheduleTask().withNodes(10).withTerminateOnIdle(true).withTimeToIdle(1000); + List packages = new ArrayList<>(); + ScheduleTaskPackage pack = new ScheduleTaskPackage().withPackageId(4991L); + Map vars = new HashMap<>(); + vars.put("testvar", "testval"); + pack.withVariables(vars); + packages.add(pack); + task.withPackages(packages); + sched.withTask(task); + assertEquals(String.format("{\"id\":189,\"name\":\"1111SUPER SCHEDULE LAUNCH IT NOW MEGA TEST\",\"description\":\"1111LAUNCH YOUR SCHEDULE NO SMS NO PAYMENT\",\"status\":\"disabled\",\"task\":{\"nodes\":10,\"packages\":{\"0\":{\"variables\":{\"testvar\":\"testval\"},\"package_id\":4991}},\"terminate_on_idle\":true,\"time_to_idle\":1000},\"start_at\":\"%s\",\"interval_amount\":100,\"interval_unit\":\"hours\"}", dFormat.format(now)), JsonMapperFactory.getInstance().writeValueAsString(sched)); + + } + + public static Schedule createMockSchedule(Date now) { + final Schedule sched = new Schedule(); + + sched.createdAt = now; + sched.updatedAt = now; + sched.startAt = now; + sched.nextRunAt = now; + sched.lastRunAt = now; + sched.intervalAmount = 10L; + sched.intervalUnit = Xplenty.ScheduleIntervalUnit.days; + sched.id = 666L; + sched.name = "testSchedule"; + sched.description = "some neat description"; + sched.ownerId = 1L; + sched.status = Xplenty.ScheduleStatus.enabled; + sched.lastRunStatus = "Successfully stored zillion records"; + sched.executionCount = 1L; + sched.url = String.format("https://api.xplenty.com/testacc/api/%s", Xplenty.Resource.Schedule.format(Long.toString(sched.id))); + final ScheduleTask task = new ScheduleTask(); + task.nodes = 3; + task.terminateOnIdle = true; + task.timeToIdle = 1800; + final ArrayList packages = new ArrayList(); + final ScheduleTaskPackage taskPackage = new ScheduleTaskPackage(); + taskPackage.packageId = 1L; + final Map vars = new HashMap(); + vars.put("var1", "varvalue"); + taskPackage.variables = vars; + packages.add(taskPackage); + task.packages = packages; + sched.task = task; + sched.overlap = true; + sched.reuseClusterStrategy = Xplenty.ReuseClusterStrategy.self; + + return sched; + } +} diff --git a/src/test/java/com/xplenty/api/model/SubscriptionTest.java b/src/test/java/com/xplenty/api/model/SubscriptionTest.java new file mode 100644 index 0000000..f8c1936 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/model/UserTest.java b/src/test/java/com/xplenty/api/model/UserTest.java new file mode 100644 index 0000000..f213bb7 --- /dev/null +++ b/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, true); + 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/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/src/test/java/com/xplenty/api/request/account/AccountInfoTest.java b/src/test/java/com/xplenty/api/request/account/AccountInfoTest.java new file mode 100644 index 0000000..70a3ce3 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/account/CreateAccountTest.java b/src/test/java/com/xplenty/api/request/account/CreateAccountTest.java new file mode 100644 index 0000000..3a54aee --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/account/DeleteAccountTest.java b/src/test/java/com/xplenty/api/request/account/DeleteAccountTest.java new file mode 100644 index 0000000..a54e949 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/account/ListAccountRegionsTest.java b/src/test/java/com/xplenty/api/request/account/ListAccountRegionsTest.java new file mode 100644 index 0000000..6a8a3b0 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/account/ListAccountsTest.java b/src/test/java/com/xplenty/api/request/account/ListAccountsTest.java new file mode 100644 index 0000000..5a5b1d6 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/account/UpdateAccountTest.java b/src/test/java/com/xplenty/api/request/account/UpdateAccountTest.java new file mode 100644 index 0000000..7e01e03 --- /dev/null +++ b/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()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ClusterInfoTest.java b/src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java similarity index 54% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/ClusterInfoTest.java rename to src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java index fd78949..21dd3e8 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ClusterInfoTest.java +++ b/src/test/java/com/xplenty/api/request/cluster/ClusterInfoTest.java @@ -1,29 +1,30 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.cluster; 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.ClusterBootstrapAction; import com.xplenty.api.model.ClusterTest; -import com.xplenty.api.util.Http; +import com.xplenty.api.model.Creator; 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; +import java.util.List; /** * @author Yuriy Kovalek @@ -53,10 +54,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()); @@ -71,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 @@ -81,10 +115,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 +128,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/src/test/java/com/xplenty/api/request/cluster/CreateClusterTest.java similarity index 77% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateClusterTest.java rename to src/test/java/com/xplenty/api/request/cluster/CreateClusterTest.java index 8ccf499..370628c 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateClusterTest.java +++ b/src/test/java/com/xplenty/api/request/cluster/CreateClusterTest.java @@ -1,31 +1,27 @@ /** * */ -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; +package com.xplenty.api.request.cluster; 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 com.xplenty.api.request.cluster.CreateCluster; 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 +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.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 +86,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 +100,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/src/test/java/com/xplenty/api/request/cluster/ListClusterInstancesTest.java b/src/test/java/com/xplenty/api/request/cluster/ListClusterInstancesTest.java new file mode 100644 index 0000000..b3d8e9b --- /dev/null +++ b/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()); + } + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListClustersTest.java b/src/test/java/com/xplenty/api/request/cluster/ListClustersTest.java similarity index 70% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/ListClustersTest.java rename to src/test/java/com/xplenty/api/request/cluster/ListClustersTest.java index 26a3ffb..6b6198c 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListClustersTest.java +++ b/src/test/java/com/xplenty/api/request/cluster/ListClustersTest.java @@ -1,30 +1,23 @@ /** * */ -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; +package com.xplenty.api.request.cluster; 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 com.xplenty.api.request.cluster.ListClusters; import junit.framework.TestCase; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; /** * @author Yuriy Kovalek @@ -50,10 +43,10 @@ public void testValidResponseHandling() 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 +59,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/TerminateClusterTest.java b/src/test/java/com/xplenty/api/request/cluster/TerminateClusterTest.java similarity index 72% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/TerminateClusterTest.java rename to src/test/java/com/xplenty/api/request/cluster/TerminateClusterTest.java index 0304321..febf270 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/TerminateClusterTest.java +++ b/src/test/java/com/xplenty/api/request/cluster/TerminateClusterTest.java @@ -1,25 +1,24 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.cluster; 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 com.xplenty.api.request.cluster.TerminateCluster; 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 +43,10 @@ public void testValidResponseHandling() 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 +57,10 @@ public void testInvalidResponseHandling() 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/src/test/java/com/xplenty/api/request/cluster/UpdateClusterTest.java similarity index 77% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateClusterTest.java rename to src/test/java/com/xplenty/api/request/cluster/UpdateClusterTest.java index 0063126..807109f 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateClusterTest.java +++ b/src/test/java/com/xplenty/api/request/cluster/UpdateClusterTest.java @@ -1,31 +1,28 @@ /** * */ -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; +package com.xplenty.api.request.cluster; 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 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; + +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.HashMap; /** * @author Yuriy Kovalek @@ -62,10 +59,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 +88,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 +102,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/src/test/java/com/xplenty/api/request/connection/ConnectionInfoTest.java b/src/test/java/com/xplenty/api/request/connection/ConnectionInfoTest.java new file mode 100644 index 0000000..090ad35 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/connection/DeleteConnectionTest.java b/src/test/java/com/xplenty/api/request/connection/DeleteConnectionTest.java new file mode 100644 index 0000000..6d44b0f --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/connection/ListConnectionTypesTest.java b/src/test/java/com/xplenty/api/request/connection/ListConnectionTypesTest.java new file mode 100644 index 0000000..c76b428 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/connection/ListConnectionsTest.java b/src/test/java/com/xplenty/api/request/connection/ListConnectionsTest.java new file mode 100644 index 0000000..6372a99 --- /dev/null +++ b/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()); + } + + } +} diff --git a/src/test/java/com/xplenty/api/request/hook/CreateHookTest.java b/src/test/java/com/xplenty/api/request/hook/CreateHookTest.java new file mode 100644 index 0000000..8893040 --- /dev/null +++ b/src/test/java/com/xplenty/api/request/hook/CreateHookTest.java @@ -0,0 +1,109 @@ +/** + * + */ +package com.xplenty.api.request.hook; + +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.Hook; +import com.xplenty.api.model.HookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.HookTest; +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 CreateHookTest 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"); + + CreateHook cc = new CreateHook("test", whs, events); + assertEquals(Xplenty.Resource.CreateHook.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.CreateHook.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(); + Hook c = HookTest.createMockHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + WebHookSettings whs = new WebHookSettings("http://localhost/test", true, false, "somedata"); + List events = new ArrayList<>(); + events.add("job"); + + CreateHook cc = new CreateHook("test", 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("test", c.getName()); + assertEquals(true, c.getActive().booleanValue()); + assertEquals("000abcdead", c.getSalt()); + final WebHookSettings settings = (WebHookSettings) c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + final HookEvent 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(); + Hook c = HookTest.createMockHook(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 { + CreateHook cc = new CreateHook("test", whs, events); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + Status.CREATED.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.CreateHook.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/src/test/java/com/xplenty/api/request/hook/DeleteHookTest.java b/src/test/java/com/xplenty/api/request/hook/DeleteHookTest.java new file mode 100644 index 0000000..d2d59fc --- /dev/null +++ b/src/test/java/com/xplenty/api/request/hook/DeleteHookTest.java @@ -0,0 +1,93 @@ +package com.xplenty.api.request.hook; + +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.Hook; +import com.xplenty.api.model.HookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.HookTest; +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 DeleteHookTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + DeleteHook cc = new DeleteHook(1L); + assertEquals(Xplenty.Resource.DeleteHook.format("1"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.DeleteHook.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(); + Hook c = HookTest.createMockHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + DeleteHook cc = new DeleteHook(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 = (WebHookSettings) c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + final HookEvent 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(); + Hook c = HookTest.createMockHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + try { + + DeleteHook cc = new DeleteHook(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.DeleteHook.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/src/test/java/com/xplenty/api/request/hook/HookInfoTest.java b/src/test/java/com/xplenty/api/request/hook/HookInfoTest.java new file mode 100644 index 0000000..7964c47 --- /dev/null +++ b/src/test/java/com/xplenty/api/request/hook/HookInfoTest.java @@ -0,0 +1,94 @@ +package com.xplenty.api.request.hook; + +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.Hook; +import com.xplenty.api.model.HookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.HookTest; +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 HookInfoTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + HookInfo cc = new HookInfo(1L); + assertEquals(Xplenty.Resource.Hook.format("1"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.Hook.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(); + Hook c = HookTest.createMockHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + HookInfo cc = new HookInfo(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 = (WebHookSettings) c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + assertEquals("wtjnIcvVp1fLC2fy9rAsSQ==\\n", settings.getEncryptedBasicAuthData()); + final HookEvent 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(); + Hook c = HookTest.createMockHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + try { + + HookInfo cc = new HookInfo(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.Hook.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/src/test/java/com/xplenty/api/request/hook/HookResetSaltTest.java b/src/test/java/com/xplenty/api/request/hook/HookResetSaltTest.java new file mode 100644 index 0000000..16992c7 --- /dev/null +++ b/src/test/java/com/xplenty/api/request/hook/HookResetSaltTest.java @@ -0,0 +1,76 @@ +package com.xplenty.api.request.hook; + +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.Hook; +import com.xplenty.api.model.HookTest; +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 HookResetSaltTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + HookResetSalt cc = new HookResetSalt(1L); + assertEquals(Xplenty.Resource.HookResetSalt.format("1"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.HookResetSalt.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(); + Hook c = HookTest.createMockHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + HookResetSalt cc = new HookResetSalt(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 { + + HookResetSalt cc = new HookResetSalt(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.HookResetSalt.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/src/test/java/com/xplenty/api/request/hook/ListHookTest.java b/src/test/java/com/xplenty/api/request/hook/ListHookTest.java new file mode 100644 index 0000000..8374926 --- /dev/null +++ b/src/test/java/com/xplenty/api/request/hook/ListHookTest.java @@ -0,0 +1,116 @@ +package com.xplenty.api.request.hook; + +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.Hook; +import com.xplenty.api.model.HookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.HookTest; +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 ListHookTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + ListHooks cc = new ListHooks(new Properties()); + assertEquals(Xplenty.Resource.Hooks.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.Hooks.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 ListHooks(props); + assertEquals(Xplenty.Resource.Hooks.value + "?" + AbstractListRequest.PARAMETER_SORT + "=created&" + + AbstractListRequest.PARAMETER_DIRECTION + "=desc", cc.getEndpoint()); + assertEquals(Xplenty.Resource.Hooks.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<>(); + + Hook c = HookTest.createMockHook(now); + whList.add(c); + + + String json = JsonMapperFactory.getInstance().writeValueAsString(whList); + + ListHooks cc = new ListHooks(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 = (WebHookSettings) c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + final HookEvent 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<>(); + + Hook c = HookTest.createMockHook(now); + whList.add(c); + + + String json = JsonMapperFactory.getInstance().writeValueAsString(whList).replace("{", "["); + + try { + + ListHooks cc = new ListHooks(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.Hooks.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/src/test/java/com/xplenty/api/request/hook/PingHookTest.java b/src/test/java/com/xplenty/api/request/hook/PingHookTest.java new file mode 100644 index 0000000..9df4206 --- /dev/null +++ b/src/test/java/com/xplenty/api/request/hook/PingHookTest.java @@ -0,0 +1,93 @@ +package com.xplenty.api.request.hook; + +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.Hook; +import com.xplenty.api.model.HookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.HookTest; +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 PingHookTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + PingHook cc = new PingHook(1L); + assertEquals(Xplenty.Resource.PingHook.format("1"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.PingHook.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(); + Hook c = HookTest.createMockHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + PingHook cc = new PingHook(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 = (WebHookSettings) c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + final HookEvent 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(); + Hook c = HookTest.createMockHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + try { + + PingHook cc = new PingHook(666); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.PingHook.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/src/test/java/com/xplenty/api/request/hook/ToggleHookTest.java b/src/test/java/com/xplenty/api/request/hook/ToggleHookTest.java new file mode 100644 index 0000000..92c1f06 --- /dev/null +++ b/src/test/java/com/xplenty/api/request/hook/ToggleHookTest.java @@ -0,0 +1,93 @@ +package com.xplenty.api.request.hook; + +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.Hook; +import com.xplenty.api.model.HookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.HookTest; +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 ToggleHookTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + + ToggleHook cc = new ToggleHook(1L, false); + assertEquals(Xplenty.Resource.UpdateHook.format("1"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.UpdateHook.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(); + Hook c = HookTest.createMockHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c); + + ToggleHook cc = new ToggleHook(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 = (WebHookSettings) c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + final HookEvent 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(); + Hook c = HookTest.createMockHook(now); + + String json = JsonMapperFactory.getInstance().writeValueAsString(c).replace("{", "["); + try { + + ToggleHook cc = new ToggleHook(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.UpdateHook.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/src/test/java/com/xplenty/api/request/hook/UpdateHookTest.java b/src/test/java/com/xplenty/api/request/hook/UpdateHookTest.java new file mode 100644 index 0000000..1e6d4a1 --- /dev/null +++ b/src/test/java/com/xplenty/api/request/hook/UpdateHookTest.java @@ -0,0 +1,109 @@ +package com.xplenty.api.request.hook; + +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.Hook; +import com.xplenty.api.model.HookEvent; +import com.xplenty.api.model.WebHookSettings; +import com.xplenty.api.model.HookTest; +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 UpdateHookTest 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"); + + UpdateHook cc = new UpdateHook(1L, null, whs, events); + assertEquals(Xplenty.Resource.UpdateHook.format("1"), cc.getEndpoint()); + assertEquals(Xplenty.Resource.UpdateHook.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(); + Hook c = HookTest.createMockHook(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"); + + UpdateHook cc = new UpdateHook(666L, null, whs, events); + 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 = (WebHookSettings) c.getSettings(); + assertEquals("http://localhost/test", settings.getUrl()); + assertEquals(false, settings.getBasicAuth().booleanValue()); + assertEquals(true, settings.getInsecureSSL().booleanValue()); + final HookEvent 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(); + Hook c = HookTest.createMockHook(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 { + + UpdateHook cc = new UpdateHook(666L, null, whs, events); + c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + json, + ClientResponse.Status.OK.getStatusCode(), + new HashMap())); + assertTrue(false); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.UpdateHook.name + ": error parsing response object", e.getMessage()); + } + + } +} diff --git a/src/test/java/com/xplenty/api/request/job/JobExecutionVariablesTest.java b/src/test/java/com/xplenty/api/request/job/JobExecutionVariablesTest.java new file mode 100644 index 0000000..2e04085 --- /dev/null +++ b/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/JobInfoTest.java b/src/test/java/com/xplenty/api/request/job/JobInfoTest.java similarity index 69% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/JobInfoTest.java rename to src/test/java/com/xplenty/api/request/job/JobInfoTest.java index b59ab0d..adbcc62 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/JobInfoTest.java +++ b/src/test/java/com/xplenty/api/request/job/JobInfoTest.java @@ -1,26 +1,24 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.job; 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 @@ -30,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()); @@ -38,19 +36,26 @@ 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); - 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()); @@ -74,15 +79,15 @@ 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"); 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/src/test/java/com/xplenty/api/request/job/JobLogsTest.java b/src/test/java/com/xplenty/api/request/job/JobLogsTest.java new file mode 100644 index 0000000..a0c0def --- /dev/null +++ b/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()); + } + + } +} diff --git a/src/test/java/com/xplenty/api/request/job/JobPreviewOutputTest.java b/src/test/java/com/xplenty/api/request/job/JobPreviewOutputTest.java new file mode 100644 index 0000000..6e71e88 --- /dev/null +++ b/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/ListJobsTest.java b/src/test/java/com/xplenty/api/request/job/ListJobsTest.java similarity index 58% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/ListJobsTest.java rename to src/test/java/com/xplenty/api/request/job/ListJobsTest.java index 35fe61c..7c0c0cd 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListJobsTest.java +++ b/src/test/java/com/xplenty/api/request/job/ListJobsTest.java @@ -1,30 +1,23 @@ /** * */ -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; +package com.xplenty.api.request.job; 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 com.xplenty.api.request.AbstractListRequest; +import junit.framework.TestCase; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.*; /** * @author Yuriy Kovalek @@ -33,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()); @@ -41,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 @@ -50,10 +53,10 @@ public void testValidResponseHandling() 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 +69,10 @@ public void testInvalidResponseHandling() 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/RunJobTest.java b/src/test/java/com/xplenty/api/request/job/RunJobTest.java similarity index 72% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/RunJobTest.java rename to src/test/java/com/xplenty/api/request/job/RunJobTest.java index 784efa3..ca3df6d 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/RunJobTest.java +++ b/src/test/java/com/xplenty/api/request/job/RunJobTest.java @@ -1,27 +1,24 @@ /** * */ -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; +package com.xplenty.api.request.job; 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 com.xplenty.api.request.job.RunJob; +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 +43,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 +57,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/StopJobTest.java b/src/test/java/com/xplenty/api/request/job/StopJobTest.java similarity index 71% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/StopJobTest.java rename to src/test/java/com/xplenty/api/request/job/StopJobTest.java index e0f4f85..6c40b91 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/StopJobTest.java +++ b/src/test/java/com/xplenty/api/request/job/StopJobTest.java @@ -1,25 +1,24 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.job; 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 com.xplenty.api.request.job.StopJob; 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 +43,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 +57,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/src/test/java/com/xplenty/api/request/member/CreateMemberTest.java b/src/test/java/com/xplenty/api/request/member/CreateMemberTest.java new file mode 100644 index 0000000..b5c8eb4 --- /dev/null +++ b/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, "testuser"); + 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, "testuser"); + 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, "testuser"); + 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/src/test/java/com/xplenty/api/request/member/DeleteMemberTest.java b/src/test/java/com/xplenty/api/request/member/DeleteMemberTest.java new file mode 100644 index 0000000..b7f52a5 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/member/MemberInfoTest.java b/src/test/java/com/xplenty/api/request/member/MemberInfoTest.java new file mode 100644 index 0000000..2bd69ea --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/member/MemberListTest.java b/src/test/java/com/xplenty/api/request/member/MemberListTest.java new file mode 100644 index 0000000..1f0f9c7 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/member/SetMemberRoleTest.java b/src/test/java/com/xplenty/api/request/member/SetMemberRoleTest.java new file mode 100644 index 0000000..363a17e --- /dev/null +++ b/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()); + } + + } +} diff --git a/src/test/java/com/xplenty/api/request/misc/LikeProductUpdateTest.java b/src/test/java/com/xplenty/api/request/misc/LikeProductUpdateTest.java new file mode 100644 index 0000000..8e8a3b9 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/misc/ListProductUpdatesTest.java b/src/test/java/com/xplenty/api/request/misc/ListProductUpdatesTest.java new file mode 100644 index 0000000..f66db40 --- /dev/null +++ b/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()); + } + + } +} diff --git a/src/test/java/com/xplenty/api/request/misc/ListRegionsTest.java b/src/test/java/com/xplenty/api/request/misc/ListRegionsTest.java new file mode 100644 index 0000000..01315ae --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/misc/ListStacksTest.java b/src/test/java/com/xplenty/api/request/misc/ListStacksTest.java new file mode 100644 index 0000000..1506d8b --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/misc/ListSystemVariablesTest.java b/src/test/java/com/xplenty/api/request/misc/ListSystemVariablesTest.java new file mode 100644 index 0000000..ba466ac --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/misc/ListTimezonesTest.java b/src/test/java/com/xplenty/api/request/misc/ListTimezonesTest.java new file mode 100644 index 0000000..f52543e --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/public_key/CreatePublicKeyTest.java b/src/test/java/com/xplenty/api/request/public_key/CreatePublicKeyTest.java new file mode 100644 index 0000000..58d880a --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/public_key/DeletePublicKeyTest.java b/src/test/java/com/xplenty/api/request/public_key/DeletePublicKeyTest.java new file mode 100644 index 0000000..99bbd02 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/public_key/ListPublicKeysTest.java b/src/test/java/com/xplenty/api/request/public_key/ListPublicKeysTest.java new file mode 100644 index 0000000..1627853 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/public_key/PublicKeyInfoTest.java b/src/test/java/com/xplenty/api/request/public_key/PublicKeyInfoTest.java new file mode 100644 index 0000000..284cedf --- /dev/null +++ b/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()); + } + + } +} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CloneScheduleTest.java b/src/test/java/com/xplenty/api/request/schedule/CloneScheduleTest.java similarity index 79% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/CloneScheduleTest.java rename to src/test/java/com/xplenty/api/request/schedule/CloneScheduleTest.java index ad95e95..0fcba14 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CloneScheduleTest.java +++ b/src/test/java/com/xplenty/api/request/schedule/CloneScheduleTest.java @@ -1,26 +1,25 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; 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 com.xplenty.api.request.schedule.CloneSchedule; 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 +53,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 +73,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/CreateScheduleTest.java b/src/test/java/com/xplenty/api/request/schedule/CreateScheduleTest.java similarity index 80% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateScheduleTest.java rename to src/test/java/com/xplenty/api/request/schedule/CreateScheduleTest.java index 603bb21..8a2b184 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/CreateScheduleTest.java +++ b/src/test/java/com/xplenty/api/request/schedule/CreateScheduleTest.java @@ -1,26 +1,25 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; 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 com.xplenty.api.request.schedule.CreateSchedule; 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 +54,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 +74,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/src/test/java/com/xplenty/api/request/schedule/DeleteScheduleTest.java similarity index 79% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/DeleteScheduleTest.java rename to src/test/java/com/xplenty/api/request/schedule/DeleteScheduleTest.java index e3c46cd..8af66ca 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/DeleteScheduleTest.java +++ b/src/test/java/com/xplenty/api/request/schedule/DeleteScheduleTest.java @@ -1,26 +1,25 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; 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 com.xplenty.api.request.schedule.DeleteSchedule; 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 +53,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 +73,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/ListSchedulesTest.java b/src/test/java/com/xplenty/api/request/schedule/ListSchedulesTest.java similarity index 61% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java rename to src/test/java/com/xplenty/api/request/schedule/ListSchedulesTest.java index 3df6551..7217978 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ListSchedulesTest.java +++ b/src/test/java/com/xplenty/api/request/schedule/ListSchedulesTest.java @@ -1,28 +1,23 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; 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 com.xplenty.api.request.AbstractListRequest; 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 @@ -41,17 +36,35 @@ public void testIntegrity() { } @Test - public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + public void testValidResponceHandling() throws JsonProcessingException, UnsupportedEncodingException { + ListSchedules ls = new ListSchedules(new Properties()); + List list = new ArrayList(); + 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(), + new HashMap())); + assertNotNull(list); + assertFalse(list.isEmpty()); + } + + @Test + public void testInvalidResponceHandling() throws JsonProcessingException, UnsupportedEncodingException { ListSchedules ls = new ListSchedules(new Properties()); List list = new ArrayList(); list.add(ScheduleTest.createMockSchedule(new Date())); 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()); @@ -61,8 +74,8 @@ public void testInvalidResponseHandling() 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())); @@ -73,8 +86,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); @@ -83,14 +96,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()); diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ScheduleInfoTest.java b/src/test/java/com/xplenty/api/request/schedule/ScheduleInfoTest.java similarity index 79% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/ScheduleInfoTest.java rename to src/test/java/com/xplenty/api/request/schedule/ScheduleInfoTest.java index 30ebe9a..6ca9429 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/ScheduleInfoTest.java +++ b/src/test/java/com/xplenty/api/request/schedule/ScheduleInfoTest.java @@ -1,26 +1,25 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; 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 com.xplenty.api.request.schedule.ScheduleInfo; 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 +53,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 +73,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/UpdateScheduleTest.java b/src/test/java/com/xplenty/api/request/schedule/UpdateScheduleTest.java similarity index 80% rename from xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateScheduleTest.java rename to src/test/java/com/xplenty/api/request/schedule/UpdateScheduleTest.java index f7c834e..e11d3da 100644 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/request/UpdateScheduleTest.java +++ b/src/test/java/com/xplenty/api/request/schedule/UpdateScheduleTest.java @@ -1,26 +1,25 @@ /** * */ -package com.xplenty.api.request; +package com.xplenty.api.request.schedule; 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 com.xplenty.api.request.schedule.UpdateSchedule; 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 +54,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 +75,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()); diff --git a/src/test/java/com/xplenty/api/request/subscription/ListPlansTest.java b/src/test/java/com/xplenty/api/request/subscription/ListPlansTest.java new file mode 100644 index 0000000..931b9d3 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/subscription/PaymentMethodInfoTest.java b/src/test/java/com/xplenty/api/request/subscription/PaymentMethodInfoTest.java new file mode 100644 index 0000000..9100bc3 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/subscription/SubscriptionInfoTest.java b/src/test/java/com/xplenty/api/request/subscription/SubscriptionInfoTest.java new file mode 100644 index 0000000..042aef7 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlanTest.java b/src/test/java/com/xplenty/api/request/subscription/UpdatePaymentAndPlanTest.java new file mode 100644 index 0000000..e65b250 --- /dev/null +++ b/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()); + } + + } +} diff --git a/src/test/java/com/xplenty/api/request/user/CurrentUserInfoTest.java b/src/test/java/com/xplenty/api/request/user/CurrentUserInfoTest.java new file mode 100644 index 0000000..a655d5e --- /dev/null +++ b/src/test/java/com/xplenty/api/request/user/CurrentUserInfoTest.java @@ -0,0 +1,120 @@ +/** + * + */ +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.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/src/test/java/com/xplenty/api/request/user/ListNotificationsTest.java b/src/test/java/com/xplenty/api/request/user/ListNotificationsTest.java new file mode 100644 index 0000000..febaa50 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/user/MarkNotificationsReadTest.java b/src/test/java/com/xplenty/api/request/user/MarkNotificationsReadTest.java new file mode 100644 index 0000000..b7f341f --- /dev/null +++ b/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()); + + } +} diff --git a/src/test/java/com/xplenty/api/request/user/ResetUserPasswordTest.java b/src/test/java/com/xplenty/api/request/user/ResetUserPasswordTest.java new file mode 100644 index 0000000..6aa4cca --- /dev/null +++ b/src/test/java/com/xplenty/api/request/user/ResetUserPasswordTest.java @@ -0,0 +1,67 @@ +package com.xplenty.api.request.user; + +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.Response; +import com.xplenty.api.model.User; +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; +import java.util.HashMap; + +/** + * Author: Xardas + * Date: 23.02.16 + * Time: 19:02 + */ +public class ResetUserPasswordTest extends TestCase { + @Before + public void setUp() { + + } + + @Test + public void testIntegrity() { + ResetUserPassword cc = new ResetUserPassword("test@test.com"); + assertEquals(Xplenty.Resource.ResetUserPassword.value, cc.getEndpoint()); + assertEquals(Xplenty.Resource.ResetUserPassword.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 { + + ResetUserPassword cc = new ResetUserPassword("test@test.com"); + User c = cc.getResponse(Response.forContentType(Http.MediaType.JSON, + "", + ClientResponse.Status.CREATED.getStatusCode(), + new HashMap())); + + assertNull(c); + } + + @Test + public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { + + ResetUserPassword cc = new ResetUserPassword("test@test.com"); + try { + final Response response = Response.forContentType(Http.MediaType.JSON, + "", + ClientResponse.Status.BAD_REQUEST.getStatusCode(), + new HashMap()); + response.validate(cc.getName()); + User c = cc.getResponse(response); + fail(); + } catch (XplentyAPIException e) { + assertEquals(Xplenty.Resource.ResetUserPassword.name + " failed, HTTP status code: 400[Bad Request: The request was invalid. An accompanying error message will explain why.], server response: []", e.getMessage()); + } + } +} diff --git a/src/test/java/com/xplenty/api/request/user/UpdateCurrentUserTest.java b/src/test/java/com/xplenty/api/request/user/UpdateCurrentUserTest.java new file mode 100644 index 0000000..a275bd2 --- /dev/null +++ b/src/test/java/com/xplenty/api/request/user/UpdateCurrentUserTest.java @@ -0,0 +1,85 @@ +/** + * + */ +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.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()); + } + } +} diff --git a/src/test/java/com/xplenty/api/request/xpackage/CreatePackageTest.java b/src/test/java/com/xplenty/api/request/xpackage/CreatePackageTest.java new file mode 100644 index 0000000..dd82313 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/xpackage/DeletePackageTest.java b/src/test/java/com/xplenty/api/request/xpackage/DeletePackageTest.java new file mode 100644 index 0000000..d52df29 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/xpackage/ListPackageTemplatesTest.java b/src/test/java/com/xplenty/api/request/xpackage/ListPackageTemplatesTest.java new file mode 100644 index 0000000..a3513a2 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/xpackage/ListPackageValidationsTest.java b/src/test/java/com/xplenty/api/request/xpackage/ListPackageValidationsTest.java new file mode 100644 index 0000000..1167b44 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java b/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java new file mode 100644 index 0000000..502cdc3 --- /dev/null +++ b/src/test/java/com/xplenty/api/request/xpackage/ListPackagesTest.java @@ -0,0 +1,120 @@ +/** + * + */ +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); + 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()); + } + + @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/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java b/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java new file mode 100644 index 0000000..0c9c000 --- /dev/null +++ b/src/test/java/com/xplenty/api/request/xpackage/PackageInfoTest.java @@ -0,0 +1,104 @@ +/** + * + */ +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, 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 + 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, false); + 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, false); + 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/src/test/java/com/xplenty/api/request/xpackage/PackageValidationInfoTest.java b/src/test/java/com/xplenty/api/request/xpackage/PackageValidationInfoTest.java new file mode 100644 index 0000000..6db3235 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/xpackage/RunPackageValidationTest.java b/src/test/java/com/xplenty/api/request/xpackage/RunPackageValidationTest.java new file mode 100644 index 0000000..c1925f8 --- /dev/null +++ b/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/src/test/java/com/xplenty/api/request/xpackage/UpdatePackageTest.java b/src/test/java/com/xplenty/api/request/xpackage/UpdatePackageTest.java new file mode 100644 index 0000000..1502d48 --- /dev/null +++ b/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()); + } + + } +} 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/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/router/ClusterRouter.scala b/src/test/scala/com/xplenty/api/router/ClusterRouter.scala new file mode 100644 index 0000000..2b269c3 --- /dev/null +++ b/src/test/scala/com/xplenty/api/router/ClusterRouter.scala @@ -0,0 +1,44 @@ +package com.xplenty.api.router + +import org.scalatra.ScalatraServlet + +/** + * Mock server returning fixed values + * @author $accountId + */ +class ClusterRouter extends ScalatraServlet { + + post("/:accountId/api/clusters") { + val accountId = params("accountId") + s"""{"id":666,"name":"brown-sea-277","description":"sea is brown in this time of the year","status":"terminated","owner_id":352,"plan_id":"pro","nodes":7,"type":"production","created_at":"2016-01-22T13:22:20Z","updated_at":"2016-01-22T14:33:03Z","available_since":"2016-01-22T13:31:07Z","terminated_at":"2016-01-22T14:33:02Z","launched_at":"2016-01-22T13:31:06Z","terminate_on_idle":true,"time_to_idle":3600,"terminated_on_idle":true,"region":"amazon-web-services::eu-west-1","zone":null,"master_instance_type":null,"slave_instance_type":null,"master_spot_price":null,"slave_spot_price":null,"master_spot_percentage":null,"slave_spot_percentage":null,"allow_fallback":true,"stack":"white-everest","idle_since":"2016-01-22T14:33:02Z","running_jobs_count":0,"url":"https://localhost/$accountId/api/clusters/666","html_url":"https://localhost/$accountId/clusters/666","creator":{"type":"User","id":352,"display_name":"Alexey Gromov","html_url":"https://localhost/$accountId/settings/members/352","url":"https://localhost/$accountId/api/members/352"}, "bootstrap_actions" : [{"script_path" : "my/super/script.sh", "args" : ["arg1"]}]}""" + } + + put("/:accountId/api/clusters/:clusterId") { + val accountId = params("accountId") + val clusterId = params("clusterId") + s"""{"id":$clusterId,"name":"brown-sea-277","description":"sea is brown in this time of the year","status":"terminated","owner_id":352,"plan_id":"pro","nodes":7,"type":"production","created_at":"2016-01-22T13:22:20Z","updated_at":"2016-01-22T14:33:03Z","available_since":"2016-01-22T13:31:07Z","terminated_at":"2016-01-22T14:33:02Z","launched_at":"2016-01-22T13:31:06Z","terminate_on_idle":true,"time_to_idle":3600,"terminated_on_idle":true,"region":"amazon-web-services::eu-west-1","zone":null,"master_instance_type":null,"slave_instance_type":null,"master_spot_price":null,"slave_spot_price":null,"master_spot_percentage":null,"slave_spot_percentage":null,"allow_fallback":true,"stack":"white-everest","idle_since":"2016-01-22T14:33:02Z","running_jobs_count":0,"url":"https://localhost/$accountId/api/clusters/$clusterId","html_url":"https://localhost/$accountId/clusters/$clusterId","creator":{"type":"User","id":352,"display_name":"Alexey Gromov","html_url":"https://localhost/$accountId/settings/members/352","url":"https://localhost/$accountId/api/members/352"}, "bootstrap_actions" : [{"script_path" : "my/super/script.sh", "args" : ["arg1"]}]}""" + } + + get("/:accountId/api/clusters/:clusterId") { + val accountId = params("accountId") + val clusterId = params("clusterId") + s"""{"id":$clusterId,"name":"brown-sea-277","description":"sea is brown in this time of the year","status":"terminated","owner_id":352,"plan_id":"pro","nodes":7,"type":"production","created_at":"2016-01-22T13:22:20Z","updated_at":"2016-01-22T14:33:03Z","available_since":"2016-01-22T13:31:07Z","terminated_at":"2016-01-22T14:33:02Z","launched_at":"2016-01-22T13:31:06Z","terminate_on_idle":true,"time_to_idle":3600,"terminated_on_idle":true,"region":"amazon-web-services::eu-west-1","zone":null,"master_instance_type":null,"slave_instance_type":null,"master_spot_price":null,"slave_spot_price":null,"master_spot_percentage":null,"slave_spot_percentage":null,"allow_fallback":true,"stack":"white-everest","idle_since":"2016-01-22T14:33:02Z","running_jobs_count":0,"url":"https://localhost/$accountId/api/clusters/$clusterId","html_url":"https://localhost/$accountId/clusters/$clusterId","creator":{"type":"User","id":352,"display_name":"Alexey Gromov","html_url":"https://localhost/$accountId/settings/members/352","url":"https://localhost/$accountId/api/members/352"}, "bootstrap_actions" : [{"script_path" : "my/super/script.sh", "args" : ["arg1"]}]}""" + } + + delete("/:accountId/api/clusters/:clusterId") { + val accountId = params("accountId") + val clusterId = params("clusterId") + s"""{"id":$clusterId,"name":"brown-sea-277","description":"sea is brown in this time of the year","status":"terminated","owner_id":352,"plan_id":"pro","nodes":7,"type":"production","created_at":"2016-01-22T13:22:20Z","updated_at":"2016-01-22T14:33:03Z","available_since":"2016-01-22T13:31:07Z","terminated_at":"2016-01-22T14:33:02Z","launched_at":"2016-01-22T13:31:06Z","terminate_on_idle":true,"time_to_idle":3600,"terminated_on_idle":true,"region":"amazon-web-services::eu-west-1","zone":null,"master_instance_type":null,"slave_instance_type":null,"master_spot_price":null,"slave_spot_price":null,"master_spot_percentage":null,"slave_spot_percentage":null,"allow_fallback":true,"stack":"white-everest","idle_since":"2016-01-22T14:33:02Z","running_jobs_count":0,"url":"https://localhost/$accountId/api/clusters/$clusterId","html_url":"https://localhost/$accountId/clusters/$clusterId","creator":{"type":"User","id":352,"display_name":"Alexey Gromov","html_url":"https://localhost/$accountId/settings/members/352","url":"https://localhost/$accountId/api/members/352"}, "bootstrap_actions" : [{"script_path" : "my/super/script.sh", "args" : ["arg1"]}]}""" + } + + get("/:accountId/api/clusters") { + val accountId = params("accountId") + s"""[{"id":666,"name":"brown-sea-277","description":"sea is brown in this time of the year","status":"terminated","owner_id":352,"plan_id":"pro","nodes":7,"type":"production","created_at":"2016-01-22T13:22:20Z","updated_at":"2016-01-22T14:33:03Z","available_since":"2016-01-22T13:31:07Z","terminated_at":"2016-01-22T14:33:02Z","launched_at":"2016-01-22T13:31:06Z","terminate_on_idle":true,"time_to_idle":3600,"terminated_on_idle":true,"region":"amazon-web-services::eu-west-1","zone":null,"master_instance_type":null,"slave_instance_type":null,"master_spot_price":null,"slave_spot_price":null,"master_spot_percentage":null,"slave_spot_percentage":null,"allow_fallback":true,"stack":"white-everest","idle_since":"2016-01-22T14:33:02Z","running_jobs_count":0,"url":"https://localhost/$accountId/api/clusters/666","html_url":"https://localhost/$accountId/clusters/666","creator":{"type":"User","id":352,"display_name":"Alexey Gromov","html_url":"https://localhost/$accountId/settings/members/352","url":"https://localhost/$accountId/api/members/352"}, "bootstrap_actions" : [{"script_path" : "my/super/script.sh", "args" : ["arg1"]}]}]""" + } + + get("/:accountId/api/clusters/:clusterId/instances") { + val accountId = params("accountId") + val clusterId = params("clusterId") + s"""[{"status":"available","master":true,"spot":false,"vpc":false,"zone":"eu-west1","url":"https://localhost/$accountId/api/clusters/$clusterId/instances/i-4d1b39a7","instance_id":"i-4d1b39a7","private_dns":"ip-10-124-29-23.ec2.internal","public_dns":"ec2-55-27-210-201.compute-1.amazonaws.com","instance_type":"sometype"}]""" + } +} diff --git a/src/test/scala/com/xplenty/api/router/HookRouter.scala b/src/test/scala/com/xplenty/api/router/HookRouter.scala new file mode 100644 index 0000000..cabec5b --- /dev/null +++ b/src/test/scala/com/xplenty/api/router/HookRouter.scala @@ -0,0 +1,67 @@ +package com.xplenty.api.router + +import com.fasterxml.jackson.databind.{ObjectMapper, JsonNode} +import org.scalatra.ScalatraServlet + +/** + * Mock server returning fixed values + * @author xardazz + */ +class HookRouter extends ScalatraServlet { + + post("/:accountId/api/hooks") { + val accountId = params("accountId") + val json = request.body + val jsonNode: JsonNode = new ObjectMapper().readTree(json) + val hookType = jsonNode.get("type").asText() + val settings = hookType match { + case "email" => """{"emails":"a@a.com,b@b.com"}""" + case "hipchat" => """{"room":"xplenty","auth_token":"aaaa"}""" + case "pagerduty" => """{"pd_account":"xplenty1","service_name":"xplenty","service_key":"aaaaaa"}""" + case "slack" => """{"team":"xplenty","channel":"xplentych","url":"http://localhost", "username" : "xardazz"}""" + case "web" => """{"url":"http://localhost/test","insecure_ssl":true,"basic_auth":false,"basic_auth_data":"somedata","encrypted_basic_auth_data":"wtjnIcvVp1fLC2fy9rAsSQ==\\n"}""" + } + + s"""{"id":666,"active":true,"type" : "$hookType","settings": $settings,"salt":"000abcdead","events":[{"id" : 333, "name" : "job", "last_response" : {"code": "200", "body" : "nice event"}, "last_trigger_status" : "success", "last_trigger_time" : "2016-01-18T11:19:20Z"}]}""" + } + + put("/:accountId/api/hooks/:hookId") { + val accountId = params("accountId") + val hookId = params("hookId") + s"""{"id":$hookId,"active":true,"type" : "web","settings":{"url":"http://localhost/test","insecure_ssl":true,"basic_auth":false,"basic_auth_data":"somedata","encrypted_basic_auth_data":"wtjnIcvVp1fLC2fy9rAsSQ==\\n"},"salt":"000abcdead","events":[{"id" : 333, "name" : "job", "last_response" : {"code": "200", "body" : "nice event"}, "last_trigger_status" : "success", "last_trigger_time" : "2016-01-18T11:19:20Z"}]}""" + } + + delete("/:accountId/api/hooks/:hookId") { + val accountId = params("accountId") + val hookId = params("hookId") + s"""{"id":$hookId,"active":true,"type" : "web","settings":{"url":"http://localhost/test","insecure_ssl":true,"basic_auth":false,"basic_auth_data":"somedata","encrypted_basic_auth_data":"wtjnIcvVp1fLC2fy9rAsSQ==\\n"},"salt":"000abcdead","events":[{"id" : 333, "name" : "job", "last_response" : {"code": "200", "body" : "nice event"}, "last_trigger_status" : "success", "last_trigger_time" : "2016-01-18T11:19:20Z"}]}""" + } + + get("/:accountId/api/hooks/:hookId") { + val accountId = params("accountId") + val hookId = params("hookId") + s"""{"id":$hookId,"active":true,"type" : "web","settings":{"url":"http://localhost/test","insecure_ssl":true,"basic_auth":false,"basic_auth_data":"somedata","encrypted_basic_auth_data":"wtjnIcvVp1fLC2fy9rAsSQ==\\n"},"salt":"000abcdead","events":[{"id" : 333, "name" : "job", "last_response" : {"code": "200", "body" : "nice event"}, "last_trigger_status" : "success", "last_trigger_time" : "2016-01-18T11:19:20Z"}]}""" + } + + get("/:accountId/api/hooks") { + val accountId = params("accountId") + s"""[{"id":666,"active":true,"type" : "web","settings":{"url":"http://localhost/test","insecure_ssl":true,"basic_auth":false,"basic_auth_data":"somedata","encrypted_basic_auth_data":"wtjnIcvVp1fLC2fy9rAsSQ==\\n"},"salt":"000abcdead","events":[{"id" : 333, "name" : "job", "last_response" : {"code": "200", "body" : "nice event"}, "last_trigger_status" : "success", "last_trigger_time" : "2016-01-18T11:19:20Z"}]}]""" + } + + get("/:accountId/api/hooks/:hookId/ping") { + val accountId = params("accountId") + val hookId = params("hookId") + s"""{"id":$hookId,"active":true,"type" : "web","settings":{"url":"http://localhost/test","insecure_ssl":true,"basic_auth":false,"basic_auth_data":"somedata","encrypted_basic_auth_data":"wtjnIcvVp1fLC2fy9rAsSQ==\\n"},"salt":"000abcdead","events":[{"id" : 333, "name" : "job", "last_response" : {"code": "200", "body" : "nice event"}, "last_trigger_status" : "success", "last_trigger_time" : "2016-01-18T11:19:20Z"}]}""" + } + + put("/:accountId/api/hooks/:hookId/reset_salt") { + val accountId = params("accountId") + val hookId = params("hookId") + s"""{"salt" : "newsalt"}""" + } + + get("/hook_events") { + s"""[{"id":"job","group_name":"Job","name":"All Job Notifications"},{"id":"job.submitted","group_name":"Job","name":"Job Submitted"},{"id":"job.started","group_name":"Job","name":"Job Started"},{"id":"job.stopped","group_name":"Job","name":"Job Stopped"},{"id":"job.completed","group_name":"Job","name":"Job Completed"},{"id":"job.failed","group_name":"Job","name":"Job Failed"},{"id":"cluster","group_name":"Cluster","name":"All Cluster Notifications"},{"id":"cluster.requested","group_name":"Cluster","name":"Cluster Requested"},{"id":"cluster.available","group_name":"Cluster","name":"Cluster Available"},{"id":"cluster.terminated","group_name":"Cluster","name":"Cluster Terminated"},{"id":"cluster.idled","group_name":"Cluster","name":"Cluster Idled"},{"id":"cluster.error","group_name":"Cluster","name":"Cluster Error"}]""" + } + +} 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/PackageRouter.scala b/src/test/scala/com/xplenty/api/router/PackageRouter.scala new file mode 100644 index 0000000..a66c15d --- /dev/null +++ b/src/test/scala/com/xplenty/api/router/PackageRouter.scala @@ -0,0 +1,63 @@ +package com.xplenty.api.router + +import org.scalatra.ScalatraServlet + +/** + * Mock server returning fixed values + * @author xardazz + */ +class PackageRouter extends ScalatraServlet { + + post("/:accountId/api/packages") { + val accountId = params("accountId") + s"""{"id":666,"name":"TestPack","description":"TestPack Description","variables":{"var_1":"val1","var_2":"supercomplex"},"url":"https://localhost/$accountId/api/package/666","status":"active","owner_id":111,"created_at":"2016-01-14T20:25:12Z","updated_at":"2016-01-14T20:25:12Z","html_url":"https://localhost/$accountId/package/666","flow_type":"workflow"}""" + } + + put("/:accountId/api/packages/:entityId") { + val accountId = params("accountId") + val entityId = params("entityId") + s"""{"id":$entityId,"name":"TestPack","description":"TestPack Description","variables":{"var_1":"val1","var_2":"supercomplex"},"url":"https://localhost/$accountId/api/package/$entityId","status":"active","owner_id":111,"created_at":"2016-01-14T20:25:12Z","updated_at":"2016-01-14T20:25:12Z","html_url":"https://localhost/$accountId/package/$entityId","flow_type":"workflow"}""" + } + + get("/:accountId/api/packages/:entityId") { + val accountId = params("accountId") + val entityId = params("entityId") + s"""{"id":$entityId,"name":"TestPack","description":"TestPack Description","variables":{"var_1":"val1","var_2":"supercomplex"},"url":"https://localhost/$accountId/api/package/$entityId","status":"active","owner_id":111,"created_at":"2016-01-14T20:25:12Z","updated_at":"2016-01-14T20:25:12Z","html_url":"https://localhost/$accountId/package/$entityId","flow_type":"workflow"}""" + } + + get("/:accountId/api/packages") { + val accountId = params("accountId") + s"""[{"id":666,"name":"TestPack","description":"TestPack Description","variables":{"var_1":"val1","var_2":"supercomplex"},"url":"https://localhost/$accountId/api/package/666","status":"active","owner_id":111,"created_at":"2016-01-14T20:25:12Z","updated_at":"2016-01-14T20:25:12Z","html_url":"https://localhost/$accountId/package/666","flow_type":"workflow"}]""" + } + + delete("/:accountId/api/packages/:entityId") { + val accountId = params("accountId") + val entityId = params("entityId") + s"""{"id":$entityId,"name":"TestPack","description":"TestPack Description","variables":{"var_1":"val1","var_2":"supercomplex"},"url":"https://localhost/$accountId/api/package/$entityId","status":"active","owner_id":111,"created_at":"2016-01-14T20:25:12Z","updated_at":"2016-01-14T20:25:12Z","html_url":"https://localhost/$accountId/package/$entityId","flow_type":"workflow"}""" + } + + get("/:accountId/api/packages/templates") { + val accountId = params("accountId") + s"""[{"id":333,"name":"test template","description":"really good template","position":1,"author":{"id":333,"name":"best template author","avatar_url":"https://localhost/$accountId/api/user/333"}}]""" + } + + post("/:accountId/api/packages/:packageId/validations") { + val accountId = params("accountId") + val packageId = params("packageId") + s"""{"id":666,"status":"failed","runtime":1234,"errors":[{"message":"couldn't obtain value for var_1","component_id":"12"}],"url":"https://localhost/$accountId/api/packages/$packageId/validations/666","status_message":"Something bad happened","package_id":$packageId,"owner_id":222,"account_id":111,"created_at":"2016-01-14T20:34:27Z","updated_at":"2016-01-14T20:34:27Z"}""" + } + + get("/:accountId/api/packages/:packageId/validations") { + val accountId = params("accountId") + val packageId = params("packageId") + s"""[{"id":666,"status":"failed","runtime":1234,"errors":[{"message":"couldn't obtain value for var_1","component_id":"12"}],"url":"https://localhost/$accountId/api/packages/$packageId/validations/666","status_message":"Something bad happened","package_id":$packageId,"owner_id":222,"account_id":111,"created_at":"2016-01-14T20:34:27Z","updated_at":"2016-01-14T20:34:27Z"}]""" + } + + get("/:accountId/api/packages/:packageId/validations/:entityId") { + val accountId = params("accountId") + val packageId = params("packageId") + val entityId = params("entityId") + s"""{"id":$entityId,"status":"failed","runtime":1234,"errors":[{"message":"couldn't obtain value for var_1","component_id":"12"}],"url":"https://localhost/$accountId/api/packages/$packageId/validations/$entityId","status_message":"Something bad happened","package_id":$packageId,"owner_id":222,"account_id":111,"created_at":"2016-01-14T20:34:27Z","updated_at":"2016-01-14T20:34:27Z"}""" + } + +} 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/ScheduleRouter.scala b/src/test/scala/com/xplenty/api/router/ScheduleRouter.scala new file mode 100644 index 0000000..22e9445 --- /dev/null +++ b/src/test/scala/com/xplenty/api/router/ScheduleRouter.scala @@ -0,0 +1,45 @@ +package com.xplenty.api.router + +import org.scalatra.ScalatraServlet + +/** + * Author: Xardas + * Date: 24.01.16 + * Time: 10:08 + */ +class ScheduleRouter extends ScalatraServlet { + + post("/:accountId/api/schedules") { + val accountId = params("accountId") + s"""{"id":666,"name":"testSchedule","description":"some neat description","status":"enabled","html_url":"https://localhost/$accountId/schedules/666", "url":"https://localhost/$accountId/api/schedules/666","task":{"nodes":3,"packages":[{"variables":{"var1":"varvalue"},"package_id":1}],"terminate_on_idle":true,"time_to_idle":1800},"owner_id":1,"start_at":"2016-01-24T10:07:42Z","next_run_at":"2016-01-24T10:07:42Z","interval_amount":10,"interval_unit":"days","last_run_at":"2016-01-24T10:07:42Z","last_run_status":"Successfully stored zillion records","execution_count":1,"created_at":"2016-01-24T10:07:42Z","updated_at":"2016-01-24T10:07:42Z", "reuse_cluster_strategy":"any","overlap":true}""" + } + + post("/:accountId/api/schedules/:scheduleId/clone") { + val accountId = params("accountId") + val scheduleId = params("scheduleId") + s"""{"id":666,"name":"testSchedule","description":"some neat description","status":"enabled","html_url":"https://localhost/$accountId/schedules/$scheduleId", "url":"https://localhost/$accountId/api/schedules/$scheduleId","task":{"nodes":3,"packages":[{"variables":{"var1":"varvalue"},"package_id":1}],"terminate_on_idle":true,"time_to_idle":1800},"owner_id":1,"start_at":"2016-01-24T10:07:42Z","next_run_at":"2016-01-24T10:07:42Z","interval_amount":10,"interval_unit":"days","last_run_at":"2016-01-24T10:07:42Z","last_run_status":"Successfully stored zillion records","execution_count":1,"created_at":"2016-01-24T10:07:42Z","updated_at":"2016-01-24T10:07:42Z", "reuse_cluster_strategy":"any","overlap":true}""" + } + + put("/:accountId/api/schedules/:scheduleId") { + val accountId = params("accountId") + val scheduleId = params("scheduleId") + s"""{"id":666,"name":"testSchedule","description":"some neat description","status":"enabled","html_url":"https://localhost/$accountId/schedules/$scheduleId","url":"https://localhost/$accountId/api/schedules/$scheduleId","task":{"nodes":3,"packages":[{"variables":{"var1":"varvalue"},"package_id":1}],"terminate_on_idle":true,"time_to_idle":1800},"owner_id":1,"start_at":"2016-01-24T10:07:42Z","next_run_at":"2016-01-24T10:07:42Z","interval_amount":10,"interval_unit":"days","last_run_at":"2016-01-24T10:07:42Z","last_run_status":"Successfully stored zillion records","execution_count":1,"created_at":"2016-01-24T10:07:42Z","updated_at":"2016-01-24T10:07:42Z", "reuse_cluster_strategy":"any","overlap":true}""" + } + + get("/:accountId/api/schedules/:scheduleId") { + val accountId = params("accountId") + val scheduleId = params("scheduleId") + s"""{"id":666,"name":"testSchedule","description":"some neat description","status":"enabled","html_url":"https://localhost/$accountId/schedules/$scheduleId","url":"https://localhost/$accountId/api/schedules/$scheduleId","task":{"nodes":3,"packages":[{"variables":{"var1":"varvalue"},"package_id":1}],"terminate_on_idle":true,"time_to_idle":1800},"owner_id":1,"start_at":"2016-01-24T10:07:42Z","next_run_at":"2016-01-24T10:07:42Z","interval_amount":10,"interval_unit":"days","last_run_at":"2016-01-24T10:07:42Z","last_run_status":"Successfully stored zillion records","execution_count":1,"created_at":"2016-01-24T10:07:42Z","updated_at":"2016-01-24T10:07:42Z", "reuse_cluster_strategy":"any","overlap":true}""" + } + + delete("/:accountId/api/schedules/:scheduleId") { + val accountId = params("accountId") + val scheduleId = params("scheduleId") + s"""{"id":666,"name":"testSchedule","description":"some neat description","status":"enabled","html_url":"https://localhost/$accountId/schedules/$scheduleId","url":"https://localhost/$accountId/api/schedules/$scheduleId","task":{"nodes":3,"packages":[{"variables":{"var1":"varvalue"},"package_id":1}],"terminate_on_idle":true,"time_to_idle":1800},"owner_id":1,"start_at":"2016-01-24T10:07:42Z","next_run_at":"2016-01-24T10:07:42Z","interval_amount":10,"interval_unit":"days","last_run_at":"2016-01-24T10:07:42Z","last_run_status":"Successfully stored zillion records","execution_count":1,"created_at":"2016-01-24T10:07:42Z","updated_at":"2016-01-24T10:07:42Z", "reuse_cluster_strategy":"any","overlap":true}""" + } + + get("/:accountId/api/schedules") { + val accountId = params("accountId") + s"""[{"id":666,"name":"testSchedule","description":"some neat description","status":"enabled","html_url":"https://localhost/$accountId/schedules/666","url":"https://localhost/$accountId/api/schedules/666","task":{"nodes":3,"packages":[{"variables":{"var1":"varvalue"},"package_id":1}],"terminate_on_idle":true,"time_to_idle":1800},"owner_id":1,"start_at":"2016-01-24T10:07:42Z","next_run_at":"2016-01-24T10:07:42Z","interval_amount":10,"interval_unit":"days","last_run_at":"2016-01-24T10:07:42Z","last_run_status":"Successfully stored zillion records","execution_count":1,"created_at":"2016-01-24T10:07:42Z","updated_at":"2016-01-24T10:07:42Z", "reuse_cluster_strategy":"any","overlap":true}]""" + } +} diff --git a/src/test/scala/com/xplenty/api/router/UserRouter.scala b/src/test/scala/com/xplenty/api/router/UserRouter.scala new file mode 100644 index 0000000..ab15607 --- /dev/null +++ b/src/test/scala/com/xplenty/api/router/UserRouter.scala @@ -0,0 +1,32 @@ +package com.xplenty.api.router + +import org.scalatra.ScalatraServlet + +/** + * Mock server returning fixed values + * @author xardazz + */ +class UserRouter extends ScalatraServlet { + + put("/user") { + s"""{"id":666,"name":"Vasya Pupkin","email":"test@xplenty.com","location":"Colorado","confirmed":true,"url":"https://api.xplenty.com/user","gravatar_email":"test@gravatar.com","avatar_url":"https://secure.gravatar.com/avatar/8318e89006033f0f8eec181f1fcec54e276.png","time_zone":"UTC","confirmed_at":"2016-01-17T19:41:12Z","notifications_count":7,"unread_notifications_count":3,"notification_settings":{"email":true,"web":false},"receive_newsletter":true,"created_at":"2016-01-17T19:41:12Z","updated_at":"2016-01-17T19:41:12Z","api_key":null,"last_login":"2016-01-17T19:41:12Z"}""" + } + + get("/user") { + val json = request.body + val apiKey = if (json.contains("\"current_password\":\"supersecretpassword\"")) "\"yepitsapikey\"" else "null" + s"""{"id":666,"name":"Vasya Pupkin","email":"test@xplenty.com","location":"Colorado","confirmed":true,"url":"https://api.xplenty.com/user","gravatar_email":"test@gravatar.com","avatar_url":"https://secure.gravatar.com/avatar/8318e89006033f0f8eec181f1fcec54e276.png","time_zone":"UTC","confirmed_at":"2016-01-17T19:41:12Z","notifications_count":7,"unread_notifications_count":3,"notification_settings":{"email":true,"web":false},"receive_newsletter":true,"created_at":"2016-01-17T19:41:12Z","updated_at":"2016-01-17T19:41:12Z","api_key":$apiKey,"last_login":"2016-01-17T19:41:12Z"}""" + } + + get("/user/notifications") { + s"""[{"id":777,"title":"Cluster available","message":"Cluster is available","last_read_at":"2016-01-17T19:42:18Z","created_at":"2016-01-17T19:42:18Z","updated_at":"2016-01-17T19:42:18Z"}]""" + } + + post("/user/notifications/mark") { + """""" + } + + post("/user_password") { + halt(201) + } +} diff --git a/xplenty.jar-integ-test/src/main/com/xplenty/Router.scala b/src/test/scala/com/xplenty/api/router/WatchersRouter.scala similarity index 57% rename from xplenty.jar-integ-test/src/main/com/xplenty/Router.scala rename to src/test/scala/com/xplenty/api/router/WatchersRouter.scala index e441b27..73525fe 100644 --- a/xplenty.jar-integ-test/src/main/com/xplenty/Router.scala +++ b/src/test/scala/com/xplenty/api/router/WatchersRouter.scala @@ -1,22 +1,19 @@ -package com.xplenty +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. + * Mock server returning fixed values + * @author alexanderdomeshek */ -class Router extends ScalatraServlet{ +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: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,10 +25,11 @@ 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"){ 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 new file mode 100644 index 0000000..3af133d --- /dev/null +++ b/src/test/scala/com/xplenty/api/webapp/WEB-INF/web.xml @@ -0,0 +1,80 @@ + + + + Watchers + com.xplenty.api.router.WatchersRouter + + + Members + com.xplenty.api.router.MemberRouter + + + PK + com.xplenty.api.router.PublicKeyRouter + + + Account + com.xplenty.api.router.AccountRouter + + + Package + com.xplenty.api.router.PackageRouter + + + User + com.xplenty.api.router.UserRouter + + + Hook + com.xplenty.api.router.HookRouter + + + Cluster + com.xplenty.api.router.ClusterRouter + + + Schedule + com.xplenty.api.router.ScheduleRouter + + + + Watchers + /mock/watchers/* + + + Members + /mock/members/* + + + PK + /mock/pk/* + + + Account + /mock/account/* + + + Package + /mock/package/* + + + User + /mock/user/* + + + Hook + /mock/hook/* + + + Cluster + /mock/cluster/* + + + Schedule + /mock/schedule/* + + \ No newline at end of file diff --git a/xplenty.jar-core/pom.xml b/xplenty.jar-core/pom.xml deleted file mode 100644 index 22bd140..0000000 --- a/xplenty.jar-core/pom.xml +++ /dev/null @@ -1,89 +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 - - - - com.sun.jersey - jersey-core - 1.9.1 - - - - com.sun.jersey - jersey-json - 1.9.1 - - - - 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 - - - - 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/main/java/com/xplenty/api/Xplenty.java b/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java deleted file mode 100644 index b13c0d9..0000000 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/Xplenty.java +++ /dev/null @@ -1,186 +0,0 @@ -/** - * - */ -package com.xplenty.api; - -import com.fasterxml.jackson.annotation.JsonFormat; - -/** - * A bunch of convenience structures - * @author Yuriy Kovalek - * - */ -@SuppressWarnings("unused") -public class Xplenty { - public static final int MAX_LIMIT = 100; - - public enum Sort { - updated("updated"), - created("created"); - - public final String value; - - Sort(String value) { - this.value = value; - } - - public String toString() { - return value; - } - } - - public enum SortDirection { - ascending("asc"), - descending("desc"); - - public final String value; - - SortDirection(String value) { - this.value = value; - } - - public String toString() { - return value; - } - } - - public enum JobStatus { - idle("idle"), - stopped("stopped"), - completed("completed"), - pending("pending"), - failed("failed"), - running("running"), - pending_stoppage("pending_stoppage"), - stopping("stopping"); - - @SuppressWarnings("unused") - private final String status; - - JobStatus(String status) { - this.status = status; - } - - } - - public enum ScheduleStatus { - enabled("enabled"), - disabled("disabled"), - /** - * used only for filtering meaning all the statuses above - */ - all("all") - ; - @SuppressWarnings("unused") - private final String status; - - ScheduleStatus(String status) { - this.status = status; - } - } - - public enum ScheduleIntervalUnit { - - minutes("minutes"), - hours("hours"), - days("days"), - weeks("weeks"), - months("months"); - - @SuppressWarnings("unused") - private final String unit; - - ScheduleIntervalUnit(String unit) { - this.unit = unit; - } - } - - public enum ClusterType { - production("production"), - sandbox("sandbox"); - - @SuppressWarnings("unused") - private final String type; - - ClusterType(String type) { - this.type = type; - } - - } - - @JsonFormat(shape= JsonFormat.Shape.SCALAR) - public static enum ClusterStatus { - pending("pending"), - error("error"), - creating("creating"), - available("available"), - scaling("scaling"), - pending_terminate("pending_terminate"), - terminating("terminating"), - terminated("terminated"); - - @SuppressWarnings("unused") - private final String status; - - ClusterStatus(String status) { - this.status = status; - } - } - - /** - * Endpoints and short descriptions for REST resources - */ - public static enum Resource { - Package("packages/%s", "Get package info"), - Packages("packages", "List packages"), - ClusterPlans("cluster_plans", "List cluster plans"), - Clusters("clusters", "List clusters"), - Cluster("clusters/%s", "Get cluster information"), - CreateCluster("clusters", "Create cluster"), - UpdateCluster("clusters/%s", "Update cluster"), - TerminateCluster("clusters/%s", "Terminate cluster"), - Jobs("jobs", "List jobs"), - 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"), - 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") - ; - - public final String value; - public final String name; - - Resource(String val, String name) { - this.value = val; - this.name = name; - } - - public String format(String... values) { - return String.format(value, (Object[]) values); - } - } - - public static enum Version { - V1(1); - - private final int value; - - Version(int ver) { - this.value = ver; - } - - public String format() { - return "version=" + Integer.toString(value); - } - } - - public static enum SubjectType { - CLUSTER, JOB - } -} 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 deleted file mode 100644 index fbceeb1..0000000 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyAPI.java +++ /dev/null @@ -1,470 +0,0 @@ -/** - * - */ -package com.xplenty.api; - -import java.util.List; -import java.util.Map; -import java.util.Properties; - -import com.xplenty.api.Xplenty.ClusterType; -import com.xplenty.api.Xplenty.Version; -import com.xplenty.api.exceptions.XplentyAPIException; -import com.xplenty.api.model.Cluster; -import com.xplenty.api.model.ClusterWatchingLogEntry; -import com.xplenty.api.model.Job; -import com.xplenty.api.model.JobWatchingLogEntry; -import com.xplenty.api.model.Package; -import com.xplenty.api.model.Schedule; -import com.xplenty.api.model.Watcher; -import com.xplenty.api.request.AbstractParametrizedRequest; -import com.xplenty.api.request.CloneSchedule; -import com.xplenty.api.request.ClusterInfo; -import com.xplenty.api.request.CreateCluster; -import com.xplenty.api.request.CreateSchedule; -import com.xplenty.api.request.DeleteSchedule; -import com.xplenty.api.request.JobInfo; -import com.xplenty.api.request.ListClusters; -import com.xplenty.api.request.ListJobs; -import com.xplenty.api.request.ListPackages; -import com.xplenty.api.request.ListSchedules; -import com.xplenty.api.request.PackageInfo; -import com.xplenty.api.request.RunJob; -import com.xplenty.api.request.ScheduleInfo; -import com.xplenty.api.request.StopJob; -import com.xplenty.api.request.TerminateCluster; -import com.xplenty.api.request.UpdateCluster; -import com.xplenty.api.request.UpdateSchedule; -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.util.Http; -/** - * A convenience class for making HTTP requests to the Xplenty API for a given user. An underlying {@link XplentyWebConnectivity} is created - * for each instance of XplentyAPI. - *

- * Example usage: - *

{@code
- *		XplentyAPI api = new XplentyAPI("accountName", "apiKey");
- *List plans = api.listClusterPlans();
- * }
- * 
- * - * {@link RuntimeException} will be thrown for any request failures. - * - * @author Yuriy Kovalek - */ -public class XplentyAPI extends XplentyWebConnectivity { - - /** - * Constructs a XplentyAPI with a {@link XplentyWebConnectivity} 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); - } - - /** - * Constructs a XplentyAPI with a {@link XplentyWebConnectivity} 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 - * @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) - */ - public XplentyAPI(String accountName, String apiKey, String host, Http.Protocol proto){ - this(accountName, apiKey, host, proto, false); - } - - /** - * 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 - */ - 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 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 - * @param packageId of the package to get - * @return package object - */ - public Package getPackageInfo(long packageId) { - return this.execute(new PackageInfo(packageId)); - } - - /** - * List of packages associated with the account - * @param props map of request parameters - * @return list of packages - */ - public List listPackages(Properties props) { - return this.execute(new ListPackages(props)); - } - - /** - * List of schedules associated with the account - * @param offset number of record to start results from - * @param limit number of results - * @return list of schedules - */ - public List listPackages(int offset, int limit) { - final Properties props = new Properties(); - props.put(AbstractParametrizedRequest.PARAMETER_LIMIT, limit); - props.put(AbstractParametrizedRequest.PARAMETER_OFFSET, offset); - return listPackages(props); - } - - /** - * List of schedules associated with the account - * @return list of schedules - */ - public List listSchedules() { - return listSchedules(new Properties()); - } - - /** - * List of schedules associated with the account - * @param offset number of record to start results from - * @param limit number of results - * @return list of schedules - */ - public List listSchedules(int offset, int limit) { - final Properties props = new Properties(); - props.put(AbstractParametrizedRequest.PARAMETER_LIMIT, limit); - props.put(AbstractParametrizedRequest.PARAMETER_OFFSET, offset); - return listSchedules(props); - } - - /** - * 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} - * @return list of schedules - */ - public List listSchedules(Properties props) { - return this.execute(new ListSchedules(props)); - } - - /** - * List of clusters associated with the account - * @return list of clusters - */ - public List listClusters() { - return listClusters(new Properties()); - } - - /** - * List of clusters associated with the account - * @param offset number of record to start results from - * @param limit number of results - * @return list of clusters - */ - public List listClusters(int offset, int limit) { - final Properties props = new Properties(); - props.put(AbstractParametrizedRequest.PARAMETER_LIMIT, limit); - props.put(AbstractParametrizedRequest.PARAMETER_OFFSET, offset); - return listClusters(props); - } - - - /** - * 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} - * @return list of clusters - */ - public List listClusters(Properties props) { - return this.execute(new ListClusters(props)); - } - /** - * Information about a particular cluster - * @param clusterId id of the cluster, see {@link #listClusters()} to get a list of clusters with id's - * @return - */ - public Cluster clusterInformation(long clusterId) { - return this.execute(new ClusterInfo(clusterId)).withParentApiInstance(this); - } - - /** - * Create cluster with specified properties - * @param nodes number of nodes - * @param type of cluster - 'sandbox' if sandbox - * @param name cluster name - * @param description cluster description - * @param terminateOnIdle should the cluster terminate on idle status - * @param timeToIdle time in seconds before cluster is considered idle - * @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); - } - - /** - * Update cluster with specified properties - * @param id id of cluster - * @param nodes number of nodes - * @param name cluster name - * @param description cluster description - * @param terminateOnIdle should the cluster terminate on idle status - * @param timeToIdle time in seconds before cluster is considered idle - * @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); - } - - /** - * Terminate cluster with given id - * @param clusterId is clusterId - * @return - */ - public Cluster terminateCluster(long clusterId) { - return this.execute(new TerminateCluster(clusterId)).withParentApiInstance(this); - } - - /** - * List of jobs associated with the account - * @return list of jobs - */ - public List listJobs() { - return listJobs(new Properties()); - } - - /** - * List of jobs associated with the account - * @param offset number of record to start results from - * @param limit number of results - * @return list of jobs - */ - public List listJobs(int offset, int limit) { - final Properties props = new Properties(); - props.put(AbstractParametrizedRequest.PARAMETER_LIMIT, limit); - props.put(AbstractParametrizedRequest.PARAMETER_OFFSET, offset); - return listJobs(props); - } - - /** - * 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} - * @return list of jobs - */ - public List listJobs(Properties params) { - return this.execute(new ListJobs(params)); - } - - /** - * 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 this.execute(new JobInfo(jobId)).withParentApiInstance(this); - } - - /** - * Execute a job on a cluster - * @param clusterId cluster to execute on, see {@link #listClusters()} to get a list of available clusters - * @param packageId package id, obtained from account web page - * @param variables map of static variables to be passed to the job - * @return - */ - public Job runJob(long clusterId, long packageId, Map variables) { - return runJob(clusterId, packageId, variables, false); - } - - /** - * Execute a job on a cluster - * @param clusterId cluster to execute on, see {@link #listClusters()} to get a list of available clusters - * @param packageId package id, obtained from account web page - * @param variables map of variables to be passed to the job - * @param dynamicVariables indication whether the variables should be treated as dynamic - * @return - */ - public Job runJob(long clusterId, long packageId, Map variables, boolean dynamicVariables) { - Job job = new Job().onCluster(clusterId).withPackage(packageId); - if (dynamicVariables) - job = job.withDynamicVariables(variables); - else - job = job.withVariables(variables); - return this.execute(new RunJob(job)).withParentApiInstance(this); - } - - /** - * Stop a particular job - * @param jobId id of job to stop, see {@link #listJobs()} to get a list of jobs - * @return - */ - public Job stopJob(long jobId) { - return this.execute(new StopJob(jobId)).withParentApiInstance(this); - } - - public List listClusterWatchers(long clusterId) { - return this.execute(new ListWatchers(Xplenty.SubjectType.CLUSTER, clusterId)); - } - - public List listJobWatchers(long clusterId) { - return this.execute(new ListWatchers(Xplenty.SubjectType.JOB, clusterId)); - } - - public ClusterWatchingLogEntry addClusterWatchers(long clusterId) { - return this.execute(new AddClusterWatcher(clusterId)).withParentApiInstance(this); - } - - public JobWatchingLogEntry addJobWatchers(long jobId) { - return this.execute(new AddJobWatcher(jobId)).withParentApiInstance(this); - } - - - public Boolean removeClusterWatchers(long clusterId) { - return this.execute(new WatchingStop(Xplenty.SubjectType.CLUSTER, clusterId)); - } - - public Boolean removeJobWatchers(long jobId) { - return this.execute(new WatchingStop(Xplenty.SubjectType.JOB, jobId)); - } - - /** - * Creates new schedule - * @param schedule Schedule to create. All fields that have public setters (except for id) should be set - * @return newly created schedule object - */ - public Schedule createSchedule(Schedule schedule) { - if (schedule.getId() != null) { - schedule.setId(null); - } - return this.execute(new CreateSchedule(schedule)); - } - - /** - * Update schedule - * @param schedule Schedule to update. Any fields that have public setters can be set. Id must be set! - * @return updated schedule object - */ - public Schedule updateSchedule(Schedule schedule) { - if (schedule.getId() == null) { - throw new XplentyAPIException("No id specified!"); - } - return this.execute(new UpdateSchedule(schedule)); - } - - /** - * Delete schedule - * @param scheduleId Id of schedule to delete - * @return deleted schedule object - */ - public Schedule deleteSchedule(long scheduleId) { - if (scheduleId == 0) { - throw new XplentyAPIException("No id specified!"); - } - return this.execute(new DeleteSchedule(scheduleId)); - } - - /** - * Clone schedule - * @param scheduleId Id of schedule to clone - * @return cloned schedule object - */ - public Schedule cloneSchedule(long scheduleId) { - if (scheduleId == 0) { - throw new XplentyAPIException("No id specified!"); - } - return this.execute(new CloneSchedule(scheduleId)); - } - - /** - * Get schedule info - * @param scheduleId Id of schedule to get - * @return schedule object - */ - public Schedule getScheduleInfo(long scheduleId) { - if (scheduleId == 0) { - throw new XplentyAPIException("No id specified!"); - } - return this.execute(new ScheduleInfo(scheduleId)); - } - - /** - * Account name this XplentyAPI instance is associated with - * @return - */ - public String getAccountName() { - return super.getAccountName(); - } - - /** - * API key used by this XplentyAPI instance - * @return - */ - public String getApiKey() { - return super.getApiKey(); - } - - /** - * API host this instance uses - * @return - */ - public String getHost() { - return super.getHost(); - } - - /** - * Protocol this API instance uses - * @return - */ - public Http.Protocol getProtocol() { - return super.getProtocol(); - } - - /** - * API version - * @return - */ - public Version getVersion() { - return super.getVersion(); - } -} diff --git a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyWebConnectivity.java b/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyWebConnectivity.java deleted file mode 100644 index 00a795f..0000000 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/XplentyWebConnectivity.java +++ /dev/null @@ -1,171 +0,0 @@ -/** - * - */ -package com.xplenty.api; - -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.WebResource; -import com.sun.jersey.api.client.config.ClientConfig; -import com.sun.jersey.api.client.config.DefaultClientConfig; -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; -import com.xplenty.api.util.Http; - -import java.io.StringWriter; -import java.text.DateFormat; -import java.text.SimpleDateFormat; - -/** - * Proxy for connecting to the XplentyAPI over HTTP - * - * @author Yuriy Kovalek - * - */ -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; - - private final Client client; - private Version version = null; - - /** - * 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 - */ - XplentyWebConnectivity(String accountName, String apiKey) { - this(accountName, apiKey, false); - } - - /** - * 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, "")); - if (logHttpCommunication) { - client.addFilter(new LoggingFilter()); - } - } - - /** - * Synchronously execute given request - * @param request request to execute - * @return respective response type - */ - public T execute(Request request) { - WebResource.Builder builder = getConfiguredResource(request); - ClientResponse response = null; - switch (request.getHttpMethod()) { - case GET: response = builder.get(ClientResponse.class); break; - case POST: response = builder.post(ClientResponse.class); break; - case PUT: response = builder.put(ClientResponse.class); break; - case DELETE: response = builder.delete(ClientResponse.class); break; - } - validate(request, response); - return request.getResponse(response); - } - - /** - * Convenience method for getting a configured {@link WebResource.Builder} for given request - * @param request that would be submitted to the XPlenty Server - * @return builder - */ - private WebResource.Builder getConfiguredResource(Request request) { - WebResource.Builder b = client.resource(getMethodURL(request.getEndpoint())) - .accept(request.getResponseType().value - + (version == null ? "" : "; " + version.format()) - ); - if (request.hasBody()) { - StringWriter sw = new StringWriter(); - try { - final ObjectMapper objectMapper = new ObjectMapper(); - - objectMapper.setDateFormat(dateFormat); - 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; - } - - /** - * Check the response status and throws exception on errors - * @param request used request - * @param response received response - * @throws AuthFailedException - * @throws 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)); - } - - String getAccountName() { - return ACCOUNT_NAME; - } - - String getApiKey() { - return API_KEY; - } - - 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; - } - - public String getHost() { - return HOST; - } - - public Version getVersion() { - return version; - } -} 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 deleted file mode 100644 index 47251ef..0000000 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/exceptions/RequestFailedException.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * - */ -package com.xplenty.api.exceptions; - -/** - * @author Yuriy Kovalek - * - */ -public class RequestFailedException extends XplentyAPIException { - private static final long serialVersionUID = -456749863406425145L; - - private int status; - private String response; - - public RequestFailedException(String msg, int status, String response) { - super(msg + " HTTP status code: " + status + ", server response: " + response); - this.status = status; - this.response = response; - } - - public int getStatus() { - return status; - } - - public String getResponse() { - return response; - } -} 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 deleted file mode 100644 index d0856f7..0000000 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Cluster.java +++ /dev/null @@ -1,240 +0,0 @@ -/** - * - */ -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; - -/** - * Data model for Xplenty cluster - * - * @author Yuriy Kovalek - * - */ -@XmlRootElement -@JsonInclude(Include.NON_NULL) -@JsonIgnoreProperties(ignoreUnknown = true) -public class Cluster extends XplentyObject{ - protected Long id; - protected String name; - protected String description; - protected ClusterStatus status; - @JsonProperty("owner_id") - protected Long ownerId; - protected Integer nodes; - protected ClusterType type; - @JsonProperty("created_at") - protected Date createdAt; - @JsonProperty("updated_at") - protected Date updatedAt; - @JsonProperty("available_since") - protected Date availableSince; - @JsonProperty("terminated_at") - protected Date terminatedAt; - @JsonProperty("running_jobs_count") - protected Long runningJobsCount; - protected String url; - @JsonProperty("terminate_on_idle") - protected Boolean terminateOnIdle; - @JsonProperty("time_to_idle") - protected Long timeToIdle; - @JsonProperty("terminated_on_idle") - protected Boolean terminatedOnIdle; - - public Cluster() { - super(Cluster.class); - } - - public Cluster withNodes(Integer nodes) { - this.nodes = nodes; - return this; - } - - public Cluster withId(Long id) { - this.id = id; - return this; - } - - public Cluster named(String name) { - this.name = name; - return this; - } - - public Cluster withDescription(String description) { - this.description = description; - return this; - } - - public Cluster ofType(ClusterType type) { - this.type = type; - return this; - } - - public Cluster withTerminateOnIdle(Boolean terminateOnIdle) { - this.terminateOnIdle = terminateOnIdle; - return this; - } - - public Cluster withTimeToIdle(Long timeToIdle) { - this.timeToIdle = timeToIdle; - 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...)} - */ - public void waitForStatus(ClusterStatus... statuses) { - waitForStatus(null, statuses); - } - - /** - * Blocks execution until required status is received from the Xplenty server, or until timeout occurs. - * @param timeout time in seconds before terminating the wait, {@code null} to wait forever - * @param statuses list of statuses to wait for, see {@link ClusterStatus} for the list of supported statuses - */ - public void waitForStatus(Long timeout, ClusterStatus... statuses) { - if (getParentApiInstance() == null) - throw new XplentyAPIException("The parent API instance is not set"); - long start = System.currentTimeMillis(); - statusWait: - while (true) { - try { - Thread.sleep(XplentyObject.StatusRefreshInterval); - } catch (InterruptedException e) { - throw new XplentyAPIException("Error sleeping", e); - } - Cluster c = getParentApiInstance().clusterInformation(id); - for (ClusterStatus status: statuses) { - if (c.getStatus() == status) - break statusWait; - } - if (timeout != null && System.currentTimeMillis() - timeout*1000 > start) - throw new XplentyAPIException("Timeout occurred while waiting for required cluster status"); - } - } - - public Long getId() { - return id; - } - public String getName() { - return name; - } - public String getDescription() { - return description; - } - public ClusterStatus getStatus() { - return status; - } - public Long getOwnerId() { - return ownerId; - } - public Integer getNodes() { - return nodes; - } - public ClusterType getType() { - return type; - } - public Date getCreatedAt() { - return createdAt; - } - public Date getUpdatedAt() { - return updatedAt; - } - public Date getAvailableSince() { - return availableSince; - } - public Date getTerminatedAt() { - return terminatedAt; - } - public Long getRunningJobsCount() { - return runningJobsCount; - } - public String getUrl() { - return url; - } - public Boolean getTerminateOnIdle() { - return terminateOnIdle; - } - public Long getTimeToIdle() { - return timeToIdle; - } - 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; - } - @SuppressWarnings("unused") - private void setType(ClusterType type) { - this.type = type; - } - @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; - } - @SuppressWarnings("unused") - private void setTerminatedAt(Date terminatedAt) { - this.terminatedAt = terminatedAt; - } - @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/Package.java b/xplenty.jar-core/src/main/java/com/xplenty/api/model/Package.java deleted file mode 100644 index a5402bb..0000000 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/model/Package.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.xplenty.api.model; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; - -import java.util.Date; -import java.util.Map; - -/** - * Data model for Xplenty package - * Author: Xardas - * 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 - protected String name; - @JsonProperty - protected String description; - @JsonProperty - protected Map variables; - @JsonProperty("owner_id") - protected Long ownerId; - @JsonProperty("created_at") - protected Date createdAt; - @JsonProperty("updated_at") - protected Date updatedAt; - @JsonProperty - protected String url; - - protected Package() { - super(Package.class); - } - - - /** - * - * @return the numeric package ID - */ - public Long getId() { - return id; - } - - /** - * - * @return the name given to the package upon creation - */ - public String getName() { - return name; - } - - /** - * - * @return the description given to the package upon creation - */ - public String getDescription() { - return description; - } - - /** - * - * @return the list of package variables - */ - public Map getVariables() { - return variables; - } - - /** - * - * @return the numeric user id of the package owner - */ - public Long getOwnerId() { - return ownerId; - } - - /** - * - * @return the date and time the package was created - */ - public Date getCreatedAt() { - return createdAt; - } - - /** - * - * @return the date and time the package was last updated - */ - public Date getUpdatedAt() { - return updatedAt; - } - - /** - * - * @return the package resource URL - */ - 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; - } - - @SuppressWarnings("unused") - private void setUpdatedAt(Date updatedAt) { - this.updatedAt = updatedAt; - } - - @SuppressWarnings("unused") - private void setUrl(String url) { - this.url = url; - } -} 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/JobInfo.java deleted file mode 100644 index 9d160b5..0000000 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/JobInfo.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * - */ -package com.xplenty.api.request; - -import com.xplenty.api.Xplenty; -import com.xplenty.api.model.Job; - -/** - * @author Yuriy Kovalek - * - */ -public class JobInfo extends AbstractInfoRequest { - - public JobInfo(long entityId) { - super(entityId); - } - - @Override - public String getName() { - return Xplenty.Resource.Job.name; - } - - @Override - public String getEndpoint() { - return Xplenty.Resource.Job.format(Long.toString(entityId)); - } - -} 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 deleted file mode 100644 index 1571a20..0000000 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListJobs.java +++ /dev/null @@ -1,79 +0,0 @@ -/** - * - */ -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.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; - -/** - * @author Yuriy Kovalek - * - */ -public class ListJobs extends AbstractParametrizedRequest> { - - public ListJobs(Properties params) { - super(params, true); - validateParameters(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"); - } - - @Override - 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() { - return Xplenty.Resource.Jobs.value; - } - - @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); - } - } - - @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 deleted file mode 100644 index 1f1127c..0000000 --- a/xplenty.jar-core/src/main/java/com/xplenty/api/request/ListPackages.java +++ /dev/null @@ -1,72 +0,0 @@ -/** - * - */ -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.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; - - -/** - * Request for retrieval of all available packages - * Author: Xardas - * Date: 16.12.15 - * Time: 18:08 - */ -public class ListPackages extends AbstractParametrizedRequest> { - - 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; - } - - @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); - } - } - - @Override - 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/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/XplentyAPITest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java deleted file mode 100644 index a299387..0000000 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/XplentyAPITest.java +++ /dev/null @@ -1,86 +0,0 @@ -/** - * - */ -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.request.Request; - -import junit.framework.Assert; - -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; - -/** - * @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() { - XplentyAPI api = new XplentyAPI("testAcc", "testKey").withHost("www.example.com") - .withProtocol(Http.Protocol.Http) - .withVersion(Xplenty.Version.V1); - - assertNotNull(api); - assertEquals("www.example.com", api.getHost()); - assertEquals(Http.Protocol.Http, api.getProtocol()); - assertEquals(Xplenty.Version.V1, api.getVersion()); - } - - public void testListClusters() { - XplentyAPI api = new XplentyAPI("dontcare", "dontcare"){ - @Override - public T execute(Request request) { - ClientResponse mockedResponse = mock(ClientResponse.class); - when(mockedResponse.getEntity(String.class)).thenReturn( - "[{\"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); - } - }; - 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() { - XplentyAPI api = new XplentyAPI("don't care", "don't care"){ - @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); - } - }; - 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-core/src/test/java/com/xplenty/api/model/ClusterTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/ClusterTest.java deleted file mode 100644 index 4c4bec2..0000000 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/model/ClusterTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * - */ -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; - -/** - * @author Yuriy Kovalek - * - */ -public class ClusterTest extends TestCase { - @Test - public void testBuilder() { - Cluster c = new Cluster().withDescription("description") - .named("my cluster") - .withNodes(2); - - assertEquals("description", c.getDescription()); - assertEquals("my cluster", c.getName()); - assertEquals("description", c.getDescription()); - assertEquals(new Integer(2), c.getNodes()); - } - - public static Cluster createMockCluster(Date now) { - Cluster c = new Cluster(); - c.createdAt = now; - c.updatedAt = now; - c.availableSince = now; - c.terminatedAt = now; - c.name = "my cluster"; - c.description = "description"; - c.ownerId = 1L; - c.nodes = 2; - c.type = ClusterType.production; - c.id = 3L; - c.runningJobsCount = 0L; - c.status = ClusterStatus.available; - c.url = "https://www.xplenty.com/api/" + Xplenty.Resource.Cluster.format(Long.toString(3)); - c.terminateOnIdle = true; - c.timeToIdle = 3600L; - c.terminatedOnIdle = false; - return c; - } -} diff --git a/xplenty.jar-core/src/test/java/com/xplenty/api/model/ScheduleTest.java b/xplenty.jar-core/src/test/java/com/xplenty/api/model/ScheduleTest.java deleted file mode 100644 index f009d59..0000000 --- a/xplenty.jar-core/src/test/java/com/xplenty/api/model/ScheduleTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.xplenty.api.model; - -import com.xplenty.api.Xplenty; -import junit.framework.TestCase; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -/** - * Author: Xardas - * Date: 17.12.15 - * Time: 21:18 - */ -public class ScheduleTest extends TestCase { - - - @Test - public void testBuilder() { - Schedule sched = new Schedule(); - assertNotNull(sched); - } - - public static Schedule createMockSchedule(Date now) { - final Schedule sched = new Schedule(); - - sched.createdAt = now; - sched.updatedAt = now; - sched.startAt = now; - sched.nextRunAt = now; - sched.lastRunAt = now; - sched.intervalAmount = 10L; - sched.intervalUnit = Xplenty.ScheduleIntervalUnit.days; - sched.id = 666L; - sched.name = "testSchedule"; - sched.description = "some neat description"; - sched.ownerId = 1L; - sched.status = Xplenty.ScheduleStatus.enabled; - sched.lastRunStatus = "Successfully stored zillion records"; - sched.executionCount = 1L; - sched.url = String.format("https://api.xplenty.com/testacc/api/%s", Xplenty.Resource.Schedule.format(Long.toString(sched.id))); - final ScheduleTask task = new ScheduleTask(); - task.nodes = 3; - task.terminateOnIdle = true; - task.timeToIdle = 1800; - final ArrayList packages = new ArrayList(); - final ScheduleTaskPackage taskPackage = new ScheduleTaskPackage(); - taskPackage.packageId = 1L; - final Map vars = new HashMap(); - vars.put("var1", "varvalue"); - taskPackage.variables = vars; - packages.add(taskPackage); - task.packages = packages; - sched.task = task; - - return sched; - } -} 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 diff --git a/xplenty.jar-integ-test/src/main/webapp/WEB-INF/web.xml b/xplenty.jar-integ-test/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index cb00261..0000000 --- a/xplenty.jar-integ-test/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - Hello - com.xplenty.Router - - - Hello - /mock/* - - \ No newline at end of file