Commit 86118dee authored by mo khan's avatar mo khan

Introduce ObjectMother for report objects

* https://martinfowler.com/bliki/ObjectMother.html
parent 712f0771
...@@ -32,23 +32,19 @@ export default class V2Report { ...@@ -32,23 +32,19 @@ export default class V2Report {
if (matchingLicense) matchingLicense.count += 1; if (matchingLicense) matchingLicense.count += 1;
} }
mapFromDependency(dependency) { mapFromDependency({ name, description, url, licenses }) {
const combinedLicense = this.combine(dependency.licenses, license => { const combinedLicense = this.combine(licenses, license => {
this.incrementCountFor(license.name); this.incrementCountFor(license.name);
}); });
return { return {
license: combinedLicense, license: combinedLicense,
dependency: { dependency: { name, url, description },
name: dependency.name,
url: dependency.url,
description: dependency.description,
},
}; };
} }
static mapFromLicense(license) { static mapFromLicense({ name, url = '', count = 0 }) {
return { name: license.name, count: 0 }; return { name, url, count };
} }
static createLicenseMap(licenses) { static createLicenseMap(licenses) {
......
import { LICENSE_APPROVAL_STATUS } from 'ee/vue_shared/license_management/constants'; import { LICENSE_APPROVAL_STATUS } from 'ee/vue_shared/license_management/constants';
const urlFor = ({ scheme = 'https', host = 'www.example.org', path = '/' }) =>
`${scheme}://${host}${path}`;
const licenseUrlFor = name =>
urlFor({ host: 'opensource.org', path: `/licenses/${name.split(' ')[0]}` });
const dependencyUrlFor = name => urlFor({ path: `/${name}` });
const normalizeV1License = ({ name, url = licenseUrlFor(name) }) => ({ name, url });
const V1 = {
normalizeLicenseSummary: ({ name, url = licenseUrlFor(name), count = 0 }) => ({
name,
url,
count,
}),
normalizeDependency: ({
name,
url = dependencyUrlFor(name),
description = name.toUpperCase(),
license = {},
}) => ({ dependency: { name, url, description }, license: normalizeV1License(license) }),
};
const V2 = {
normalizeLicenseSummary: ({ id, name, url = licenseUrlFor(id) }) => ({ id, name, url }),
normalizeDependency: ({
name,
url = dependencyUrlFor(name),
description = name.toUpperCase(),
licenses = [],
}) => ({ name, url, licenses, description }),
};
export class Builder {
static for(version, template = { licenses: [], dependencies: [] }) {
return new Builder(version, template);
}
static forV1() {
return this.for(V1);
}
static forV2() {
return this.for(V2, { version: '2.0', licenses: [], dependencies: [] });
}
constructor(version, template = { licenses: [], dependencies: [] }) {
this.report = template;
this.version = version;
}
addLicense(license) {
this.report.licenses.push(this.version.normalizeLicenseSummary(license));
return this;
}
addDependency(dependency) {
this.report.dependencies.push(this.version.normalizeDependency(dependency));
return this;
}
build(override = {}) {
return Object.assign(this.report, override);
}
}
export const approvedLicense = { export const approvedLicense = {
id: 5, id: 5,
name: 'MIT', name: 'MIT',
...@@ -12,94 +74,46 @@ export const blacklistedLicense = { ...@@ -12,94 +74,46 @@ export const blacklistedLicense = {
approvalStatus: LICENSE_APPROVAL_STATUS.BLACKLISTED, approvalStatus: LICENSE_APPROVAL_STATUS.BLACKLISTED,
}; };
export const licenseBaseIssues = { export const licenseBaseIssues = Builder.forV1()
licenses: [ .addLicense({ name: 'MIT', count: 1 })
{ .addDependency({
count: 1, name: 'bundler',
name: 'MIT', url: 'http://bundler.io',
}, description: "The best way to manage your application's dependencies",
], license: { name: 'MIT', url: 'http://opensource.org/licenses/mit-license' },
dependencies: [ })
{ .build();
license: {
name: 'MIT',
url: 'http://opensource.org/licenses/mit-license',
},
dependency: {
name: 'bundler',
url: 'http://bundler.io',
description: "The best way to manage your application's dependencies",
paths: ['.'],
},
},
],
};
export const licenseHeadIssues = { export const licenseHeadIssues = Builder.forV1()
licenses: [ .addLicense({ name: 'New BSD', count: 3 })
{ .addLicense({ name: 'MIT', count: 1 })
count: 3, .addDependency({
name: 'New BSD', name: 'pg',
}, url: 'https://bitbucket.org/ged/ruby-pg',
{ description: 'Pg is the Ruby interface to the {PostgreSQL RDBMS}[http://www.postgresql.org/]',
count: 1, license: { name: 'New BSD', url: 'http://opensource.org/licenses/BSD-3-Clause' },
name: 'MIT', })
}, .addDependency({
], name: 'puma',
dependencies: [ url: 'http://puma.io',
{ description:
license: { 'Puma is a simple, fast, threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications',
name: 'New BSD', license: { name: 'New BSD', url: 'http://opensource.org/licenses/BSD-3-Clause' },
url: 'http://opensource.org/licenses/BSD-3-Clause', })
}, .addDependency({
dependency: { name: 'foo',
name: 'pg', url: 'http://foo.io',
url: 'https://bitbucket.org/ged/ruby-pg', description:
description: 'Foo is a simple, fast, threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications',
'Pg is the Ruby interface to the {PostgreSQL RDBMS}[http://www.postgresql.org/]', license: { name: 'New BSD', url: 'http://opensource.org/licenses/BSD-3-Clause' },
paths: ['.'], })
}, .addDependency({
}, name: 'execjs',
{ url: 'https://github.com/rails/execjs',
license: { description: 'Run JavaScript code from Ruby',
name: 'New BSD', license: { name: 'MIT', url: 'http://opensource.org/licenses/mit-license' },
url: 'http://opensource.org/licenses/BSD-3-Clause', })
}, .build();
dependency: {
name: 'puma',
url: 'http://puma.io',
description:
'Puma is a simple, fast, threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications',
paths: ['.'],
},
},
{
license: {
name: 'New BSD',
url: 'http://opensource.org/licenses/BSD-3-Clause',
},
dependency: {
name: 'foo',
url: 'http://foo.io',
description:
'Foo is a simple, fast, threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications',
paths: ['.'],
},
},
{
license: {
name: 'MIT',
url: 'http://opensource.org/licenses/mit-license',
},
dependency: {
name: 'execjs',
url: 'https://github.com/rails/execjs',
description: 'Run JavaScript code from Ruby',
paths: ['.'],
},
},
],
};
export const licenseReport = [ export const licenseReport = [
{ {
......
import { Builder } from '../../license_management/mock_data';
describe('build', () => {
it('creates a v1 report', () => {
const result = Builder.forV1()
.addLicense({ name: 'MIT License' })
.addDependency({ name: 'rails', license: { name: 'MIT License' } })
.build();
expect(result).toMatchObject({
licenses: [{ name: 'MIT License', url: 'https://opensource.org/licenses/MIT', count: 0 }],
dependencies: [
{
license: { name: 'MIT License', url: 'https://opensource.org/licenses/MIT' },
dependency: { name: 'rails', description: 'RAILS', url: 'https://www.example.org/rails' },
},
],
});
});
it('creates a v2 report', () => {
const result = Builder.forV2()
.addLicense({ id: 'MIT', name: 'MIT License' })
.addDependency({ name: 'rails', licenses: ['MIT'] })
.build();
expect(result).toMatchObject({
version: '2.0',
licenses: [{ id: 'MIT', name: 'MIT License', url: 'https://opensource.org/licenses/MIT' }],
dependencies: [{ name: 'rails', description: 'RAILS', licenses: ['MIT'] }],
});
});
});
import ReportMapper from 'ee/vue_shared/license_management/report_mapper'; import ReportMapper from 'ee/vue_shared/license_management/report_mapper';
import { Builder } from '../../license_management/mock_data';
describe('mapFrom', () => { describe('mapFrom', () => {
let subject = null; let subject = null;
...@@ -8,80 +9,34 @@ describe('mapFrom', () => { ...@@ -8,80 +9,34 @@ describe('mapFrom', () => {
}); });
it('converts a v2 schema report to v1', () => { it('converts a v2 schema report to v1', () => {
const report = { const report = Builder.forV2()
version: '2.0', .addLicense({ id: 'MIT', name: 'MIT License' })
licenses: [ .addLicense({ id: 'BSD', name: 'BSD License' })
{ id: 'MIT', name: 'MIT License', url: 'https://opensource.org/licenses/MIT' }, .addDependency({ name: 'x', licenses: ['MIT'] })
{ id: 'BSD', name: 'BSD License', url: 'https://opensource.org/licenses/BSD' }, .addDependency({ name: 'y', licenses: ['BSD'] })
], .addDependency({ name: 'z', licenses: ['BSD', 'MIT'] })
dependencies: [ .build();
{ name: 'x', url: 'https://www.example.com/x', licenses: ['MIT'], description: 'X' },
{ name: 'y', url: 'https://www.example.com/y', licenses: ['BSD'], description: 'Y' },
{
name: 'z',
url: 'https://www.example.com/z',
licenses: ['BSD', 'MIT'],
description: 'Z',
},
],
};
const result = subject.mapFrom(report);
expect(result).toMatchObject({ const result = subject.mapFrom(report);
licenses: [{ name: 'BSD License', count: 2 }, { name: 'MIT License', count: 2 }], expect(result).toMatchObject(
dependencies: [ Builder.forV1()
{ .addLicense({ name: 'BSD License', count: 2 })
license: { .addLicense({ name: 'MIT License', count: 2 })
name: 'MIT License', .addDependency({ name: 'x', license: { name: 'MIT License' } })
url: 'https://opensource.org/licenses/MIT', .addDependency({ name: 'y', license: { name: 'BSD License' } })
}, .addDependency({ name: 'z', license: { name: 'BSD License, MIT License', url: '' } })
dependency: { .build(),
name: 'x', );
url: 'https://www.example.com/x',
description: 'X',
},
},
{
license: {
name: 'BSD License',
url: 'https://opensource.org/licenses/BSD',
},
dependency: {
name: 'y',
url: 'https://www.example.com/y',
description: 'Y',
},
},
{
license: {
name: 'BSD License, MIT License',
url: '',
},
dependency: {
name: 'z',
url: 'https://www.example.com/z',
description: 'Z',
},
},
],
});
}); });
it('returns a v1 schema report', () => { it('returns a v1 schema report', () => {
const report = { const report = Builder.forV1().build();
licenses: [],
dependencies: [],
};
expect(subject.mapFrom(report)).toBe(report); expect(subject.mapFrom(report)).toBe(report);
}); });
it('returns a v1.1 schema report', () => { it('returns a v1.1 schema report', () => {
const report = { const report = Builder.forV1().build({ version: '1.1' });
version: '1.1',
licenses: [],
dependencies: [],
};
expect(subject.mapFrom(report)).toBe(report); expect(subject.mapFrom(report)).toBe(report);
}); });
......
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
import { LICENSE_APPROVAL_STATUS } from 'ee/vue_shared/license_management/constants'; import { LICENSE_APPROVAL_STATUS } from 'ee/vue_shared/license_management/constants';
import { STATUS_FAILED, STATUS_NEUTRAL, STATUS_SUCCESS } from '~/reports/constants'; import { STATUS_FAILED, STATUS_NEUTRAL, STATUS_SUCCESS } from '~/reports/constants';
import { import {
Builder,
approvedLicense, approvedLicense,
blacklistedLicense, blacklistedLicense,
licenseHeadIssues, licenseHeadIssues,
...@@ -65,31 +66,18 @@ describe('utils', () => { ...@@ -65,31 +66,18 @@ describe('utils', () => {
it('compares a v2 report with a v2 report', () => { it('compares a v2 report with a v2 report', () => {
const policies = [{ id: 100, name: 'BSD License', approvalStatus: 'blacklisted' }]; const policies = [{ id: 100, name: 'BSD License', approvalStatus: 'blacklisted' }];
const baseReport = { const baseReport = Builder.forV2()
version: '2.0', .addLicense({ id: 'MIT', name: 'MIT License' })
licenses: [{ id: 'MIT', name: 'MIT License', url: 'https://opensource.org/licenses/MIT' }], .addDependency({ name: 'x', licenses: ['MIT'] })
dependencies: [ .build();
{ name: 'x', url: 'https://www.example.com/x', licenses: ['MIT'], description: 'X' },
], const headReport = Builder.forV2()
}; .addLicense({ id: 'MIT', name: 'MIT License' })
.addLicense({ id: 'BSD', name: 'BSD License' })
const headReport = { .addDependency({ name: 'x', licenses: ['MIT'] })
version: '2.0', .addDependency({ name: 'y', licenses: ['BSD'] })
licenses: [ .addDependency({ name: 'z', licenses: ['BSD', 'MIT'] })
{ id: 'MIT', name: 'MIT License', url: 'https://opensource.org/licenses/MIT' }, .build();
{ id: 'BSD', name: 'BSD License', url: 'https://opensource.org/licenses/BSD' },
],
dependencies: [
{ name: 'x', url: 'https://www.example.com/x', licenses: ['MIT'], description: 'X' },
{ name: 'y', url: 'https://www.example.com/y', licenses: ['BSD'], description: 'Y' },
{
name: 'z',
url: 'https://www.example.com/z',
licenses: ['BSD', 'MIT'],
description: 'Z',
},
],
};
const result = parseLicenseReportMetrics(headReport, baseReport, policies); const result = parseLicenseReportMetrics(headReport, baseReport, policies);
...@@ -101,43 +89,29 @@ describe('utils', () => { ...@@ -101,43 +89,29 @@ describe('utils', () => {
count: 2, count: 2,
status: 'failed', status: 'failed',
name: 'BSD License', name: 'BSD License',
packages: [{ name: 'y', url: 'https://www.example.com/y', description: 'Y' }], url: 'https://opensource.org/licenses/BSD',
packages: [{ name: 'y', url: 'https://www.example.org/y', description: 'Y' }],
}), }),
); );
}); });
it('compares a v1 report with a v2 report', () => { it('compares a v1 report with a v2 report', () => {
const policies = [{ id: 101, name: 'BSD License', approvalStatus: 'blacklisted' }]; const policies = [{ id: 101, name: 'BSD License', approvalStatus: 'blacklisted' }];
const baseReport = { const baseReport = Builder.forV1()
licenses: [{ name: 'MIT License', url: 'https://opensource.org/licenses/MIT' }], .addLicense({ name: 'MIT License' })
dependencies: [ .addDependency({
{ name: 'x',
license: { license: { name: 'MIT License', url: 'https://opensource.org/licenses/MIT' },
name: 'MIT License', })
url: 'https://opensource.org/licenses/MIT', .build();
},
dependency: { name: 'x', url: 'https://www.example.com/x', description: 'X' }, const headReport = Builder.forV2()
}, .addLicense({ id: 'MIT', name: 'MIT License' })
], .addLicense({ id: 'BSD', name: 'BSD License' })
}; .addDependency({ name: 'x', licenses: ['MIT'] })
.addDependency({ name: 'y', licenses: ['BSD'] })
const headReport = { .addDependency({ name: 'z', licenses: ['BSD', 'MIT'] })
version: '2.0', .build();
licenses: [
{ id: 'MIT', name: 'MIT License', url: 'https://opensource.org/licenses/MIT' },
{ id: 'BSD', name: 'BSD License', url: 'https://opensource.org/licenses/BSD' },
],
dependencies: [
{ name: 'x', url: 'https://www.example.com/x', licenses: ['MIT'], description: 'X' },
{ name: 'y', url: 'https://www.example.com/y', licenses: ['BSD'], description: 'Y' },
{
name: 'z',
url: 'https://www.example.com/z',
licenses: ['BSD', 'MIT'],
description: 'Z',
},
],
};
const result = parseLicenseReportMetrics(headReport, baseReport, policies); const result = parseLicenseReportMetrics(headReport, baseReport, policies);
...@@ -149,7 +123,8 @@ describe('utils', () => { ...@@ -149,7 +123,8 @@ describe('utils', () => {
count: 2, count: 2,
status: 'failed', status: 'failed',
name: 'BSD License', name: 'BSD License',
packages: [{ name: 'y', url: 'https://www.example.com/y', description: 'Y' }], url: 'https://opensource.org/licenses/BSD',
packages: [{ name: 'y', url: 'https://www.example.org/y', description: 'Y' }],
}), }),
); );
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment