diff --git a/src/modules/geography/core/domain/route.types.ts b/src/modules/geography/core/domain/route.types.ts index 74d1c6c..172d207 100644 --- a/src/modules/geography/core/domain/route.types.ts +++ b/src/modules/geography/core/domain/route.types.ts @@ -1,6 +1,6 @@ import { GeorouterPort } from '../application/ports/georouter.port'; import { GeorouterSettings } from '../application/types/georouter-settings.type'; -import { SpacetimePointProps } from './value-objects/timepoint.value-object'; +import { SpacetimePointProps } from './value-objects/spacetime-point.value-object'; import { WaypointProps } from './value-objects/waypoint.value-object'; // All properties that a Route has diff --git a/src/modules/geography/core/domain/value-objects/timepoint.value-object.ts b/src/modules/geography/core/domain/value-objects/spacetime-point.value-object.ts similarity index 70% rename from src/modules/geography/core/domain/value-objects/timepoint.value-object.ts rename to src/modules/geography/core/domain/value-objects/spacetime-point.value-object.ts index b6c7971..c7bfbce 100644 --- a/src/modules/geography/core/domain/value-objects/timepoint.value-object.ts +++ b/src/modules/geography/core/domain/value-objects/spacetime-point.value-object.ts @@ -1,4 +1,8 @@ -import { ArgumentInvalidException, ValueObject } from '@mobicoop/ddd-library'; +import { + ArgumentInvalidException, + ArgumentOutOfRangeException, + ValueObject, +} from '@mobicoop/ddd-library'; /** Note: * Value Objects with multiple properties can contain @@ -38,5 +42,9 @@ export class SpacetimePoint extends ValueObject { throw new ArgumentInvalidException( 'distance must be greater than or equal to 0', ); + if (props.lon > 180 || props.lon < -180) + throw new ArgumentOutOfRangeException('lon must be between -180 and 180'); + if (props.lat > 90 || props.lat < -90) + throw new ArgumentOutOfRangeException('lat must be between -90 and 90'); } } diff --git a/src/modules/geography/tests/unit/core/spacetime-point.value-object.spec.ts b/src/modules/geography/tests/unit/core/spacetime-point.value-object.spec.ts new file mode 100644 index 0000000..bf18bd5 --- /dev/null +++ b/src/modules/geography/tests/unit/core/spacetime-point.value-object.spec.ts @@ -0,0 +1,88 @@ +import { + ArgumentInvalidException, + ArgumentOutOfRangeException, +} from '@mobicoop/ddd-library'; +import { SpacetimePoint } from '@modules/geography/core/domain/value-objects/spacetime-point.value-object'; + +describe('Timepoint value object', () => { + it('should create a timepoint value object', () => { + const timepointVO = new SpacetimePoint({ + lon: 48.689445, + lat: 6.17651, + duration: 150, + distance: 12000, + }); + expect(timepointVO.duration).toBe(150); + expect(timepointVO.distance).toBe(12000); + expect(timepointVO.lon).toBe(48.689445); + expect(timepointVO.lat).toBe(6.17651); + }); + it('should throw an exception if longitude is invalid', () => { + try { + new SpacetimePoint({ + lon: 348.689445, + lat: 6.17651, + duration: 150, + distance: 12000, + }); + } catch (e: any) { + expect(e).toBeInstanceOf(ArgumentOutOfRangeException); + } + try { + new SpacetimePoint({ + lon: -348.689445, + lat: 6.17651, + duration: 150, + distance: 12000, + }); + } catch (e: any) { + expect(e).toBeInstanceOf(ArgumentOutOfRangeException); + } + }); + it('should throw an exception if latitude is invalid', () => { + try { + new SpacetimePoint({ + lon: 48.689445, + lat: 96.17651, + duration: 150, + distance: 12000, + }); + } catch (e: any) { + expect(e).toBeInstanceOf(ArgumentOutOfRangeException); + } + try { + new SpacetimePoint({ + lon: 48.689445, + lat: -96.17651, + duration: 150, + distance: 12000, + }); + } catch (e: any) { + expect(e).toBeInstanceOf(ArgumentOutOfRangeException); + } + }); + it('should throw an exception if distance is invalid', () => { + try { + new SpacetimePoint({ + lon: 48.689445, + lat: 6.17651, + duration: 150, + distance: -12000, + }); + } catch (e: any) { + expect(e).toBeInstanceOf(ArgumentInvalidException); + } + }); + it('should throw an exception if duration is invalid', () => { + try { + new SpacetimePoint({ + lon: 48.689445, + lat: 6.17651, + duration: -150, + distance: 12000, + }); + } catch (e: any) { + expect(e).toBeInstanceOf(ArgumentInvalidException); + } + }); +});