Skip to content

Commit

Permalink
fix: #268 Import URL entities need a REDIRECT status, and additional …
Browse files Browse the repository at this point in the history
…properties for reporting
  • Loading branch information
blefebvre committed Jun 18, 2024
1 parent 8ae6705 commit 93cdc8f
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 13 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/spacecat-shared-data-access/src/dto/import-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export const ImportUrlDto = {
jobId: importUrl.getJobId(),
url: importUrl.getUrl(),
status: importUrl.getStatus(),
reason: importUrl.getReason(),
path: importUrl.getPath(),
file: importUrl.getFile(),
}),

/**
Expand All @@ -37,6 +40,9 @@ export const ImportUrlDto = {
jobId: dynamoItem.jobId,
url: dynamoItem.url,
status: dynamoItem.status,
reason: dynamoItem.reason,
path: dynamoItem.path,
file: dynamoItem.file,
};
return createImportUrl(importUrlData);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
* governing permissions and limitations under the License.
*/

import { isValidUrl } from '@adobe/spacecat-shared-utils';
import { hasText, isValidUrl } from '@adobe/spacecat-shared-utils';
import { Base } from '../base.js';
import { ImportJobStatus } from './import-job.js';

export const ImportUrlStatus = {
PENDING: 'PENDING',
REDIRECT: 'REDIRECT',
...ImportJobStatus,
};

Expand All @@ -31,11 +32,16 @@ const ImportUrl = (data) => {
self.getJobId = () => self.state.jobId;
self.getUrl = () => self.state.url;
self.getStatus = () => self.state.status;
self.getReason = () => self.state.reason;
// Absolute path to the resource that is being imported for the given URL
self.getPath = () => self.state.path;
// Resulting path and filename of the imported .docx file
self.getFile = () => self.state.file;

/**
* Updates the status of the ImportUrl
*/
self.updateStatus = (status) => {
* Updates the status of the ImportUrl
*/
self.setStatus = (status) => {
if (!Object.values(ImportUrlStatus).includes(status)) {
throw new Error(`Invalid Import URL status during update: ${status}`);
}
Expand All @@ -45,6 +51,47 @@ const ImportUrl = (data) => {

return self;
};

/**
* Updates the reason that the import of this URL was not successful
*/
self.setReason = (reason) => {
if (!hasText(reason)) {
return self; // no-op
}

self.state.reason = reason;
self.touch();
return self;
};

/**
* Updates the path of the ImportUrl
*/
self.setPath = (path) => {
if (!hasText(path)) {
return self; // no-op
}

self.state.path = path;
self.touch();
return self;
};

/**
* Updates the file of the ImportUrl. This is the path and file name of the file which
* was imported.
*/
self.setFile = (file) => {
if (!hasText(file)) {
return self; // no-op
}

self.state.file = file;
self.touch();
return self;
};

return Object.freeze(self);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
/* eslint-env mocha */
import { expect } from 'chai';

import { createImportUrl } from '../../../../src/models/importer/import-url.js';
import { createImportUrl, ImportUrlStatus } from '../../../../src/models/importer/import-url.js';
import { ImportUrlDto } from '../../../../src/dto/import-url.js';

const validImportUrlData = {
id: '123',
Expand All @@ -22,6 +23,16 @@ const validImportUrlData = {
status: 'RUNNING',
};

const importUrlRedirectData = {
id: '456',
url: 'https://www.example.com/redirect',
jobId: '456',
status: 'REDIRECT',
reason: 'https://www.example.com/redirect/destination',
path: '/test-data',
file: '/test-data.docx',
};

describe('ImportUrl Model tests', () => {
describe('Validation Tests', () => {
it('throws an error if url is not a valid URL', () => {
Expand All @@ -34,21 +45,84 @@ describe('ImportUrl Model tests', () => {
});
describe('Import URL Functionality Tests', () => {
let importUrl;

beforeEach(() => {
importUrl = createImportUrl({ ...validImportUrlData });
});

it('updates the status of the import URL', () => {
importUrl.updateStatus('COMPLETE');
importUrl.setStatus('COMPLETE');
expect(importUrl.getStatus()).to.equal('COMPLETE');
});

it('returns the url attribute of the import URL', () => {
expect(importUrl.getUrl()).to.equal('https://www.example.com');
});

it('returns the job ID of the import URL', () => {
expect(importUrl.getJobId()).to.equal('456');
});

it('throws an error if the status is invalid', () => {
expect(() => importUrl.updateStatus('invalid')).to.throw('Invalid Import URL status during update: invalid');
expect(() => importUrl.setStatus('invalid')).to.throw('Invalid Import URL status during update: invalid');
});

it('updates the status and reason for a url', () => {
importUrl.setStatus('REDIRECT');
importUrl.setReason('https://www.example.com/redirect/destination');
expect(importUrl.getStatus()).to.equal(ImportUrlStatus.REDIRECT);
expect(importUrl.getReason()).to.equal('https://www.example.com/redirect/destination');
});

it('does not update properties if the setters are passed invalid data', () => {
const importUrlRedirect = createImportUrl(importUrlRedirectData);

importUrlRedirect.setReason(undefined);
expect(importUrlRedirect.getReason()).to.equal('https://www.example.com/redirect/destination');

importUrlRedirect.setPath(null);
expect(importUrlRedirect.getPath()).to.equal('/test-data');

importUrlRedirect.setFile('');
expect(importUrlRedirect.getFile()).to.equal('/test-data.docx');
});

it('updates the file and path for a url', () => {
importUrl.setStatus('COMPLETE');
importUrl.setPath('/index');
importUrl.setFile('/index.docx');

expect(importUrl.getStatus()).to.equal(ImportUrlStatus.COMPLETE);
expect(importUrl.getPath()).to.equal('/index');
expect(importUrl.getFile()).to.equal('/index.docx');
});
});

describe('Import URL DTO Tests', () => {
it('should serialize to a Dynamo-compatible object', () => {
const importUrlRedirect = createImportUrl(importUrlRedirectData);
expect(ImportUrlDto.toDynamoItem(importUrlRedirect)).to.deep.equal({
id: '456',
url: 'https://www.example.com/redirect',
jobId: '456',
status: 'REDIRECT',
reason: 'https://www.example.com/redirect/destination',
path: '/test-data',
file: '/test-data.docx',
});
});

it('should deserialize from a Dynamo object', () => {
const urlFromDynamo = ImportUrlDto.fromDynamoItem(importUrlRedirectData);

const importUrlRedirect = createImportUrl(importUrlRedirectData);
expect(urlFromDynamo.getId()).to.deep.equal(importUrlRedirect.getId());
expect(urlFromDynamo.getUrl()).to.deep.equal(importUrlRedirect.getUrl());
expect(urlFromDynamo.getJobId()).to.deep.equal(importUrlRedirect.getJobId());
expect(urlFromDynamo.getStatus()).to.deep.equal(importUrlRedirect.getStatus());
expect(urlFromDynamo.getReason()).to.deep.equal(importUrlRedirect.getReason());
expect(urlFromDynamo.getPath()).to.deep.equal(importUrlRedirect.getPath());
expect(urlFromDynamo.getFile()).to.deep.equal(importUrlRedirect.getFile());
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('Import Url Tests', () => {
mockDynamoClient.getItem.resolves(mockImportUrl);

const importUrl = await exportedFunctions.getImportUrlById('test-id');
importUrl.updateStatus('COMPLETE');
importUrl.setStatus('COMPLETE');
const result = await exportedFunctions.updateImportUrl(importUrl);

expect(result).to.be.not.null;
Expand Down

0 comments on commit 93cdc8f

Please sign in to comment.