SimpleStateManager implements StateManager { @Override public Map<Resource, Model> getStates(@Nullable String username, @Nullable String applicationId, Set<Resource> subjects) { Map<Resource, Model> states = new HashMap<>(); try (RepositoryConnection conn = repository.getConnection()) { TupleQuery statesQuery; if (applicationId != null && !applicationId.isEmpty()) { Application app = applicationManager.getApplication(applicationId).orElseThrow(() -> new IllegalArgumentException("Application not found")); statesQuery = conn.prepareTupleQuery(GET_APPLICATION_STATES_QUERY); statesQuery.setBinding(APPLICATION_BINDING, app.getResource()); } else { statesQuery = conn.prepareTupleQuery(GET_STATES_QUERY); } if (username != null && !username.isEmpty()) { User user = engineManager.retrieveUser(username).orElseThrow(() -> new IllegalArgumentException("User not found")); statesQuery.setBinding(USER_BINDING, user.getResource()); } TupleQueryResult results = statesQuery.evaluate(); BindingSet bindings; while (results.hasNext() && (bindings = results.next()).getBindingNames().contains(STATE_ID_BINDING)) { Resource stateId = Bindings.requiredResource(bindings, STATE_ID_BINDING); bindings.getBinding(RESOURCES_BINDING).ifPresent(binding -> { Set<Resource> resources = Arrays.stream(StringUtils.split(binding.getValue().stringValue(), ",")) .map(factory::createIRI) .collect(Collectors.toSet()); if (subjects.isEmpty() || resources.containsAll(subjects)) { Model stateModel = modelFactory.createModel(); resources.forEach(resource -> conn.getStatements(resource, null, null) .forEach(stateModel::add)); states.put(stateId, stateModel); } }); } return states; } }  @Override boolean stateExists(Resource stateId); @Override boolean stateExistsForUser(Resource stateId, String username); @Override Map<Resource, Model> getStates(@Nullable String username, @Nullable String applicationId,
                                          Set<Resource> subjects); @Override Resource storeState(Model newState, String username); @Override Resource storeState(Model newState, String username, String applicationId); @Override Model getState(Resource stateId); @Override void updateState(Resource stateId, Model newState); @Override void deleteState(Resource stateId);  }
@Test public void getStatesNoFiltersTest() throws Exception { Model newState = MODEL_FACTORY.createModel(); newState.add(VALUE_FACTORY.createIRI("http: newState.add(VALUE_FACTORY.createIRI("http: State state0 = stateFactory.createNew(VALUE_FACTORY.createIRI("http: state0.setStateResource(Collections.singleton(VALUE_FACTORY.createIRI("http: state0.setForUser(user); State state1 = stateFactory.createNew(VALUE_FACTORY.createIRI("http: RepositoryConnection conn = repo.getConnection(); conn.add(state0.getModel()); conn.add(state1.getModel()); conn.add(newState); Map<Resource, Model> result = manager.getStates(null, null, new HashSet<>()); assertEquals(1, result.size()); assertTrue(result.containsKey(state0.getResource())); assertTrue(result.get(state0.getResource()).equals(newState)); conn.close(); }
@Test public void getStatesApplicationFilterTest() throws Exception { Model state0Model = MODEL_FACTORY.createModel(); state0Model.add(VALUE_FACTORY.createIRI("http: State state0 = stateFactory.createNew(VALUE_FACTORY.createIRI("http: state0.setStateResource(stateResource0); state0.setForUser(user); Model state1Model = MODEL_FACTORY.createModel(); state1Model.add(VALUE_FACTORY.createIRI("http: ApplicationState state1 = applicationStateFactory.createNew(VALUE_FACTORY.createIRI("http: state1.setStateResource(stateResource1); state1.setForUser(user); state1.setApplication(application); RepositoryConnection conn = repo.getConnection(); conn.add(state0.getModel()); conn.add(state0Model); conn.add(state1.getModel()); conn.add(state1Model); Map<Resource, Model> result = manager.getStates(null, "test", new HashSet<>()); assertEquals(1, result.size()); assertTrue(result.containsKey(state1.getResource())); assertTrue(result.get(state1.getResource()).equals(state1Model)); conn.close(); }
@Test public void getStatesSubjectsFilterTest() throws Exception { Model state0Model = MODEL_FACTORY.createModel(); state0Model.add(VALUE_FACTORY.createIRI("http: State state0 = stateFactory.createNew(VALUE_FACTORY.createIRI("http: state0.setStateResource(stateResource0); state0.setForUser(user); Model state1Model = MODEL_FACTORY.createModel(); state1Model.add(VALUE_FACTORY.createIRI("http: State state1 = stateFactory.createNew(VALUE_FACTORY.createIRI("http: state1.setStateResource(stateResource1); state1.setForUser(user); RepositoryConnection conn = repo.getConnection(); conn.add(state0.getModel()); conn.add(state0Model); conn.add(state1.getModel()); conn.add(state1Model); Map<Resource, Model> result = manager.getStates(null, null, Collections.singleton(VALUE_FACTORY.createIRI("http: assertEquals(1, result.size()); assertTrue(result.containsKey(state1.getResource())); assertTrue(result.get(state1.getResource()).equals(state1Model)); conn.close(); }
@Test public void getStatesUserFilterTest() throws Exception { Model state0Model = MODEL_FACTORY.createModel(); state0Model.add(VALUE_FACTORY.createIRI("http: State state0 = stateFactory.createNew(VALUE_FACTORY.createIRI("http: state0.setStateResource(stateResource0); state0.setForUser(user); Model state1Model = MODEL_FACTORY.createModel(); state1Model.add(VALUE_FACTORY.createIRI("http: State state1 = stateFactory.createNew(VALUE_FACTORY.createIRI("http: state1.setStateResource(stateResource1); state1.setForUser(userFactory.createNew(VALUE_FACTORY.createIRI("http: RepositoryConnection conn = repo.getConnection(); conn.add(state0.getModel()); conn.add(state0Model); conn.add(state1.getModel()); conn.add(state1Model); Map<Resource, Model> result = manager.getStates("test", null, new HashSet<>()); assertEquals(1, result.size()); assertTrue(result.containsKey(state0.getResource())); assertTrue(result.get(state0.getResource()).equals(state0Model)); conn.close(); }
@Test public void getStatesAllFiltersTest() throws Exception { Model state0Model = MODEL_FACTORY.createModel(); state0Model.add(VALUE_FACTORY.createIRI("http: State state0 = stateFactory.createNew(VALUE_FACTORY.createIRI("http: state0.setStateResource(stateResource0); state0.setForUser(user); Model state1Model = MODEL_FACTORY.createModel(); state1Model.add(VALUE_FACTORY.createIRI("http: ApplicationState state1 = applicationStateFactory.createNew(VALUE_FACTORY.createIRI("http: state1.setStateResource(stateResource1); state1.setForUser(user); state1.setApplication(application); Model state2Model = MODEL_FACTORY.createModel(); state2Model.add(VALUE_FACTORY.createIRI("http: ApplicationState state2 = applicationStateFactory.createNew(VALUE_FACTORY.createIRI("http: state2.setStateResource(Collections.singleton(VALUE_FACTORY.createIRI("http: state2.setForUser(user); state2.setApplication(application); RepositoryConnection conn = repo.getConnection(); conn.add(state0.getModel()); conn.add(state0Model); conn.add(state1.getModel()); conn.add(state1Model); conn.add(state2.getModel()); conn.add(state2Model); Map<Resource, Model> result = manager.getStates("test", "test", Collections.singleton(VALUE_FACTORY.createIRI("http: assertEquals(1, result.size()); assertTrue(result.containsKey(state2.getResource())); assertTrue(result.get(state2.getResource()).equals(state2Model)); conn.close(); }
@Test(expected = IllegalArgumentException.class) public void getStatesWithUserThatDoesNotExistTest() { when(engineManager.retrieveUser(anyString())).thenReturn(Optional.empty()); manager.getStates("error", null, new HashSet<>()); }
@Test(expected = IllegalArgumentException.class) public void getStatesWithApplicationThatDoesNotExistTest() { when(applicationManager.getApplication(anyString())).thenReturn(Optional.empty()); manager.getStates(null, "error", new HashSet<>()); }