Skip to content

Commit

Permalink
Fixed issue #4808
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Aug 18, 2015
1 parent e6b2fc5 commit 2094128
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 8 deletions.
Expand Up @@ -677,11 +677,11 @@ public Map<String, Object> toMap() {

final ORID id = getIdentity();
if (id.isValid())
map.put("@rid", id);
map.put(ODocumentHelper.ATTRIBUTE_RID, id);

final String className = getClassName();
if (className != null)
map.put("@class", className);
map.put(ODocumentHelper.ATTRIBUTE_CLASS, className);

return map;
}
Expand Down Expand Up @@ -945,12 +945,24 @@ public ODocument field(String iFieldName, Object iPropertyValue, OType... iField
if (iFieldName.isEmpty())
throw new IllegalArgumentException("Field name is empty");

if ("@class".equals(iFieldName)) {
if (ODocumentHelper.ATTRIBUTE_CLASS.equals(iFieldName)) {
setClassName(iPropertyValue.toString());
return this;
} else if ("@rid".equals(iFieldName)) {
} else if (ODocumentHelper.ATTRIBUTE_RID.equals(iFieldName)) {
_recordId.fromString(iPropertyValue.toString());
return this;
} else if (ODocumentHelper.ATTRIBUTE_VERSION.equals(iFieldName)) {
if (iPropertyValue != null) {
int v = _recordVersion.getCounter();

if (iPropertyValue instanceof Number)
v = ((Number) iPropertyValue).intValue();
else
Integer.parseInt(iPropertyValue.toString());

_recordVersion.setCounter(v);
}
return this;
}

final int lastSep = _allowChainedAccess ? iFieldName.lastIndexOf('.') : -1;
Expand Down Expand Up @@ -1090,9 +1102,9 @@ public Object removeField(final String iFieldName) {
checkForLoading();
checkForFields();

if ("@class".equalsIgnoreCase(iFieldName)) {
if (ODocumentHelper.ATTRIBUTE_CLASS.equalsIgnoreCase(iFieldName)) {
setClassName(null);
} else if ("@rid".equalsIgnoreCase(iFieldName)) {
} else if (ODocumentHelper.ATTRIBUTE_RID.equalsIgnoreCase(iFieldName)) {
_recordId = new ORecordId();
}

Expand Down Expand Up @@ -2415,7 +2427,7 @@ private void fetchClassName() {
if (database != null && database.getStorageVersions() != null && database.getStorageVersions().classesAreDetectedByClusterId()) {
if (_recordId.clusterId < 0) {
checkForLoading();
checkForFields("@class");
checkForFields(ODocumentHelper.ATTRIBUTE_CLASS);
} else {
final OSchema schema = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot();
if (schema != null) {
Expand All @@ -2427,7 +2439,7 @@ private void fetchClassName() {
} else {
// CLASS NOT FOUND: CHECK IF NEED LOADING AND UNMARSHALLING
checkForLoading();
checkForFields("@class");
checkForFields(ODocumentHelper.ATTRIBUTE_CLASS);
}
}

Expand Down
@@ -0,0 +1,68 @@
package com.orientechnologies.orient.server.network.http;

import com.orientechnologies.orient.core.record.impl.ODocument;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;

/**
* Test HTTP "batch" command.
*
* @author Luca Garulli (l.garulli--at-orientechnologies.com)
*/
@Test
public class HttpBatchTest extends BaseHttpDatabaseTest {
public void batchUpdate() throws IOException {
Assert.assertEquals(post("command/" + getDatabaseName() + "/sql/").payload("create class User", CONTENT.TEXT).getResponse()
.getStatusLine().getStatusCode(), 200);

Assert.assertEquals(
post("command/" + getDatabaseName() + "/sql/").payload("insert into User content {\"userID\": \"35862601\"}", CONTENT.TEXT)
.setUserName("admin").setUserPassword("admin").getResponse().getStatusLine().getStatusCode(), 200);

String response = EntityUtils.toString(getResponse().getEntity());

Assert.assertNotNull(response);

ODocument insertedDocument = ((List<ODocument>) new ODocument().fromJSON(response).field("result")).get(0);

// TEST UPDATE
Assert.assertEquals(
post("batch/" + getDatabaseName() + "/sql/")
.payload(
"{\n" + " \"transaction\": true,\n" + " \"operations\": [{\n" + " \"record\": {\n"
+ " \"userID\": \"35862601\",\n" + " \"externalID\": \"35862601\",\n"
+ " \"@rid\": \"" + insertedDocument.getIdentity() + "\", \"@class\": \"User\", \"@version\": "
+ insertedDocument.getVersion() + "\n" + " },\n" + " \"type\": \"u\"\n" + " }]\n" + "}",
CONTENT.JSON).getResponse().getStatusLine().getStatusCode(), 200);

// TEST DOUBLE UPDATE
Assert.assertEquals(
post("batch/" + getDatabaseName() + "/sql/")
.payload(
"{\n" + " \"transaction\": true,\n" + " \"operations\": [{\n" + " \"record\": {\n"
+ " \"userID\": \"35862601\",\n" + " \"externalID\": \"35862601\",\n"
+ " \"@rid\": \"" + insertedDocument.getIdentity() + "\", \"@class\": \"User\", \"@version\": "
+ (insertedDocument.getVersion() + 1) + "\n" + " },\n" + " \"type\": \"u\"\n" + " }]\n" + "}",
CONTENT.JSON).getResponse().getStatusLine().getStatusCode(), 200);

// TEST WRONG VERSION ON UPDATE
Assert.assertEquals(
post("batch/" + getDatabaseName() + "/sql/")
.payload(
"{\n" + " \"transaction\": true,\n" + " \"operations\": [{\n" + " \"record\": {\n"
+ " \"userID\": \"35862601\",\n" + " \"externalID\": \"35862601\",\n"
+ " \"@rid\": \"" + insertedDocument.getIdentity() + "\", \"@class\": \"User\", \"@version\": "
+ (insertedDocument.getVersion() + 1) + "\n" + " },\n" + " \"type\": \"u\"\n" + " }]\n" + "}",
CONTENT.JSON).getResponse().getStatusLine().getStatusCode(), 409);

}

@Override
public String getDatabaseName() {
return "httpscript";
}
}

0 comments on commit 2094128

Please sign in to comment.