Skip to content

Commit

Permalink
Added junit tests for new endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
giampieroferrara committed Dec 10, 2024
1 parent 3f47c28 commit 5f540d7
Showing 1 changed file with 71 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package it.pagopa.selfcare.onboarding.service;

import static it.pagopa.selfcare.onboarding.common.TokenType.ATTACHMENT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import io.quarkus.mongodb.panache.common.reactive.ReactivePanacheUpdate;
import io.quarkus.mongodb.panache.reactive.ReactivePanacheQuery;
import io.quarkus.panache.mock.PanacheMock;
import io.quarkus.test.InjectMock;
Expand All @@ -16,9 +19,13 @@
import io.smallrye.mutiny.helpers.test.UniAssertSubscriber;
import it.pagopa.selfcare.azurestorage.AzureBlobClient;
import it.pagopa.selfcare.onboarding.entity.Token;
import it.pagopa.selfcare.onboarding.util.QueryUtils;
import jakarta.inject.Inject;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.jboss.resteasy.reactive.RestResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -41,11 +48,11 @@ void getToken() {

PanacheMock.mock(Token.class);
when(Token.find("onboardingId", onboardingId))
.thenReturn(queryPage);
.thenReturn(queryPage);

tokenService.getToken(onboardingId)
.subscribe().withSubscriber(UniAssertSubscriber.create())
.assertCompleted();
.subscribe().withSubscriber(UniAssertSubscriber.create())
.assertCompleted();
}

@Test
Expand All @@ -57,16 +64,16 @@ void retrieveContractNotSigned() {

PanacheMock.mock(Token.class);
when(Token.find("onboardingId", onboardingId))
.thenReturn(queryPage);
.thenReturn(queryPage);

when(azureBlobClient.getFileAsPdf(anyString())).thenReturn(new File("fileName"));

UniAssertSubscriber<RestResponse<File>> subscriber = tokenService.retrieveContractNotSigned(onboardingId)
.subscribe().withSubscriber(UniAssertSubscriber.create());
.subscribe().withSubscriber(UniAssertSubscriber.create());

RestResponse<File> actual = subscriber.awaitItem().getItem();
Assertions.assertNotNull(actual);
Assertions.assertEquals(RestResponse.Status.OK.getStatusCode(), actual.getStatus());
assertNotNull(actual);
assertEquals(RestResponse.Status.OK.getStatusCode(), actual.getStatus());
}

@Test
Expand All @@ -80,16 +87,16 @@ void retrieveAttachment() {

PanacheMock.mock(Token.class);
when(Token.find("onboardingId = ?1 and type = ?2 and name = ?3", onboardingId, ATTACHMENT.name(), filename))
.thenReturn(queryPage);
.thenReturn(queryPage);

when(azureBlobClient.getFileAsPdf(anyString())).thenReturn(new File("fileName"));

UniAssertSubscriber<RestResponse<File>> subscriber = tokenService.retrieveAttachment(onboardingId, filename)
.subscribe().withSubscriber(UniAssertSubscriber.create());
.subscribe().withSubscriber(UniAssertSubscriber.create());

RestResponse<File> actual = subscriber.awaitItem().getItem();
Assertions.assertNotNull(actual);
Assertions.assertEquals(RestResponse.Status.OK.getStatusCode(), actual.getStatus());
assertNotNull(actual);
assertEquals(RestResponse.Status.OK.getStatusCode(), actual.getStatus());
}

@Test
Expand All @@ -103,14 +110,63 @@ void getTokenAttachments() {

PanacheMock.mock(Token.class);
when(Token.find("onboardingId = ?1 and type = ?2", onboardingId, ATTACHMENT.name()))
.thenReturn(queryPage);
.thenReturn(queryPage);

UniAssertSubscriber<List<String>> subscriber = tokenService.getAttachments(onboardingId)
.subscribe().withSubscriber(UniAssertSubscriber.create());
.subscribe().withSubscriber(UniAssertSubscriber.create());

List<String> actual = subscriber.awaitItem().getItem();
Assertions.assertNotNull(actual);
assertNotNull(actual);
Assertions.assertFalse(actual.isEmpty());
Assertions.assertEquals(filename, actual.get(0));
assertEquals(filename, actual.get(0));
}

@Test
void updateContractSignedTest_OK() {
// given
String onboardingId = "testContractSigned";
String documentSignedPath = "/test/";
Map<String, Object> testMap = new HashMap<>();
testMap.put("contractSigned", documentSignedPath);

ReactivePanacheUpdate queryPage = mock(ReactivePanacheUpdate.class);
PanacheMock.mock(Token.class);
when(queryPage.where(anyString(), anyString())).thenReturn(Uni.createFrom().item(1L));
when(Token.update(QueryUtils.buildUpdateDocument(testMap))).thenReturn(queryPage);

// when
UniAssertSubscriber<Long> result = tokenService.updateContractSigned(onboardingId, documentSignedPath).subscribe()
.withSubscriber(UniAssertSubscriber.create());

// then
assertNotNull(result);
assertEquals(1L, result.assertCompleted().awaitItem().getItem());

}

@Test
void updateContractSignedTest_KO() {
// given
String onboardingId = "testContractSigned";
String documentSignedPath = "/test/";

Map<String, Object> testMap = new HashMap<>();
testMap.put("contractSigned", documentSignedPath);

ReactivePanacheUpdate queryPage = mock(ReactivePanacheUpdate.class);
PanacheMock.mock(Token.class);
when(queryPage.where(anyString(), anyString())).thenReturn(Uni.createFrom().item(Long.valueOf("0")));
when(Token.update(QueryUtils.buildUpdateDocument(testMap))).thenReturn(queryPage);

// when
UniAssertSubscriber<Long> result = tokenService.updateContractSigned(onboardingId, documentSignedPath).subscribe()
.withSubscriber(UniAssertSubscriber.create());

// then
assertNotNull(result);
assertEquals("Token with id testContractSigned not found or already deleted", result.getFailure().getMessage());

}


}

0 comments on commit 5f540d7

Please sign in to comment.