Search in sources :

Example 1 with OkapiParams

use of org.folio.common.OkapiParams in project mod-kb-ebsco-java by folio-org.

the class AccessTypesServiceImpl method save.

@Override
public CompletableFuture<AccessType> save(String credentialsId, AccessTypePostRequest postRequest, Map<String, String> okapiHeaders) {
    AccessType requestData = postRequest.getData();
    bodyValidator.validate(credentialsId, requestData);
    if (requestData.getAttributes().getCredentialsId() == null) {
        requestData.getAttributes().setCredentialsId(credentialsId);
    }
    return validateAccessTypeLimit(credentialsId, okapiHeaders).thenApply(o -> accessTypeToDbConverter.convert(requestData)).thenCombine(usersService.findByToken(new OkapiParams(okapiHeaders)), this::setCreatorMetaInfo).thenCompose(dbAccessType -> repository.save(dbAccessType, tenantId(okapiHeaders))).thenApply(accessTypeFromDbConverter::convert);
}
Also used : RecordType(org.folio.repository.RecordType) DbAccessType(org.folio.repository.accesstypes.DbAccessType) Autowired(org.springframework.beans.factory.annotation.Autowired) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) AccessType(org.folio.rest.jaxrs.model.AccessType) Value(org.springframework.beans.factory.annotation.Value) AccessTypeCollection(org.folio.rest.jaxrs.model.AccessTypeCollection) ListUtils.mapItems(org.folio.common.ListUtils.mapItems) Map(java.util.Map) Qualifier(org.springframework.beans.factory.annotation.Qualifier) BadRequestException(javax.ws.rs.BadRequestException) AccessTypesBodyValidator(org.folio.rest.validator.AccessTypesBodyValidator) FutureUtils.mapVertxFuture(org.folio.util.FutureUtils.mapVertxFuture) Converter(org.springframework.core.convert.converter.Converter) KbCredentialsService(org.folio.service.kbcredentials.KbCredentialsService) User(org.folio.service.users.User) Configuration(org.folio.config.Configuration) DbExcUtils(org.folio.db.exc.DbExcUtils) Collection(java.util.Collection) AccessTypeDataAttributes(org.folio.rest.jaxrs.model.AccessTypeDataAttributes) TenantTool.tenantId(org.folio.rest.tools.utils.TenantTool.tenantId) RowSetUtils.toUUID(org.folio.db.RowSetUtils.toUUID) OkapiParams(org.folio.common.OkapiParams) String.format(java.lang.String.format) NotFoundException(javax.ws.rs.NotFoundException) AccessTypePostRequest(org.folio.rest.jaxrs.model.AccessTypePostRequest) List(java.util.List) Component(org.springframework.stereotype.Component) OffsetDateTime(java.time.OffsetDateTime) AccessTypePutRequest(org.folio.rest.jaxrs.model.AccessTypePutRequest) Optional(java.util.Optional) UsersService(org.folio.service.users.UsersService) RecordKey(org.folio.repository.RecordKey) AccessTypesRepository(org.folio.repository.accesstypes.AccessTypesRepository) OkapiParams(org.folio.common.OkapiParams) DbAccessType(org.folio.repository.accesstypes.DbAccessType) AccessType(org.folio.rest.jaxrs.model.AccessType)

Example 2 with OkapiParams

use of org.folio.common.OkapiParams in project mod-kb-ebsco-java by folio-org.

the class UsersLookUpServiceTest method shouldReturn200WhenUserIdIsValid.

@Test
public void shouldReturn200WhenUserIdIsValid(TestContext context) throws IOException, URISyntaxException {
    final String stubUserId = "88888888-8888-4888-8888-888888888888";
    final String stubUserIdEndpoint = GET_USER_ENDPOINT + stubUserId;
    Async async = context.async();
    OKAPI_HEADERS.put(XOkapiHeaders.TENANT, STUB_TENANT);
    OKAPI_HEADERS.put(XOkapiHeaders.URL, getWiremockUrl());
    OKAPI_HEADERS.put(XOkapiHeaders.USER_ID, stubUserId);
    stubFor(get(new UrlPathPattern(new RegexPattern(stubUserIdEndpoint), true)).willReturn(new ResponseDefinitionBuilder().withBody(TestUtil.readFile(USER_INFO_STUB_FILE))));
    CompletableFuture<User> info = usersLookUpService.lookUpUser(new OkapiParams(OKAPI_HEADERS));
    info.thenCompose(userInfo -> {
        context.assertNotNull(userInfo);
        context.assertEquals("cedrick", userInfo.getUserName());
        context.assertEquals("firstname_test", userInfo.getFirstName());
        context.assertNull(userInfo.getMiddleName());
        context.assertEquals("lastname_test", userInfo.getLastName());
        async.complete();
        return null;
    }).exceptionally(throwable -> {
        context.fail(throwable);
        async.complete();
        return null;
    });
}
Also used : ResponseDefinitionBuilder(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RegexPattern(com.github.tomakehurst.wiremock.matching.RegexPattern) TestRule(org.junit.rules.TestRule) XOkapiHeaders(org.folio.okapi.common.XOkapiHeaders) URISyntaxException(java.net.URISyntaxException) WireMockConfiguration(com.github.tomakehurst.wiremock.core.WireMockConfiguration) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) WireMockRule(com.github.tomakehurst.wiremock.junit.WireMockRule) Map(java.util.Map) TestUtil(org.folio.test.util.TestUtil) WireMock.get(com.github.tomakehurst.wiremock.client.WireMock.get) UrlPathPattern(com.github.tomakehurst.wiremock.matching.UrlPathPattern) ResponseDefinitionBuilder(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) Test(org.junit.Test) STUB_TENANT(org.folio.test.util.TestUtil.STUB_TENANT) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) OkapiParams(org.folio.common.OkapiParams) NotFoundException(javax.ws.rs.NotFoundException) TestStartLoggingRule(org.folio.test.junit.TestStartLoggingRule) Rule(org.junit.Rule) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) Slf4jNotifier(com.github.tomakehurst.wiremock.common.Slf4jNotifier) OkapiParams(org.folio.common.OkapiParams) UrlPathPattern(com.github.tomakehurst.wiremock.matching.UrlPathPattern) RegexPattern(com.github.tomakehurst.wiremock.matching.RegexPattern) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 3 with OkapiParams

use of org.folio.common.OkapiParams in project mod-kb-ebsco-java by folio-org.

the class AccessTypesServiceImpl method update.

@Override
public CompletableFuture<Void> update(String credentialsId, String accessTypeId, AccessTypePutRequest putRequest, Map<String, String> okapiHeaders) {
    AccessType requestData = putRequest.getData();
    bodyValidator.validate(credentialsId, accessTypeId, requestData);
    return fetchDbAccessType(credentialsId, accessTypeId, okapiHeaders).thenCombine(usersService.findByToken(new OkapiParams(okapiHeaders)), this::setUpdaterMetaInfo).thenApply(accessType -> updateFields(accessType, requestData.getAttributes())).thenCompose(dbAccessType -> repository.save(dbAccessType, tenantId(okapiHeaders))).thenApply(accessType -> null);
}
Also used : RecordType(org.folio.repository.RecordType) DbAccessType(org.folio.repository.accesstypes.DbAccessType) Autowired(org.springframework.beans.factory.annotation.Autowired) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) AccessType(org.folio.rest.jaxrs.model.AccessType) Value(org.springframework.beans.factory.annotation.Value) AccessTypeCollection(org.folio.rest.jaxrs.model.AccessTypeCollection) ListUtils.mapItems(org.folio.common.ListUtils.mapItems) Map(java.util.Map) Qualifier(org.springframework.beans.factory.annotation.Qualifier) BadRequestException(javax.ws.rs.BadRequestException) AccessTypesBodyValidator(org.folio.rest.validator.AccessTypesBodyValidator) FutureUtils.mapVertxFuture(org.folio.util.FutureUtils.mapVertxFuture) Converter(org.springframework.core.convert.converter.Converter) KbCredentialsService(org.folio.service.kbcredentials.KbCredentialsService) User(org.folio.service.users.User) Configuration(org.folio.config.Configuration) DbExcUtils(org.folio.db.exc.DbExcUtils) Collection(java.util.Collection) AccessTypeDataAttributes(org.folio.rest.jaxrs.model.AccessTypeDataAttributes) TenantTool.tenantId(org.folio.rest.tools.utils.TenantTool.tenantId) RowSetUtils.toUUID(org.folio.db.RowSetUtils.toUUID) OkapiParams(org.folio.common.OkapiParams) String.format(java.lang.String.format) NotFoundException(javax.ws.rs.NotFoundException) AccessTypePostRequest(org.folio.rest.jaxrs.model.AccessTypePostRequest) List(java.util.List) Component(org.springframework.stereotype.Component) OffsetDateTime(java.time.OffsetDateTime) AccessTypePutRequest(org.folio.rest.jaxrs.model.AccessTypePutRequest) Optional(java.util.Optional) UsersService(org.folio.service.users.UsersService) RecordKey(org.folio.repository.RecordKey) AccessTypesRepository(org.folio.repository.accesstypes.AccessTypesRepository) OkapiParams(org.folio.common.OkapiParams) DbAccessType(org.folio.repository.accesstypes.DbAccessType) AccessType(org.folio.rest.jaxrs.model.AccessType)

Example 4 with OkapiParams

use of org.folio.common.OkapiParams in project mod-kb-ebsco-java by folio-org.

the class UsersLookUpServiceTest method shouldReturn404WhenUserNotFound.

@Test
public void shouldReturn404WhenUserNotFound(TestContext context) {
    final String stubUserId = "xyz";
    final String stubUserIdEndpoint = GET_USER_ENDPOINT + stubUserId;
    Async async = context.async();
    OKAPI_HEADERS.put(XOkapiHeaders.TENANT, STUB_TENANT);
    OKAPI_HEADERS.put(XOkapiHeaders.URL, getWiremockUrl());
    OKAPI_HEADERS.put(XOkapiHeaders.USER_ID, stubUserId);
    stubFor(get(new UrlPathPattern(new RegexPattern(stubUserIdEndpoint), true)).willReturn(new ResponseDefinitionBuilder().withStatus(404).withStatusMessage("User Not Found")));
    CompletableFuture<User> info = usersLookUpService.lookUpUser(new OkapiParams(OKAPI_HEADERS));
    info.thenCompose(result -> {
        context.assertNull(result);
        async.complete();
        return null;
    }).exceptionally(exception -> {
        context.assertTrue(exception.getCause() instanceof NotFoundException);
        async.complete();
        return null;
    });
}
Also used : ResponseDefinitionBuilder(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RegexPattern(com.github.tomakehurst.wiremock.matching.RegexPattern) TestRule(org.junit.rules.TestRule) XOkapiHeaders(org.folio.okapi.common.XOkapiHeaders) URISyntaxException(java.net.URISyntaxException) WireMockConfiguration(com.github.tomakehurst.wiremock.core.WireMockConfiguration) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) WireMockRule(com.github.tomakehurst.wiremock.junit.WireMockRule) Map(java.util.Map) TestUtil(org.folio.test.util.TestUtil) WireMock.get(com.github.tomakehurst.wiremock.client.WireMock.get) UrlPathPattern(com.github.tomakehurst.wiremock.matching.UrlPathPattern) ResponseDefinitionBuilder(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) Test(org.junit.Test) STUB_TENANT(org.folio.test.util.TestUtil.STUB_TENANT) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) OkapiParams(org.folio.common.OkapiParams) NotFoundException(javax.ws.rs.NotFoundException) TestStartLoggingRule(org.folio.test.junit.TestStartLoggingRule) Rule(org.junit.Rule) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) Slf4jNotifier(com.github.tomakehurst.wiremock.common.Slf4jNotifier) OkapiParams(org.folio.common.OkapiParams) UrlPathPattern(com.github.tomakehurst.wiremock.matching.UrlPathPattern) RegexPattern(com.github.tomakehurst.wiremock.matching.RegexPattern) Async(io.vertx.ext.unit.Async) NotFoundException(javax.ws.rs.NotFoundException) Test(org.junit.Test)

Example 5 with OkapiParams

use of org.folio.common.OkapiParams in project mod-kb-ebsco-java by folio-org.

the class UsersLookUpServiceTest method shouldReturn401WhenUnauthorizedAccess.

@Test
public void shouldReturn401WhenUnauthorizedAccess(TestContext context) {
    final String stubUserId = "a49cefad-7447-4f2f-9004-de32e7a6cc53";
    final String stubUserIdEndpoint = GET_USER_ENDPOINT + stubUserId;
    Async async = context.async();
    OKAPI_HEADERS.put(XOkapiHeaders.URL, getWiremockUrl());
    OKAPI_HEADERS.put(XOkapiHeaders.USER_ID, stubUserId);
    stubFor(get(new UrlPathPattern(new RegexPattern(stubUserIdEndpoint), true)).willReturn(new ResponseDefinitionBuilder().withStatus(401).withStatusMessage("Authorization Failure")));
    CompletableFuture<User> info = usersLookUpService.lookUpUser(new OkapiParams(OKAPI_HEADERS));
    info.thenCompose(result -> {
        context.assertNull(result);
        async.complete();
        return null;
    }).exceptionally(exception -> {
        context.assertTrue(exception.getCause() instanceof NotAuthorizedException);
        async.complete();
        return null;
    });
}
Also used : ResponseDefinitionBuilder(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RegexPattern(com.github.tomakehurst.wiremock.matching.RegexPattern) TestRule(org.junit.rules.TestRule) XOkapiHeaders(org.folio.okapi.common.XOkapiHeaders) URISyntaxException(java.net.URISyntaxException) WireMockConfiguration(com.github.tomakehurst.wiremock.core.WireMockConfiguration) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) WireMockRule(com.github.tomakehurst.wiremock.junit.WireMockRule) Map(java.util.Map) TestUtil(org.folio.test.util.TestUtil) WireMock.get(com.github.tomakehurst.wiremock.client.WireMock.get) UrlPathPattern(com.github.tomakehurst.wiremock.matching.UrlPathPattern) ResponseDefinitionBuilder(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) Test(org.junit.Test) STUB_TENANT(org.folio.test.util.TestUtil.STUB_TENANT) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) OkapiParams(org.folio.common.OkapiParams) NotFoundException(javax.ws.rs.NotFoundException) TestStartLoggingRule(org.folio.test.junit.TestStartLoggingRule) Rule(org.junit.Rule) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) Slf4jNotifier(com.github.tomakehurst.wiremock.common.Slf4jNotifier) OkapiParams(org.folio.common.OkapiParams) UrlPathPattern(com.github.tomakehurst.wiremock.matching.UrlPathPattern) RegexPattern(com.github.tomakehurst.wiremock.matching.RegexPattern) Async(io.vertx.ext.unit.Async) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) Test(org.junit.Test)

Aggregations

Map (java.util.Map)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 NotFoundException (javax.ws.rs.NotFoundException)5 OkapiParams (org.folio.common.OkapiParams)5 ResponseDefinitionBuilder (com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder)3 WireMock.get (com.github.tomakehurst.wiremock.client.WireMock.get)3 WireMock.stubFor (com.github.tomakehurst.wiremock.client.WireMock.stubFor)3 Slf4jNotifier (com.github.tomakehurst.wiremock.common.Slf4jNotifier)3 WireMockConfiguration (com.github.tomakehurst.wiremock.core.WireMockConfiguration)3 WireMockRule (com.github.tomakehurst.wiremock.junit.WireMockRule)3 RegexPattern (com.github.tomakehurst.wiremock.matching.RegexPattern)3 UrlPathPattern (com.github.tomakehurst.wiremock.matching.UrlPathPattern)3 Vertx (io.vertx.core.Vertx)3 Async (io.vertx.ext.unit.Async)3 TestContext (io.vertx.ext.unit.TestContext)3 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)3 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 NotAuthorizedException (javax.ws.rs.NotAuthorizedException)3