SimpleStateManager implements StateManager { @Override public void deleteState(Resource stateId) { if (!stateExists(stateId)) { throw new IllegalArgumentException("State not found"); } try (RepositoryConnection conn = repository.getConnection()) { Model stateModel = modelFactory.createModel(); conn.getStatements(stateId, null, null).forEach(stateModel::add); stateFactory.getExisting(stateId, stateModel).ifPresent(state -> removeState(state, conn)); } }  @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 deleteStateTest() throws Exception { Model model = MODEL_FACTORY.createModel(); model.add(VALUE_FACTORY.createIRI("http: State state = stateFactory.createNew(VALUE_FACTORY.createIRI("http: state.setStateResource(Collections.singleton(VALUE_FACTORY.createIRI("http: RepositoryConnection conn = repo.getConnection(); conn.add(state.getModel()); conn.add(model); manager.deleteState(state.getResource()); Model stateModel = MODEL_FACTORY.createModel(); conn.getStatements(state.getResource(), null, null).forEach(stateModel::add); assertTrue(stateModel.isEmpty()); model.forEach(statement -> assertFalse(conn.getStatements(statement.getSubject(), statement.getPredicate(), statement.getObject()).hasNext())); conn.close(); }
@Test public void deleteStateWithResourcesUsedForAnotherStateTest() throws Exception { Model model = MODEL_FACTORY.createModel(); model.add(VALUE_FACTORY.createIRI("http: State state0 = stateFactory.createNew(VALUE_FACTORY.createIRI("http: State state1 = stateFactory.createNew(VALUE_FACTORY.createIRI("http: state0.setStateResource(Collections.singleton(VALUE_FACTORY.createIRI("http: state1.setStateResource(Collections.singleton(VALUE_FACTORY.createIRI("http: RepositoryConnection conn = repo.getConnection(); conn.add(state0.getModel()); conn.add(state1.getModel()); conn.add(model); manager.deleteState(state0.getResource()); Model stateModel = MODEL_FACTORY.createModel(); conn.getStatements(state0.getResource(), null, null).forEach(stateModel::add); assertTrue(stateModel.isEmpty()); model.forEach(statement -> assertTrue(conn.getStatements(statement.getSubject(), statement.getPredicate(), statement.getObject()).hasNext())); conn.close(); }
@Test public void deleteStateThatWasEmptyTest() throws Exception { State state = stateFactory.createNew(VALUE_FACTORY.createIRI("http: RepositoryConnection conn = repo.getConnection(); conn.add(state.getModel()); manager.deleteState(state.getResource()); Model stateModel = MODEL_FACTORY.createModel(); conn.getStatements(state.getResource(), null, null).forEach(stateModel::add); assertTrue(stateModel.isEmpty()); conn.close(); }
@Test(expected = IllegalArgumentException.class) public void deleteStateThatDoesNotExist() { manager.deleteState(VALUE_FACTORY.createIRI("http: }