@Test public void testUserIdNumberToString() throws Exception { AbstractUserService userService = getUserService(); userService.setUserProperties(PROJECT_NAME, 3, sampleProperties); User test = userService.getUser(CONTEXT, "3").join(); assertEquals(test.id, "3"); assertEquals((Object) test.properties, samplePropertiesExpected); }
@Test public void testIncrementProperties() throws Exception { AbstractUserService userService = getUserService(); userService.incrementProperty(PROJECT_NAME, 8, "test", 10); assertEquals(userService.getUser(CONTEXT, 8).join().properties.get("test").asDouble(), 10.0); userService.incrementProperty(PROJECT_NAME, 8, "test", 10); assertEquals(userService.getUser(CONTEXT, 8).join().properties.get("test").asDouble(), 20.0); }
@Test public void testSetOncePropertiesLatterSet() throws Exception { AbstractUserService userService = getUserService(); userService.setUserProperties(PROJECT_NAME, 6, sampleProperties); userService.setUserPropertiesOnce(PROJECT_NAME, 6, JsonHelper.jsonObject() .put("test", 2) .put("test1 Naber Abi", "value1") .put("test4 samil", false) .put("created_at", Instant.now().toEpochMilli()) .put("test5", 2.5)); User test = userService.getUser(CONTEXT, 6).join(); assertEquals((Object) test.properties, samplePropertiesExpected); }
@Test public void testSetOncePropertiesFirstSet() throws Exception { AbstractUserService userService = getUserService(); userService.setUserPropertiesOnce(PROJECT_NAME, 5, sampleProperties); User test = userService.getUser(CONTEXT, 5).join(); assertEquals(test.id, 5); assertEquals((Object) test.properties, samplePropertiesExpected); }
@Test public void testUnsetSetProperties() throws Exception { AbstractUserService userService = getUserService(); userService.setUserProperties(PROJECT_NAME, 7, sampleProperties); userService.unsetProperties(PROJECT_NAME, 7, ImmutableList.of( "test", "test1 Naber Abi", "test4 samil")); User test = userService.getUser(CONTEXT, 7).join(); assertEquals(test.id, 7); assertEquals((Object) test.properties, JsonHelper.jsonObject() .put("created_at", Instant.ofEpochMilli(100).toString()) .put("test5", 1.5)); }
@ApiOperation(value = "Set user properties", request = User.class, response = Integer.class) @ApiResponses(value = {@ApiResponse(code = 404, message = "User does not exist.")}) @Path("/set_properties") @POST public void setProperties(RakamHttpRequest request) { setPropertiesInline(request, (project, user) -> service.setUserProperties(project, user.id, user.properties)); }
@JsonRequest @ApiOperation(value = "Get user", authorizations = @Authorization(value = "read_key")) @ApiResponses(value = {@ApiResponse(code = 404, message = "User does not exist.")}) @Path("/get") public CompletableFuture<User> getUser(@Named("project") RequestContext context, @ApiParam("user") Object user) { return service.getUser(context, user); }
@JsonRequest @ApiOperation(value = "Unset user property") @ApiResponses(value = {@ApiResponse(code = 404, message = "User does not exist.")}) @Path("/unset_properties") @AllowCookie public SuccessMessage unsetProperty(@ApiParam("api") User.UserContext api, @ApiParam("id") Object id, @ApiParam("properties") List<String> properties) { String project = apiKeyService.getProjectOfApiKey(api.apiKey, WRITE_KEY); service.unsetProperties(project, id, properties); return SuccessMessage.success(); }
@JsonRequest @ApiOperation(value = "Set user property", authorizations = @Authorization(value = "master_key")) @ApiResponses(value = {@ApiResponse(code = 404, message = "User does not exist.")}) @Path("/increment_property") @AllowCookie public SuccessMessage incrementProperty(@ApiParam("api") User.UserContext api, @ApiParam("id") String user, @ApiParam("property") String property, @ApiParam("value") double value) { String project = apiKeyService.getProjectOfApiKey(api.apiKey, WRITE_KEY); service.incrementProperty(project, user, property, value); return SuccessMessage.success(); }
@JsonRequest @ApiOperation(value = "Set user properties once", request = User.class, response = Integer.class) @ApiResponses(value = {@ApiResponse(code = 404, message = "User does not exist.")}) @Path("/set_properties_once") public void setPropertiesOnce(RakamHttpRequest request) { request.bodyHandler(s -> { User req; try { req = JsonHelper.readSafe(s, User.class); } catch (IOException e) { returnError(request, e.getMessage(), BAD_REQUEST); return; } String project = apiKeyService.getProjectOfApiKey(req.api.apiKey, WRITE_KEY); DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, wrappedBuffer(OK_MESSAGE)); response.headers().set(ACCESS_CONTROL_ALLOW_ORIGIN, request.headers().get(ORIGIN)); List<Cookie> cookies = mapProperties(project, req, request); if (cookies != null) { response.headers().add(SET_COOKIE, STRICT.encode(cookies)); } String headerList = getHeaderList(response.headers().iterator()); if (headerList != null) { response.headers().set(ACCESS_CONTROL_EXPOSE_HEADERS, headerList); } // TODO: we may cache these values and reduce the db hit. service.setUserPropertiesOnce(project, req.id, req.properties); request.response(OK_MESSAGE).end(); }); }
@ApiOperation(value = "Batch operation on a single user properties", request = SingleUserBatchOperationRequest.class, response = Integer.class, authorizations = {@Authorization(value = "write_key")}) @ApiResponses(value = {@ApiResponse(code = 404, message = "User does not exist.")}) @Path("/batch") @JsonRequest public void batchSingleUserOperations(RakamHttpRequest request) { request.bodyHandler(s -> { SingleUserBatchOperationRequest req; try { req = JsonHelper.read(s, SingleUserBatchOperationRequest.class); } catch (Exception e) { returnError(request, e.getMessage(), BAD_REQUEST); return; } String project = apiKeyService.getProjectOfApiKey(req.api.apiKey, WRITE_KEY); InetAddress socketAddress = ((InetSocketAddress) request.context().channel() .remoteAddress()).getAddress(); DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, wrappedBuffer(OK_MESSAGE)); List<Cookie> cookies = mapEvent(mapper -> mapper.map(project, req.data, new HttpRequestParams(request), socketAddress)); service.batch(project, req.data); setBrowser(request, response); if (cookies != null && !cookies.isEmpty()) { response.headers().add(SET_COOKIE, STRICT.encode(cookies)); } request.response(response).end(); }); }
@ApiOperation(value = "Create new user", request = User.class, response = Integer.class) @Path("/create") @POST public void createUser(RakamHttpRequest request) { setPropertiesInline(request, (project, user) -> service.create(project, user.id, user.properties)); }
@JsonRequest @ApiOperation(value = "Create multiple new users", authorizations = @Authorization(value = "write_key"), notes = "Returns user ids. User id may be string or numeric.") @Path("/batch/create") public List<Object> createUsers(@Named("project") RequestContext context, @ApiParam("users") List<User> users) { try { return service.batchCreate(context, users); } catch (Exception e) { throw new RakamException(e.getMessage(), BAD_REQUEST); } }
@AfterMethod public void deleteProject() throws Exception { getUserService().dropProject(PROJECT_NAME); getConfigManager().clear(); }
@POST @JsonRequest @ApiOperation(value = "Get events of the user", authorizations = @Authorization(value = "master_key")) @ApiResponses(value = {@ApiResponse(code = 404, message = "User does not exist.")}) @Path("/create_segment") public SuccessMessage createSegment(@Named("project") RequestContext context, @ApiParam("name") String name, @ApiParam("table_name") String tableName, @ApiParam(value = "filter_expression", required = false) String filterExpression, @ApiParam(value = "event_filters", required = false) List<UserStorage.EventFilter> eventFilters, @ApiParam("cache_eviction") Duration duration) { if (filterExpression == null && (eventFilters == null || eventFilters.isEmpty())) { throw new RakamException("At least one predicate is required", BAD_REQUEST); } Expression expression = null; if (filterExpression != null) { synchronized (sqlParser) { expression = sqlParser.createExpression(filterExpression); } } service.createSegment(context, name, tableName, expression, eventFilters, duration); return SuccessMessage.success(); }
mapper.map(project, req.data, new HttpRequestParams(request), socketAddress)); service.batch(project, req.data).whenComplete((result, ex) -> { setBrowser(request, response);
@Test public void testCreate() throws Exception { AbstractUserService userService = getUserService(); userService.setUserProperties(PROJECT_NAME, 1L, sampleProperties); User test = userService.getUser(CONTEXT, 1L).join(); assertEquals(test.id, 1L); assertEquals((Object) test.properties, samplePropertiesExpected); }
@Test(expectedExceptions = CompletionException.class) public void testUserIdInvalid() throws Exception { AbstractUserService userService = getUserService(); userService.setUserProperties(PROJECT_NAME, 3, sampleProperties); User test = userService.getUser(CONTEXT, "selami").join(); assertEquals(test.id, 3); assertEquals((Object) test.properties, samplePropertiesExpected); }
@Test public void testSetProperties() throws Exception { AbstractUserService userService = getUserService(); userService.setUserProperties(PROJECT_NAME, 3, sampleProperties); User test = userService.getUser(CONTEXT, 3).join(); assertEquals(test.id, 3); assertEquals((Object) test.properties, samplePropertiesExpected); }
@Test public void testUserIdStringToNumber() throws Exception { AbstractUserService userService = getUserService(); userService.setUserProperties(PROJECT_NAME, "3", sampleProperties); User test = userService.getUser(CONTEXT, 3).join(); assertEquals(test.id, 3); assertEquals((Object) test.properties, samplePropertiesExpected); }