Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CDAP-8075] Adds ability to interpret Version Ranges for CDAP UI #7607

Merged
merged 1 commit into from Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 93 additions & 0 deletions cdap-ui/app/cdap/services/VersionRange/Version.js
@@ -0,0 +1,93 @@
/*
* Copyright © 2017 Cask Data, Inc.
*
* Licensed 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.
*/

// FE implementation of ArtifactVersion java class
// co.cask.cdap.api.artifact.ArtifactVersion
//
// [major].[minor].[fix](-)[suffix]

export default class Version {
constructor(versionString) {
let major,
minor,
fix,
suffix;

// Get Suffix
let versionSuffixSplit = versionString.split('-');
suffix = versionSuffixSplit[1] || null;

// Split versions
let versionSplit = versionSuffixSplit[0].split('.');
major = versionSplit[0];
minor = versionSplit[1];
fix = versionSplit[2];

this.version = versionString;
this.major = parseInt(major, 10) || null;
this.minor = parseInt(minor, 10) || null;
this.fix = parseInt(fix, 10) || null;
this.suffix = suffix;
}

isSnapshot() {
return this.suffix !== null && this.suffix.length !== 0 && this.suffix.toLowerCase() === 'snapshot';
}

// -1, 0, 1 smaller, equal, greater
// special case for no suffix is greater than with suffix. (release > snapshot)
compareTo(other) {
let cmp = this.compare(this.major, other.major);
if (cmp !== 0) {
return cmp;
}

cmp = this.compare(this.minor, other.minor);
if (cmp !== 0) {
return cmp;
}

cmp = this.compare(this.fix, other.fix);
if (cmp !== 0) {
return cmp;
}

// All numerical part of the version are the same, compare the suffix.
// A special case is no suffix is "greater" than with suffix. This is usually true (e.g. release > snapshot)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not be true right? 4.0.0 is not greater than 4.1.0-SNAPSHOT? Or am I missing something here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4.0.0 would have been returned as less than 4.1.0 because of the minor comparison before this line. This line will simply compare 4.0.0 with 4.0.0-SNAPSHOT.

The comparisons in order:

  1. Major
  2. Minor
  3. Fix
  4. Suffix

if (this.suffix === null) {
return other.suffix === null ? 0 : 1;
}

return other.suffix === null ? -1 : 0;
}

compare(first, second) {
if ((first === null && second === null) || first === second) {
return 0;
}

if (first === null || first < second) {
return -1;
}

if (second === null || first > second) {
return 1;
}

// for invalid comparison
return null;
}
}
85 changes: 85 additions & 0 deletions cdap-ui/app/cdap/services/VersionRange/__tests__/version.test.js
@@ -0,0 +1,85 @@
/*
* Copyright © 2017 Cask Data, Inc.
*
* Licensed 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.
*/

import Version from 'services/VersionRange/Version';

describe('Version Class', () => {
const SNAPSHOT_VERSION = '1.0.0-SNAPSHOT';
const RELEASE_VERSION = '1.0.0';
const MINOR_VERSION = '1.1.0';
const FIX_VERSION = '1.0.1';
const MINOR_FIX_VERSION = '1.1.1';

it('should detect SNAPSHOT version', () => {
let snapshotVersion = new Version(SNAPSHOT_VERSION);
let releaseVersion = new Version(RELEASE_VERSION);

expect(snapshotVersion.isSnapshot()).toBe(true);
expect(releaseVersion.isSnapshot()).toBe(false);
});

it('should correctly compare same version', () => {
let version1 = new Version(RELEASE_VERSION);
let version2 = new Version(RELEASE_VERSION);

let snapshot1 = new Version(SNAPSHOT_VERSION);
let snapshot2 = new Version(SNAPSHOT_VERSION);

expect(version1.compareTo(version2)).toBe(0);
expect(snapshot1.compareTo(snapshot2)).toBe(0);
});

it('should correctly compare release and snasphot', () => {
let snapshotVersion = new Version(SNAPSHOT_VERSION);
let releaseVersion = new Version(RELEASE_VERSION);

expect(snapshotVersion.compareTo(releaseVersion)).toBe(-1);
expect(releaseVersion.compareTo(snapshotVersion)).toBe(1);
});

it('should correctly compare minor version', () => {
let snapshotVersion = new Version(SNAPSHOT_VERSION);
let releaseVersion = new Version(RELEASE_VERSION);
let minorVersion = new Version(MINOR_VERSION);

expect(releaseVersion.compareTo(minorVersion)).toBe(-1);
expect(minorVersion.compareTo(releaseVersion)).toBe(1);
expect(minorVersion.compareTo(snapshotVersion)).toBe(1);
expect(snapshotVersion.compareTo(minorVersion)).toBe(-1);
});

it('should correctly compare fix version', () => {
let snapshotVersion = new Version(SNAPSHOT_VERSION);
let releaseVersion = new Version(RELEASE_VERSION);
let fixVersion = new Version(FIX_VERSION);

expect(releaseVersion.compareTo(fixVersion)).toBe(-1);
expect(fixVersion.compareTo(releaseVersion)).toBe(1);
expect(fixVersion.compareTo(snapshotVersion)).toBe(1);
expect(snapshotVersion.compareTo(fixVersion)).toBe(-1);
});

it('should correctly compare fix version', () => {
let snapshotVersion = new Version(SNAPSHOT_VERSION);
let fixVersion = new Version(FIX_VERSION);
let minorFixVersion = new Version(MINOR_FIX_VERSION);

expect(fixVersion.compareTo(minorFixVersion)).toBe(-1);
expect(minorFixVersion.compareTo(fixVersion)).toBe(1);
expect(minorFixVersion.compareTo(snapshotVersion)).toBe(1);
expect(snapshotVersion.compareTo(minorFixVersion)).toBe(-1);
});
});
@@ -0,0 +1,48 @@
/*
* Copyright © 2017 Cask Data, Inc.
*
* Licensed 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.
*/

import VersionRange from 'services/VersionRange';
import Version from 'services/VersionRange/Version';

describe('Version Range Class', () => {
const EXCLUSIVE_RANGE = new VersionRange('[1.0.0, 1.1.0)');
const INCLUSIVE_RANGE = new VersionRange('[1.0.0, 1.1.0]');
const EXCLUSIVE_SNAPSHOT_RANGE = new VersionRange('[1.0.0-SNAPSHOT, 1.1.0)');

const IN_RANGE = new Version('1.0.1');
const OUT_RANGE = new Version('1.2.0');
const EDGE_RANGE = new Version('1.1.0');
const RELEASE_VERSION = new Version('1.0.0');
const SNAPSHOT_VERSION = new Version('1.0.0-SNAPSHOT');

it('should detect a version is in range', () => {
expect(EXCLUSIVE_RANGE.versionIsInRange(IN_RANGE)).toBe(true);
});

it('should detect a version out of range', () => {
expect(EXCLUSIVE_RANGE.versionIsInRange(OUT_RANGE)).toBe(false);
});

it('should detect edge range version', () => {
expect(EXCLUSIVE_RANGE.versionIsInRange(EDGE_RANGE)).toBe(false);
expect(INCLUSIVE_RANGE.versionIsInRange(EDGE_RANGE)).toBe(true);
});

it('should correctly determine snapshot range', () => {
expect(EXCLUSIVE_SNAPSHOT_RANGE.versionIsInRange(RELEASE_VERSION)).toBe(true);
expect(EXCLUSIVE_RANGE.versionIsInRange(SNAPSHOT_VERSION)).toBe(false);
});
});
69 changes: 69 additions & 0 deletions cdap-ui/app/cdap/services/VersionRange/index.js
@@ -0,0 +1,69 @@
/*
* Copyright © 2017 Cask Data, Inc.
*
* Licensed 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.
*/

import Version from 'services/VersionRange/Version';

// FE implementation of ArtifactRange
// co.cask.cdap.proto.artifact.ArtifactRange
export default class VersionRange {
constructor(range) {
let {
lower,
upper,
isLowerInclusive,
isUpperInclusive
} = parseRange(range);

this.lower = lower;
this.upper = upper;
this.isLowerInclusive = isLowerInclusive;
this.isUpperInclusive = isUpperInclusive;
}

versionIsInRange(version) {
let lowerCompare = version.compareTo(this.lower);
let lowerSatisfied = this.isLowerInclusive ? lowerCompare >= 0 : lowerCompare > 0;

let upperCompare = version.compareTo(this.upper);
let upperSatisfied = this.isUpperInclusive ? upperCompare <= 0 : upperCompare < 0;

return lowerSatisfied && upperSatisfied;
}
}

function parseRange(range) {
let lower,
upper,
isLowerInclusive,
isUpperInclusive;

let trimmedRange = range.trim();

isLowerInclusive = trimmedRange.charAt(0) === '[';
isUpperInclusive = trimmedRange.charAt(trimmedRange.length - 1) === ']';

let split = trimmedRange.split(',');

lower = new Version(split[0].trim().substr(1).trim());
upper = new Version(split[1].trim().substr(0, split[1].length - 1).trim());

return {
lower,
upper,
isLowerInclusive,
isUpperInclusive
};
}