modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/domain/OperationDefinitionRest.java
| 4
modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/OperationsTest.java
| 248 ++++++++++
modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/Operation.java
| 129 +++++
modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/OperationHistory.java
| 108 ++++
4 files changed, 486 insertions(+), 3 deletions(-)
New commits:
commit 245fb391a81fe6a3c5c7331bb6c683fce63a7530
Author: Heiko W. Rupp <hwr(a)redhat.com>
Date: Tue Dec 18 17:10:41 2012 +0100
Add tests for the operations part of the rest-api
diff --git
a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/domain/OperationDefinitionRest.java
b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/domain/OperationDefinitionRest.java
index 2f5055c..f84149e 100644
---
a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/domain/OperationDefinitionRest.java
+++
b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/domain/OperationDefinitionRest.java
@@ -1,9 +1,7 @@
package org.rhq.enterprise.server.rest.domain;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
@@ -15,7 +13,7 @@ import com.wordnik.swagger.annotations.ApiProperty;
* @author Heiko W. Rupp
*/
@ApiClass(value = "The template from which operations are created")
-@XmlRootElement
+@XmlRootElement(name = "operationDefinition")
public class OperationDefinitionRest {
public OperationDefinitionRest() {
diff --git
a/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/OperationsTest.java
b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/OperationsTest.java
new file mode 100644
index 0000000..72d40c9
--- /dev/null
+++
b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/OperationsTest.java
@@ -0,0 +1,248 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2012 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+package org.rhq.modules.integrationTests.restApi;
+
+import java.util.List;
+import java.util.Map;
+
+import com.jayway.restassured.http.ContentType;
+import com.jayway.restassured.response.Response;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import org.rhq.modules.integrationTests.restApi.d.Operation;
+
+import static com.jayway.restassured.RestAssured.expect;
+import static com.jayway.restassured.RestAssured.get;
+import static com.jayway.restassured.RestAssured.given;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test the operations part of the rest api
+ * @author Heiko W. Rupp
+ */
+public class OperationsTest extends AbstractBase {
+
+ private int definitionId;
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp(); // Junit does not call that for us
+ Response r =
+ given()
+ .header(acceptJson)
+ .queryParam("resourceId",10001)
+ .expect()
+ .statusCode(200)
+ .log().ifError()
+ .when()
+ .get("/operation/definitions");
+
+ definitionId = -1;
+ List<Map<String,Object>> list = r.as(List.class);
+ for (Map<String,Object> map : list) {
+ if (map.get("name").equals("discovery"))
+ definitionId = (Integer) map.get("id");
+ }
+
+ assert definitionId !=-1 : "No discovery operation found";
+ }
+
+ @Test
+ public void testGetDefinitionById() throws Exception {
+
+ // Now retrieve that definition by id
+
+ given()
+ .header(acceptJson)
+ .pathParam("did",definitionId)
+ .expect()
+ .statusCode(200)
+ .body("name",is("discovery"))
+ .when()
+ .get("/operation/definition/{did}");
+
+ }
+
+ @Test
+ public void testCreateDraftOperation() throws Exception {
+
+ Operation draft =
+ given()
+ .header(acceptJson)
+ .pathParam("definitionId",definitionId)
+ .queryParam("resourceId",10001)
+ .expect()
+ .statusCode(200)
+ .log().ifError()
+ .when()
+ .post("/operation/definition/{definitionId}")
+ .as(Operation.class);
+
+ assert draft != null;
+ assert draft.getDefinitionId() == definitionId;
+
+ int draftId = draft.getId();
+
+ // check if we can retrieve one single draft
+
+ Operation op = get("/operation/" + draftId).as(Operation.class);
+ assert op !=null;
+ assert op.equals(draft);
+
+ }
+
+ @Test
+ public void testCreateAndUpdateDraftOperation() throws Exception {
+
+ Operation draft =
+ given()
+ .header(acceptJson)
+ .pathParam("definitionId",definitionId)
+ .queryParam("resourceId",10001)
+ .expect()
+ .statusCode(200)
+ .log().ifError()
+ .when()
+ .post("/operation/definition/{definitionId}")
+ .as(Operation.class);
+
+ assert draft != null;
+ assert draft.getDefinitionId() == definitionId;
+
+ int draftId = draft.getId();
+ draft.getParams().put("detailed",true);
+
+ try {
+ given()
+ .contentType(ContentType.JSON)
+ .pathParam("id", draftId)
+ .body(draft)
+ .expect()
+ .statusCode(200)
+ .log().ifError()
+ .when()
+ .put("/operation/{id}");
+ } finally {
+ // delete the draft again
+ expect()
+ .statusCode(204)
+ .when()
+ .delete("/operation/" + draftId);
+ }
+
+
+ }
+
+ @Test
+ public void testCreateDraftOperationAndScheduleExecution() throws Exception {
+
+ Operation draft =
+ given()
+ .header(acceptJson)
+ .pathParam("definitionId",definitionId)
+ .queryParam("resourceId",10001)
+ .expect()
+ .statusCode(200)
+ .log().ifError()
+ .when()
+ .post("/operation/definition/{definitionId}")
+ .as(Operation.class);
+
+ assert draft != null;
+ assert draft.getDefinitionId() == definitionId;
+
+ int draftId = draft.getId();
+
+ draft.setReadyToSubmit(true);
+ draft.getParams().put("detailed", true);
+
+ // update to schedule
+ Operation scheduled =
+ given()
+ .contentType(ContentType.JSON)
+ .pathParam("id",draftId)
+ .body(draft)
+ .expect()
+ .statusCode(200)
+ .log().ifError()
+ .when()
+ .put("/operation/{id}")
+ .as(Operation.class);
+
+ System.out.println(scheduled);
+ String history = null;
+ List<Map<String,Object>> links = scheduled.getLinks();
+ for (Map<String,Object> link : links) {
+ if (link.get("rel").equals("history"))
+ history = (String) link.get("href");
+ }
+ assert history != null;
+
+ String historyId = history.substring(history.lastIndexOf("/")+1);
+ try {
+ Thread.sleep(5000); // we need to wait a little as the execution may take
time
+
+ given()
+ .pathParam("hid",historyId)
+ .expect()
+ .statusCode(200)
+ .log().everything()
+ .when()
+ .get("/operation/history/{hid}");
+
+ // See if we also find it when we are looking for histories on the resource
+ Response response =
+ given()
+ .queryParam("resourceId",10001)
+ .header(acceptJson)
+ .expect()
+ .statusCode(200)
+ .log().ifError()
+ .when()
+ .get("/operation/history");
+
+ // compare
+ List<Map<String,Object>> list = response.as(List.class);
+ boolean found = false;
+ for (Map<String,Object> map : list) {
+ if (map.get("jobId").equals(historyId)) {
+ found = true;
+ }
+ }
+ assert found;
+
+ } finally {
+
+ // Delete the history item
+ given()
+ .pathParam("hid",historyId)
+ .expect()
+ .statusCode(204)
+ .log().ifError()
+ .when()
+ .delete("/operation/history/{hid}");
+
+ }
+
+ }
+
+}
diff --git
a/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/Operation.java
b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/Operation.java
new file mode 100644
index 0000000..54a1505
--- /dev/null
+++
b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/Operation.java
@@ -0,0 +1,129 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2012 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+package org.rhq.modules.integrationTests.restApi.d;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * An operation for testing
+ * @author Heiko W. Rupp
+ */
+public class Operation {
+
+ int id;
+ String name;
+ boolean readyToSubmit;
+ int resourceId;
+ int definitionId;
+ Map<String,Object> params = new HashMap<String, Object>();
+ List<Map<String,Object>> links;
+
+ public Operation() {
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public boolean isReadyToSubmit() {
+ return readyToSubmit;
+ }
+
+ public void setReadyToSubmit(boolean readyToSubmit) {
+ this.readyToSubmit = readyToSubmit;
+ }
+
+ public int getResourceId() {
+ return resourceId;
+ }
+
+ public void setResourceId(int resourceId) {
+ this.resourceId = resourceId;
+ }
+
+ public int getDefinitionId() {
+ return definitionId;
+ }
+
+ public void setDefinitionId(int definitionId) {
+ this.definitionId = definitionId;
+ }
+
+ public Map<String, Object> getParams() {
+ return params;
+ }
+
+ public void setParams(Map<String, Object> params) {
+ this.params = params;
+ }
+
+ public List<Map<String, Object>> getLinks() {
+ return links;
+ }
+
+ public void setLinks(List<Map<String, Object>> links) {
+ this.links = links;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ Operation operation = (Operation) o;
+
+ if (definitionId != operation.definitionId) return false;
+ if (id != operation.id) return false;
+ if (readyToSubmit != operation.readyToSubmit) return false;
+ if (resourceId != operation.resourceId) return false;
+ if (links != null ? !links.equals(operation.links) : operation.links != null)
return false;
+ if (!name.equals(operation.name)) return false;
+ if (params != null ? !params.equals(operation.params) : operation.params != null)
return false;
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = id;
+ result = 31 * result + name.hashCode();
+ result = 31 * result + (readyToSubmit ? 1 : 0);
+ result = 31 * result + resourceId;
+ result = 31 * result + definitionId;
+ result = 31 * result + (params != null ? params.hashCode() : 0);
+ result = 31 * result + (links != null ? links.hashCode() : 0);
+ return result;
+ }
+}
diff --git
a/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/OperationHistory.java
b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/OperationHistory.java
new file mode 100644
index 0000000..fa0af5c
--- /dev/null
+++
b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/OperationHistory.java
@@ -0,0 +1,108 @@
+/*
+ * RHQ Management Platform
+ * Copyright (C) 2005-2012 Red Hat, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+package org.rhq.modules.integrationTests.restApi.d;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * An operationhistory item for testing
+ * @author Heiko W. Rupp
+ */
+public class OperationHistory {
+
+ String operationName;
+ String resourceName;
+ long lastModified;
+ String status;
+ String errorMessage;
+ String jobId;
+
+ Map<String,Object> result = new HashMap<String, Object>();
+ List<Object> links;
+
+ public OperationHistory() {
+ }
+
+ public String getOperationName() {
+ return operationName;
+ }
+
+ public void setOperationName(String operationName) {
+ this.operationName = operationName;
+ }
+
+ public String getResourceName() {
+ return resourceName;
+ }
+
+ public void setResourceName(String resourceName) {
+ this.resourceName = resourceName;
+ }
+
+ public long getLastModified() {
+ return lastModified;
+ }
+
+ public void setLastModified(long lastModified) {
+ this.lastModified = lastModified;
+ }
+
+ public String getStatus() {
+ return status;
+ }
+
+ public void setStatus(String status) {
+ this.status = status;
+ }
+
+ public String getErrorMessage() {
+ return errorMessage;
+ }
+
+ public void setErrorMessage(String errorMessage) {
+ this.errorMessage = errorMessage;
+ }
+
+ public String getJobId() {
+ return jobId;
+ }
+
+ public void setJobId(String jobId) {
+ this.jobId = jobId;
+ }
+
+ public Map<String, Object> getResult() {
+ return result;
+ }
+
+ public void setResult(Map<String, Object> result) {
+ this.result = result;
+ }
+
+ public List<Object> getLinks() {
+ return links;
+ }
+
+ public void setLinks(List<Object> links) {
+ this.links = links;
+ }
+}