Search in sources :

Example 1 with org.eclipse.che.commons.lang.pair

use of org.eclipse.che.commons.lang.pair in project che by eclipse.

the class MachineServiceLinksInjectorTest method shouldInjectLinksIntoMachineDto.

@Test
public void shouldInjectLinksIntoMachineDto() {
    when(terminalServerMock.getRef()).thenReturn(TERMINAL_REFERENCE);
    when(terminalServerMock.getUrl()).thenReturn(URI_BASE + "/pty");
    when(execAgentServerMock.getRef()).thenReturn(EXEC_AGENT_REFERENCE);
    when(execAgentServerMock.getUrl()).thenReturn(URI_BASE + "/connect");
    when(machineRuntimeInfoDtoMock.getServers()).thenReturn(ImmutableMap.of(TERMINAL_REFERENCE, terminalServerMock, EXEC_AGENT_REFERENCE, execAgentServerMock));
    final MachineDto machineDto = DtoFactory.newDto(MachineDto.class).withId("id").withWorkspaceId("wsId").withConfig(machineConfigDtoMock).withRuntime(machineRuntimeInfoDtoMock);
    machineLinksInjector.injectLinks(machineDto, serviceContextMock);
    final Set<Pair<String, String>> links = machineDto.getLinks().stream().map(link -> Pair.of(link.getMethod(), link.getRel())).collect(Collectors.toSet());
    final Set<Pair<String, String>> expectedLinks = new HashSet<>(asList(Pair.of("GET", TERMINAL_REFERENCE), Pair.of("GET", EXEC_AGENT_REFERENCE), Pair.of("GET", LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL), Pair.of("GET", ENVIRONMENT_STATUS_CHANNEL_TEMPLATE)));
    assertEquals(links, expectedLinks, "Difference " + Sets.symmetricDifference(links, expectedLinks) + "\n");
}
Also used : Listeners(org.testng.annotations.Listeners) ServiceContext(org.eclipse.che.api.core.rest.ServiceContext) Mock(org.mockito.Mock) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) EXEC_AGENT_REFERENCE(org.eclipse.che.api.machine.shared.Constants.EXEC_AGENT_REFERENCE) ServerDto(org.eclipse.che.api.machine.shared.dto.ServerDto) UriBuilderImpl(org.everrest.core.impl.uri.UriBuilderImpl) HashSet(java.util.HashSet) MachineConfigDto(org.eclipse.che.api.machine.shared.dto.MachineConfigDto) Arrays.asList(java.util.Arrays.asList) UriBuilder(javax.ws.rs.core.UriBuilder) DtoFactory(org.eclipse.che.dto.server.DtoFactory) LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL(org.eclipse.che.api.machine.shared.Constants.LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ImmutableMap(com.google.common.collect.ImmutableMap) BeforeMethod(org.testng.annotations.BeforeMethod) TERMINAL_REFERENCE(org.eclipse.che.api.machine.shared.Constants.TERMINAL_REFERENCE) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) Pair(org.eclipse.che.commons.lang.Pair) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) MachineRuntimeInfoDto(org.eclipse.che.api.machine.shared.dto.MachineRuntimeInfoDto) ENVIRONMENT_STATUS_CHANNEL_TEMPLATE(org.eclipse.che.api.machine.shared.Constants.ENVIRONMENT_STATUS_CHANNEL_TEMPLATE) MachineDto(org.eclipse.che.api.machine.shared.dto.MachineDto) MachineDto(org.eclipse.che.api.machine.shared.dto.MachineDto) Pair(org.eclipse.che.commons.lang.Pair) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 2 with org.eclipse.che.commons.lang.pair

use of org.eclipse.che.commons.lang.pair in project che by eclipse.

the class FactoryDaoTest method shouldGetFactoryByIdAttribute.

@Test
public void shouldGetFactoryByIdAttribute() throws Exception {
    final FactoryImpl factory = factories[0];
    final List<Pair<String, String>> attributes = ImmutableList.of(Pair.of("id", factory.getId()));
    final List<FactoryImpl> result = factoryDao.getByAttribute(1, 0, attributes);
    assertEquals(new HashSet<>(result), ImmutableSet.of(factory));
}
Also used : FactoryImpl(org.eclipse.che.api.factory.server.model.impl.FactoryImpl) Pair(org.eclipse.che.commons.lang.Pair) Test(org.testng.annotations.Test)

Example 3 with org.eclipse.che.commons.lang.pair

use of org.eclipse.che.commons.lang.pair in project che by eclipse.

the class FactoryService method getFactoryByAttribute.

@GET
@Path("/find")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Get factory by attribute, " + "the attribute must match one of the Factory model fields with type 'String', " + "e.g. (factory.name, factory.creator.name)", notes = "If specify more than one value for a single query parameter then will be taken the first one")
@ApiResponses({ @ApiResponse(code = 200, message = "Response contains list requested factories"), @ApiResponse(code = 400, message = "When query does not contain at least one attribute to search for"), @ApiResponse(code = 500, message = "Internal server error") })
public List<FactoryDto> getFactoryByAttribute(@DefaultValue("0") @QueryParam("skipCount") Integer skipCount, @DefaultValue("30") @QueryParam("maxItems") Integer maxItems, @Context UriInfo uriInfo) throws BadRequestException, ServerException {
    final Set<String> skip = ImmutableSet.of("token", "skipCount", "maxItems");
    final List<Pair<String, String>> query = URLEncodedUtils.parse(uriInfo.getRequestUri()).entrySet().stream().filter(param -> !skip.contains(param.getKey()) && !param.getValue().isEmpty()).map(entry -> Pair.of(entry.getKey(), entry.getValue().iterator().next())).collect(toList());
    checkArgument(!query.isEmpty(), "Query must contain at least one attribute");
    final List<FactoryDto> factories = new ArrayList<>();
    for (Factory factory : factoryManager.getByAttribute(maxItems, skipCount, query)) {
        factories.add(injectLinks(asDto(factory), null));
    }
    return factories;
}
Also used : URLEncodedUtils(org.eclipse.che.commons.lang.URLEncodedUtils) Produces(javax.ws.rs.Produces) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) FactoryBuilder(org.eclipse.che.api.factory.server.builder.FactoryBuilder) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) ApiOperation(io.swagger.annotations.ApiOperation) QueryParam(javax.ws.rs.QueryParam) Service(org.eclipse.che.api.core.rest.Service) PreferenceManager(org.eclipse.che.api.user.server.PreferenceManager) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) NameGenerator(org.eclipse.che.commons.lang.NameGenerator) DELETE(javax.ws.rs.DELETE) WorkspaceManager(org.eclipse.che.api.workspace.server.WorkspaceManager) ImmutableSet(com.google.common.collect.ImmutableSet) Context(javax.ws.rs.core.Context) Predicate(java.util.function.Predicate) Set(java.util.Set) Pair(org.eclipse.che.commons.lang.Pair) AuthorDto(org.eclipse.che.api.factory.shared.dto.AuthorDto) List(java.util.List) BadRequestException(org.eclipse.che.api.core.BadRequestException) Response(javax.ws.rs.core.Response) ProjectConfig(org.eclipse.che.api.core.model.project.ProjectConfig) UriInfo(javax.ws.rs.core.UriInfo) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) FactoryLinksHelper.createLinks(org.eclipse.che.api.factory.server.FactoryLinksHelper.createLinks) PathParam(javax.ws.rs.PathParam) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GET(javax.ws.rs.GET) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) ApiResponses(io.swagger.annotations.ApiResponses) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ApiException(org.eclipse.che.api.core.ApiException) User(org.eclipse.che.api.core.model.user.User) ConflictException(org.eclipse.che.api.core.ConflictException) MULTIPART_FORM_DATA(javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA) Api(io.swagger.annotations.Api) DtoFactory(org.eclipse.che.dto.server.DtoFactory) CONTENT_DISPOSITION(javax.ws.rs.core.HttpHeaders.CONTENT_DISPOSITION) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) Iterator(java.util.Iterator) JsonSyntaxException(com.google.gson.JsonSyntaxException) FileItem(org.apache.commons.fileupload.FileItem) IOException(java.io.IOException) NotFoundException(org.eclipse.che.api.core.NotFoundException) Factory(org.eclipse.che.api.core.model.factory.Factory) Collectors.toList(java.util.stream.Collectors.toList) Boolean.parseBoolean(java.lang.Boolean.parseBoolean) ServerException(org.eclipse.che.api.core.ServerException) ApiResponse(io.swagger.annotations.ApiResponse) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ProjectConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ProjectConfigImpl) PUT(javax.ws.rs.PUT) UserManager(org.eclipse.che.api.user.server.UserManager) Collections(java.util.Collections) InputStream(java.io.InputStream) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) ArrayList(java.util.ArrayList) LoggerFactory(org.slf4j.LoggerFactory) DtoFactory(org.eclipse.che.dto.server.DtoFactory) Factory(org.eclipse.che.api.core.model.factory.Factory) Pair(org.eclipse.che.commons.lang.Pair) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with org.eclipse.che.commons.lang.pair

use of org.eclipse.che.commons.lang.pair in project che by eclipse.

the class JpaTckModule method configure.

@Override
protected void configure() {
    H2DBTestServer server = H2DBTestServer.startDefault();
    install(new PersistTestModuleBuilder().setDriver(Driver.class).runningOn(server).addEntityClasses(UserImpl.class, ProfileImpl.class, PreferenceEntity.class, AccountImpl.class).setExceptionHandler(H2ExceptionHandler.class).build());
    bind(DBInitializer.class).asEagerSingleton();
    bind(SchemaInitializer.class).toInstance(new FlywaySchemaInitializer(server.getDataSource(), "che-schema"));
    bind(TckResourcesCleaner.class).toInstance(new H2JpaCleaner(server.getDataSource()));
    bind(new TypeLiteral<TckRepository<UserImpl>>() {
    }).to(UserJpaTckRepository.class);
    bind(new TypeLiteral<TckRepository<ProfileImpl>>() {
    }).toInstance(new JpaTckRepository<>(ProfileImpl.class));
    bind(new TypeLiteral<TckRepository<Pair<String, Map<String, String>>>>() {
    }).to(PreferenceJpaTckRepository.class);
    bind(UserDao.class).to(JpaUserDao.class);
    bind(ProfileDao.class).to(JpaProfileDao.class);
    bind(PreferenceDao.class).to(JpaPreferenceDao.class);
    // SHA-512 encryptor is faster than PBKDF2 so it is better for testing
    bind(PasswordEncryptor.class).to(SHA512PasswordEncryptor.class).in(Singleton.class);
}
Also used : TckResourcesCleaner(org.eclipse.che.commons.test.tck.TckResourcesCleaner) H2DBTestServer(org.eclipse.che.commons.test.db.H2DBTestServer) AccountImpl(org.eclipse.che.account.spi.AccountImpl) Driver(org.h2.Driver) PreferenceDao(org.eclipse.che.api.user.server.spi.PreferenceDao) SHA512PasswordEncryptor(org.eclipse.che.security.SHA512PasswordEncryptor) H2JpaCleaner(org.eclipse.che.commons.test.db.H2JpaCleaner) PersistTestModuleBuilder(org.eclipse.che.commons.test.db.PersistTestModuleBuilder) SchemaInitializer(org.eclipse.che.core.db.schema.SchemaInitializer) FlywaySchemaInitializer(org.eclipse.che.core.db.schema.impl.flyway.FlywaySchemaInitializer) FlywaySchemaInitializer(org.eclipse.che.core.db.schema.impl.flyway.FlywaySchemaInitializer) ProfileDao(org.eclipse.che.api.user.server.spi.ProfileDao) TypeLiteral(com.google.inject.TypeLiteral) UserDao(org.eclipse.che.api.user.server.spi.UserDao) ProfileImpl(org.eclipse.che.api.user.server.model.impl.ProfileImpl) DBInitializer(org.eclipse.che.core.db.DBInitializer) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl) Pair(org.eclipse.che.commons.lang.Pair)

Example 5 with org.eclipse.che.commons.lang.pair

use of org.eclipse.che.commons.lang.pair in project che by eclipse.

the class ProfileLinksInjectorTest method shouldInjectProfileLinks.

@Test
public void shouldInjectProfileLinks() throws Exception {
    final ProfileDto profileDto = DtoFactory.newDto(ProfileDto.class).withUserId("user123").withEmail("user@codenvy.com");
    linksInjector.injectLinks(profileDto, serviceContext);
    // [rel, method] pairs links
    final Set<Pair<String, String>> links = profileDto.getLinks().stream().map(link -> Pair.of(link.getMethod(), link.getRel())).collect(Collectors.toSet());
    final Set<Pair<String, String>> expectedLinks = new HashSet<>(asList(Pair.of("GET", Constants.LINK_REL_SELF), Pair.of("GET", Constants.LINK_REL_CURRENT_PROFILE), Pair.of("PUT", Constants.LINK_REL_PROFILE_ATTRIBUTES), Pair.of("PUT", Constants.LINK_REL_CURRENT_PROFILE_ATTRIBUTES), Pair.of("DELETE", Constants.LINK_REL_CURRENT_PROFILE_ATTRIBUTES)));
    assertEquals(links, expectedLinks, "Difference " + Sets.symmetricDifference(links, expectedLinks) + "\n");
}
Also used : InjectMocks(org.mockito.InjectMocks) Listeners(org.testng.annotations.Listeners) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ServiceContext(org.eclipse.che.api.core.rest.ServiceContext) ProfileDto(org.eclipse.che.api.user.shared.dto.ProfileDto) Mock(org.mockito.Mock) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Mockito.when(org.mockito.Mockito.when) Pair(org.eclipse.che.commons.lang.Pair) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) UriBuilderImpl(org.everrest.core.impl.uri.UriBuilderImpl) HashSet(java.util.HashSet) Arrays.asList(java.util.Arrays.asList) DtoFactory(org.eclipse.che.dto.server.DtoFactory) ProfileDto(org.eclipse.che.api.user.shared.dto.ProfileDto) Pair(org.eclipse.che.commons.lang.Pair) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

Pair (org.eclipse.che.commons.lang.Pair)13 HashSet (java.util.HashSet)5 Test (org.testng.annotations.Test)5 Set (java.util.Set)4 Sets (com.google.common.collect.Sets)3 ArrayList (java.util.ArrayList)3 Arrays.asList (java.util.Arrays.asList)3 Collectors (java.util.stream.Collectors)3 ServiceContext (org.eclipse.che.api.core.rest.ServiceContext)3 DtoFactory (org.eclipse.che.dto.server.DtoFactory)3 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)2 TypeLiteral (com.google.inject.TypeLiteral)2 List (java.util.List)2 Map (java.util.Map)2 UriBuilder (javax.ws.rs.core.UriBuilder)2 VirtualFile (org.eclipse.che.api.vfs.VirtualFile)2 UriBuilderImpl (org.everrest.core.impl.uri.UriBuilderImpl)2 Test (org.junit.Test)2 Mock (org.mockito.Mock)2 Mockito.when (org.mockito.Mockito.when)2