aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwukai2013-12-12 00:43:39 (EST)
committerSamuel Padgett2013-12-16 15:32:07 (EST)
commit3230956f0ac5797fccca1d8b4fd58e063bb3da9b (patch)
treee1b47a2f5924ec308ee2878aff0934a2b9721803
parent43bc0ae74fa111be57e6150530321b52f8430005 (diff)
downloadorg.eclipse.lyo.testsuite-3230956f0ac5797fccca1d8b4fd58e063bb3da9b.zip
org.eclipse.lyo.testsuite-3230956f0ac5797fccca1d8b4fd58e063bb3da9b.tar.gz
org.eclipse.lyo.testsuite-3230956f0ac5797fccca1d8b4fd58e063bb3da9b.tar.bz2
Bug 397688 - Test missing If-Match headerrefs/changes/72/19872/3
OSLC Core: provider SHOULD return HTTP Bad Request (400) when client sends an update request without the If-Match header Add protected method: CreationAndUpdateBaseTests.updateCreatedResourceWithEmptyPrecondition() Add 3 tests: CreationAndUpdateJsonTests.updateCreatedResourceWithEmptyPrecondition() CreationAndUpdateRdfXmlTests.updateCreatedResourceWithEmptyPrecondition() CreationAndUpdateXmlTests.updateCreatedResourceWithEmptyPrecondition() Change-Id: If3a3a37521d4b72e0d10eb77e60c778bfbc14595 Signed-off-by: wukai <bjwukai@cn.ibm.com>
-rw-r--r--org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateBaseTests.java342
-rw-r--r--org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateJsonTests.java8
-rw-r--r--org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateRdfXmlTests.java8
-rw-r--r--org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateXmlTests.java8
4 files changed, 222 insertions, 144 deletions
diff --git a/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateBaseTests.java b/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateBaseTests.java
index e547e99..8ba5b69 100644
--- a/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateBaseTests.java
+++ b/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateBaseTests.java
@@ -14,8 +14,8 @@
* Steve Speicher - initial API and implementation
* Samuel Padgett - create and update resources using shapes
*******************************************************************************/
-package org.eclipse.lyo.testsuite.oslcv2;
-
+package org.eclipse.lyo.testsuite.oslcv2;
+
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -53,54 +53,54 @@ import com.hp.hpl.jena.rdf.model.SimpleSelector;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDF;
-
-/**
- * This class provides JUnit tests for the validation of the OSLCv2 creation and
- * updating of change requests. It uses the template files specified in
- * setup.properties as the entity to be POST or PUT, for creation and updating
- * respectively.
- *
- * After each test, it attempts to perform a DELETE call on the resource that
- * was presumably created, but this DELETE call is not technically required in
- * the OSLC spec, so the created change request may still exist for some service
- * providers.
- */
-@RunWith(Parameterized.class)
-public abstract class CreationAndUpdateBaseTests extends TestsBase {
-
- @SuppressWarnings("rawtypes")
+
+/**
+ * This class provides JUnit tests for the validation of the OSLCv2 creation and
+ * updating of change requests. It uses the template files specified in
+ * setup.properties as the entity to be POST or PUT, for creation and updating
+ * respectively.
+ *
+ * After each test, it attempts to perform a DELETE call on the resource that
+ * was presumably created, but this DELETE call is not technically required in
+ * the OSLC spec, so the created change request may still exist for some service
+ * providers.
+ */
+@RunWith(Parameterized.class)
+public abstract class CreationAndUpdateBaseTests extends TestsBase {
+
+ @SuppressWarnings("rawtypes")
public static String[] getCreateTemplateTypes() throws FileNotFoundException {
if (rdfXmlUpdateTemplate == null) {
return null;
}
-
- Model m = ModelFactory.createDefaultModel();
+
+ Model m = ModelFactory.createDefaultModel();
m.read(new StringReader(rdfXmlUpdateTemplate), "http://base.url", OSLCConstants.JENA_RDF_XML);
- RDFUtils.validateModel(m);
- Property rdfType = m.getProperty(OSLCConstants.RDF_TYPE_PROP);
- Selector select = new SimpleSelector(null, rdfType, (RDFNode)null);
- List l = m.listStatements(select).toList();
- String[] types = new String[l.size()];
- for (int i=0; i<l.size(); i++) {
- types[i] = ((Statement)l.get(i)).getObject().toString();
- }
- return types;
- }
-
- public CreationAndUpdateBaseTests(String url) {
- super(url);
- }
-
- @Test
- public void createResourceWithInvalidContentType() throws IOException {
- // Issue post request using the provided template and an invalid
+ RDFUtils.validateModel(m);
+ Property rdfType = m.getProperty(OSLCConstants.RDF_TYPE_PROP);
+ Selector select = new SimpleSelector(null, rdfType, (RDFNode)null);
+ List l = m.listStatements(select).toList();
+ String[] types = new String[l.size()];
+ for (int i=0; i<l.size(); i++) {
+ types[i] = ((Statement)l.get(i)).getObject().toString();
+ }
+ return types;
+ }
+
+ public CreationAndUpdateBaseTests(String url) {
+ super(url);
+ }
+
+ @Test
+ public void createResourceWithInvalidContentType() throws IOException {
+ // Issue post request using the provided template and an invalid
// contentType
- String entity = getEntity(xmlCreateTemplate, OSLCConstants.CT_XML);
- HttpResponse resp = OSLCUtils.postDataToUrl(currentUrl, basicCreds,
- "*/*", "weird/type", entity, headers);
+ String entity = getEntity(xmlCreateTemplate, OSLCConstants.CT_XML);
+ HttpResponse resp = OSLCUtils.postDataToUrl(currentUrl, basicCreds,
+ "*/*", "weird/type", entity, headers);
EntityUtils.consume(resp.getEntity());
- assertTrue(resp.getStatusLine().getStatusCode() == 415);
- }
+ assertTrue(resp.getStatusLine().getStatusCode() == 415);
+ }
private HttpResponse doPost (String contentType, String accept, String content)
throws IOException {
@@ -125,45 +125,45 @@ public abstract class CreationAndUpdateBaseTests extends TestsBase {
return resp;
}
-
- protected void createValidResourceUsingTemplate(String contentType,
+
+ protected void createValidResourceUsingTemplate(String contentType,
String accept, String content) throws IOException {
String entity = getEntity(content, contentType);
HttpResponse resp = doPost(contentType, accept, entity);
-
- // Assert the response gave a 201 Created
- String responseBody = EntityUtils.toString(resp.getEntity());
+
+ // Assert the response gave a 201 Created
+ String responseBody = EntityUtils.toString(resp.getEntity());
EntityUtils.consume(resp.getEntity());
- assertEquals(responseBody, HttpStatus.SC_CREATED, resp.getStatusLine()
- .getStatusCode());
- Header location = resp.getFirstHeader("Location");
- // Assert that we were given a Location header pointing to the resource,
- // which is not a MUST according to oslc v2, but probably should be
- // present
- // none the less.
- assertFalse(location == null);
- // Attempt to clean up after the test by calling delete on the given
- // url,
- // which is not a MUST according to the oslc cm spec
- resp = OSLCUtils.deleteFromUrl(location.getValue(), basicCreds, "*/*");
- if (resp.getEntity() != null) {
+ assertEquals(responseBody, HttpStatus.SC_CREATED, resp.getStatusLine()
+ .getStatusCode());
+ Header location = resp.getFirstHeader("Location");
+ // Assert that we were given a Location header pointing to the resource,
+ // which is not a MUST according to oslc v2, but probably should be
+ // present
+ // none the less.
+ assertFalse(location == null);
+ // Attempt to clean up after the test by calling delete on the given
+ // url,
+ // which is not a MUST according to the oslc cm spec
+ resp = OSLCUtils.deleteFromUrl(location.getValue(), basicCreds, "*/*");
+ if (resp.getEntity() != null) {
EntityUtils.consume(resp.getEntity());
- }
- }
-
- protected void createResourceWithInvalidContent(String contentType,
- String accept, String content) throws IOException {
- // Issue post request using valid content type but invalid content
- HttpResponse resp = OSLCUtils.postDataToUrl(currentUrl, basicCreds,
- accept, accept, content, headers);
+ }
+ }
+
+ protected void createResourceWithInvalidContent(String contentType,
+ String accept, String content) throws IOException {
+ // Issue post request using valid content type but invalid content
+ HttpResponse resp = OSLCUtils.postDataToUrl(currentUrl, basicCreds,
+ accept, accept, content, headers);
EntityUtils.consume(resp.getEntity());
- // TODO: What is right sc forbidden?
- assertFalse("Expecting error but received OK", HttpStatus.SC_OK == resp
- .getStatusLine().getStatusCode());
- }
-
- protected void createResourceAndUpdateIt(String contentType, String accept,
+ // TODO: What is right sc forbidden?
+ assertFalse("Expecting error but received OK", HttpStatus.SC_OK == resp
+ .getStatusLine().getStatusCode());
+ }
+
+ protected void createResourceAndUpdateIt(String contentType, String accept,
String newContent, String updateContent) throws IOException {
HttpResponse resp = createResource(contentType, accept, newContent);
@@ -191,21 +191,21 @@ public abstract class CreationAndUpdateBaseTests extends TestsBase {
putHeaders[i] = headers[i];
}
- if ( hasPayLoad ) {
- // then fail, because we expect at least one, if not both
- assertTrue("ETag("+eTag+") or Last-Modified("+lastModified+") must not be null", eTag != null || lastModified != null ) ;
+ if ( hasPayLoad ) {
+ // then fail, because we expect at least one, if not both
+ assertTrue("ETag("+eTag+") or Last-Modified("+lastModified+") must not be null", eTag != null || lastModified != null ) ;
}
-
- if( eTag != null ) {
- putHeaders[i++] = new BasicHeader("If-Match", eTag.getValue() );
+
+ if( eTag != null ) {
+ putHeaders[i++] = new BasicHeader("If-Match", eTag.getValue() );
}
else {
putHeaders[i++] = new BasicHeader("bogus1", "bogus1");
}
-
- if( lastModified != null ) {
- putHeaders[i++] = new BasicHeader("If-Unmodified-Since", lastModified.getValue());
- }
+
+ if( lastModified != null ) {
+ putHeaders[i++] = new BasicHeader("If-Unmodified-Since", lastModified.getValue());
+ }
else {
putHeaders[i++] = new BasicHeader("bogus1", "bogus1");
}
@@ -226,33 +226,33 @@ public abstract class CreationAndUpdateBaseTests extends TestsBase {
if (updateParams != null && !updateParams.isEmpty())
updateUrl = updateUrl + updateParams;
}
-
- resp = OSLCUtils.putDataToUrl(updateUrl, basicCreds, accept,
- contentType, updateContent, putHeaders);
- String responseBody = EntityUtils.toString(resp.getEntity());
- if (resp.getEntity() != null)
+
+ resp = OSLCUtils.putDataToUrl(updateUrl, basicCreds, accept,
+ contentType, updateContent, putHeaders);
+ String responseBody = EntityUtils.toString(resp.getEntity());
+ if (resp.getEntity() != null)
EntityUtils.consume(resp.getEntity());
- // Assert that a proper PUT resulted in a 200 OK
- assertEquals("HTTP Response body: \n " + responseBody,
- HttpStatus.SC_OK, resp.getStatusLine().getStatusCode());
-
- // Clean up after the test by attempting to delete the created resource
- if (location != null) {
- resp = OSLCUtils.deleteFromUrl(location.getValue(), basicCreds,
- "*/*");
- if (resp != null && resp.getEntity() != null)
+ // Assert that a proper PUT resulted in a 200 OK
+ assertEquals("HTTP Response body: \n " + responseBody,
+ HttpStatus.SC_OK, resp.getStatusLine().getStatusCode());
+
+ // Clean up after the test by attempting to delete the created resource
+ if (location != null) {
+ resp = OSLCUtils.deleteFromUrl(location.getValue(), basicCreds,
+ "*/*");
+ if (resp != null && resp.getEntity() != null)
EntityUtils.consume(resp.getEntity());
- }
- }
-
- protected void updateCreatedResourceWithInvalidContent(String contentType,
- String accept, String content, String invalidContent)
- throws IOException {
+ }
+ }
+
+ protected void updateCreatedResourceWithInvalidContent(String contentType,
+ String accept, String content, String invalidContent)
+ throws IOException {
HttpResponse resp = createResource(contentType, accept, content);
Header location = getRequiredLocationHeader(resp);
Header eTag = resp.getFirstHeader("ETag");
Header lastModified = resp.getFirstHeader("Last-Modified");
-
+
// Ignore ETag and Last-Modified for these tests
int size = headers.length;
if( eTag != null ) size++;
@@ -269,32 +269,32 @@ public abstract class CreationAndUpdateBaseTests extends TestsBase {
putHeaders[i++] = new BasicHeader("If-Unmodified-Since", lastModified.getValue());
}
- // Now, go to the url of the new change request and update it.
- resp = OSLCUtils.putDataToUrl(location.getValue(), basicCreds, accept,
- contentType, invalidContent, putHeaders);
- if (resp.getEntity() != null) {
+ // Now, go to the url of the new change request and update it.
+ resp = OSLCUtils.putDataToUrl(location.getValue(), basicCreds, accept,
+ contentType, invalidContent, putHeaders);
+ if (resp.getEntity() != null) {
EntityUtils.consume(resp.getEntity());
- }
- // Assert that an invalid PUT resulted in a 400 BAD REQUEST
- assertEquals(HttpStatus.SC_BAD_REQUEST, resp.getStatusLine()
- .getStatusCode());
-
- // Clean up after the test by attempting to delete the created resource
- if (location != null)
- resp = OSLCUtils.deleteFromUrl(location.getValue(), basicCreds, "");
-
- if (resp != null && resp.getEntity() != null)
+ }
+ // Assert that an invalid PUT resulted in a 400 BAD REQUEST
+ assertEquals(HttpStatus.SC_BAD_REQUEST, resp.getStatusLine()
+ .getStatusCode());
+
+ // Clean up after the test by attempting to delete the created resource
+ if (location != null)
+ resp = OSLCUtils.deleteFromUrl(location.getValue(), basicCreds, "");
+
+ if (resp != null && resp.getEntity() != null)
EntityUtils.consume(resp.getEntity());
- }
-
- protected void updateCreatedResourceWithBadType(String contentType,
- String accept, String createContent, String updateContent,
- String badType) throws IOException {
+ }
+
+ protected void updateCreatedResourceWithBadType(String contentType,
+ String accept, String createContent, String updateContent,
+ String badType) throws IOException {
HttpResponse resp = createResource(contentType, accept, createContent);
Header location = getRequiredLocationHeader(resp);
Header eTag = resp.getFirstHeader("ETag");
- Header lastModified = resp.getFirstHeader("Last-Modified");
-
+ Header lastModified = resp.getFirstHeader("Last-Modified");
+
// Ignore eTag and Last-Modified for this test
int size = headers.length;
if( eTag != null ) size++;
@@ -310,26 +310,26 @@ public abstract class CreationAndUpdateBaseTests extends TestsBase {
if( lastModified != null ) {
putHeaders[i++] = new BasicHeader("If-Unmodified-Since", lastModified.getValue());
}
-
+
if (updateContent == null && !OSLCConstants.CT_JSON.equals(contentType)) {
updateContent = updateResourceFromShape(location.getValue(), contentType);
}
-
- // Now, go to the url of the new change request and update it.
+
+ // Now, go to the url of the new change request and update it.
//resp = OSLCUtils.putDataToUrl(location.getValue(), basicCreds, "*/*",
- resp = OSLCUtils.putDataToUrl(location.getValue(), basicCreds, "application/xml",
- badType, updateContent, putHeaders);
- if (resp != null && resp.getEntity() != null)
+ resp = OSLCUtils.putDataToUrl(location.getValue(), basicCreds, "application/xml",
+ badType, updateContent, putHeaders);
+ if (resp != null && resp.getEntity() != null)
EntityUtils.consume(resp.getEntity());
-
- assertEquals(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, resp.getStatusLine()
- .getStatusCode());
-
- // Clean up after the test by attempting to delete the created resource
- if (location != null)
- resp = OSLCUtils.deleteFromUrl(location.getValue(), basicCreds, "");
-
- if (resp != null && resp.getEntity() != null)
+
+ assertEquals(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, resp.getStatusLine()
+ .getStatusCode());
+
+ // Clean up after the test by attempting to delete the created resource
+ if (location != null)
+ resp = OSLCUtils.deleteFromUrl(location.getValue(), basicCreds, "");
+
+ if (resp != null && resp.getEntity() != null)
EntityUtils.consume(resp.getEntity());
}
@@ -353,7 +353,7 @@ public abstract class CreationAndUpdateBaseTests extends TestsBase {
String entity = getEntity(createContent, contentType);
HttpResponse resp = doPost(contentType, accept, entity);
-
+
// Assert the response gave a 201 Created
EntityUtils.consume(resp.getEntity());
assertEquals(HttpStatus.SC_CREATED, resp.getStatusLine()
@@ -440,6 +440,60 @@ public abstract class CreationAndUpdateBaseTests extends TestsBase {
EntityUtils.consume(resp.getEntity());
}
+ protected void updateCreatedResourceWithEmptyPrecondition(
+ String contentType, String accept, String createContent,
+ String updateContent) throws IOException {
+
+ HttpResponse resp = createResource(contentType, accept, createContent);
+
+ Header location = getRequiredLocationHeader(resp);
+ assertTrue("Location(" + location + ")" + " must not be null",
+ location != null);
+
+ // check whether a POST response body is empty (which is allowed)
+ Header contentLength = resp.getFirstHeader("Content-Length");
+ boolean hasPayLoad = true;
+ if (contentLength != null) {
+ String len = contentLength.getValue().toString();
+ if (len.equalsIgnoreCase("0")) {
+ hasPayLoad = false;
+ }
+ }
+
+ Header eTag = resp.getFirstHeader("ETag");
+ Header lastModified = resp.getFirstHeader("Last-Modified");
+
+ if (hasPayLoad) {
+ assertTrue("Either ETag(" + eTag + ") or Last-Modified("
+ + lastModified + ") must not be null", eTag != null
+ || lastModified != null);
+ }
+
+ int size = headers.length;
+
+ // Put headers but ignore the precondition headers: "If-Match", and "If-Unmodified-Since"
+ Header[] putHeaders = new Header[size];
+ int i = 0;
+ for (; i < headers.length; i++) {
+ putHeaders[i] = headers[i];
+ }
+
+ // Now, go to the url of the new change request and update it.
+ resp = OSLCUtils.putDataToUrl(location.getValue(), basicCreds, accept,
+ contentType, updateContent, putHeaders);
+ if (resp != null && resp.getEntity() != null)
+ EntityUtils.consume(resp.getEntity());
+
+ assertEquals(HttpStatus.SC_BAD_REQUEST, resp.getStatusLine()
+ .getStatusCode());
+
+ // Clean up after the test by attempting to delete the created resource
+ resp = OSLCUtils.deleteFromUrl(location.getValue(), basicCreds, "");
+
+ if (resp != null && resp.getEntity() != null)
+ EntityUtils.consume(resp.getEntity());
+ }
+
/**
* Change a resource using its instance shape. Will only attempt to change
* one property to limit the chance of errors.
@@ -751,4 +805,4 @@ public abstract class CreationAndUpdateBaseTests extends TestsBase {
return model;
}
-}
+}
diff --git a/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateJsonTests.java b/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateJsonTests.java
index ceb57bd..425564f 100644
--- a/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateJsonTests.java
+++ b/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateJsonTests.java
@@ -100,4 +100,12 @@ public class CreationAndUpdateJsonTests extends CreationAndUpdateBaseTests {
OSLCConstants.CT_JSON, jsonCreateTemplate,
jsonUpdateTemplate);
}
+
+ @Test
+ public void updateCreatedResourceWithEmptyPrecondition()
+ throws IOException {
+ updateCreatedResourceWithEmptyPrecondition(OSLCConstants.CT_JSON,
+ OSLCConstants.CT_JSON, jsonCreateTemplate,
+ jsonUpdateTemplate);
+ }
}
diff --git a/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateRdfXmlTests.java b/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateRdfXmlTests.java
index 012812a..e586967 100644
--- a/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateRdfXmlTests.java
+++ b/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateRdfXmlTests.java
@@ -106,5 +106,13 @@ public class CreationAndUpdateRdfXmlTests extends CreationAndUpdateBaseTests {
updateCreatedResourceWithFailedPrecondition(OSLCConstants.CT_RDF,
OSLCConstants.CT_RDF, rdfXmlCreateTemplate,
rdfXmlUpdateTemplate);
+ }
+
+ @Test
+ public void updateCreatedResourceWithEmptyPrecondition()
+ throws IOException {
+ updateCreatedResourceWithEmptyPrecondition(OSLCConstants.CT_RDF,
+ OSLCConstants.CT_RDF, rdfXmlCreateTemplate,
+ rdfXmlUpdateTemplate);
}
}
diff --git a/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateXmlTests.java b/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateXmlTests.java
index 2908330..3d83b64 100644
--- a/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateXmlTests.java
+++ b/org.eclipse.lyo.testsuite.server/src/main/java/org/eclipse/lyo/testsuite/oslcv2/CreationAndUpdateXmlTests.java
@@ -108,5 +108,13 @@ public class CreationAndUpdateXmlTests extends CreationAndUpdateBaseTests {
updateCreatedResourceWithFailedPrecondition(OSLCConstants.CT_XML,
OSLCConstants.CT_XML, xmlCreateTemplate,
xmlUpdateTemplate);
+ }
+
+ @Test
+ public void updateCreatedResourceWithEmptyPrecondition()
+ throws IOException {
+ updateCreatedResourceWithEmptyPrecondition(OSLCConstants.CT_XML,
+ OSLCConstants.CT_XML, xmlCreateTemplate,
+ xmlUpdateTemplate);
}
}