diff --git a/src/__tests__/ClientHandler.test.ts b/src/__tests__/ClientHandler.test.ts index 294af5e..e82efae 100644 --- a/src/__tests__/ClientHandler.test.ts +++ b/src/__tests__/ClientHandler.test.ts @@ -1,20 +1,28 @@ import GRPCClientHandler from '../implementations/GRPCClientHandler' -import { AISServiceClientImpl, VesselInfoResponse } from '../../proto/AIS-protobuf/ais' +import { AISServiceClientImpl, VesselInfoResponse, VesselPathResponse } from '../../proto/AIS-protobuf/ais' +import { IDetailedVessel } from '../models/detailedVessel' +import { IVesselPath } from '../models/vesselPath' -// Create a mock implementation of Rpc +// Create a mock implementation of AISServiceClientImpl const mockClient: jest.Mocked = { GetVesselInfo: jest.fn(), } as unknown as jest.Mocked jest.mock('../../proto/AIS-protobuf/ais') -describe('GRPCClientHandler', () => { - let clientHandler: GRPCClientHandler +let clientHandler: GRPCClientHandler - beforeEach(() => { - clientHandler = new GRPCClientHandler(mockClient) - }) +// Setup: runs before each test +beforeEach(() => { + clientHandler = new GRPCClientHandler(mockClient) +}) + +// Cleanup: runs after each test to reset mocks +describe('GRPCClientHandler - getVesselInfo', () => { + afterEach(() => { + jest.clearAllMocks() + }) it('should return detailed vessel information', async () => { const mockResponse = { mmsi: 123456789, name: 'Test Vessel', shipType: 'Cargo' } as VesselInfoResponse // Mock the actual response mockClient.GetVesselInfo.mockResolvedValue(mockResponse) @@ -32,3 +40,69 @@ describe('GRPCClientHandler', () => { }) }) }) + +describe('GRPCClientHandler - converters', () => { + it('should convert grpcVessel to DetailedVessel', () => { + const grpcVessel: VesselInfoResponse = { + mmsi: 123456789, + name: 'Tom Cruise', + shipType: 'Fighter', + imo: 123, + callSign: 'maverick', + width: 12, + length: 13, + positionFixingDevice: 'gps', + } + + const privateMethodProto = Object.getPrototypeOf(clientHandler) //to test private method this is necessary https://stackoverflow.com/questions/48906484/how-to-unit-test-private-methods-in-typescript + const res = privateMethodProto.convertToDetailedVessel(grpcVessel) + + expect(res).toEqual({ + mmsi: 123456789, + name: 'Tom Cruise', + shipType: 'Fighter', + imo: 123, + callSign: 'maverick', + width: 12, + length: 13, + positionFixingDevice: 'gps', + }) + }) + + it('should convert grpcVesselPath to VesselPath', () => { + const grpcVesselPath: VesselPathResponse = { + mmsi: 123456789, + pathHistory: { + locations: [ + { + point: { lon: 12, lat: 13 }, + heading: 90, + timestamp: new Date('2024-01-01T00:00:00Z').getTime(), + }, + ], + }, + pathForecast: { + locations: [], + }, + } + + const privateMethodProto = Object.getPrototypeOf(clientHandler) //to test private method this is necessary https://stackoverflow.com/questions/48906484/how-to-unit-test-private-methods-in-typescript + const res = privateMethodProto.convertToVesselPath(grpcVesselPath) + + expect(res).toEqual({ + mmsi: 123456789, + pathHistory: { + locations: [ + { + point: { lon: 12, lat: 13 }, + heading: 90, + timestamp: new Date('2024-01-01T00:00:00Z'), + }, + ], + }, + pathForecast: { + locations: [], + }, + }) + }) +}) diff --git a/src/models/detailedVessel.ts b/src/models/detailedVessel.ts index 5766cc4..7f06383 100644 --- a/src/models/detailedVessel.ts +++ b/src/models/detailedVessel.ts @@ -8,16 +8,3 @@ export interface IDetailedVessel { length?: number positionFixingDevice?: string } - -export default class DetailedVessel implements IDetailedVessel { - constructor( - public mmsi: number, - public name?: string, - public shipType?: string, - public imo?: number, - public callSign?: string, - public width?: number, - public length?: number, - public positionFixingDevice?: string - ) {} -} diff --git a/src/models/location.ts b/src/models/location.ts index bc06d21..b295704 100644 --- a/src/models/location.ts +++ b/src/models/location.ts @@ -5,11 +5,3 @@ export interface ILocation { heading?: number timestamp: Date } - -export default class Location implements ILocation { - constructor( - public point: IPoint, - public heading: number, - public timestamp: Date - ) {} -} diff --git a/src/models/monitoredVessel.ts b/src/models/monitoredVessel.ts index 5b72f43..9067a20 100644 --- a/src/models/monitoredVessel.ts +++ b/src/models/monitoredVessel.ts @@ -3,11 +3,3 @@ export interface IMonitoredVessel { trustworthiness: number reason?: string } - -export default class MonitoredVessel implements IMonitoredVessel { - constructor( - public mmsi: number, - public trustworthiness: number, - public reason?: string - ) {} -} diff --git a/src/models/point.ts b/src/models/point.ts index aff8c68..a8b3033 100644 --- a/src/models/point.ts +++ b/src/models/point.ts @@ -2,10 +2,3 @@ export interface IPoint { lat: number lon: number } - -export default class Point implements IPoint { - constructor( - public lat: number, - public lon: number - ) {} -} diff --git a/src/models/selectionArea.ts b/src/models/selectionArea.ts index 298396b..15cffea 100644 --- a/src/models/selectionArea.ts +++ b/src/models/selectionArea.ts @@ -3,7 +3,3 @@ import { IPoint } from './point' export interface ISelectionArea { points: IPoint[] } - -export default class SelectionArea implements ISelectionArea { - constructor(public points: IPoint[]) {} -} diff --git a/src/models/simpleVessel.ts b/src/models/simpleVessel.ts index 9ae6e19..d043a2a 100644 --- a/src/models/simpleVessel.ts +++ b/src/models/simpleVessel.ts @@ -5,11 +5,3 @@ export interface ISimpleVessel { heading?: number mmsi: number } - -export default class SimpleVessel implements ISimpleVessel { - constructor( - public location: ILocation, - public mmsi: number, - public heading?: number - ) {} -} diff --git a/src/models/vesselPath.ts b/src/models/vesselPath.ts index bca5906..ab669cb 100644 --- a/src/models/vesselPath.ts +++ b/src/models/vesselPath.ts @@ -9,11 +9,3 @@ export interface IVesselPath { pathForecast: IPath pathHistory: IPath } - -export default class VesselPath implements IVesselPath { - constructor( - public mmsi: number, - public pathForecast: IPath, - public pathHistory: IPath - ) {} -}