Navigation Menu

Skip to content

Commit

Permalink
Add DescribeIndexFields action
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 3, 2012
1 parent 8ae9e11 commit 12a2ae6
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 7 deletions.
51 changes: 51 additions & 0 deletions lib/api/2011-02-01/configuration.js
Expand Up @@ -279,6 +279,57 @@ handlers.DeleteIndexField = function(context, request, response) {
}
};

function createIndexFields(options) {
var doc = xmlbuilder.create();
var indexFields = doc.begin('IndexFields', {version: '1.0'});
options.fields.forEach(function(field) {
indexFields.importXMLBuilder(createIndexFieldStatus({
field: field,
element: 'member'
}));
});
return doc;
}

function createDescribeIndexFieldsResponse(options) {
var doc = xmlbuilder.create();
doc.begin('DescribeIndexFieldsResponse', { version: '1.0' })
.attribute('xmlns', XMLNS)
.element('DescribeIndexFieldsResult')
.importXMLBuilder(createIndexFields(options))
.up()
.element('ResponseMetadata')
.element('RequestId').text(options.requestId || '').up()
.up();
return doc.toString();
}

handlers.DescribeIndexFields = function(context, request, response) {
var domain = new Domain(request.query.DomainName, context);

try {
var keys = Object.keys(request.query).filter(function(key) {
return /^FieldNames\.member\.\d+$/.test(key);
});
var fieldNames = keys.sort().map(function(key) {
return request.query[key];
});
var fields = fieldNames.length ?
fieldNames.map(function(name) {
return domain.getIndexField(name);
}) :
domain.indexFields ;
response.contentType('application/xml');
response.send(createDescribeIndexFieldsResponse({
fields: fields
}));
} catch (error) {
var body = createCommonErrorResponse('InternalFailure', error.message);
response.contentType('application/xml');
response.send(body, 400);
}
};

function createIndexDocumentsResponse(options) {
var doc = xmlbuilder.create();
var root = doc.begin('IndexDocumentsResponse', {version: '1.0'})
Expand Down
123 changes: 116 additions & 7 deletions test/api-configuration.test.js
Expand Up @@ -147,6 +147,24 @@ var PATTERN_DeleteIndexFieldResponse = {
}
};

function PATTERN_DescribeIndexFieldsResponse(members) {
return {
IndexFieldsResponse: {
'@': { xmlns: '' },
IndexFieldsResult: {
IndexFields: (function() {
var pattern = {};
members.forEach(function(member, index) {
pattern[index] = member;
});
return { member: pattern };
})()
},
ResponseMetadata: PATTERN_ResponseMetadata
}
};
}

function PATTERN_IndexDocumentsResponse(members) {
return {
IndexDocumentsResponse: {
Expand Down Expand Up @@ -331,14 +349,14 @@ suite('Configuration API', function() {
});

function getActualDescribedDomains(response) {
var actualDomains = response.body.DescribeDomainsResponse
.DescribeDomainsResult
.DomainStatusList
.member;
var members = response.body.DescribeDomainsResponse
.DescribeDomainsResult
.DomainStatusList
.member;
var domains = [];
for (var i in actualDomains) {
if (actualDomains.hasOwnProperty(i))
domains.push(actualDomains[i].DomainName);
for (var i in members) {
if (members.hasOwnProperty(i))
domains.push(members[i].DomainName);
}
return domains;
}
Expand Down Expand Up @@ -615,6 +633,97 @@ suite('Configuration API', function() {
});
});

function getActualDescribedIndexFields(response) {
var members = response.body.DescribeIndexFieldsResponse
.DescribeIndexFieldsResult
.IndexFields
.member;
var domains = [];
for (var i in members) {
if (members.hasOwnProperty(i))
domains.push(members[i].FieldName);
}
return domains;
}

test('Get, Action=DescribeIndexFields (all fields)', function(done) {
var domain;
utils
.get('/?DomainName=companies&Action=CreateDomain&Version=2011-02-01', {
'Host': 'cloudsearch.localhost'
})
.get('/?DomainName=companies&IndexField.IndexFieldName=name&' +
'IndexField.IndexFieldType=text&' +
'Action=DefineIndexField&Version=2011-02-01')
.get('/?DomainName=companies&IndexField.IndexFieldName=age&' +
'IndexField.IndexFieldType=uint&' +
'Action=DefineIndexField&Version=2011-02-01')
.get('/?DomainName=companies&IndexField.IndexFieldName=product&' +
'IndexField.IndexFieldType=literal&' +
'Action=DefineIndexField&Version=2011-02-01')
.get('/?Action=DescribeIndexFields&Version=2011-02-01', {
'Host': 'cloudsearch.localhost'
})
.next(function(response) {
response = toParsedResponse(response);
assert.deepEqual(response.pattern,
{ statusCode: 200,
body: PATTERN_DescribeIndexFieldsResponse([
PATTERN_IndexFieldStatus_UInt,
PATTERN_IndexFieldStatus_Literal,
PATTERN_IndexFieldStatus_Text
]) });

var expectedFields = ['age', 'name', 'product'];
var actualFields = getActualDescribedIndexFields(response);
assert.deepEqual(actualFields, expectedFields);

done();
})
.error(function(error) {
done(error);
});
});

test('Get, Action=DescribeIndexFields (specified fields)', function(done) {
utils
.get('/?DomainName=companies&Action=CreateDomain&Version=2011-02-01', {
'Host': 'cloudsearch.localhost'
})
.get('/?DomainName=companies&IndexField.IndexFieldName=name&' +
'IndexField.IndexFieldType=text&' +
'Action=DefineIndexField&Version=2011-02-01')
.get('/?DomainName=companies&IndexField.IndexFieldName=age&' +
'IndexField.IndexFieldType=uint&' +
'Action=DefineIndexField&Version=2011-02-01')
.get('/?DomainName=companies&IndexField.IndexFieldName=product&' +
'IndexField.IndexFieldType=literal&' +
'Action=DefineIndexField&Version=2011-02-01')
.get('/?Action=DescribeIndexFields&Version=2011-02-01' +
'&FieldNames.member.1=name' +
'&FieldNames.member.2=age', {
'Host': 'cloudsearch.localhost'
})
.next(function(response) {
response = toParsedResponse(response);
assert.deepEqual(response.pattern,
{ statusCode: 200,
body: PATTERN_DescribeIndexFieldsResponse([
PATTERN_IndexFieldStatus_Text,
PATTERN_IndexFieldStatus_UInt
]) });

var expectedFields = ['name', 'age'];
var actualFields = getActualDescribedIndexFields(response);
assert.deepEqual(actualFields, expectedFields);

done();
})
.error(function(error) {
done(error);
});
});

test('Get, Action=IndexDocuments', function(done) {
utils
.get('/?DomainName=companies&Action=CreateDomain&Version=2011-02-01', {
Expand Down

0 comments on commit 12a2ae6

Please sign in to comment.