public void AddsMissingVersionInfo() { // Arrange var avcVersion = new AvcVersion { version = new Version("1.0.0"), ksp_version = new KSPVersion("1.0.4") }; var mHttp = new Mock<IHttpService>(); var mModuleService = new Mock<IModuleService>(); mModuleService.Setup(i => i.GetInternalAvc(It.IsAny<CkanModule>(), It.IsAny<string>(), It.IsAny<string>())) .Returns(avcVersion); var sut = new AvcTransformer(mHttp.Object, mModuleService.Object); var json = new JObject(); json["spec_version"] = 1; json["identifier"] = "AwesomeMod"; json["$vref"] = "#/ckan/ksp-avc"; json["download"] = "https://awesomemod.example/AwesomeMod.zip"; // Act var result = sut.Transform(new Metadata(json)); var transformedJson = result.Json(); // Assert Assert.That((string)transformedJson["version"], Is.EqualTo("1.0.0"), "AvcTransformer should add the version specified in the AVC version file." ); Assert.That((string)transformedJson["ksp_version"], Is.EqualTo("1.0.4"), "AvcTransformer should add the KSP version specified in the AVC version file." ); }
public IEnumerable <Metadata> Transform(Metadata metadata, TransformOptions opts) { if (metadata.Kref?.Source == "ksp-avc") { var json = metadata.Json(); Log.InfoFormat("Executing KSP-AVC $kref transformation with {0}", metadata.Kref); Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json); var url = new Uri(metadata.Kref.Id); AvcVersion remoteAvc = JsonConvert.DeserializeObject <AvcVersion>( githubSrc?.DownloadText(url) ?? httpSvc.DownloadText(CKAN.Net.GetRawUri(url)) ); json.SafeAdd("name", remoteAvc.Name); json.Remove("$kref"); json.SafeAdd("download", remoteAvc.Download); // Set .resources.repository based on GITHUB properties if (remoteAvc.Github?.Username != null && remoteAvc.Github?.Repository != null) { // Make sure resources exist. if (json["resources"] == null) { json["resources"] = new JObject(); } var resourcesJson = (JObject)json["resources"]; resourcesJson.SafeAdd("repository", $"https://github.com/{remoteAvc.Github.Username}/{remoteAvc.Github.Repository}"); } // Use standard KSP-AVC logic to set version and the ksp_version_* properties AvcTransformer.ApplyVersions(json, remoteAvc); Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json); yield return(new Metadata(json)); } else { yield return(metadata); } }
public void OverridesExistingVersionInfo(string propertyName) { // Arrange var avcVersion = new AvcVersion(); switch(propertyName) { case "version": avcVersion.version = new Version("1.2.3"); break; case "ksp_version": avcVersion.ksp_version = new KSPVersion("1.2.3"); break; case "ksp_version_min": avcVersion.ksp_version_min = new KSPVersion("1.2.3"); break; case "ksp_version_max": avcVersion.ksp_version_max = new KSPVersion("1.2.3"); break; } var mHttp = new Mock<IHttpService>(); var mModuleService = new Mock<IModuleService>(); mModuleService.Setup(i => i.GetInternalAvc(It.IsAny<CkanModule>(), It.IsAny<string>(), It.IsAny<string>())) .Returns(avcVersion); var sut = new AvcTransformer(mHttp.Object, mModuleService.Object); var json = new JObject(); json["spec_version"] = 1; json["identifier"] = "AwesomeMod"; json["$vref"] = "#/ckan/ksp-avc"; json["download"] = "https://awesomemod.example/AwesomeMod.zip"; json[propertyName] = "9001"; // Act var result = sut.Transform(new Metadata(json)); var transformedJson = result.Json(); // Assert Assert.That((string)transformedJson[propertyName], Is.EqualTo("1.2.3"), string.Format("AvcTransformer should override an existing {0}.", propertyName) ); }
public void PreferentiallyAddsRangedKspVersionInfo() { // Arrange var avcVersion = new AvcVersion { ksp_version = KspVersion.Parse("1.0.4"), ksp_version_min = KspVersion.Parse("0.90"), ksp_version_max = KspVersion.Parse("1.0.3") }; var mHttp = new Mock<IHttpService>(); var mModuleService = new Mock<IModuleService>(); mModuleService.Setup(i => i.GetInternalAvc(It.IsAny<CkanModule>(), It.IsAny<string>(), It.IsAny<string>())) .Returns(avcVersion); var sut = new AvcTransformer(mHttp.Object, mModuleService.Object); var json = new JObject(); json["spec_version"] = 1; json["identifier"] = "AwesomeMod"; json["$vref"] = "#/ckan/ksp-avc"; json["download"] = "https://awesomemod.example/AwesomeMod.zip"; // Act var result = sut.Transform(new Metadata(json)); var transformedJson = result.Json(); // Assert Assert.That((string)transformedJson["ksp_version_min"], Is.EqualTo("0.90"), "AvcTransformer should add the KSP min version specified in the AVC version file." ); Assert.That((string)transformedJson["ksp_version_max"], Is.EqualTo("1.0.3"), "AvcTransformer should add the KSP min version specified in the AVC version file." ); Assert.That(transformedJson["ksp_version"], Is.Null, "AvcTransformer should not add a KSP version if min or max versions are specified." ); }
public void DoesNotOverrideExistingVersionInfo() { // Arrange var avcVersion = new AvcVersion { version = new Version("1.2.3") }; var mHttp = new Mock<IHttpService>(); var mModuleService = new Mock<IModuleService>(); mModuleService.Setup(i => i.GetInternalAvc(It.IsAny<CkanModule>(), It.IsAny<string>(), It.IsAny<string>())) .Returns(avcVersion); var sut = new AvcTransformer(mHttp.Object, mModuleService.Object); var json = new JObject(); json["spec_version"] = 1; json["identifier"] = "AwesomeMod"; json["$vref"] = "#/ckan/ksp-avc"; json["download"] = "https://awesomemod.example/AwesomeMod.zip"; json["version"] = "9001"; // Act var result = sut.Transform(new Metadata(json)); var transformedJson = result.Json(); // Assert Assert.That((string)transformedJson["version"], Is.EqualTo("9001"), "AvcTransformer should not override an existing version." ); }
public void CorrectlyCalculatesKspVersionInfo( string existingKsp, string existingKspMin, string existingKspMax, string avcKsp, string avcKspMin, string avcKspMax, string expectedKsp, string expectedKspMin, string expectedKspMax ) { // Arrange var json = new JObject(); json["spec_version"] = 1; json["identifier"] = "AwesomeMod"; json["$vref"] = "#/ckan/ksp-avc"; json["download"] = "https://awesomemod.example/AwesomeMod.zip"; if (!string.IsNullOrWhiteSpace(existingKsp)) json["ksp_version"] = existingKsp; if (!string.IsNullOrWhiteSpace(existingKspMin)) json["ksp_version_min"] = existingKspMin; if (!string.IsNullOrWhiteSpace(existingKspMax)) json["ksp_version_max"] = existingKspMax; var avcVersion = new AvcVersion(); if (!string.IsNullOrWhiteSpace(avcKsp)) avcVersion.ksp_version = KspVersion.Parse(avcKsp); if (!string.IsNullOrWhiteSpace(avcKspMin)) avcVersion.ksp_version_min = KspVersion.Parse(avcKspMin); if (!string.IsNullOrWhiteSpace(avcKspMax)) avcVersion.ksp_version_max = KspVersion.Parse(avcKspMax); var mHttp = new Mock<IHttpService>(); var mModuleService = new Mock<IModuleService>(); mModuleService.Setup(i => i.GetInternalAvc(It.IsAny<CkanModule>(), It.IsAny<string>(), It.IsAny<string>())) .Returns(avcVersion); var sut = new AvcTransformer(mHttp.Object, mModuleService.Object); // Act var result = sut.Transform(new Metadata(json)); var transformedJson = result.Json(); // Assert Assert.That((string)transformedJson["ksp_version"], Is.EqualTo(expectedKsp), "AvcTransformer should calculate ksp_version correctly" ); Assert.That((string)transformedJson["ksp_version_min"], Is.EqualTo(expectedKspMin), "AvcTransformer should calculate ksp_version_min correctly" ); Assert.That((string)transformedJson["ksp_version_max"], Is.EqualTo(expectedKspMax), "AvcTransformer should calculate ksp_version_max correctly" ); }