Skip to content

Commit

Permalink
Merge pull request #210 from siemens/feat/codecoverage_sep10
Browse files Browse the repository at this point in the history
Feat/codecoverage sep10
  • Loading branch information
Vijayalakshmi027 authored Oct 22, 2024
2 parents 2556a52 + 84a8485 commit aad946c
Show file tree
Hide file tree
Showing 71 changed files with 3,028 additions and 360 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,6 @@ ASALocalRun/
/Build/runtime/nuget.exe
/Build/runtime/tempNuGet.config
/src/out/netcoreapp3.1
/src/.sonarlint
/src/.sonarlint
/TestFiles/MavenTestFile/ArtifactoryUploaderTestData
/TestFiles/IntegrationTestFiles/ArtifactoryUploaderTestData
175 changes: 173 additions & 2 deletions src/AritfactoryUploader.UTest/ArtifactoryUploaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
using LCT.APICommunications.Interfaces;
using LCT.APICommunications.Model;
using Moq;
using Newtonsoft.Json;
using System.Net;
using LCT.ArtifactoryUploader;
using System.Net.Http;
using System;
using NUnit.Framework;
using System.Threading.Tasks;
using LCT.APICommunications;
Expand All @@ -22,9 +20,12 @@
using LCT.Services;
using UnitTestUtilities;
using LCT.ArtifactoryUploader.Model;
using LCT.APICommunications.Model.AQL;
using System;

namespace AritfactoryUploader.UTest
{
[TestFixture]
public class ArtifactoryUploader
{
[SetUp]
Expand Down Expand Up @@ -97,5 +98,175 @@ private static IJFrogService GetJfrogService(CommonAppSettings appSettings)
IJFrogService jFrogService = new JFrogService(jFrogApiCommunicationFacade);
return jFrogService;
}

[Test]
public async Task UploadPackageToRepo_WhenPackageInfoIsNull_ReturnsNotFoundResponse()
{
// Arrange
var component = new ComponentsToArtifactory();
component.SrcRepoName = "";
component.DestRepoName = "";
component.JfrogPackageName = "";
component.Path = "";
var timeout = 10000;
var displayPackagesInfo = new DisplayPackagesInfo();
var jFrogServiceMock = new Mock<IJFrogService>();
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync((AqlResult)null);
ArtfactoryUploader.jFrogService = jFrogServiceMock.Object;
// Act
var response = await ArtfactoryUploader.UploadPackageToRepo(component, timeout, displayPackagesInfo);

// Assert
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
Assert.AreEqual(ApiConstant.PackageNotFound, response.ReasonPhrase);
}

[Test]
public async Task UploadPackageToRepo_WhenPackageTypeIsClearedThirdPartyOrDevelopment_CallsCopyFromRemoteRepo()
{
// Arrange
var component = new ComponentsToArtifactory
{
PackageType = PackageType.ClearedThirdParty,
ApiKey = "apiKey",
Email = "[email protected]"
};
component.SrcRepoName = "";
component.DestRepoName = "";
component.JfrogPackageName = "";
component.Path = "";
var timeout = 10000;
var displayPackagesInfo = new DisplayPackagesInfo();
var jFrogServiceMock = new Mock<IJFrogService>();
var jfrogApicommunicationMock = new Mock<IJFrogApiCommunication>();
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new AqlResult());
jfrogApicommunicationMock.Setup(x => x.CopyFromRemoteRepo(It.IsAny<ComponentsToArtifactory>()))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));
ArtfactoryUploader.jFrogService = jFrogServiceMock.Object;
ArtfactoryUploader.JFrogApiCommInstance = jfrogApicommunicationMock.Object;
// Act
_= await ArtfactoryUploader.UploadPackageToRepo(component, timeout, displayPackagesInfo);

// Assert
jfrogApicommunicationMock.Verify(x => x.CopyFromRemoteRepo(component), Times.Once);
}

[Test]
public async Task UploadPackageToRepo_WhenPackageTypeIsInternal_CallsMoveFromRepo()
{
// Arrange
var component = new ComponentsToArtifactory
{
PackageType = PackageType.Internal,
ApiKey = "apiKey",
Email = "[email protected]"
};
component.SrcRepoName = "";
component.DestRepoName = "";
component.JfrogPackageName = "";
component.Path = "";
var timeout = 10000;
var displayPackagesInfo = new DisplayPackagesInfo();
var jFrogServiceMock = new Mock<IJFrogService>();
var jfrogApicommunicationMock = new Mock<IJFrogApiCommunication>();
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new AqlResult());
jfrogApicommunicationMock.Setup(x => x.MoveFromRepo(It.IsAny<ComponentsToArtifactory>()))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));
ArtfactoryUploader.jFrogService = jFrogServiceMock.Object;
ArtfactoryUploader.JFrogApiCommInstance = jfrogApicommunicationMock.Object;
// Act
var response = await ArtfactoryUploader.UploadPackageToRepo(component, timeout, displayPackagesInfo);

// Assert
jfrogApicommunicationMock.Verify(x => x.MoveFromRepo(component), Times.Once);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}

[Test]
public async Task UploadPackageToRepo_WhenPackageTypeIsNotSupported_ReturnsNotFoundResponse()
{
// Arrange
var component = new ComponentsToArtifactory
{
PackageType = PackageType.Unknown,
ApiKey = "apiKey",
Email = "[email protected]"
};
component.SrcRepoName = "";
component.DestRepoName = "";
component.JfrogPackageName = "";
component.Path = "";
var timeout = 10000;
var displayPackagesInfo = new DisplayPackagesInfo();
var jFrogServiceMock = new Mock<IJFrogService>();
ArtfactoryUploader.jFrogService = jFrogServiceMock.Object;
// Act
var response = await ArtfactoryUploader.UploadPackageToRepo(component, timeout, displayPackagesInfo);

// Assert
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
}
[Test]
public async Task UploadPackageToRepo_WhenHttpRequestExceptionOccurs_ReturnsErrorResponse()
{
// Arrange
var component = new ComponentsToArtifactory
{
PackageType = PackageType.Unknown,
ApiKey = "apiKey",
Email = "[email protected]"
};
component.SrcRepoName = "";
component.DestRepoName = "";
component.JfrogPackageName = "";
component.Path = "";
var timeout = 10000;
var displayPackagesInfo = new DisplayPackagesInfo();
var jFrogServiceMock = new Mock<IJFrogService>();
var jfrogApicommunicationMock = new Mock<IJFrogApiCommunication>();
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.ThrowsAsync(new HttpRequestException());
ArtfactoryUploader.jFrogService = jFrogServiceMock.Object;
ArtfactoryUploader.JFrogApiCommInstance = jfrogApicommunicationMock.Object;

// Act
var response = await ArtfactoryUploader.UploadPackageToRepo(component, timeout, displayPackagesInfo);

// Assert
Assert.AreEqual(ApiConstant.ErrorInUpload, response.ReasonPhrase);
}

[Test]
public async Task UploadPackageToRepo_WhenInvalidOperationExceptionOccurs_ReturnsErrorResponse()
{
// Arrange
var component = new ComponentsToArtifactory
{
PackageType = PackageType.Unknown,
ApiKey = "apiKey",
Email = "[email protected]"
};
component.SrcRepoName = "";
component.DestRepoName = "";
component.JfrogPackageName = "";
component.Path = "";
var timeout = 10000;
var displayPackagesInfo = new DisplayPackagesInfo();
var jFrogServiceMock = new Mock<IJFrogService>();
var jfrogApicommunicationMock = new Mock<IJFrogApiCommunication>();
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.ThrowsAsync(new InvalidOperationException());
ArtfactoryUploader.jFrogService = jFrogServiceMock.Object;
ArtfactoryUploader.JFrogApiCommInstance = jfrogApicommunicationMock.Object;

// Act
var response = await ArtfactoryUploader.UploadPackageToRepo(component, timeout, displayPackagesInfo);

// Assert
Assert.AreEqual(ApiConstant.ErrorInUpload, response.ReasonPhrase);
}
}
}
24 changes: 24 additions & 0 deletions src/AritfactoryUploader.UTest/ArtifactoryValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,29 @@ public void ValidateArtifactoryCredentials_InputAppsettings_ThrowsInvalidDataExc
// assert will not pass always
// Assert.ThrowsAsync<InvalidDataException>(async () => await artifactoryValidator.ValidateArtifactoryCredentials(appSettings))
}

[Test]
public async Task ValidateArtifactoryCredentials_InputAppsettings_ThrowsHttpRequestException()
{
// Arrange
CommonAppSettings appSettings = new CommonAppSettings()
{
ArtifactoryUploadApiKey = "tyyteye",
ArtifactoryUploadUser = "[email protected]"
};
ArtifactoryCredentials artifactoryCredentials = new ArtifactoryCredentials()
{
ApiKey = "tyyteye",
Email = "[email protected]"
};
Mock<NpmJfrogApiCommunication> jfrogCommunicationMck = new Mock<NpmJfrogApiCommunication>(UTParams.JFrogURL, "test", artifactoryCredentials, 100);
ArtifactoryValidator artifactoryValidator = new ArtifactoryValidator(jfrogCommunicationMck.Object);
jfrogCommunicationMck.Setup(x => x.GetApiKey()).ThrowsAsync(new HttpRequestException());

// Act and Assert
var isvalid= await artifactoryValidator.ValidateArtifactoryCredentials(appSettings);
Assert.AreEqual(-1, isvalid);

}
}
}
Loading

0 comments on commit aad946c

Please sign in to comment.