diff --git a/Jenkinsfile b/Jenkinsfile index 7d491ce4542e23b29aa346d3116cce75aa25c313..6cc0d7b7f97fdea0784e1c2f8b464698afeaa161 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -64,7 +64,7 @@ pipeline { steps { container('mvn') { withCredentials([file(credentialsId: 'mavenSettings.xml', variable: 'FILE')]) { - sh 'mvn --settings $FILE clean install -Dmaven.test.skip=true' + sh 'mvn --settings $FILE clean install' } dockerBuildAndPushToRegistry "${NAMESPACE}/${APP_NAME}", [IMAGE_BUILD_TAG] } diff --git a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java index e2cbdd8702c40e820178122cec888970d49f847c..9396f1081274136c3baa913a126a6a43721b2ec7 100644 --- a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java @@ -37,6 +37,7 @@ import biz.nynja.account.grpc.GetByEmailRequest; import biz.nynja.account.grpc.GetByPhoneNumberRequest; import biz.nynja.account.grpc.GetByQrCodeRequest; import biz.nynja.account.grpc.GetByUsernameRequest; +import biz.nynja.account.grpc.ProfileByProfileIdRequest; import biz.nynja.account.grpc.ProfileResponse; import biz.nynja.account.grpc.SearchResponse; import biz.nynja.account.grpc.SearchResultDetails; @@ -59,6 +60,7 @@ import biz.nynja.account.repositories.ProfileByAuthenticationProviderRepository; import biz.nynja.account.repositories.ProfileRepository; import biz.nynja.account.services.decomposition.AccountCreator; import biz.nynja.account.services.decomposition.AccountProvider; +import biz.nynja.account.services.decomposition.ProfileProvider; import biz.nynja.account.validation.Validation; import biz.nynja.account.validation.ValidationError; import io.grpc.stub.StreamObserver; @@ -84,6 +86,7 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas private final AccountProvider accountProvider; private final PhoneNumberNormalizer phoneNumberNormalizer; private final AccountCreator accountCreator; + private final ProfileProvider profileProvider; public AccountServiceImpl(AccountRepositoryAdditional accountRepositoryAdditional, ProfileRepository profileRepository, @@ -91,7 +94,8 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas AccountByQrCodeRepository accountByQrCodeRepository, AccountByUsernameRepository accountByUsernameRepository, Validator validator, PhoneNumberValidator phoneNumberValidator, AccountProvider accountProvider, - PhoneNumberNormalizer phoneNumberNormalizer, AccountCreator accountCreator) { + PhoneNumberNormalizer phoneNumberNormalizer, AccountCreator accountCreator, + ProfileProvider profileProvider) { this.accountRepositoryAdditional = accountRepositoryAdditional; this.profileRepository = profileRepository; this.profileByAutheticationProviderRepository = profileByAutheticationProviderRepository; @@ -102,6 +106,7 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas this.accountProvider = accountProvider; this.phoneNumberNormalizer = phoneNumberNormalizer; this.accountCreator = accountCreator; + this.profileProvider = profileProvider; } @Override @@ -693,6 +698,34 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas return; } + @Override + public void getProfileByProfileId(ProfileByProfileIdRequest request, StreamObserver responseObserver) { + logger.info("Getting profile by profile id: {}", request.getProfileId()); + + if ((request.getProfileId() == null) || (request.getProfileId().isEmpty())) { + logAndBuildGrpcProfileResponse(responseObserver, ProfileResponse.newBuilder(), "Missing profile id", "", + Cause.MISSING_PROFILE_ID); + return; + } + + if (!validator.isValidUuid(request.getProfileId())) { + logAndBuildGrpcProfileResponse(responseObserver, ProfileResponse.newBuilder(), "Invalid profile id: {}", + request.getProfileId(), Cause.INVALID_PROFILE_ID); + return; + } + + Optional profile = profileProvider.getProfileByProfileId(request); + + if (!profile.isPresent()) { + logAndBuildGrpcProfileResponse(responseObserver, ProfileResponse.newBuilder(), "Profile id not found: {}", + request.getProfileId(), Cause.PROFILE_NOT_FOUND); + } else { + responseObserver.onNext(profile.get()); + responseObserver.onCompleted(); + return; + } + } + @Override public void editContactInfoForAccount(EditContactInfoRequest request, StreamObserver responseObserver) { diff --git a/src/main/java/biz/nynja/account/services/decomposition/ProfileProvider.java b/src/main/java/biz/nynja/account/services/decomposition/ProfileProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..9c138bfb73fc3f8bb0f6de6fe0d9e367d40d70c9 --- /dev/null +++ b/src/main/java/biz/nynja/account/services/decomposition/ProfileProvider.java @@ -0,0 +1,36 @@ +package biz.nynja.account.services.decomposition; + +import java.util.Optional; +import java.util.UUID; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import biz.nynja.account.grpc.ProfileByProfileIdRequest; +import biz.nynja.account.grpc.ProfileResponse; +import biz.nynja.account.models.Profile; +import biz.nynja.account.repositories.ProfileRepository; + +@Service +public class ProfileProvider { + + private static final Logger logger = LoggerFactory.getLogger(AccountCreator.class); + + private final ProfileRepository profileRepository; + + public ProfileProvider(ProfileRepository profileRepository) { + this.profileRepository = profileRepository; + } + + public Optional getProfileByProfileId(ProfileByProfileIdRequest request) { + logger.info("Getting profile by profile id: {}", request.getProfileId()); + Profile profile = profileRepository.findByProfileId(UUID.fromString(request.getProfileId())); + if (profile == null || profile.getProfileId() == null) { + return Optional.empty(); + } + ProfileResponse response = ProfileResponse.newBuilder().setProfileDetails(profile.toProto()).build(); + logger.debug("Returned response: \"{}\".", response); + return Optional.of(response); + } +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 333924bc292b78b3965b8bea3cb71e41dd9cc5cc..7ef04e2c4cd0e1f9845d262432b18b738160d360 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -5,6 +5,7 @@ grpc: unitTest: port: accountServiceTest: 11001 + accountServiceHelperTest: 11002 spring: data: diff --git a/src/main/resources/application-production.yml b/src/main/resources/application-production.yml index b13753a35877424e9df59acd29e604266efe8885..053a9e833483d8790dbe6bbc92147a1918f90cfe 100644 --- a/src/main/resources/application-production.yml +++ b/src/main/resources/application-production.yml @@ -5,6 +5,7 @@ grpc: unitTest: port: accountServiceTest: 11001 + accountServiceHelperTest: 11002 spring: data: diff --git a/src/test/java/biz/nynja/account/components/AccountServiceHelperTests.java b/src/test/java/biz/nynja/account/components/AccountServiceHelperTests.java index 96150641bf99eb6d5e8dd93a923faf80902c670d..613daf4ed50dae86fd7a3a704024ad64948a8b69 100644 --- a/src/test/java/biz/nynja/account/components/AccountServiceHelperTests.java +++ b/src/test/java/biz/nynja/account/components/AccountServiceHelperTests.java @@ -27,7 +27,7 @@ import biz.nynja.account.utils.Util; @SpringBootTest(classes = { Util.class, CassandraTestsConfig.class }, webEnvironment = WebEnvironment.RANDOM_PORT, properties = { - "grpc.port=${grpc.unitTest.port.accountServiceTest}", + "grpc.port=${grpc.unitTest.port.accountServiceHelperTest}", "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration", "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration" }) @ActiveProfiles("dev") diff --git a/src/test/java/biz/nynja/account/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/services/AccountServiceTests.java index 75712ae7700a145043503bc2e6bed831a030b837..47b99fafede44b6d4f2f062f8f108cc5b8b976fe 100644 --- a/src/test/java/biz/nynja/account/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/services/AccountServiceTests.java @@ -60,6 +60,7 @@ import biz.nynja.account.grpc.GetByEmailRequest; import biz.nynja.account.grpc.GetByPhoneNumberRequest; import biz.nynja.account.grpc.GetByQrCodeRequest; import biz.nynja.account.grpc.GetByUsernameRequest; +import biz.nynja.account.grpc.ProfileByProfileIdRequest; import biz.nynja.account.grpc.ProfileResponse; import biz.nynja.account.grpc.Role; import biz.nynja.account.grpc.SearchResponse; @@ -86,6 +87,7 @@ import biz.nynja.account.repositories.PendingAccountRepository; import biz.nynja.account.repositories.ProfileByAuthenticationProviderRepository; import biz.nynja.account.repositories.ProfileRepository; import biz.nynja.account.services.decomposition.AccountProvider; +import biz.nynja.account.services.decomposition.ProfileProvider; import biz.nynja.account.utils.GrpcServerTestBase; import biz.nynja.account.utils.Util; @@ -210,6 +212,9 @@ public class AccountServiceTests extends GrpcServerTestBase { @MockBean private AccountProvider accountProvider; + @MockBean + private ProfileProvider profileProvider; + @Test public void testGetAccountByAccountId() throws ExecutionException, InterruptedException { final AccountByAccountIdRequest request = AccountByAccountIdRequest.newBuilder() @@ -1703,4 +1708,58 @@ public class AccountServiceTests extends GrpcServerTestBase { reply.getError().getCause().equals(Cause.MISSING_QR_CODE)); } + @Test + public void testGetProfileByProfileId() throws ExecutionException, InterruptedException { + final ProfileByProfileIdRequest request = ProfileByProfileIdRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()).build(); + + Profile profile = profile1AuthProvider; + ProfileResponse response = ProfileResponse.newBuilder().setProfileDetails(profile.toProto()).build(); + Optional profileResponse = Optional.of(response); + given(profileProvider.getProfileByProfileId(request)).willReturn(profileResponse); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final ProfileResponse reply = accountServiceBlockingStub.getProfileByProfileId(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain profile ID '%s'", Util.PROFILE_ID.toString()), + reply.getProfileDetails().getProfileId().equals(Util.PROFILE_ID.toString())); + } + + @Test + public void testGetProfileByProfileIdNotFound() throws ExecutionException, InterruptedException { + final ProfileByProfileIdRequest request = ProfileByProfileIdRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()).build(); + + Profile profile = new Profile(); + + given(profileRepository.findByProfileId(UUID.fromString(request.getProfileId()))).willReturn(profile); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final ProfileResponse reply = accountServiceBlockingStub.getProfileByProfileId(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.PROFILE_NOT_FOUND), + reply.getError().getCause().equals(Cause.PROFILE_NOT_FOUND)); + } + + @Test + public void testGetProfileByProfileIdBadRequest() throws ExecutionException, InterruptedException { + + final ProfileByProfileIdRequest request = ProfileByProfileIdRequest.newBuilder().build(); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final ProfileResponse reply = accountServiceBlockingStub.getProfileByProfileId(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.MISSING_PROFILE_ID), + reply.getError().getCause().equals(Cause.MISSING_PROFILE_ID)); + } + }