You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ch...@apache.org on 2014/08/28 05:48:15 UTC
[08/51] [partial] rename folder /datajs into /odatajs. no file
modification.
http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/d5ec5557/odatajs/tests/odata-batch-tests.js
----------------------------------------------------------------------
diff --git a/odatajs/tests/odata-batch-tests.js b/odatajs/tests/odata-batch-tests.js
new file mode 100644
index 0000000..71a1547
--- /dev/null
+++ b/odatajs/tests/odata-batch-tests.js
@@ -0,0 +1,559 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+// odata-batch-tests.js
+
+(function (window, undefined) {
+ // DATAJS INTERNAL START
+ var defaultAcceptString = "application/json;q=0.9, */*;q=0.1";
+
+ var testPayload = {
+ CategoryID : 42,
+ Name: "New Category",
+ ID : "odata",
+ version: "4.0"
+ };
+
+ var jsonPayload = window.odatajs.oData.json.jsonSerializer(window.odatajs.oData.json.jsonHandler, testPayload, { "OData-Version": "4.0" });
+
+ djstest.addTest(function writeRequestTest() {
+ var request = {
+ headers: { "Content-Type": "plain/text; charset=utf-8", Accept: "*/*", "OData-Version": "2.0" },
+ requestUri: "http://temp.org",
+ method: "GET",
+ body: "test request"
+ };
+ var expected = "GET http://temp.org HTTP/1.1\r\n" +
+ "Content-Type: plain/text; charset=utf-8\r\n" +
+ "Accept: */*\r\n" +
+ "OData-Version: 2.0\r\n" +
+ "\r\n" +
+ "test request";
+
+ var actual = window.odatajs.oData.batch.writeRequest(request);
+ djstest.assertAreEqual(actual, expected, "WriteRequest serializes a request properly");
+ djstest.done();
+ });
+
+ djstest.addTest(function serializeSimpleBatchTest() {
+
+ var request = {
+ requestUri: "http://temp.org",
+ method: "POST",
+ data: { __batchRequests: [
+ { requestUri: "http://feed(1)", headers: {} },
+ { requestUri: "http://feed(2)", headers: { "Accept": "application/json;odata.metadata=minimal" }, method: "GET" }
+ ]
+ }
+ };
+
+ var template = "\r\n--<batchBoundary>\r\n" +
+ "Content-Type: application/http\r\n" +
+ "Content-Transfer-Encoding: binary\r\n" +
+ "\r\n" +
+ "GET http://feed(1) HTTP/1.1\r\n" +
+ "Accept: " + defaultAcceptString + "\r\n" +
+ "OData-MaxVersion: 4.0\r\n" +
+ "\r\n" +
+ "\r\n--<batchBoundary>\r\n" +
+ "Content-Type: application/http\r\n" +
+ "Content-Transfer-Encoding: binary\r\n" +
+ "\r\n" +
+ "GET http://feed(2) HTTP/1.1\r\n" +
+ "Accept: application/json;odata.metadata=minimal\r\n" +
+ "OData-MaxVersion: 4.0\r\n" +
+ "\r\n" +
+ "\r\n--<batchBoundary>--\r\n";
+
+ MockHttpClient.clear().addRequestVerifier(request.requestUri, function (request) {
+ var cType = window.odatajs.oData.handler.contentType(request.headers["Content-Type"]);
+ var boundary = cType.properties["boundary"];
+ var expected = template.replace(/<batchBoundary>/g, boundary);
+
+ djstest.assert(boundary, "Request content type has its boundary set");
+ djstest.assertAreEqual(request.body, expected, "Request body is serialized properly");
+ djstest.done();
+ });
+
+ odatajs.oData.request(request, null, null, window.odatajs.oData.batch.batchHandler, MockHttpClient);
+ });
+
+ djstest.addTest(function serializeComplexBatchTest() {
+
+ var request = {
+ requestUri: "http://temp.org",
+ method: "POST",
+ data: { __batchRequests: [
+ { requestUri: "http://feed(1)", headers: {} },
+ { requestUri: "http://feed(2)", headers: { "Accept": "application/json;odata.metadata=minimal" }, method: "GET" },
+ { __changeRequests: [
+ { requestUri: "http://feed(1)", headers: {}, method: "POST", data: testPayload }
+ ]
+ },
+ { requestUri: "http://feed(1)", headers: {} }
+ ]
+ }
+ };
+
+ //
+ var template = "\r\n--<batchBoundary>\r\n" +
+ "Content-Type: application/http\r\n" +
+ "Content-Transfer-Encoding: binary\r\n" +
+ "\r\n" +
+ "GET http://feed(1) HTTP/1.1\r\n" +
+ "Accept: " + defaultAcceptString + "\r\n" +
+ "OData-MaxVersion: 4.0\r\n" +
+ "\r\n" +
+ "\r\n--<batchBoundary>\r\n" +
+ "Content-Type: application/http\r\n" +
+ "Content-Transfer-Encoding: binary\r\n" +
+ "\r\n" +
+ "GET http://feed(2) HTTP/1.1\r\n" +
+ "Accept: application/json;odata.metadata=minimal\r\n" +
+ "OData-MaxVersion: 4.0\r\n" +
+ "\r\n" +
+ "\r\n--<batchBoundary>\r\n" +
+ "Content-Type: multipart/mixed; boundary=<changesetBoundary>\r\n" +
+ "\r\n--<changesetBoundary>\r\n" +
+ "Content-Type: application/http\r\n" +
+ "Content-Transfer-Encoding: binary\r\n" +
+ "\r\n" +
+ "POST http://feed(1) HTTP/1.1\r\n" +
+ "Accept: " + defaultAcceptString + "\r\n" +
+ "OData-Version: 4.0\r\n" +
+ "Content-Type: application/json\r\n" +
+ "OData-MaxVersion: 4.0\r\n" +
+ "\r\n" +
+ jsonPayload +
+ "\r\n--<changesetBoundary>--\r\n" +
+ "\r\n--<batchBoundary>\r\n" +
+ "Content-Type: application/http\r\n" +
+ "Content-Transfer-Encoding: binary\r\n" +
+ "\r\n" +
+ "GET http://feed(1) HTTP/1.1\r\n" +
+ "Accept: " + defaultAcceptString + "\r\n" +
+ "OData-MaxVersion: 4.0\r\n" +
+ "\r\n" +
+ "\r\n--<batchBoundary>--\r\n";
+
+ MockHttpClient.clear().addRequestVerifier(request.requestUri, function (request) {
+ // Get the boundaries from the request.
+ var start = request.body.indexOf("multipart/mixed");
+ var end = request.body.indexOf("\r\n", start);
+
+ var csetBoundary = window.odatajs.oData.handler.contentType(request.body.substring(start, end)).properties["boundary"];
+ var batchBoundary = window.odatajs.oData.handler.contentType(request.headers["Content-Type"]).properties["boundary"];
+
+ var expected = template.replace(/<batchBoundary>/g, batchBoundary);
+ expected = expected.replace(/<changesetBoundary>/g, csetBoundary);
+
+ djstest.assert(batchBoundary, "Request content type has its boundary set");
+ djstest.assert(csetBoundary, "Changeset content type has its boundary set");
+ djstest.assertAreEqual(request.body, expected, "Request body is serialized properly");
+ djstest.done();
+ });
+
+ odatajs.oData.request(request, null, null, window.odatajs.oData.batch.batchHandler, MockHttpClient);
+ });
+
+ djstest.addTest(function serializeChangeSetTest() {
+ var request = {
+ requestUri: "http://temp.org",
+ method: "POST",
+ data: {
+ __batchRequests: [
+ { __changeRequests: [
+ { requestUri: "http://feed(1)", headers: {}, method: "POST", data: testPayload }
+ ]
+ }
+ ]
+ }
+ };
+
+ var template = "\r\n--<batchBoundary>\r\n" +
+ "Content-Type: multipart/mixed; boundary=<changesetBoundary>\r\n" +
+ "\r\n--<changesetBoundary>\r\n" +
+ "Content-Type: application/http\r\n" +
+ "Content-Transfer-Encoding: binary\r\n" +
+ "\r\n" +
+ "POST http://feed(1) HTTP/1.1\r\n" +
+ "Accept: " + defaultAcceptString + "\r\n" +
+ "OData-Version: 4.0\r\n" +
+ "Content-Type: application/json\r\n" +
+ "OData-MaxVersion: 4.0\r\n" +
+ "\r\n" +
+ jsonPayload +
+ "\r\n--<changesetBoundary>--\r\n" +
+ "\r\n--<batchBoundary>--\r\n";
+
+ MockHttpClient.clear().addRequestVerifier(request.requestUri, function (request) {
+ // Get the boundaries from the request.
+ var start = request.body.indexOf("multipart/mixed");
+ var end = request.body.indexOf("\r\n", start);
+
+ var csetBoundary = window.odatajs.oData.handler.contentType(request.body.substring(start, end)).properties["boundary"];
+ var batchBoundary = window.odatajs.oData.handler.contentType(request.headers["Content-Type"]).properties["boundary"];
+
+ var expected = template.replace(/<batchBoundary>/g, batchBoundary);
+ expected = expected.replace(/<changesetBoundary>/g, csetBoundary);
+
+ djstest.assert(batchBoundary, "Request content type has its boundary set");
+ djstest.assert(csetBoundary, "Changeset content type has its boundary set");
+ djstest.assertAreEqual(request.body, expected, "Request body is serialized properly");
+ djstest.done();
+ });
+
+ odatajs.oData.request(request, null, null, window.odatajs.oData.batch.batchHandler, MockHttpClient);
+ });
+
+ djstest.addTest(function serializeNestedChangeSetsTest() {
+ var request = {
+ requestUri: "http://temp.org",
+ method: "POST",
+ data: testPayload
+ };
+
+ djstest.expectException(function () {
+ odatajs.oData.request(request, null, null, window.odatajs.oData.batch.batchHandler);
+ });
+
+ djstest.done();
+ });
+
+ djstest.addTest(function serializeNonBatchObjectTest() {
+ var request = {
+ requestUri: "http://temp.org",
+ method: "POST",
+ data: {
+ __batchRequests: [
+ { __changeRequests: [
+ { __changeRequests: [
+ { requestUri: "http://feed(2)", headers: { "Content-Type": "application/json", "OData-Version": "4.0" }, method: "PUT", data: testPayload }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ };
+
+ djstest.expectException(function () {
+ odatajs.oData.request(request, null, null, window.odatajs.oData.batch.batchHandler);
+ });
+
+ djstest.done();
+ });
+
+ djstest.addTest(function readSimpleBatchTest() {
+ var response = {
+ statusCode: 202,
+ statusText: "Accepted",
+ headers: {
+ "Content-Type": "multipart/mixed; boundary=batchresponse_b61ab173-39c7-45ea-ade4-941efae85ab9"
+ },
+ body: "--batchresponse_b61ab173-39c7-45ea-ade4-941efae85ab9\r\n\
+Content-Type: application/http\r\n\
+Content-Transfer-Encoding: binary\r\n\
+\r\n\
+HTTP/1.1 201 Created\r\n\
+OData-Version: 4.0;\r\n\
+Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\r\n\
+X-Content-Type-Options: nosniff\r\n\
+Cache-Control: no-cache\r\n\
+Location: http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(42)\r\n\
+\r\n\
+{\"@odata.context\":\"http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/$metadata#Categories/$entity\",\"Icon@odata.mediaContentType\":\"image/gif\",\"CategoryID\":42,\"Name\":\"New Category\"}\r\n\
+--batchresponse_b61ab173-39c7-45ea-ade4-941efae85ab9\r\n\
+Content-Type: application/http\r\n\
+Content-Transfer-Encoding: binary\r\n\
+\r\n\
+HTTP/1.1 201 Created\r\n\
+OData-Version: 4.0;\r\n\
+Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\r\n\
+X-Content-Type-Options: nosniff\r\n\
+Cache-Control: no-cache\r\n\
+Location: http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(43)\r\n\
+\r\n\
+{\"@odata.context\":\"http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/$metadata#Categories/$entity\",\"Icon@odata.mediaContentType\":\"image/gif\",\"CategoryID\":43,\"Name\":\"New Category\"}\r\n\
+--batchresponse_b61ab173-39c7-45ea-ade4-941efae85ab9--\r\n\
+"
+ };
+
+ MockHttpClient.clear().addResponse("http://testuri.org", response);
+ odatajs.oData.read("http://testuri.org", function (data, response) {
+ djstest.assert(data.__batchResponses, "data.__batchResponses is defined");
+ djstest.assertAreEqual(data.__batchResponses[0].headers["Location"], "http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(42)", "part 1 of the response was read");
+ djstest.assertAreEqual(data.__batchResponses[1].headers["Location"], "http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(43)", "part 2 of the response was read");
+ djstest.assertAreEqual(data.__batchResponses[0].data["CategoryID"], 42, "part 1 data of the response was read");
+ djstest.assertAreEqual(data.__batchResponses[1].data["CategoryID"], 43, "part 2 data of the response was read");
+ djstest.done();
+ }, null, window.odatajs.oData.batch.batchHandler, MockHttpClient);
+ });
+
+ djstest.addTest(function readBatchWithChangesetTest() {
+ var response = {
+ statusCode: 202,
+ statusText: "Accepted",
+ headers: {
+ "Content-Type": "multipart/mixed; boundary=batchresponse_fb681875-73dc-4e62-9898-a0af89021341"
+ },
+ body: "--batchresponse_fb681875-73dc-4e62-9898-a0af89021341\r\n\
+Content-Type: multipart/mixed; boundary=changesetresponse_905a1494-fd76-4846-93f9-a3431f0bf5a2\r\n\
+\r\n\
+--changesetresponse_905a1494-fd76-4846-93f9-a3431f0bf5a2\r\n\
+Content-Type: application/http\r\n\
+Content-Transfer-Encoding: binary\r\n\
+\r\n\
+HTTP/1.1 201 OK\r\n\
+OData-Version: 4.0;\r\n\
+Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\r\n\
+X-Content-Type-Options: nosniff\r\n\
+Cache-Control: no-cache\r\n\
+Location: http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(42)\r\n\
+\r\n\
+{\"@odata.context\":\"http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/$metadata#Categories/$entity\",\"Icon@odata.mediaContentType\":\"image/gif\",\"CategoryID\":42,\"Name\":\"New Category\"}\r\n\
+--changesetresponse_905a1494-fd76-4846-93f9-a3431f0bf5a2\r\n\
+Content-Type: application/http\r\n\
+Content-Transfer-Encoding: binary\r\n\
+\r\n\
+HTTP/1.1 204 No Content\r\n\
+X-Content-Type-Options: nosniff\r\n\
+Cache-Control: no-cache\r\n\
+OData-Version: 4.0;\r\n\
+\r\n\
+\r\n\
+--changesetresponse_905a1494-fd76-4846-93f9-a3431f0bf5a2--\r\n\
+--batchresponse_fb681875-73dc-4e62-9898-a0af89021341\r\n\
+Content-Type: application/http\r\n\
+Content-Transfer-Encoding: binary\r\n\
+\r\n\
+HTTP/1.1 201 Created\r\n\
+OData-Version: 4.0;\r\n\
+Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\r\n\
+X-Content-Type-Options: nosniff\r\n\
+Cache-Control: no-cache\r\n\
+Location: http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(41)\r\n\
+\r\n\
+{\"@odata.context\":\"http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/$metadata#Categories/$entity\",\"Icon@odata.mediaContentType\":\"image/gif\",\"CategoryID\":41,\"Name\":\"New Category\"}\r\n\
+--batchresponse_fb681875-73dc-4e62-9898-a0af89021341\r\n\
+Content-Type: multipart/mixed; boundary=changesetresponse_92cc2ae8-a5f2-47fc-aaa3-1ff9e7453b07\r\n\
+\r\n\
+--changesetresponse_92cc2ae8-a5f2-47fc-aaa3-1ff9e7453b07\r\n\
+Content-Type: application/http\r\n\
+Content-Transfer-Encoding: binary\r\n\
+\r\n\
+HTTP/1.1 201 OK\r\n\
+OData-Version: 4.0;\r\n\
+Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\r\n\
+X-Content-Type-Options: nosniff\r\n\
+Cache-Control: no-cache\r\n\
+Location: http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(43)\r\n\
+\r\n\
+{\"@odata.context\":\"http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/$metadata#Categories/$entity\",\"Icon@odata.mediaContentType\":\"image/gif\",\"CategoryID\":43,\"Name\":\"New Category\"}\r\n\
+--changesetresponse_92cc2ae8-a5f2-47fc-aaa3-1ff9e7453b07\r\n\
+Content-Type: application/http\r\n\
+Content-Transfer-Encoding: binary\r\n\
+\r\n\
+HTTP/1.1 204 No Content\r\n\
+X-Content-Type-Options: nosniff\r\n\
+Cache-Control: no-cache\r\n\
+OData-Version: 4.0;\r\n\
+\r\n\
+\r\n\
+--changesetresponse_92cc2ae8-a5f2-47fc-aaa3-1ff9e7453b07--\r\n\
+--batchresponse_fb681875-73dc-4e62-9898-a0af89021341--\r\n\
+"
+ };
+
+ MockHttpClient.clear().addResponse("http://testuri.org", response);
+ odatajs.oData.read("http://testuri.org", function (data, response) {
+
+ var batchResponses = data.__batchResponses;
+ djstest.assert(batchResponses, "data contains the batch responses");
+
+ var changesetResponses = batchResponses[0].__changeResponses;
+ djstest.assert(changesetResponses, "batch response 1 contains the change set responses");
+ var changesetResponses3 = batchResponses[2].__changeResponses;
+ djstest.assert(changesetResponses3, "batch response 3 contains the change set responses");
+
+ djstest.assertAreEqual(batchResponses[0].data, undefined, "No data defined for batch response 1");
+ djstest.assertAreEqual(changesetResponses[0].headers["Location"], "http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(42)", "part 1 of the changeset response of the response 1 was read");
+ djstest.assertAreEqual(changesetResponses[0].data["CategoryID"], 42, "part 1 data of the changeset response of the response 1 was read");
+ djstest.assertAreEqual(changesetResponses[1].data, undefined, "No data defined for no content only response in part 2 of the changeset response of the response 1");
+
+ djstest.assertAreEqual(batchResponses[1].headers["Location"], "http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(41)", "response 2 was read");
+ djstest.assertAreEqual(batchResponses[1].data["CategoryID"], 41, "response 2 data was read");
+
+ djstest.assertAreEqual(batchResponses[2].data, undefined, "No data defined for");
+ djstest.assertAreEqual(changesetResponses3[0].headers["Location"], "http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(43)", "part 1 of the changeset response of the response 3 was read");
+ djstest.assertAreEqual(changesetResponses3[0].data["CategoryID"], 43, "part 1 data of the changeset response of the response 3 was read");
+ djstest.assertAreEqual(changesetResponses3[1].data, undefined, "No data defined for no content only response in part 2 of the changeset response of the response 3");
+ djstest.done();
+ }, null, window.odatajs.oData.batch.batchHandler, MockHttpClient);
+ });
+
+ djstest.addTest(function readBatchWithErrorPartTest() {
+ var response = {
+ statusCode: 202,
+ statusText: "Accepted",
+ headers: {
+ "Content-Type": "multipart/mixed; boundary=batchresponse_9402a3ab-260f-4fa4-af01-0b30db397c8d"
+ },
+ body: "--batchresponse_9402a3ab-260f-4fa4-af01-0b30db397c8d\r\n\
+Content-Type: application/http\r\n\
+Content-Transfer-Encoding: binary\r\n\
+\r\n\
+HTTP/1.1 200 OK\r\n\
+Cache-Control: no-cache\r\n\
+OData-Version: 4.0;\r\n\
+Content-Type: application/json;charset=utf-8\r\n\
+Location: http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(1)\r\n\
+\r\n\
+{\"error\":{\"code\":\"\",\"message\":\"Resource not found for the segment 'Categories(1)'.\"}}\r\n\
+--batchresponse_9402a3ab-260f-4fa4-af01-0b30db397c8d\r\n\
+Content-Type: application/http\r\n\
+Content-Transfer-Encoding: binary\r\n\
+\r\n\
+HTTP/1.1 400 Bad Request\r\n\
+OData-Version: 4.0;\r\n\
+Content-Type: application/json\r\n\
+{\"error\":{\"code\":\"\",\"message\":\"Error processing request stream.'.\"}}\r\n\
+--batchresponse_9402a3ab-260f-4fa4-af01-0b30db397c8d--\r\n\
+"
+ };
+
+ MockHttpClient.clear().addResponse("http://testuri.org", response);
+ odatajs.oData.read("http://testuri.org", function (data, response) {
+ var batchResponses = data.__batchResponses;
+ djstest.assert(batchResponses, "data.__batchResponses is defined");
+ djstest.assertAreEqual(batchResponses.length, 2, "batch contains two responses");
+ djstest.assertAreEqual(batchResponses[0].headers["Location"], "http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(1)", "part 1 of the response was read");
+ djstest.assert(batchResponses[1].response, "part 2 of the response was read");
+ djstest.done();
+ }, null, window.odatajs.oData.batch.batchHandler, MockHttpClient);
+ });
+
+
+ djstest.addTest(function readMalformedMultipartResponseThrowsException() {
+ var response = {
+ statusCode: 202,
+ statusText: "Accepted",
+ headers: {
+ "Content-Type": "multipart/mixed; boundary=batchresponse_fb681875-73dc-4e62-9898-a0af89021341"
+ },
+ body: "--batchresponse_fb681875-73dc-4e62-9898-a0af89021341\r\n\
+Content-Type: application/http\r\n\
+Content-Transfer-Encoding: binary\r\n\
+\r\n\
+HTTP/1.1 200 OK\r\n\
+Cache-Control: no-cache\r\n\
+OData-Version: 4.0;\r\n\
+Content-Type: application/json;charset=utf-8\r\n\
+Location: http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(1)\r\n\
+\r\n\
+{\"error\":{\"code\":\"\",\"message\":\"Resource not found for the segment 'Categories(1)'.\"}}\r\n\
+--batchresponse_fb681875-73dc-4e62-9898-a0af89021341\r\n\
+Content-Type: multipart/mixed; boundary=changesetresponse_2f9c6ba7-b330-4e7c-bf2a-db521996c243\r\n\
+\r\n\
+--changesetresponse_2f9c6ba7-b330-4e7c-bf2a-db521996c243\r\n\
+Content-Type: application/http\r\n\
+Content-Transfer-Encoding: binary\r\n\
+\r\n\
+HTTP/1.1 404 Not Found\r\n\
+X-Content-Type-Options: nosniff\r\n\
+OData-Version: 4.0;\r\n\
+Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\r\n\
+\r\n\
+{\"error\":{\"code\":\"\",\"message\":\GET operation cannot be specified in a change set. Only PUT, POST and DELETE operations can be specified in a change set..'.\"}}\r\n\
+--changesetresponse_2f9c6ba7-b330-4e7c-bf2a-db521996c243--\r\n\
+--batchresponse_fb681875-73dc-4e62-9898-a0af89021341--\r\n\
+"
+ };
+
+ MockHttpClient.clear().addResponse("http://testuri.org", response);
+ odatajs.oData.read("http://testuri.org", function (data, response) {
+ var batchResponses = data.__batchResponses;
+ djstest.assert(batchResponses, "data.__batchResponses is defined");
+ djstest.assertAreEqual(batchResponses.length, 2, "batch contains two responses");
+ djstest.assertAreEqual(batchResponses[0].headers["Location"], "http://localhost:4002/tests/endpoints/FoodStoreDataServiceV4.svc/Categories(1)", "part 1 of the response was read");
+
+ var error = batchResponses[1].__changeResponses[0];
+ djstest.assert(error.response.body.indexOf("GET operation cannot be specified in a change set") > -1, "Response contains expected message");
+ djstest.done();
+ }, null, window.odatajs.oData.batch.batchHandler, MockHttpClient);
+ djstest.done();
+ });
+
+ djstest.addTest(function batchRequestContextIsPushedToThePartsHandlersTest() {
+ var testHandler = {
+ read: function (response, context) {
+ djstest.assert(context.recognizeDates, "Recognize dates was set properly on the part request context");
+ },
+ write: function (request, context) {
+ djstest.assert(context.recognizeDates, "Recognize dates was set properly on the part request context");
+ }
+ };
+
+ var batch = {
+ headers: {},
+ __batchRequests: [
+ { requestUri: "http://someUri" },
+ { __changeRequests: [
+ { requestUri: "http://someUri", method: "POST", data: { p1: 500} }
+ ]
+ }
+ ]
+ };
+
+ var request = { requestUri: "http://someuri", headers: {}, data: batch };
+ var response = {
+ statusCode: 202,
+ statusText: "Accepted",
+ headers: {
+ "Content-Type": "multipart/mixed; boundary=batchresponse_fb681875-73dc-4e62-9898-a0af89021341"
+ },
+ body: '--batchresponse_fb681875-73dc-4e62-9898-a0af89021341\r\n' +
+ 'Content-Type: application/http\r\n' +
+ 'Content-Transfer-Encoding: binary\r\n' +
+ '\r\n' +
+ 'HTTP/1.1 200 OK\r\n' +
+ 'Cache-Control: no-cache\r\n' +
+ 'OData-Version: 1.0;\r\n' +
+ 'Content-Type: application/json\r\n' +
+ '\r\n' +
+ '{ "p1": 500 }\r\n' +
+ '\r\n' +
+ '--batchresponse_fb681875-73dc-4e62-9898-a0af89021341--\r\n'
+ };
+
+ var oldPartHandler = window.odatajs.oData.batch.batchHandler.partHandler;
+
+ window.odatajs.oData.batch.batchHandler.partHandler = testHandler;
+
+ window.odatajs.oData.batch.batchHandler.write(request, { recognizeDates: true });
+ window.odatajs.oData.batch.batchHandler.read(response, { recognizeDates: true });
+
+ window.odatajs.oData.batch.batchHandler.partHandler = oldPartHandler;
+
+ djstest.done();
+ });
+
+
+ // DATAJS INTERNAL END
+})(this);
http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/d5ec5557/odatajs/tests/odata-cache-filter-functional-tests.html
----------------------------------------------------------------------
diff --git a/odatajs/tests/odata-cache-filter-functional-tests.html b/odatajs/tests/odata-cache-filter-functional-tests.html
new file mode 100644
index 0000000..94cfe2e
--- /dev/null
+++ b/odatajs/tests/odata-cache-filter-functional-tests.html
@@ -0,0 +1,52 @@
+<!--
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+ -->
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+ <title>datajs.cache filter functional tests</title>
+ <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
+ <meta http-equiv="cache-control" content="no-cache"/>
+ <meta http-equiv="pragma" content="no-cache"/>
+ <meta http-equiv="expires" content="-1"/>
+
+ <link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.10.0.css" type="text/css" />
+ <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
+ <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
+ <script type="text/javascript" src="http://code.jquery.com/qunit/qunit-1.10.0.js"></script>
+ <script type="text/javascript" src="common/TestSynchronizerClient.js"></script>
+ <script type="text/javascript">
+ window.TestSynchronizer.init(QUnit);
+ </script>
+ <script type="text/javascript" src="../build/odatajs-4.0.0-beta-01.js"></script>
+ <script type="text/javascript" src="common/common.js"></script>
+ <script type="text/javascript" src="common/djstest.js"></script>
+ <script type="text/javascript" src="common/CacheOracle.js"></script>
+ <script type="text/javascript" src="common/ObservableHttpClient.js"></script>
+ <script type="text/javascript" src="common/ODataReadOracle.js"></script>
+ <script type="text/javascript" src="odata-cache-filter-functional-tests.js"></script>
+</head>
+<body>
+ <h1 id="qunit-header">datajs.cache filter functional tests</h1>
+ <h2 id="qunit-banner"></h2>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+</body>
+</html>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/d5ec5557/odatajs/tests/odata-cache-filter-functional-tests.js
----------------------------------------------------------------------
diff --git a/odatajs/tests/odata-cache-filter-functional-tests.js b/odatajs/tests/odata-cache-filter-functional-tests.js
new file mode 100644
index 0000000..0fd08f3
--- /dev/null
+++ b/odatajs/tests/odata-cache-filter-functional-tests.js
@@ -0,0 +1,431 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+(function (window, undefined) {
+ OData.defaultHandler.accept = "application/json;q=0.9, */*;q=0.1";
+ var feeds = [
+ { feed: "./endpoints/FoodStoreDataServiceV4.svc/Foods" }
+ ];
+
+ var itemsInCollection = 16;
+
+ var pageSize = 3;
+ var readRangeStart = pageSize + 1;
+ var readRangeTake = pageSize;
+
+ // Indices for filterForward after an initial readRange has partially filled the cache
+ var filterForwardAfterReadIndices = [
+ readRangeStart - 1, // before items in the cache
+ readRangeStart, // beginning of items in the cache
+ readRangeStart + readRangeTake, // last item prefetched in the cache
+ readRangeStart + readRangeTake + 1 // past range already in cache
+ ];
+
+ // Indices for filterBack after an initial readRange has partially filled the cache
+ var filterBackAfterReadIndices = [
+ readRangeStart - 1, // before items in the cache
+ readRangeStart, // beginning of items in the cache
+ readRangeStart + readRangeTake, // last item prefetched in the cache
+ readRangeStart + readRangeTake + 1 // past range already in cache
+ ];
+
+ // Counts for filterForward after a readRange has partially filled the cache
+ var filterForwardAfterReadCounts = [
+ -5, // Get all items
+ 3, // Subset of all items found in the cache
+ itemsInCollection
+ ];
+
+ // Counts for filterBack after a readRange has partially filled the cache
+ var filterBackAfterReadCounts = [
+ -5, // Get all items
+ 3, // Subset of all items found in the cache
+ itemsInCollection
+ ];
+
+ // Indices for single filterForward
+ var singleFilterForwardIndices = [
+ -5,
+ itemsInCollection - 1,
+ itemsInCollection, // beyond the end of the collection
+ itemsInCollection + pageSize // more than one page beyond the collection
+ ];
+
+ // Indices for single filterBack
+ var singleFilterBackIndices = [
+ -1,
+ 0,
+ itemsInCollection - 1
+ ];
+
+ // Count for single filterForward
+ var singleFilterForwardCounts = [
+ 5,
+ itemsInCollection + 1 // more than number of items in collection
+ ];
+
+ // Count for single filterBack
+ var singleFilterBackCounts = [
+ 5,
+ itemsInCollection + 1 // more than number of items in collection
+ ];
+
+ // Index/count variations for multiple filterForwards
+ var multipleFilterForwards = [
+ { index: 0, count: -1 }, // everything
+ {index: 2, count: 5 }, // range in first half
+ {index: 4, count: 7 }, // range in the middle to overlap first and second half
+ {index: 9, count: 4} // range in second half
+ ];
+
+ // Index/count variations for multiple filterBacks
+ var multipleFilterBacks = [
+ { index: itemsInCollection - 1, count: -1 }, // everything
+ {index: itemsInCollection - 2, count: 5 }, // range in second half
+ {index: itemsInCollection - 4, count: 7 }, // range in the middle to overlap first and second half
+ {index: itemsInCollection - 9, count: 4} // range in first half
+ ];
+
+
+ var invalidIndices = [NaN, undefined, Infinity, "not a valid value"];
+ var invalidCounts = [NaN, undefined, Infinity, "not a valid value"];
+
+ // Predicate which returns all items in the collection
+ var getAllItemsPredicate = function (item) {
+ return true;
+ };
+
+ // Predicate which returns none of the items in the collection
+ var getNoItemsPredicate = function (item) {
+ return false;
+ };
+
+ var getEveryThirdPredicate = function (item) {
+ return ((item.FoodID % 3) === 0);
+ };
+
+ var filterPredicates = [
+ getAllItemsPredicate,
+ getNoItemsPredicate,
+ getEveryThirdPredicate
+ ];
+
+ var expectException = function (cache) {
+ djstest.assert(false, "We should not get here because the an exception is expected.");
+ djstest.destroyCacheAndDone(cache);
+ };
+
+ var makeUnexpectedErrorHandler = function (cache) {
+ return function (err) {
+ djstest.assert(false, "Unexpected call to error handler with error: " + djstest.toString(err));
+ if (cache) {
+ djstest.destroyCacheAndDone(cache);
+ } else {
+ djstest.done();
+ }
+ };
+ };
+
+ var validateFilterResultsAndRequests = function (feed, cache, index, count, predicate, finished, backwards, session, cacheOracle) {
+ /** Runs filter and validates the results and network requests
+ * @param {Object} feed - The feed being read from
+ * @param {Object} cache - The cache to perform the filter on
+ * @param {Integer} index - The index value
+ * @param {Integer} count - The count value
+ * @param {Object} predicate - Filter string to append to the feed to validate the predicate
+ * @param {Function} finished - Callback function called after data is verified
+ * @param {Object} session - Session object to validate the network requests
+ * @param {Object} cacheOracle - cacheOracle object to validate the network requests
+ */
+
+ if (count < 0) {
+ count = itemsInCollection;
+ }
+
+ if (index < 0) {
+ index = 0;
+ }
+
+ window.ODataReadOracle.readJsonAcrossServerPages(feed, function (expectData) {
+ if (backwards) {
+ cache.filterBack(index, count, predicate).then(function (actualResults) {
+ var expectedResults = CacheOracle.getExpectedFilterResults(expectData, index, count, predicate, backwards);
+ djstest.assertAreEqualDeep(actualResults, expectedResults, "results for " + "filterBack requests");
+
+ if (session && cacheOracle) {
+ // If the count is not satisfied in the expected results, read to the beginning of the collection
+ // otherwise read to the first expected index
+ var firstIndex = 0;
+ if (expectedResults.value.length != 0) {
+ firstIndex = (expectedResults.value.length < count) ? 0 : expectedResults.value[0].index;
+ }
+ // The effective count is the number of items between the first and last index
+ var expectedCount = index - firstIndex + 1;
+ cacheOracle.verifyRequests(session.requests, session.responses, firstIndex, expectedCount, "filterBack requests", backwards);
+ }
+ finished();
+ });
+ }
+ else {
+ cache.filterForward(index, count, predicate).then(function (actualResults) {
+ var expectedResults = CacheOracle.getExpectedFilterResults(expectData, index, count, predicate, backwards)
+ djstest.assertAreEqualDeep(actualResults, expectedResults, "results for " + "filterForward requests");
+
+ if (session && cacheOracle) {
+ if (expectedResults.value.length > 0) {
+ // If the count is not satisfied in the expected results, read to the end of the collection
+ // otherwise read to the last index
+ var lastIndex = (expectedResults.value.length < count) ? itemsInCollection : expectedResults.value[expectedResults.value.length - 1].index + 1;
+ // One request is made if the index is outside the range of the collection if the end of the collection has not yet been found
+ var expectedCount = (index < itemsInCollection) ? (lastIndex - index) : 1;
+ }
+ else {
+ var expectedCount = itemsInCollection;
+ }
+
+ cacheOracle.verifyRequests(session.requests, session.responses, index, expectedCount, "filterForward requests", backwards);
+ }
+ finished();
+ });
+ }
+ });
+ };
+
+ var createMultipleFilterTestName = function (scenarioName, params) {
+ return "Testing " + scenarioName + (params.backwards ? "filterBack: " : "filterForward: ") + " of " + params.feed + " with predicate " + params.predicate + " [index " +
+ params.firstIndex + ", count " + params.firstCount + "] and [index " + params.secondIndex + ", count " + params.secondCount +
+ "] with pageSize " + params.pageSize + ", and prefetch " + params.prefetchSize;
+ };
+
+ var createSingleFilterTestName = function (scenarioName, params) {
+ return "Testing " + scenarioName + (params.backwards ? "filterBack: " : "filterForward: ") + " of " + params.feed + " with predicate " + params.predicate + " [index " +
+ params.index + ", count " + params.count + "] with pageSize " + params.pageSize + ", and prefetch " + params.prefetchSize;
+ };
+
+ var singleFilterTest = function (params) {
+ djstest.assertsExpected(2);
+ var options = { name: "cache" + new Date().valueOf(), source: params.feed, pageSize: params.pageSize, prefetchSize: params.prefetchSize };
+
+ var cache = odatajs.cache.createDataCache(options);
+ var cacheOracle = new CacheOracle(params.feed, params.pageSize, itemsInCollection);
+ var session = this.observableHttpClient.newSession();
+ validateFilterResultsAndRequests(params.feed, cache, params.index, params.count, params.predicate, function () { djstest.destroyCacheAndDone(cache) }, params.backwards, session, cacheOracle);
+ };
+
+ var filterAfterReadRangeTest = function (params) {
+ djstest.assertsExpected(3);
+ var options = { name: "cache" + new Date().valueOf(), source: params.feed, pageSize: params.pageSize, prefetchSize: params.prefetchSize };
+
+ var cache = odatajs.cache.createDataCache(options);
+ var cacheOracle = new CacheOracle(params.feed, params.pageSize, itemsInCollection);
+ var session = this.observableHttpClient.newSession();
+
+ cache.readRange(params.skip, params.take).then(function (data) {
+ cacheOracle.verifyRequests(session.requests, session.responses, params.skip, params.take, "readRange requests");
+ session.clear();
+ validateFilterResultsAndRequests(params.feed, cache, params.index, params.count, params.predicate, function () { djstest.destroyCacheAndDone(cache); }, params.backwards, session, cacheOracle);
+ });
+ };
+
+ var parallelFilterTest = function (params) {
+ djstest.assertsExpected(2);
+ var options = { name: "cache" + new Date().valueOf(), source: params.feed, pageSize: params.pageSize, prefetchSize: params.prefetchSize };
+
+ var cache = odatajs.cache.createDataCache(options);
+
+ var firstfilter = function (finished) {
+ validateFilterResultsAndRequests(params.feed, cache, params.firstIndex, params.firstCount, params.predicate, finished, params.backwards);
+ };
+
+ var secondfilter = function (finished) {
+ validateFilterResultsAndRequests(params.feed, cache, params.secondIndex, params.secondCount, params.predicate, finished, params.backwards);
+ };
+
+ djstest.asyncDo([firstfilter, secondfilter], function () {
+ djstest.destroyCacheAndDone(cache);
+ });
+ };
+
+ var serialFilterTest = function (params) {
+ djstest.assertsExpected(4);
+ var options = { name: "cache" + new Date().valueOf(), source: params.feed, pageSize: params.pageSize, prefetchSize: params.prefetchSize };
+
+ var cache = odatajs.cache.createDataCache(options);
+ var cacheOracle = new CacheOracle(params.feed, params.pageSize, itemsInCollection);
+ var session = this.observableHttpClient.newSession();
+
+ var filterMethod = function (index, count, predicate, backwards) {
+ if (backwards) {
+ return cache.filterBack(index, count, predicate);
+ }
+ else {
+ return cache.filterForward(index, count, predicate)
+ }
+ }
+
+ filterMethod(params.firstIndex, params.firstCount, params.predicate, params.backwards).then(
+ function (results) {
+ validateFilterResultsAndRequests(params.feed, cache, params.firstIndex, params.firstCount, params.predicate,
+ function () {
+ session.clear();
+ validateFilterResultsAndRequests(params.feed, cache, params.secondIndex, params.secondCount, params.predicate, function () { djstest.destroyCacheAndDone(cache) }, params.backwards, session, cacheOracle);
+ }, params.backwards, session, cacheOracle);
+ });
+ };
+
+ module("Functional", {
+ setup: function () {
+ this.observableHttpClient = new ObservableHttpClient();
+ window.odatajs.oData.net.defaultHttpClient = this.observableHttpClient;
+ },
+
+ teardown: function () {
+ window.odatajs.oData.net.defaultHttpClient = this.observableHttpClient.provider;
+ }
+ });
+
+ $.each(filterPredicates, function (_, filterPredicate) {
+ $.each(feeds, function (_, feedObject) {
+ $.each(filterForwardAfterReadCounts, function (_, filterCount) {
+ $.each(filterForwardAfterReadIndices, function (_, filterIndex) {
+ var parameters = { index: filterIndex, count: filterCount, predicate: filterPredicate, feed: feedObject.feed, take: readRangeTake,
+ skip: readRangeStart, pageSize: pageSize, prefetchSize: 0, backwards: false
+ };
+ djstest.addTest(filterAfterReadRangeTest, createSingleFilterTestName("after readRange, ", parameters), parameters);
+ });
+ });
+
+ $.each(filterBackAfterReadCounts, function (_, filterCount) {
+ $.each(filterBackAfterReadIndices, function (_, filterIndex) {
+ var parameters = { index: filterIndex, count: filterCount, predicate: filterPredicate, feed: feedObject.feed, take: readRangeTake,
+ skip: readRangeStart, pageSize: pageSize, prefetchSize: 0, backwards: true
+ };
+ djstest.addTest(filterAfterReadRangeTest, createSingleFilterTestName("After readRange, ", parameters), parameters);
+ });
+ });
+ });
+
+ $.each(singleFilterForwardIndices, function (_, filterIndex) {
+ $.each(singleFilterForwardCounts, function (_, filterCount) {
+ var parameters = { index: filterIndex, count: filterCount, predicate: filterPredicate, feed: feeds[0].feed, pageSize: pageSize, prefetchSize: 0, backwards: false };
+ djstest.addTest(singleFilterTest, createSingleFilterTestName("single ", parameters), parameters);
+ });
+ });
+
+ $.each(singleFilterBackIndices, function (_, filterIndex) {
+ $.each(singleFilterBackCounts, function (_, filterCount) {
+ var parameters = { index: filterIndex, count: filterCount, predicate: filterPredicate, feed: feeds[0].feed, pageSize: pageSize, prefetchSize: 0, backwards: true };
+ djstest.addTest(singleFilterTest, createSingleFilterTestName("single ", parameters), parameters);
+ });
+ });
+
+ $.each(multipleFilterForwards, function (_, firstFilter) {
+ $.each(multipleFilterForwards, function (_, secondFilter) {
+ var serialParameters = { firstIndex: firstFilter.index, firstCount: firstFilter.count, secondIndex: secondFilter.index, secondCount: secondFilter.count,
+ predicate: filterPredicate, feed: feeds[0].feed, pageSize: pageSize, prefetchSize: 0, backwards: false
+ };
+ djstest.addTest(serialFilterTest, createMultipleFilterTestName("serial ", serialParameters), serialParameters);
+ });
+ });
+
+ $.each(multipleFilterBacks, function (_, firstFilter) {
+ $.each(multipleFilterBacks, function (_, secondFilter) {
+ var serialParameters = { firstIndex: firstFilter.index, firstCount: firstFilter.count, secondIndex: secondFilter.index, secondCount: secondFilter.count,
+ predicate: filterPredicate, feed: feeds[0].feed, pageSize: pageSize, prefetchSize: 0, backwards: true
+ };
+ djstest.addTest(serialFilterTest, createMultipleFilterTestName("serial ", serialParameters), serialParameters);
+ });
+ });
+
+ $.each(multipleFilterForwards, function (_, firstFilter) {
+ $.each(multipleFilterForwards, function (_, secondFilter) {
+ var parallelParameters = { firstIndex: firstFilter.index, firstCount: firstFilter.count, secondIndex: secondFilter.index, secondCount: secondFilter.count,
+ predicate: filterPredicate, feed: feeds[0].feed, pageSize: pageSize, prefetchSize: 6, backwards: false
+ };
+ djstest.addTest(parallelFilterTest, createMultipleFilterTestName("parallel ", parallelParameters), parallelParameters);
+ });
+ });
+
+ $.each(multipleFilterBacks, function (_, firstFilter) {
+ $.each(multipleFilterBacks, function (_, secondFilter) {
+ var parallelParameters = { firstIndex: firstFilter.index, firstCount: firstFilter.count, secondIndex: secondFilter.index, secondCount: secondFilter.count,
+ predicate: filterPredicate, feed: feeds[0].feed, pageSize: pageSize, prefetchSize: 6, backwards: true
+ };
+ djstest.addTest(parallelFilterTest, createMultipleFilterTestName("parallel ", parallelParameters), parallelParameters);
+ });
+ });
+
+ $.each([true, false], function (_, isBackwards) {
+ var zeroCountParameters = { index: 0, count: 0, predicate: filterPredicate, feed: feeds[0].feed, take: readRangeTake,
+ skip: readRangeStart, pageSize: pageSize, prefetchSize: 0, backwards: isBackwards
+ };
+ djstest.addTest(singleFilterTest, createSingleFilterTestName("Count 0 ", zeroCountParameters), zeroCountParameters);
+ });
+ });
+
+ $.each([true, false], function (_, backwards) {
+ $.each(invalidIndices, function (_, invalidIndex) {
+ var invalidIndexParameters = { index: invalidIndex, count: -1, predicate: filterPredicates[0], feed: feeds[0].feed, pageSize: pageSize, prefetchSize: 0, backwards: backwards };
+
+ djstest.addTest(
+ function (params) {
+ djstest.assertsExpected(1);
+ var options = { name: "cache" + new Date().valueOf(), source: params.feed };
+ var cache = odatajs.cache.createDataCache(options);
+ try {
+ params.backwards ?
+ cache.filterForward(params.index, params.count, params.predicate).then(function (results) {
+ djstest.log(results);
+ }) :
+ cache.filterForward(params.index, params.count, params.predicate).then(function (results) {
+ djstest.log(results);
+ });
+ expectException(cache);
+ } catch (e) {
+ djstest.assertAreEqual(e.message, "'index' must be a valid number.", "Error message validation");
+ djstest.destroyCacheAndDone(cache);
+ }
+ }, createSingleFilterTestName("invalid index ", invalidIndexParameters), invalidIndexParameters);
+ });
+
+ $.each(invalidCounts, function (_, invalidCount) {
+ var invalidCountParameters = { index: 0, count: invalidCount, predicate: filterPredicates[0], feed: feeds[0].feed, pageSize: pageSize, prefetchSize: 0, backwards: backwards };
+
+ djstest.addTest(
+ function (params) {
+ djstest.assertsExpected(1);
+ var options = { name: "cache" + new Date().valueOf(), source: params.feed };
+ var cache = odatajs.cache.createDataCache(options);
+ try {
+ params.backwards ?
+ cache.filterBack(params.index, params.count, params.predicate).then(function (results) {
+ djstest.log(results);
+ }) :
+ cache.filterForward(params.index, params.count, params.predicate).then(function (results) {
+ djstest.log(results);
+ });
+ expectException(cache);
+ } catch (e) {
+ djstest.assertAreEqual(e.message, "'count' must be a valid number.", "Error message validation");
+ djstest.destroyCacheAndDone(cache);
+ }
+ }, createSingleFilterTestName("invalid count ", invalidCountParameters), invalidCountParameters);
+ });
+ });
+})(this);
http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/d5ec5557/odatajs/tests/odata-cache-fperf-tests.html
----------------------------------------------------------------------
diff --git a/odatajs/tests/odata-cache-fperf-tests.html b/odatajs/tests/odata-cache-fperf-tests.html
new file mode 100644
index 0000000..c00a814
--- /dev/null
+++ b/odatajs/tests/odata-cache-fperf-tests.html
@@ -0,0 +1,50 @@
+<!--
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+ -->
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+ <title>datajs.cache functional perf tests</title>
+ <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
+ <meta http-equiv="cache-control" content="no-cache"/>
+ <meta http-equiv="pragma" content="no-cache"/>
+ <meta http-equiv="expires" content="-1"/>
+
+ <link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.10.0.css" type="text/css" />
+ <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
+ <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
+ <script type="text/javascript" src="http://code.jquery.com/qunit/qunit-1.10.0.js"></script>
+ <script type="text/javascript" src="common/TestSynchronizerClient.js"></script>
+ <script type="text/javascript">
+ window.TestSynchronizer.init(QUnit);
+ </script>
+ <script type="text/javascript" src="../build/odatajs-4.0.0-beta-01.js"></script>
+ <script type="text/javascript" src="common/common.js"></script>
+ <script type="text/javascript" src="common/CacheOracle.js"></script>
+ <script type="text/javascript" src="common/djstest.js"></script>
+ <script type="text/javascript" src="odata-cache-fperf-tests.js"></script>
+</head>
+<body>
+ <h1 id="qunit-header">datajs.cache functional perf tests</h1>
+ <h2 id="qunit-banner"></h2>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+</body>
+</html>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/d5ec5557/odatajs/tests/odata-cache-fperf-tests.js
----------------------------------------------------------------------
diff --git a/odatajs/tests/odata-cache-fperf-tests.js b/odatajs/tests/odata-cache-fperf-tests.js
new file mode 100644
index 0000000..cfb698c
--- /dev/null
+++ b/odatajs/tests/odata-cache-fperf-tests.js
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+(function (window, undefined) {
+
+ var slowHttpClient = {
+ latency: 750,
+ provider: window.odatajs.oData.net.defaultHttpClient,
+ request: function (request, success, error) {
+ setTimeout(function () {
+ slowHttpClient.provider.request(request, success, error);
+ }, slowHttpClient.latency);
+ }
+ };
+
+ var feeds = [
+ { uri: "./endpoints/FoodStoreDataServiceV4.svc/Foods" }
+ ];
+
+ module("Functional", {
+ setup: function () {
+ window.odatajs.oData.net.defaultHttpClient = slowHttpClient;
+ },
+ teardown: function () {
+ window.odatajs.oData.net.defaultHttpClient = slowHttpClient.provider;
+ }
+ });
+
+ var cacheReadRangeWallClockTest = function (totalReads, interval, mechanism, source, pageSize, prefetchSize, generateRange, threshold) {
+ /** Cache readRange wall-clock test
+ * The average time computed by the wall-clock test does *not* include the initial readRange
+ * @param totalReads - Number of reads to collect data from
+ * @param interval - Interval (milliseconds) between reads
+ * @param mechanism - The cache store mechanism
+ * @param source - The feed source
+ * @param pageSize - The page size
+ * @param prefetchSize - The prefetch size
+ * @param generateRange - The range generator function: given the read index, returns the readRange index and count
+ * @param threshold - The average read time threshold for test to pass; if not specified, defaults to the slowHttpClient latency
+ * @returns The test function
+ */
+ return function () {
+ var cache = odatajs.cache.createDataCache({ name: "cache" + new Date().valueOf(), source: source, pageSize: pageSize, prefetchSize: prefetchSize });
+ var totalTime = 0;
+ var readCount = 0;
+
+ var callReadRange = function () {
+ var range = generateRange(readCount);
+ var startTime = new Date().valueOf();
+ cache.readRange(range.index, range.count).then(function (data) {
+ var duration = (new Date().valueOf()) - startTime;
+ djstest.log("readRange " + readCount + " [" + range.index + ", " + range.count + "]: " + duration + "ms");
+
+ // The first readRange is not counted
+ totalTime += (readCount > 0) ? duration : 0;
+ readCount += 1;
+
+ if (readCount < totalReads) {
+ setTimeout(callReadRange, interval);
+ } else {
+ // The first readRange is not counted
+ var averageTime = totalTime / (totalReads - 1);
+ var actualThreshold = threshold === undefined ? slowHttpClient.latency : threshold;
+ djstest.assert(averageTime < actualThreshold, "Average: " + averageTime + "ms, Threshold: " + actualThreshold + "ms");
+ djstest.destroyCacheAndDone(cache);
+ }
+ }, function (err) {
+ djstest.fail("Unexpected call to error handler with error: " + djstest.toString(err));
+ djstest.destroyCacheAndDone(cache);
+ });
+ };
+
+ callReadRange();
+ };
+ };
+
+ $.each(CacheOracle.mechanisms, function (_, mechanism) {
+ if (mechanism !== "best" && CacheOracle.isMechanismAvailable(mechanism)) {
+ $.each(feeds, function (_, feed) {
+ djstest.addTest(cacheReadRangeWallClockTest(2, 1000, mechanism, feed.uri, 5, 0, function () {
+ return { index: 0, count: 5 };
+ }), "Cache small single-page wall-clock test with " + mechanism + " on " + feed.uri);
+
+ djstest.addTest(cacheReadRangeWallClockTest(5, 1000, mechanism, feed.uri, 3, -1, function (readCount) {
+ return { index: readCount * 3, count: 3 };
+ }), "Cache page-by-page wall-clock test with " + mechanism + " on " + feed.uri);
+
+ djstest.addTest(cacheReadRangeWallClockTest(5, 1000, mechanism, feed.uri, 3, -1, function (readCount) {
+ return { index: readCount, count: 3 };
+ }), "Cache line-by-line wall-clock test with " + mechanism + " on " + feed.uri);
+ });
+
+ var largeFeedUri = "./endpoints/LargeCollectionService.svc/Customers";
+ djstest.addTest(cacheReadRangeWallClockTest(2, 1000, mechanism, largeFeedUri, 100, 0, function () {
+ return { index: 0, count: 500 };
+ }), "Cache large single-page wall-clock test with " + mechanism + " on " + largeFeedUri, undefined, 60000);
+ }
+ });
+})(this);
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/d5ec5557/odatajs/tests/odata-cache-functional-tests.html
----------------------------------------------------------------------
diff --git a/odatajs/tests/odata-cache-functional-tests.html b/odatajs/tests/odata-cache-functional-tests.html
new file mode 100644
index 0000000..f158030
--- /dev/null
+++ b/odatajs/tests/odata-cache-functional-tests.html
@@ -0,0 +1,52 @@
+<!--
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+ -->
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+ <title>OData tests against local service</title>
+ <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
+ <meta http-equiv="cache-control" content="no-cache"/>
+ <meta http-equiv="pragma" content="no-cache"/>
+ <meta http-equiv="expires" content="-1"/>
+
+ <link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.10.0.css" type="text/css" />
+ <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
+ <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
+ <script type="text/javascript" src="http://code.jquery.com/qunit/qunit-1.10.0.js"></script>
+ <script type="text/javascript" src="common/TestSynchronizerClient.js"></script>
+ <script type="text/javascript">
+ window.TestSynchronizer.init(QUnit);
+ </script>
+ <script type="text/javascript" src="../build/odatajs-4.0.0-beta-01.js"></script>
+ <script type="text/javascript" src="common/common.js"></script>
+ <script type="text/javascript" src="common/djstest.js"></script>
+ <script type="text/javascript" src="common/CacheOracle.js"></script>
+ <script type="text/javascript" src="common/ObservableHttpClient.js"></script>
+ <script type="text/javascript" src="common/ODataReadOracle.js"></script>
+ <script type="text/javascript" src="odata-cache-functional-tests.js"></script>
+</head>
+<body>
+ <h1 id="qunit-header">datajs.cache tests against local in-memory service</h1>
+ <h2 id="qunit-banner"></h2>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+</body>
+</html>