Skip to content

Commit cc4c8b6

Browse files
Add proximity filtering to satellite retrieval and enhance tests
1 parent a241f38 commit cc4c8b6

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

Satellite.DataAccess.Test/N2YOSatelliteClientTests.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,24 @@ public class N2YOSatelliteClientTests
1010
public async Task GetSatellitesAsync_ReturnsSatelliteData()
1111
{
1212
// Arrange
13-
var httpClientMock = new Mock<IHttpClient>();
14-
var expectedData = GetSampleSatelliteDataJson();
13+
var httpMock = new Mock<IHttpClient>();
1514
var response = new HttpResponseMessage(HttpStatusCode.OK)
1615
{
17-
Content = new StringContent(expectedData),
16+
Content = new StringContent(GetSampleSatelliteDataJson()),
1817
};
1918

20-
httpClientMock.Setup(x => x.GetAsync(It.IsAny<string>())).ReturnsAsync(response);
19+
httpMock.Setup(x => x.GetAsync(It.IsAny<string>())).ReturnsAsync(response);
2120

22-
var nasaSatellite = new N2YOSatelliteClient(httpClientMock.Object, "1234");
21+
var client = new N2YOSatelliteClient(httpMock.Object, "test-api-key");
2322

2423
// Act
25-
var satellites = await nasaSatellite.GetSatellitesAsync(0, 0, 90, 0);
24+
var satellites = await client.GetSatellitesAsync(-35.0, 158.0);
2625

2726
// Assert
28-
Assert.Multiple(() =>
29-
{
30-
Assert.NotEmpty(satellites);
31-
Assert.NotNull(satellites);
32-
});
27+
Assert.NotNull(satellites);
28+
Assert.Equal(8, satellites.Above.Count());
29+
Assert.Equal("Iridium", satellites.Info.Category);
30+
Assert.Equal(8, satellites.Info.SatCount);
3331
}
3432

3533
[Fact]
@@ -42,9 +40,9 @@ public async Task GetSatellitesAsync_HandlesEmptyResponse()
4240
Content = null
4341
};
4442

45-
#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
43+
#pragma warning disable CS8620
4644
_ = httpClientMock.Setup(x => x.GetAsync(It.IsAny<string>())).ReturnsAsync(response);
47-
#pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
45+
#pragma warning restore CS8620
4846

4947
var nasaSatellite = new N2YOSatelliteClient(httpClientMock.Object, "1234");
5048

@@ -57,7 +55,6 @@ public async Task GetSatellitesAsync_HandlesEmptyResponse()
5755
Assert.Empty(satellites);
5856
Assert.NotNull(satellites);
5957
});
60-
6158
}
6259

6360
[Fact]

Satellite.DataAccess.Test/SatelliteServiceTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,49 @@ public async Task GetSatellitesAsync_FetchesDataFromClientIfNotCached()
9393
memoryCacheMock.Verify(x => x.CreateEntry(It.IsAny<object>()), Times.Once());
9494
});
9595
}
96+
97+
[Fact]
98+
public async Task GetIridiumsAsync_FiltersByProximity_ReturnsOnlyProximateSatellites()
99+
{
100+
// Arrange
101+
var httpMock = new Mock<IHttpClient>();
102+
var apiResponse = new HttpResponseMessage(HttpStatusCode.OK)
103+
{
104+
Content = new StringContent(GetSampleSatelliteDataJson()),
105+
};
106+
107+
httpMock.Setup(x => x.GetAsync(It.IsAny<string>())).ReturnsAsync(apiResponse);
108+
109+
// Use N2YOSatelliteClient as the ISatelliteClient implementation
110+
var nasaSatelliteClient = new N2YOSatelliteClient(httpMock.Object, "test-api-key");
111+
112+
var memoryCacheMock = new Mock<IMemoryCache>();
113+
object cachedData;
114+
memoryCacheMock.Setup(x => x.TryGetValue("iridium", out cachedData))
115+
.Returns(false);
116+
117+
var cacheEntryMock = new Mock<ICacheEntry>();
118+
memoryCacheMock.Setup(x => x.CreateEntry(It.IsAny<object>())).Returns(cacheEntryMock.Object);
119+
120+
var userCoords = new CurrentCoords { Latitude = -35.0, Longitude = 158.0 };
121+
var service = new SatelliteService(nasaSatelliteClient, memoryCacheMock.Object, userCoords);
122+
123+
// Act
124+
var satellites = await service.GetIridiumsAsync();
125+
126+
// Assert
127+
Assert.NotNull(satellites);
128+
var proximateSatellite = Assert.Single(satellites);
129+
Assert.Equal("IRIDIUM 36", proximateSatellite.Name);
130+
Assert.Equal(-35.6729, proximateSatellite.Latitude);
131+
Assert.Equal(158.2153, proximateSatellite.Longitude);
132+
133+
// Verify that the HTTP client was called (due to cache miss)
134+
httpMock.Verify(x => x.GetAsync(It.IsAny<string>()), Times.Once);
135+
// Verify that the cache was attempted to be updated with the key "iridium"
136+
memoryCacheMock.Verify(x => x.CreateEntry("iridium"), Times.Once);
137+
}
138+
96139
private string GetSampleSatelliteDataJson()
97140
{
98141
var sampleData = """

Satellite.DataAccess/Services/NASASatelliteService.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ public SatelliteService(ISatelliteClient nasaSatelliteClient, IMemoryCache cache
189189
}
190190

191191
var response = await _nasaSatelliteClient.GetSatellitesAsync(_coords.Longitude, _coords.Latitude, 4, (int)type);
192+
193+
const double proximityThresholdDegrees = 1.0;
194+
192195
var satellites = response.Select(i =>
193196
{
194197
return new Models.Satellite
@@ -199,7 +202,9 @@ public SatelliteService(ISatelliteClient nasaSatelliteClient, IMemoryCache cache
199202
Altitude = i.Satalt,
200203
Age = i.LaunchDate
201204
};
202-
});
205+
})
206+
.Where(s => Math.Abs(s.Latitude - _coords.Latitude) < proximityThresholdDegrees &&
207+
Math.Abs(s.Longitude - _coords.Longitude) < proximityThresholdDegrees);
203208

204209
CacheSatelliteData(cacheKey, satellites);
205210

0 commit comments

Comments
 (0)