SimpleStateManager implements StateManager { @Override public void updateState(Resource stateId, Model newState) throws MobiException { if (!stateExists(stateId)) { throw new IllegalArgumentException("State not found"); } try (RepositoryConnection conn = repository.getConnection()) { Model stateModel = modelFactory.createModel(newState); conn.getStatements(stateId, null, null).forEach(stateModel::add); stateFactory.getExisting(stateId, stateModel).ifPresent(state -> { removeState(state, conn); state.setStateResource(newState.subjects()); conn.add(state.getModel()); }); } }  @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 updateStateTest() throws Exception { Model oldModel = MODEL_FACTORY.createModel(); Model newModel = MODEL_FACTORY.createModel(); oldModel.add(VALUE_FACTORY.createIRI("http: newModel.add(VALUE_FACTORY.createIRI("http: State state = stateFactory.createNew(VALUE_FACTORY.createIRI("http: state.setStateResource(stateResource0); RepositoryConnection conn = repo.getConnection(); conn.add(state.getModel()); conn.add(oldModel); manager.updateState(state.getResource(), newModel); Model stateModel = MODEL_FACTORY.createModel(); conn.getStatements(state.getResource(), null, null).forEach(stateModel::add); assertFalse(stateModel.isEmpty()); oldModel.forEach(statement -> { assertFalse(stateModel.contains(state.getResource(), VALUE_FACTORY.createIRI(State.stateResource_IRI), statement.getSubject())); assertFalse(conn.getStatements(statement.getSubject(), statement.getPredicate(), statement.getObject()).hasNext()); }); newModel.forEach(statement -> { assertTrue(stateModel.contains(state.getResource(), VALUE_FACTORY.createIRI(State.stateResource_IRI), statement.getSubject())); assertTrue(conn.getStatements(statement.getSubject(), statement.getPredicate(), statement.getObject()).hasNext()); }); conn.close(); }
@Test public void updateStateWithResourcesUsedForAnotherStateTest() throws Exception { Model oldModel = MODEL_FACTORY.createModel(); Model newModel = MODEL_FACTORY.createModel(); oldModel.add(VALUE_FACTORY.createIRI("http: newModel.add(VALUE_FACTORY.createIRI("http: State state0 = stateFactory.createNew(VALUE_FACTORY.createIRI("http: State state1 = stateFactory.createNew(VALUE_FACTORY.createIRI("http: state0.setStateResource(stateResource0); state1.setStateResource(stateResource0); RepositoryConnection conn = repo.getConnection(); conn.add(state0.getModel()); conn.add(state1.getModel()); conn.add(oldModel); manager.updateState(state0.getResource(), newModel); Model stateModel = MODEL_FACTORY.createModel(); conn.getStatements(state0.getResource(), null, null).forEach(stateModel::add); assertFalse(stateModel.isEmpty()); oldModel.forEach(statement -> { assertFalse(stateModel.contains(state0.getResource(), VALUE_FACTORY.createIRI(State.stateResource_IRI), statement.getSubject())); assertTrue(conn.getStatements(statement.getSubject(), statement.getPredicate(), statement.getObject()).hasNext()); }); newModel.forEach(statement -> { assertTrue(stateModel.contains(state0.getResource(), VALUE_FACTORY.createIRI(State.stateResource_IRI), statement.getSubject())); assertTrue(conn.getStatements(statement.getSubject(), statement.getPredicate(), statement.getObject()).hasNext()); }); conn.close(); }
@Test public void updateStateThatWasEmptyTest() throws Exception { Model newModel = MODEL_FACTORY.createModel(); newModel.add(VALUE_FACTORY.createIRI("http: State state = stateFactory.createNew(VALUE_FACTORY.createIRI("http: RepositoryConnection conn = repo.getConnection(); conn.add(state.getModel()); manager.updateState(state.getResource(), newModel); Model stateModel = MODEL_FACTORY.createModel(); conn.getStatements(state.getResource(), null, null).forEach(stateModel::add); assertFalse(stateModel.isEmpty()); newModel.forEach(statement -> { assertTrue(stateModel.contains(state.getResource(), VALUE_FACTORY.createIRI(State.stateResource_IRI), statement.getSubject())); assertTrue(conn.getStatements(statement.getSubject(), statement.getPredicate(), statement.getObject()).hasNext()); }); conn.close(); }
@Test(expected = IllegalArgumentException.class) public void updateStateThatDoesNotExist() { manager.updateState(VALUE_FACTORY.createIRI("http: }