Skip to content

Commit

Permalink
modify cmr getHeaders to take an optional versions string.
Browse files Browse the repository at this point in the history
ummg version is pulled from the metadata object or assumed to be '1.4'
headers match ummg version
eslint clean up for some of the integration-tests package
  • Loading branch information
flamingbear committed Feb 13, 2019
1 parent d1c5da9 commit 2fe98ca
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 23 deletions.
13 changes: 7 additions & 6 deletions packages/cmrjs/cmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
updateToken,
validate,
validateUMMG,
ummVersion,
xmlParseOptions
} = require('./utils');

Expand Down Expand Up @@ -221,17 +222,17 @@ class CMR {
* Return object containing CMR request headers
*
* @param {string} [token] - CMR request token
* @param {boolean} isUMMG - boolean to determine content type of headers.
* @param {string} ummgVersion - UMMG metadata version string or '' if echo10 metadata
* @returns {Object} CMR headers object
*/
getHeaders(token = null, isUMMG = false) {
const contentType = isUMMG ? 'application/vnd.nasa.cmr.umm+json;version=1.4' : 'application/echo10+xml';
getHeaders(token = null, ummgVersion = false) {
const contentType = !ummgVersion ? 'application/echo10+xml' : `application/vnd.nasa.cmr.umm+json;version=${ummgVersion}`;
const headers = {
'Client-Id': this.clientId,
'Content-type': contentType
};
if (token) headers['Echo-Token'] = token;
if (isUMMG) headers.Accept = 'application/json';
if (ummgVersion) headers.Accept = 'application/json';
return headers;
}

Expand Down Expand Up @@ -264,8 +265,8 @@ class CMR {
* @returns {Promise<Object>} to the CMR response object.
*/
async ingestUMMGranule(ummgMetadata) {
const isUMMG = true;
const headers = this.getHeaders(await this.getToken(), isUMMG);
const ummgVersion = ummVersion(ummgMetadata);
const headers = this.getHeaders(await this.getToken(), ummgVersion);

const granuleId = ummgMetadata.GranuleUR || 'no GranuleId found on input metadata';
logDetails.granuleId = granuleId;
Expand Down
8 changes: 4 additions & 4 deletions packages/cmrjs/tests/test-cmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,17 @@ test('searchConcept request includes CMR client id', async (t) => {

test('getHeaders returns correct Content-type for UMMG metadata', (t) => {
const cmrInstance = new CMR('provider', 'clientID', 'username', 'password');
const isUMMG = true;
const headers = cmrInstance.getHeaders(null, isUMMG);
const ummgVersion = '1.4';
const headers = cmrInstance.getHeaders(null, ummgVersion);
console.log(headers);
t.regex(headers['Content-type'], new RegExp('application/vnd\.nasa\.cmr\.umm[+]json'));
t.is(headers['Content-type'], 'application/vnd.nasa.cmr.umm+json;version=1.4');
t.is(headers.Accept, 'application/json');
});

test('getHeaders returns correct Content-type for xml metadata by default', (t) => {
const cmrInstance = new CMR('provider', 'clientID', 'username', 'password');
const headers = cmrInstance.getHeaders();
console.log(headers);
t.regex(headers['Content-type'], new RegExp('application/echo'));
t.is(headers['Content-type'], 'application/echo10+xml');
t.is(headers.Accept, undefined);
});
33 changes: 32 additions & 1 deletion packages/cmrjs/tests/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ const sinon = require('sinon');
const test = require('ava');
const publicIp = require('public-ip');

const { getIp, getHost, hostId } = require('../utils');
const {
getIp,
getHost,
hostId,
ummVersion
} = require('../utils');

let stub;

Expand Down Expand Up @@ -64,3 +69,29 @@ test('getHost returns CMR_HOST when defined', (t) => {
process.env.CMR_HOST = anotherHost;
t.is(getHost(), anotherHost);
});

test('ummVersion returns UMM version if found on metadata object.', (t) => {
const metadata = {
restOfMetadataUpHere: 'it is all fake',
MetadataSpecification: {
URL: 'https://cdn.earthdata.nasa.gov/umm/granule/v1.5',
Name: 'UMM-G',
Version: '1.5'
}
};

const actual = ummVersion(metadata);

t.is('1.5', actual);
});

test('ummVersion returns default version 1.4 if object has no metadata specification.', (t) => {
const metadata = {
restOfMetadataUpHere: 'still fake',
MissingMetadataSpecification: 'nothing here'
};

const actual = ummVersion(metadata);

t.is('1.4', actual);
});
19 changes: 17 additions & 2 deletions packages/cmrjs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ function getUrl(type, cmrProvider) {
return url;
}


/**
* Return the UMM version as a decimal string if a version cannot be found on
* the input object version 1.4 is assumed and returned.
*
* @param {Object} umm - UMM metadata object
* @returns {string} UMM version for the given object
*/
function ummVersion(umm) {
return _get(umm, 'MetadataSpecification.Version', '1.4');
}

/**
* Posts a given xml string to the validate endpoint of the CMR
* and returns the results
Expand Down Expand Up @@ -140,14 +152,16 @@ async function validate(type, xml, identifier, provider) {
* @returns {Promise<boolean>} returns true if the document is valid
*/
async function validateUMMG(ummMetadata, identifier, provider) {
const version = ummVersion(ummMetadata);
let result;

try {
result = await got.post(`${getUrl('validate', provider)}granule/${identifier}`, {
json: true,
body: ummMetadata,
headers: {
Accept: 'application/json',
'Content-type': 'application/vnd.nasa.cmr.umm+json;version=1.4'
'Content-type': `application/vnd.nasa.cmr.umm+json;version=${version}`
}
});

Expand All @@ -160,7 +174,7 @@ async function validateUMMG(ummMetadata, identifier, provider) {
}

throw new ValidationError(
`Validation was not successful, CMR error message: ${JSON.stringify(result)}`
`Validation was not successful. UMM metadata Object: ${JSON.stringify(ummMetadata)}`
);
}

Expand Down Expand Up @@ -229,6 +243,7 @@ module.exports = {
getIp,
getUrl,
hostId,
ummVersion,
updateToken,
validate,
validateUMMG,
Expand Down
5 changes: 5 additions & 0 deletions packages/integration-tests/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-console": "off"
}
}
2 changes: 1 addition & 1 deletion packages/integration-tests/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
'use strict';

const pckg = require('../package.json');
const testRunner = require('../index');
const program = require('commander');
const testRunner = require('..');

program.version(pckg.version);

Expand Down
26 changes: 17 additions & 9 deletions packages/integration-tests/cmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const sampleUmmGranule = {
EndingDateTime: '2016-01-09T11:41:12.027Z'
}
}
}
};

/**
* Returns true if the concept exists - if the cmrLink
Expand Down Expand Up @@ -287,27 +287,35 @@ async function generateAndStoreCmrUmmJson(granule, collection, bucket, additiona
* given S3 location
*
* @param {Array<Object>} granules - list of granules in the format of the sync-granules
* output
* output
* @param {Object} collection - collection object that includes name and version
* @param {string} bucket - location to save the xmls to
* @param {string} cmrFileType - CMR file type to generate. Options are echo10, umm_json_v1_4, default
* is echo10
* @param {string} cmrFileType - CMR file type to generate. Options are echo10, umm_json_v1_4,
* default is echo10
* @param {Array<string>} additionalUrls - URLs to convert to online resources or related urls
* @returns {Array<string>} list of S3 locations for CMR xml files
*/
async function generateCmrFilesForGranules(granules, collection, bucket, cmrFileType, additionalUrls) {
async function generateCmrFilesForGranules(
granules,
collection,
bucket,
cmrFileType,
additionalUrls
) {
let files;

log.info(`Generating fake CMR file with type ${cmrFileType}`);

if (cmrFileType === 'umm_json_v1_4') {
// When we do UMM-G 1.5, we'll probably need to pass the file type into this function
files = await Promise.all(granules.map((g) =>
generateAndStoreCmrUmmJson(g, collection, bucket, additionalUrls)));
files = await Promise.all(
granules.map((g) => generateAndStoreCmrUmmJson(g, collection, bucket, additionalUrls))
);
}
else {
files = await Promise.all(granules.map((g) =>
generateAndStoreCmrXml(g, collection, bucket, additionalUrls)));
files = await Promise.all(
granules.map((g) => generateAndStoreCmrXml(g, collection, bucket, additionalUrls))
);
}

return [].concat(...files);
Expand Down
1 change: 1 addition & 0 deletions packages/integration-tests/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ async function runStep(lambdaPath, lambdaHandler, message, stepName) {
process.env.CUMULUS_MESSAGE_ADAPTER_DIR = dest;

// add step name to the message
// eslint-disable-next-line no-param-reassign
message.cumulus_meta.task = stepName;

try {
Expand Down

0 comments on commit 2fe98ca

Please sign in to comment.