@Test public void testUpdateDataUsage() throws PersistenceException { ArgumentCaptor<String> keyArg1 = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> keyArg2 = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> cqlArg = ArgumentCaptor.forClass(String.class); ArgumentCaptor<PersistentItem> itemArg = ArgumentCaptor.forClass(PersistentItem.class); attributesList = new ArrayList<>(); Map<String, Object> attributes = new HashMap<>(); attributes.put(DATA_USAGE_LONG, LONG_1); attributes.put(DATA_LIMIT_LONG, LONG_1); attributesList.add(attributes); when(persistentStore.get(anyString(), anyString())).thenReturn(attributesList); attributesStore.updateUserDataUsage(USER, LONG_5); verify(persistentStore, atLeast(2)).get(keyArg1.capture(), cqlArg.capture()); verify(persistentStore).add(keyArg2.capture(), itemArg.capture()); assertThat(keyArg1.getValue(), is(PersistentStore.USER_ATTRIBUTE_TYPE)); assertThat(keyArg2.getValue(), is(PersistentStore.USER_ATTRIBUTE_TYPE)); assertThat(itemArg.getValue().getLongProperty(AttributesStore.DATA_USAGE_KEY), is(600L)); assertThat(cqlArg.getValue(), is(CQL)); }
@SuppressWarnings("unchecked") @Override public void addEmails(String id, Set<String> emails) { notBlank(id, "id must be non-blank"); notNull(emails, "emails must be non-null"); emails.forEach(email -> notBlank(email, "emails in set must be non-blank")); LOCK.lock(); try { List<Map<String, Object>> results = get(id); if (!results.isEmpty()) { PersistentItem item = convert(results.get(0)); if (item.containsKey(EMAIL_PROPERTY + PersistentItem.TEXT_SUFFIX)) { Set<String> newValue = new HashSet<>(emails); Object value = item.get(EMAIL_PROPERTY + PersistentItem.TEXT_SUFFIX); if (value instanceof String) { newValue.add((String) value); } else if (value instanceof Set) { ((Set) value) .stream() .filter(String.class::isInstance) .forEach(obj -> newValue.add((String) obj)); } item.addProperty(EMAIL_PROPERTY, newValue); } else { item.addProperty(EMAIL_PROPERTY, emails); } persistentStore.add(SubscriptionsPersistentStore.SUBSCRIPTIONS_TYPE, item); } else { PersistentItem persistentItem = new PersistentItem(); persistentItem.addIdProperty(id); persistentItem.addProperty(EMAIL_PROPERTY, emails); persistentStore.add(SubscriptionsPersistentStore.SUBSCRIPTIONS_TYPE, persistentItem); } } catch (PersistenceException e) { LOGGER.warn("unable to add emails to workspace: workspaceId={} emails={}", id, emails, e); } finally { LOCK.unlock(); } }
@Test public void testGetDataLimit() throws PersistenceException { attributesList = new ArrayList<>(); Map<String, Object> attributes = new HashMap<>(); attributes.put(DATA_USAGE_LONG, LONG_2); attributes.put(DATA_LIMIT_LONG, LONG_1); attributesList.add(attributes); when(persistentStore.get(anyString(), anyString())).thenReturn(attributesList); long usage = attributesStore.getDataLimitByUser(USER); assertThat(usage, is(LONG_1)); }
private List<Map<String, Object>> query(String q) throws PersistenceException { LOCK.lock(); try { List<Map<String, Object>> results = persistentStore.get(SubscriptionsPersistentStore.SUBSCRIPTIONS_TYPE, q); assert results.size() <= 1; return results; } finally { LOCK.unlock(); } }
private void add(String id, PersistentItem item) { LOCK.lock(); try { persistentStore.add(SubscriptionsPersistentStore.SUBSCRIPTIONS_TYPE, item); } catch (PersistenceException e) { LOGGER.warn( "unable to add PersistentItem to the PersistentStore: id={} item={}", id, item, e); } finally { LOCK.unlock(); } }
@Test public void resetUserDataUsages() throws PersistenceException { attributesList = new ArrayList<>(); Map<String, Object> attributes = new HashMap<>(); attributes.put(DATA_USAGE_LONG, LONG_2); attributes.put(DATA_LIMIT_LONG, LONG_1); attributesList.add(attributes); when(persistentStore.get(anyString())).thenReturn(attributesList); ArgumentCaptor<String> keyArg = ArgumentCaptor.forClass(String.class); ArgumentCaptor<PersistentItem> itemArg = ArgumentCaptor.forClass(PersistentItem.class); attributesStore.resetUserDataUsages(); verify(persistentStore).add(keyArg.capture(), itemArg.capture()); assertThat(keyArg.getValue(), is(PersistentStore.USER_ATTRIBUTE_TYPE)); assertThat(itemArg.getValue().getLongProperty(AttributesStore.DATA_USAGE_KEY), is(0L)); assertThat( itemArg.getValue().getLongProperty(AttributesStore.DATA_USAGE_LIMIT_KEY), is(LONG_1)); }
@Override public void handleEvent(Event event) throws IllegalArgumentException { LOGGER.debug("Received activity on topic {}", event.getTopic()); String id = (String) event.getProperty(ActivityEvent.ID_KEY); String session = (String) event.getProperty(ActivityEvent.SESSION_ID_KEY); String status = (String) event.getProperty(ActivityEvent.STATUS_KEY); if (status.equals(ActivityEvent.ActivityStatus.RUNNING.toString())) { return; } String title = (String) event.getProperty(ActivityEvent.TITLE_KEY); String message = (String) event.getProperty(ActivityEvent.MESSAGE_KEY); String timestamp = (String) event.getProperty(ActivityEvent.TIMESTAMP_KEY); Map<String, String> operations = (Map<String, String>) event.getProperty(ActivityEvent.OPERATIONS_KEY); String progress = event.getProperty(ActivityEvent.PROGRESS_KEY).toString(); String user = (String) event.getProperty(ActivityEvent.USER_ID_KEY); String category = (String) event.getProperty(ActivityEvent.CATEGORY_KEY); Long bytes = (Long) event.getProperty(ActivityEvent.BYTES_READ_KEY); String downloadId = (String) event.getProperty(ActivityEvent.DOWNLOAD_ID_KEY); PersistentItem activityToStore = new PersistentItem(); activityToStore.addIdProperty(id); activityToStore.addProperty(ActivityEvent.SESSION_ID_KEY, session); activityToStore.addProperty(ActivityEvent.STATUS_KEY, status); activityToStore.addProperty(ActivityEvent.TITLE_KEY, title); activityToStore.addProperty(ActivityEvent.MESSAGE_KEY, message); activityToStore.addProperty(ActivityEvent.TIMESTAMP_KEY, timestamp); for (Map.Entry<String, String> entry : operations.entrySet()) { activityToStore.addProperty( ActivityEvent.OPERATIONS_KEY + "_" + entry.getKey(), entry.getValue()); } activityToStore.addProperty(ActivityEvent.PROGRESS_KEY, progress); activityToStore.addProperty(ActivityEvent.USER_ID_KEY, user); activityToStore.addProperty(ActivityEvent.CATEGORY_KEY, category); activityToStore.addProperty(ActivityEvent.BYTES_READ_KEY, bytes); activityToStore.addProperty(ActivityEvent.DOWNLOAD_ID_KEY, downloadId); try { persistentStore.add(PersistentStore.ACTIVITY_TYPE, activityToStore); } catch (PersistenceException e) { LOGGER.info("Caught PersistenceException {}", e.getMessage()); } }
@Test(expected = PersistenceException.class) public void testGetAllUsersThrowsException() throws PersistenceException { when(persistentStore.get(anyString())).thenThrow(new PersistenceException()); attributesStore.getAllUsers(); }
@Test(expected = PersistenceException.class) public void testPersistenceStoreThrowsExceptionOnGet() throws PersistenceException { when(persistentStore.get(anyString(), anyString())).thenThrow(new PersistenceException()); attributesStore.updateUserDataUsage(USER, LONG_5); }
@Test public void testPersistenceStoreReturnsEmptyList() throws PersistenceException { when(persistentStore.get(anyString(), anyString())).thenReturn(new ArrayList<>()); assertThat(attributesStore.getCurrentDataUsageByUser(USER), is(0L)); }