Search in sources :

Example 1 with PersistentStore

use of org.codice.ddf.persistence.PersistentStore in project ddf by codice.

the class TestDataUsage method setUp.

@Before
public void setUp() throws PersistenceException {
    attributesStore = mock(AttributesStore.class);
    persistentStore = mock(PersistentStore.class);
    userList = generateUserList();
    when(attributesStore.getAllUsers()).thenReturn(userList);
    doAnswer((InvocationOnMock invocationOnMock) -> {
        Object[] args = invocationOnMock.getArguments();
        String user = (String) args[0];
        long dataUsage = (long) args[1];
        for (Map<String, Object> map : userList) {
            if (map.get(USER_TXT).equals(user)) {
                map.put(DATA_LIMIT, dataUsage);
            }
        }
        return null;
    }).when(attributesStore).setDataLimit(anyString(), anyLong());
    dataUsage = new DataUsage(attributesStore, persistentStore);
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) PersistentStore(org.codice.ddf.persistence.PersistentStore) Matchers.anyString(org.mockito.Matchers.anyString) DataUsage(org.codice.ddf.resourcemanagement.usage.service.DataUsage) AttributesStore(org.codice.ddf.persistence.attributes.AttributesStore) Before(org.junit.Before)

Example 2 with PersistentStore

use of org.codice.ddf.persistence.PersistentStore in project ddf by codice.

the class TestCatalog method persistToWorkspace.

private void persistToWorkspace(int size) throws Exception {
    // Generate very large data block
    Map<String, String> map = Maps.newHashMap();
    for (int i = 0; i < size; i++) {
        map.put("Key-" + i, "Val-" + i);
    }
    JSONObject jsonObject = new JSONObject();
    jsonObject.putAll(map);
    String jsonString = jsonObject.toJSONString();
    final PersistentStore pstore = getServiceManager().getService(PersistentStore.class);
    PersistentItem item = new PersistentItem();
    item.addIdProperty("itest");
    item.addProperty("user", "itest");
    item.addProperty("workspaces_json", jsonString);
    final String WORKSPACE_TYPE = PersistenceType.WORKSPACE_TYPE.toString();
    await("").atMost(1, TimeUnit.MINUTES).pollDelay(1, TimeUnit.SECONDS).ignoreExceptions().until(() -> pstore.get(WORKSPACE_TYPE).isEmpty());
    pstore.add(WORKSPACE_TYPE, item);
    await("Solr core to be spun up and item to be persisted").atMost(5, TimeUnit.MINUTES).until(() -> pstore.get(WORKSPACE_TYPE).size(), equalTo(1));
    List<Map<String, Object>> storedWs = pstore.get(WORKSPACE_TYPE, "\"id\" = 'itest'");
    assertThat(storedWs, hasSize(1));
    assertThat(storedWs.get(0).get("user_txt"), is("itest"));
    pstore.delete(WORKSPACE_TYPE, "\"id\" = 'itest'");
    await("Workspace to be empty").atMost(5, TimeUnit.MINUTES).until(() -> pstore.get(WORKSPACE_TYPE).size(), equalTo(0));
}
Also used : JSONObject(org.json.simple.JSONObject) PersistentItem(org.codice.ddf.persistence.PersistentItem) PersistentStore(org.codice.ddf.persistence.PersistentStore) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 3 with PersistentStore

use of org.codice.ddf.persistence.PersistentStore in project ddf by codice.

the class DeleteCommand method execute.

@Override
public Object execute() throws Exception {
    PrintStream console = System.out;
    // Get Notification service
    @SuppressWarnings("rawtypes") ServiceReference[] serviceReferences = bundleContext.getServiceReferences(SERVICE_PID, null);
    int numDeleted = 0;
    if (serviceReferences == null || serviceReferences.length != 1) {
        LOGGER.debug("Found no service references for {}", SERVICE_PID);
    } else {
        LOGGER.debug("Found " + serviceReferences.length + " service references for " + SERVICE_PID);
        PersistentStore persistentStore = (PersistentStore) bundleContext.getService(serviceReferences[0]);
        if (persistentStore != null) {
            if (StringUtils.isNotBlank(id)) {
                try {
                    numDeleted = persistentStore.delete(PersistentStore.NOTIFICATION_TYPE, Notification.NOTIFICATION_KEY_ID + " = '" + id + "'");
                } catch (PersistenceException e) {
                    LOGGER.info("PersistenceException during deletion of notifications for ID {}", id, e);
                }
            } else if (StringUtils.isNotBlank(userId)) {
                try {
                    numDeleted = persistentStore.delete(PersistentStore.NOTIFICATION_TYPE, Notification.NOTIFICATION_KEY_USER_ID + " = '" + userId + "'");
                } catch (PersistenceException e) {
                    LOGGER.info("PersistenceException during deletion of notifications for user {}", userId, e);
                }
            }
        } else {
            LOGGER.debug("Unable to lookup PersistentStore service");
        }
    }
    console.println(CYAN_CONSOLE_COLOR + "Deleted " + numDeleted + " notifications" + DEFAULT_CONSOLE_COLOR);
    return null;
}
Also used : PrintStream(java.io.PrintStream) PersistenceException(org.codice.ddf.persistence.PersistenceException) PersistentStore(org.codice.ddf.persistence.PersistentStore) ServiceReference(org.osgi.framework.ServiceReference)

Example 4 with PersistentStore

use of org.codice.ddf.persistence.PersistentStore in project ddf by codice.

the class ListCommand method getNotifications.

@SuppressWarnings("unchecked")
private List<Map<String, Object>> getNotifications(String userId) throws InvalidSyntaxException {
    List<Map<String, Object>> notifications = new ArrayList<>();
    // Get Notification service
    @SuppressWarnings("rawtypes") ServiceReference[] serviceReferences = bundleContext.getServiceReferences(SERVICE_PID, null);
    if (serviceReferences == null || serviceReferences.length != 1) {
        LOGGER.debug("Found no service references for {}", SERVICE_PID);
    } else {
        LOGGER.debug("Found " + serviceReferences.length + " service references for " + SERVICE_PID);
        PersistentStore persistentStore = (PersistentStore) bundleContext.getService(serviceReferences[0]);
        if (persistentStore != null) {
            try {
                List<Map<String, Object>> results;
                if (StringUtils.isNotBlank(cql)) {
                    results = persistentStore.get(PersistentStore.NOTIFICATION_TYPE, cql);
                } else if (StringUtils.isNotBlank(userId)) {
                    results = persistentStore.get(PersistentStore.NOTIFICATION_TYPE, Notification.NOTIFICATION_KEY_USER_ID + " = '" + userId + "'");
                } else {
                    results = persistentStore.get(PersistentStore.NOTIFICATION_TYPE);
                }
                for (Map<String, Object> result : results) {
                    notifications.add(PersistentItem.stripSuffixes(result));
                }
            } catch (PersistenceException e) {
                LOGGER.info("PersistenceException during retrieval of notifications", e);
            }
        } else {
            LOGGER.debug("Unable to lookup PersistentStore service");
        }
    }
    return notifications;
}
Also used : ArrayList(java.util.ArrayList) PersistenceException(org.codice.ddf.persistence.PersistenceException) PersistentStore(org.codice.ddf.persistence.PersistentStore) Map(java.util.Map) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with PersistentStore

use of org.codice.ddf.persistence.PersistentStore in project ddf by codice.

the class TestSubscriptionsPersistentStoreImpl method setup.

@Before
public void setup() {
    PersistentStore persistentStore = new MemoryPersistentStore();
    store = new SubscriptionsPersistentStoreImpl(persistentStore);
}
Also used : PersistentStore(org.codice.ddf.persistence.PersistentStore) Before(org.junit.Before)

Aggregations

PersistentStore (org.codice.ddf.persistence.PersistentStore)5 Map (java.util.Map)2 PersistenceException (org.codice.ddf.persistence.PersistenceException)2 Before (org.junit.Before)2 ServiceReference (org.osgi.framework.ServiceReference)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 PersistentItem (org.codice.ddf.persistence.PersistentItem)1 AttributesStore (org.codice.ddf.persistence.attributes.AttributesStore)1 DataUsage (org.codice.ddf.resourcemanagement.usage.service.DataUsage)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 JSONObject (org.json.simple.JSONObject)1 Matchers.anyString (org.mockito.Matchers.anyString)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1