public void WhenImageAnnotatedAsWindowsSpla_ThenQueryWindowsSplaReturnsInstance()
        {
            var history = new InstanceSetHistory(
                new DateTime(2019, 12, 1, 0, 0, 0, DateTimeKind.Utc),
                new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc),
                new[]
            {
                new InstanceHistory(
                    188550847350222232,
                    new InstanceLocator("project-1", "us-central1-a", "instance-1"),
                    InstanceHistoryState.Complete,
                    new ImageLocator("project-1", "image"),
                    new []
                {
                    new InstancePlacement(
                        new DateTime(2019, 12, 1, 0, 0, 0, DateTimeKind.Utc),
                        new DateTime(2019, 12, 2, 0, 0, 0, DateTimeKind.Utc)),
                    new InstancePlacement(
                        new DateTime(2019, 12, 2, 0, 0, 0, DateTimeKind.Utc),
                        new DateTime(2019, 12, 3, 0, 0, 0, DateTimeKind.Utc))
                })
            });

            var annotatedHistory = ReportArchive.FromInstanceSetHistory(history);

            annotatedHistory.AddLicenseAnnotation(
                new ImageLocator("project-1", "image"),
                OperatingSystemTypes.Windows,
                LicenseTypes.Spla);

            Assert.IsTrue(annotatedHistory.GetInstances(
                              OperatingSystemTypes.Windows,
                              LicenseTypes.Spla).Any());

            Assert.IsTrue(annotatedHistory.GetInstances(
                              OperatingSystemTypes.Windows | OperatingSystemTypes.Linux,
                              LicenseTypes.Spla | LicenseTypes.Byol).Any());

            Assert.IsFalse(annotatedHistory.GetInstances(
                               OperatingSystemTypes.Linux,
                               LicenseTypes.Spla).Any());

            Assert.IsFalse(annotatedHistory.GetInstances(
                               OperatingSystemTypes.Windows,
                               LicenseTypes.Byol).Any());
        }
        public void WhenSerializedAndDeserialized_ThenObjectsAreEquivalent()
        {
            var history = new InstanceSetHistory(
                new DateTime(2019, 12, 1, 0, 0, 0, DateTimeKind.Utc),
                new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc),
                new[]
            {
                new InstanceHistory(
                    188550847350222232,
                    new InstanceLocator("project-1", "us-central1-a", "instance-1"),
                    InstanceHistoryState.Complete,
                    new ImageLocator("project-1", "windows"),
                    new []
                {
                    new InstancePlacement(
                        new DateTime(2019, 12, 1, 0, 0, 0, DateTimeKind.Utc),
                        new DateTime(2019, 12, 2, 0, 0, 0, DateTimeKind.Utc)),
                    new InstancePlacement(
                        new DateTime(2019, 12, 2, 0, 0, 0, DateTimeKind.Utc),
                        new DateTime(2019, 12, 3, 0, 0, 0, DateTimeKind.Utc))
                }),
                new InstanceHistory(
                    118550847350222232,
                    null,
                    InstanceHistoryState.MissingImage,
                    null,
                    new []
                {
                    new InstancePlacement(
                        "server-1",
                        new DateTime(2019, 12, 1, 0, 0, 0, DateTimeKind.Utc),
                        new DateTime(2019, 12, 2, 0, 0, 0, DateTimeKind.Utc))
                })
            });

            var annotatedHistory = ReportArchive.FromInstanceSetHistory(history);

            annotatedHistory.AddLicenseAnnotation(
                new ImageLocator("project-1", "windows"),
                OperatingSystemTypes.Windows,
                LicenseTypes.Spla);

            using (var memoryStream = new MemoryStream())
            {
                var s = new StringWriter();
                annotatedHistory.Serialize(s);

                var writer = new StreamWriter(memoryStream);
                annotatedHistory.Serialize(writer);
                writer.Flush();

                memoryStream.Position = 0;

                var restoredAnnotatedHistory = ReportArchive
                                               .Deserialize(new StreamReader(memoryStream));
                var restoredHistory = restoredAnnotatedHistory.History;

                Assert.AreEqual(history.StartDate, restoredHistory.StartDate);
                Assert.AreEqual(history.EndDate, restoredHistory.EndDate);

                Assert.AreEqual(2, restoredHistory.Instances.Count());
                var completeInstance = restoredHistory.Instances.First(i => i.InstanceId == 188550847350222232);

                Assert.AreEqual(history.Instances.First().Reference, completeInstance.Reference);

                Assert.AreEqual(history.Instances.First().Placements.Count(), completeInstance.Placements.Count());
                Assert.AreEqual(history.Instances.First().Placements.First().From, completeInstance.Placements.First().From);
                Assert.AreEqual(history.Instances.First().Placements.First().To, completeInstance.Placements.First().To);
                Assert.AreEqual(history.Instances.First().Placements.First().ServerId, completeInstance.Placements.First().ServerId);
                Assert.AreEqual(history.Instances.First().Placements.First().Tenancy, completeInstance.Placements.First().Tenancy);

                Assert.AreEqual(history.Instances.First().Placements.Last().From, completeInstance.Placements.Last().From);
                Assert.AreEqual(history.Instances.First().Placements.Last().To, completeInstance.Placements.Last().To);
                Assert.AreEqual(history.Instances.First().Placements.Last().ServerId, completeInstance.Placements.Last().ServerId);
                Assert.AreEqual(history.Instances.First().Placements.Last().Tenancy, completeInstance.Placements.Last().Tenancy);

                var annotation = restoredAnnotatedHistory.LicenseAnnotations[history.Instances.First().Image.ToString()];
                Assert.IsNotNull(annotation);
                Assert.AreEqual(OperatingSystemTypes.Windows, annotation.OperatingSystem);
                Assert.AreEqual(LicenseTypes.Spla, annotation.LicenseType);

                var incompleteInstance = restoredHistory.Instances.First(i => i.InstanceId == 118550847350222232);

                Assert.AreEqual(history.Instances.Last().InstanceId, incompleteInstance.InstanceId);
                Assert.AreEqual(history.Instances.Last().Reference, incompleteInstance.Reference);

                Assert.AreEqual(history.Instances.Last().Placements.Count(), incompleteInstance.Placements.Count());
                Assert.AreEqual(history.Instances.Last().Placements.First().From, incompleteInstance.Placements.First().From);
                Assert.AreEqual(history.Instances.Last().Placements.First().To, incompleteInstance.Placements.First().To);
                Assert.AreEqual(history.Instances.Last().Placements.First().ServerId, incompleteInstance.Placements.First().ServerId);
                Assert.AreEqual(history.Instances.Last().Placements.First().Tenancy, incompleteInstance.Placements.First().Tenancy);
            }
        }