Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,75 @@ public async Task Handle_PromotesAircraftToCurrentDataAuthorityOnFirstDownlink()
Assert.Equal(Contracts.DataAuthorityState.CurrentDataAuthority, dto.DataAuthorityState);
}

[Fact]
public async Task Handle_DoesNotPromoteToCurrentDataAuthority_WhenNotCurrentDataAuthorityMessageReceived()
{
// Arrange
var clock = new TestClock();
var aircraftManager = new TestAircraftRepository();
var aircraft = new AircraftConnection("UAL123", "hoppies-ybbb", "YBBB", DataAuthorityState.NextDataAuthority);
aircraft.RequestLogon(clock.UtcNow());
aircraft.AcceptLogon(clock.UtcNow());
await aircraftManager.Add(new(aircraft.Callsign, aircraft.AcarsClientId), aircraft, CancellationToken.None);

var controllerManager = new TestControllerRepository();
var controller = new ControllerInfo(
Guid.NewGuid(),
"ConnectionId-1",
"YBBB",
"BN-TSN_FSS",
"1234567");
await controllerManager.Add(controller, CancellationToken.None);

var mediator = Substitute.For<IMediator>();
var hubContext = Substitute.For<IHubContext<ControllerHub>>();
var clientProxy = Substitute.For<IClientProxy>();
hubContext.Clients.Clients(Arg.Any<IReadOnlyList<string>>()).Returns(clientProxy);

var dialogueRepository = new TestDialogueRepository();

var publisher = new TestPublisher();
var handler = new DownlinkReceivedNotificationHandler(
aircraftManager,
mediator,
clock,
controllerManager,
dialogueRepository,
hubContext,
publisher,
Logger.None);

var downlinkMessage = new DownlinkMessage(
1,
null,
"UAL123",
CpdlcDownlinkResponseType.NoResponse,
AlertType.None,
"NOT CURRENT DATA AUTHORITY",
clock.UtcNow());

var notification = new DownlinkReceivedNotification(
"hoppies-ybbb",
"YBBB",
downlinkMessage);

// Assert - aircraft starts as NextDataAuthority
Assert.Equal(DataAuthorityState.NextDataAuthority, aircraft.DataAuthorityState);

// Act
await handler.Handle(notification, CancellationToken.None);

// Assert - aircraft remains as NextDataAuthority
Assert.Equal(DataAuthorityState.NextDataAuthority, aircraft.DataAuthorityState);

// Assert - AircraftConnectionUpdated event was NOT sent
var receivedCalls = clientProxy.ReceivedCalls()
.Where(call => call.GetMethodInfo().Name == "SendCoreAsync")
.ToList();

Assert.Empty(receivedCalls);
}

[Fact]
public async Task Handle_UpdatesLastSeen()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ await mediator.Send(
// Allow these to flow through to the controller
}

// Promote aircraft to CurrentDataAuthority on first downlink
if (aircraftConnection.DataAuthorityState == DataAuthorityState.NextDataAuthority)
// Promote aircraft to CurrentDataAuthority on first downlink, unless
// the aircraft explicitly indicates we are not the current data authority
if (aircraftConnection.DataAuthorityState == DataAuthorityState.NextDataAuthority &&
!ControlMessages.IsNotCurrentDataAuthority(notification.Downlink))
{
aircraftConnection.PromoteToCurrentDataAuthority();
logger.Information("{Callsign} promoted to CurrentDataAuthority", notification.Downlink.Sender);
Expand Down
5 changes: 5 additions & 0 deletions source/CPDLCServer/Model/ControlMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public static bool IsEndServiceUplink(UplinkMessage downlinkMessage)
{
return downlinkMessage.Content == "END SERVICE";
}

public static bool IsNotCurrentDataAuthority(DownlinkMessage downlinkMessage)
{
return downlinkMessage.Content == "NOT CURRENT DATA AUTHORITY";
}
}
Loading