Search in sources :

Example 11 with SimpleApplicationSettings

use of io.gravitee.rest.api.model.application.SimpleApplicationSettings in project gravitee-management-rest-api by gravitee-io.

the class ApplicationService_CreateTest method shouldCreateForUser.

@Test
public void shouldCreateForUser() throws TechnicalException {
    ApplicationSettings settings = new ApplicationSettings();
    SimpleApplicationSettings clientSettings = new SimpleApplicationSettings();
    clientSettings.setClientId(CLIENT_ID);
    settings.setApp(clientSettings);
    when(newApplication.getSettings()).thenReturn(settings);
    when(application.getName()).thenReturn(APPLICATION_NAME);
    when(application.getType()).thenReturn(ApplicationType.SIMPLE);
    when(application.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
    when(applicationRepository.create(any())).thenReturn(application);
    when(newApplication.getName()).thenReturn(APPLICATION_NAME);
    when(newApplication.getDescription()).thenReturn("My description");
    when(groupService.findByEvent(any())).thenReturn(Collections.emptySet());
    when(userService.findById(any())).thenReturn(mock(UserEntity.class));
    final ApplicationEntity applicationEntity = applicationService.create(newApplication, USER_NAME);
    assertNotNull(applicationEntity);
    assertEquals(APPLICATION_NAME, applicationEntity.getName());
}
Also used : ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) NewApplicationEntity(io.gravitee.rest.api.model.NewApplicationEntity) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 12 with SimpleApplicationSettings

use of io.gravitee.rest.api.model.application.SimpleApplicationSettings in project gravitee-management-rest-api by gravitee-io.

the class UserServiceImpl method connect.

@Override
public UserEntity connect(String userId) {
    try {
        LOGGER.debug("Connection of {}", userId);
        Optional<User> checkUser = userRepository.findById(userId);
        if (!checkUser.isPresent()) {
            throw new UserNotFoundException(userId);
        }
        User user = checkUser.get();
        User previousUser = new User(user);
        // First connection: create default application for user & notify
        if (user.getLastConnectionAt() == null && user.getFirstConnectionAt() == null) {
            notifierService.trigger(PortalHook.USER_FIRST_LOGIN, new NotificationParamsBuilder().user(convert(user, false)).build());
            user.setFirstConnectionAt(new Date());
            if (defaultApplicationForFirstConnection) {
                LOGGER.debug("Create a default application for {}", userId);
                NewApplicationEntity defaultApp = new NewApplicationEntity();
                defaultApp.setName("Default application");
                defaultApp.setDescription("My default application");
                // To preserve backward compatibility, ensure that we have at least default settings for simple application type
                ApplicationSettings settings = new ApplicationSettings();
                SimpleApplicationSettings simpleAppSettings = new SimpleApplicationSettings();
                settings.setApp(simpleAppSettings);
                defaultApp.setSettings(settings);
                try {
                    environmentService.findByUser(userId).forEach(env -> applicationService.create(defaultApp, userId, env.getId()));
                } catch (IllegalStateException ex) {
                    // do not fail to create a user even if we are not able to create its default app
                    LOGGER.warn("Not able to create default app for user {}", userId);
                }
            }
        }
        // Set date fields
        user.setLastConnectionAt(new Date());
        if (user.getFirstConnectionAt() == null) {
            user.setFirstConnectionAt(user.getLastConnectionAt());
        }
        user.setUpdatedAt(user.getLastConnectionAt());
        user.setLoginCount(user.getLoginCount() + 1);
        User updatedUser = userRepository.update(user);
        auditService.createOrganizationAuditLog(Collections.singletonMap(USER, userId), User.AuditEvent.USER_CONNECTED, user.getUpdatedAt(), previousUser, user);
        final UserEntity userEntity = convert(updatedUser, true);
        searchEngineService.index(userEntity, false);
        return userEntity;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to connect {}", userId, ex);
        throw new TechnicalManagementException("An error occurs while trying to connect " + userId, ex);
    }
}
Also used : ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) User(io.gravitee.repository.management.model.User) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NotificationParamsBuilder(io.gravitee.rest.api.service.notification.NotificationParamsBuilder)

Example 13 with SimpleApplicationSettings

use of io.gravitee.rest.api.model.application.SimpleApplicationSettings in project gravitee-management-rest-api by gravitee-io.

the class ApplicationService_UpdateTest method shouldNotUpdateBecauseTechnicalException.

@Test(expected = TechnicalManagementException.class)
public void shouldNotUpdateBecauseTechnicalException() throws TechnicalException {
    ApplicationSettings settings = new ApplicationSettings();
    SimpleApplicationSettings clientSettings = new SimpleApplicationSettings();
    clientSettings.setClientId(CLIENT_ID);
    settings.setApp(clientSettings);
    when(existingApplication.getSettings()).thenReturn(settings);
    when(application.getType()).thenReturn(ApplicationType.SIMPLE);
    when(applicationRepository.findById(APPLICATION_ID)).thenReturn(Optional.of(application));
    when(applicationRepository.update(any())).thenThrow(TechnicalException.class);
    when(existingApplication.getName()).thenReturn(APPLICATION_NAME);
    when(existingApplication.getDescription()).thenReturn("My description");
    applicationService.update(APPLICATION_ID, existingApplication);
}
Also used : ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) Test(org.junit.Test)

Example 14 with SimpleApplicationSettings

use of io.gravitee.rest.api.model.application.SimpleApplicationSettings in project gravitee-management-rest-api by gravitee-io.

the class ApplicationService_UpdateTest method shouldUpdateBecauseSameApplication.

@Test
public void shouldUpdateBecauseSameApplication() throws TechnicalException {
    when(applicationRepository.findById(APPLICATION_ID)).thenReturn(Optional.of(application));
    when(application.getId()).thenReturn(APPLICATION_ID);
    Map<String, String> metadata = new HashMap<>();
    metadata.put("client_id", CLIENT_ID);
    when(application.getMetadata()).thenReturn(metadata);
    ApplicationSettings settings = new ApplicationSettings();
    SimpleApplicationSettings clientSettings = new SimpleApplicationSettings();
    clientSettings.setClientId(CLIENT_ID);
    settings.setApp(clientSettings);
    when(existingApplication.getSettings()).thenReturn(settings);
    when(application.getName()).thenReturn(APPLICATION_NAME);
    when(application.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
    when(application.getType()).thenReturn(ApplicationType.SIMPLE);
    when(existingApplication.getName()).thenReturn(APPLICATION_NAME);
    when(existingApplication.getDescription()).thenReturn("My description");
    when(applicationRepository.update(any())).thenReturn(application);
    when(roleService.findPrimaryOwnerRoleByOrganization(any(), any())).thenReturn(mock(RoleEntity.class));
    MembershipEntity po = new MembershipEntity();
    po.setMemberId(USER_NAME);
    po.setMemberType(MembershipMemberType.USER);
    po.setReferenceId(APPLICATION_ID);
    po.setReferenceType(MembershipReferenceType.APPLICATION);
    po.setRoleId("APPLICATION_PRIMARY_OWNER");
    when(membershipService.getMembershipsByReferencesAndRole(any(), any(), any())).thenReturn(Collections.singleton(po));
    final ApplicationEntity applicationEntity = applicationService.update(APPLICATION_ID, existingApplication);
    verify(applicationRepository).update(argThat(application -> APPLICATION_NAME.equals(application.getName()) && application.getUpdatedAt() != null));
    assertNotNull(applicationEntity);
    assertEquals(APPLICATION_NAME, applicationEntity.getName());
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) ArgumentMatchers(org.mockito.ArgumentMatchers) Mock(org.mockito.Mock) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) ClientRegistrationService(io.gravitee.rest.api.service.configuration.application.ClientRegistrationService) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) Sets(org.mockito.internal.util.collections.Sets) UserService(io.gravitee.rest.api.service.UserService) UpdateApplicationEntity(io.gravitee.rest.api.model.UpdateApplicationEntity) Map(java.util.Map) ApplicationRepository(io.gravitee.repository.management.api.ApplicationRepository) MembershipMemberType(io.gravitee.rest.api.model.MembershipMemberType) ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) ParameterService(io.gravitee.rest.api.service.ParameterService) InjectMocks(org.mockito.InjectMocks) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) Assert.assertNotNull(org.junit.Assert.assertNotNull) AuditService(io.gravitee.rest.api.service.AuditService) ClientIdAlreadyExistsException(io.gravitee.rest.api.service.exceptions.ClientIdAlreadyExistsException) Test(org.junit.Test) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) RoleEntity(io.gravitee.rest.api.model.RoleEntity) Mockito(org.mockito.Mockito) MembershipReferenceType(io.gravitee.rest.api.model.MembershipReferenceType) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) io.gravitee.repository.management.model(io.gravitee.repository.management.model) Optional(java.util.Optional) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) ApplicationServiceImpl(io.gravitee.rest.api.service.impl.ApplicationServiceImpl) ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) MembershipEntity(io.gravitee.rest.api.model.MembershipEntity) HashMap(java.util.HashMap) UpdateApplicationEntity(io.gravitee.rest.api.model.UpdateApplicationEntity) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) Test(org.junit.Test)

Example 15 with SimpleApplicationSettings

use of io.gravitee.rest.api.model.application.SimpleApplicationSettings in project gravitee-management-rest-api by gravitee-io.

the class ApplicationService_UpdateTest method shouldNotUpdateBecauseDifferentApplication.

@Test(expected = ClientIdAlreadyExistsException.class)
public void shouldNotUpdateBecauseDifferentApplication() throws TechnicalException {
    Application other = mock(Application.class);
    when(other.getId()).thenReturn("other-app");
    Map<String, String> metadata = new HashMap<>();
    metadata.put("client_id", CLIENT_ID);
    when(other.getMetadata()).thenReturn(metadata);
    when(applicationRepository.findById(APPLICATION_ID)).thenReturn(Optional.of(application));
    when(application.getType()).thenReturn(ApplicationType.SIMPLE);
    when(applicationRepository.findAllByEnvironment("DEFAULT", ApplicationStatus.ACTIVE)).thenReturn(Sets.newSet(other));
    when(application.getId()).thenReturn(APPLICATION_ID);
    ApplicationSettings settings = new ApplicationSettings();
    SimpleApplicationSettings clientSettings = new SimpleApplicationSettings();
    clientSettings.setClientId(CLIENT_ID);
    settings.setApp(clientSettings);
    when(existingApplication.getSettings()).thenReturn(settings);
    applicationService.update(APPLICATION_ID, existingApplication);
}
Also used : ApplicationSettings(io.gravitee.rest.api.model.application.ApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) SimpleApplicationSettings(io.gravitee.rest.api.model.application.SimpleApplicationSettings) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

ApplicationSettings (io.gravitee.rest.api.model.application.ApplicationSettings)15 SimpleApplicationSettings (io.gravitee.rest.api.model.application.SimpleApplicationSettings)15 Test (org.junit.Test)8 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)7 OAuthClientSettings (io.gravitee.rest.api.model.application.OAuthClientSettings)4 HashMap (java.util.HashMap)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 NewApplicationEntity (io.gravitee.rest.api.model.NewApplicationEntity)3 UpdateApplicationEntity (io.gravitee.rest.api.model.UpdateApplicationEntity)3 Application (io.gravitee.rest.api.portal.rest.model.Application)3 UserService (io.gravitee.rest.api.service.UserService)3 ApplicationRepository (io.gravitee.repository.management.api.ApplicationRepository)2 io.gravitee.repository.management.model (io.gravitee.repository.management.model)2 Permissions (io.gravitee.rest.api.management.rest.security.Permissions)2 MembershipEntity (io.gravitee.rest.api.model.MembershipEntity)2 MembershipMemberType (io.gravitee.rest.api.model.MembershipMemberType)2 MembershipReferenceType (io.gravitee.rest.api.model.MembershipReferenceType)2 RoleEntity (io.gravitee.rest.api.model.RoleEntity)2 UserEntity (io.gravitee.rest.api.model.UserEntity)2 Permissions (io.gravitee.rest.api.portal.rest.security.Permissions)2