Skip to content

Commit

Permalink
added ut
Browse files Browse the repository at this point in the history
  • Loading branch information
malavikakrishnan123 committed Aug 6, 2024
1 parent 5e1da41 commit 4dacab6
Show file tree
Hide file tree
Showing 4 changed files with 528 additions and 29 deletions.
176 changes: 175 additions & 1 deletion src/LCT.APICommunications.UTest/SW360ApicommunicationUTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
// SPDX-License-Identifier: MIT
// --------------------------------------------------------------------------------------------------------------------

using LCT.APICommunications.Model;
using LCT.Common.Model;
using Moq.Protected;
using Moq;
using Newtonsoft.Json;
using System.Net;
using System.Text;

namespace LCT.APICommunications.UTest
Expand Down Expand Up @@ -148,12 +152,182 @@ public void SW360Apicommunication_UpdateComponent_ReturnsInvalidOperationExcepti
HttpContent httpContent;
var jsonString = JsonConvert.SerializeObject("");
httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");

//Arrange
SW360Apicommunication sW360Apicommunication = new SW360Apicommunication(connectionSettings);

//Assert
Assert.ThrowsAsync<InvalidOperationException>(async () => await sW360Apicommunication.UpdateComponent("", httpContent));
}
[Test]
public async Task GetComponentByName_ReturnsExpectedString_WhenResponseIsOk()
{
// Arrange
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("Test content"),
});

var httpClient = new HttpClient(mockHttpMessageHandler.Object);
var sW360Apicommunication = new SW360Apicommunication(connectionSettings);



// Act
Assert.ThrowsAsync<InvalidOperationException>(async () => await sW360Apicommunication.GetComponentByName("TestComponentName"));

}
[Test]
public async Task UpdateRelease_ReturnsHttpResponseMessage_WhenResponseIsOk()
{
// Arrange
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
});

var httpClient = new HttpClient(mockHttpMessageHandler.Object);
var sW360Apicommunication = new SW360Apicommunication(connectionSettings);


var httpContent = new StringContent("Test content", Encoding.UTF8, "application/json");

// Act
Assert.ThrowsAsync<InvalidOperationException>(async () => await sW360Apicommunication.UpdateRelease("TestReleaseId", httpContent));


}
[Test]
public async Task CreateComponent_ReturnsHttpResponseMessage_WhenCalledWithCreateComponentContent()
{
// Arrange
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
});

var httpClient = new HttpClient(mockHttpMessageHandler.Object);
var sW360Apicommunication = new SW360Apicommunication(connectionSettings);

var createComponentContent = new CreateComponent();

// Act
Assert.ThrowsAsync<InvalidOperationException>(async () => await sW360Apicommunication.CreateComponent(createComponentContent));


}
[Test]
public async Task GetComponentByExternalId_ReturnsHttpResponseMessage_WhenResponseIsOk()
{
// Arrange
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("Test content"),
});

var httpClient = new HttpClient(mockHttpMessageHandler.Object);
var sW360Apicommunication = new SW360Apicommunication(connectionSettings);
Assert.ThrowsAsync<InvalidOperationException>(async () => await sW360Apicommunication.GetComponentByExternalId("TestPurlId"));


}
[Test]
public async Task GetProjects_ReturnsContent_WhenResponseIsOk()
{
// Arrange
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("Test content"),
});

var httpClient = new HttpClient(mockHttpMessageHandler.Object);
var sW360Apicommunication = new SW360Apicommunication(connectionSettings);
// Act
Assert.ThrowsAsync<InvalidOperationException>(async () => await sW360Apicommunication.GetProjects());


}
[Test]
public async Task GetComponentByName_ReturnsContent_WhenCalledWithComponentName()
{
// Arrange
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("Test content"),
});

var httpClient = new HttpClient(mockHttpMessageHandler.Object);
var sW360Apicommunication = new SW360Apicommunication(connectionSettings);


// Act
Assert.ThrowsAsync<InvalidOperationException>(async () => await sW360Apicommunication.GetComponentByName("TestComponent"));

}
[Test]
public async Task GetReleases_ReturnsContent_WhenResponseIsOk()
{
// Arrange
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("Test content"),
});

var httpClient = new HttpClient(mockHttpMessageHandler.Object);
var sW360Apicommunication = new SW360Apicommunication(connectionSettings);

// Act
Assert.ThrowsAsync<InvalidOperationException>(async () => await sW360Apicommunication.GetReleases());


}

}
}
100 changes: 100 additions & 0 deletions src/LCT.Services.UTest/JFrogServiceUTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,105 @@ public async Task GetPackageInfo_ResultsWith_HttpRequestException()
// Assert
Assert.Null(actual);
}
[Test]
public async Task GetInternalComponentDataByRepo_ResultsWith_NullHttpResponse()
{
// Arrange
HttpResponseMessage httpResponseMessage = null;

var mockJfrogApiComFacade = new Mock<IJfrogAqlApiCommunicationFacade>();
mockJfrogApiComFacade
.Setup(x => x.GetInternalComponentDataByRepo(It.IsAny<string>()))
.ReturnsAsync(httpResponseMessage);

// Act
IJFrogService jFrogService = new JFrogService(mockJfrogApiComFacade.Object);
IList<AqlResult> actual = await jFrogService.GetInternalComponentDataByRepo("energy-dev-npm-egll");

// Assert
Assert.That(actual.Count, Is.EqualTo(0));
}

[Test]
public async Task GetInternalComponentDataByRepo_ResultsWith_NullHttpResponseContent()
{
// Arrange
HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = null
};

var mockJfrogApiComFacade = new Mock<IJfrogAqlApiCommunicationFacade>();
mockJfrogApiComFacade
.Setup(x => x.GetInternalComponentDataByRepo(It.IsAny<string>()))
.ReturnsAsync(httpResponseMessage);

// Act
IJFrogService jFrogService = new JFrogService(mockJfrogApiComFacade.Object);
IList<AqlResult> actual = await jFrogService.GetInternalComponentDataByRepo("energy-dev-npm-egll");

// Assert
Assert.That(actual.Count, Is.EqualTo(0));
}
[Test]
public async Task CheckJFrogConnectivity_SuccessfulConnection_ReturnsHttpResponseMessage()
{
// Arrange
HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
Mock<IJfrogAqlApiCommunicationFacade> mockJfrogApiComFacade = new Mock<IJfrogAqlApiCommunicationFacade>();
mockJfrogApiComFacade
.Setup(x => x.CheckConnection())
.ReturnsAsync(httpResponseMessage);

// Act
IJFrogService jFrogService = new JFrogService(mockJfrogApiComFacade.Object);
HttpResponseMessage actual = await jFrogService.CheckJFrogConnectivity();

// Assert
Assert.NotNull(actual);
Assert.AreEqual(HttpStatusCode.OK, actual.StatusCode);
}

[Test]
public async Task CheckJFrogConnectivity_UnsuccessfulConnection_ReturnsHttpResponseMessage()
{
// Arrange
HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest);
Mock<IJfrogAqlApiCommunicationFacade> mockJfrogApiComFacade = new Mock<IJfrogAqlApiCommunicationFacade>();
mockJfrogApiComFacade
.Setup(x => x.CheckConnection())
.ReturnsAsync(httpResponseMessage);

// Act
IJFrogService jFrogService = new JFrogService(mockJfrogApiComFacade.Object);
HttpResponseMessage actual = await jFrogService.CheckJFrogConnectivity();

// Assert
Assert.NotNull(actual);
Assert.AreEqual(HttpStatusCode.BadRequest, actual.StatusCode);
}

[Test]
public async Task GetInternalComponentDataByRepo_ShouldReturnEmptyList_WhenHttpResponseContentIsNull()
{
// Arrange
HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = null
};

Mock<IJfrogAqlApiCommunicationFacade> mockJfrogApiComFacade = new Mock<IJfrogAqlApiCommunicationFacade>();
mockJfrogApiComFacade
.Setup(x => x.GetInternalComponentDataByRepo(It.IsAny<string>()))
.ReturnsAsync(httpResponseMessage);

// Act
IJFrogService jFrogService = new JFrogService(mockJfrogApiComFacade.Object);
IList<AqlResult> actual = await jFrogService.GetInternalComponentDataByRepo("energy-dev-npm-egll");

// Assert
Assert.That(actual.Count, Is.EqualTo(0));
}

}
}
Loading

0 comments on commit 4dacab6

Please sign in to comment.