SimpleStateManager implements StateManager { @Override public Resource storeState(Model newState, String username) { State state = createState(newState, username, stateFactory); try (RepositoryConnection conn = repository.getConnection()) { conn.add(state.getModel()); return state.getResource(); } }  @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 storeStateTest() throws Exception { Model newState = MODEL_FACTORY.createModel(); newState.add(VALUE_FACTORY.createIRI("http: Resource result = manager.storeState(newState, "test"); verify(engineManager, times(1)).retrieveUser(eq("test")); RepositoryConnection conn = repo.getConnection(); Model stateModel = MODEL_FACTORY.createModel(); conn.getStatements(result, null, null).forEach(stateModel::add); assertFalse(stateModel.isEmpty()); assertTrue(stateModel.contains(result, VALUE_FACTORY.createIRI(RDF.TYPE.stringValue()), VALUE_FACTORY.createIRI(State.TYPE))); assertTrue(stateModel.contains(result, VALUE_FACTORY.createIRI(State.forUser_IRI), user.getResource())); newState.forEach(statement -> { assertTrue(stateModel.contains(result, 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 storeStateWithUserThatDoesNotExistTest() { when(engineManager.retrieveUser(anyString())).thenReturn(Optional.empty()); manager.storeState(MODEL_FACTORY.createModel(), "error"); }
@Test public void storeApplicationStateTest() throws Exception { Model newState = MODEL_FACTORY.createModel(); newState.add(VALUE_FACTORY.createIRI("http: Resource result = manager.storeState(newState, "test", "test"); verify(engineManager, times(1)).retrieveUser(eq("test")); verify(applicationManager, times(1)).getApplication("test"); RepositoryConnection conn = repo.getConnection(); Model stateModel = MODEL_FACTORY.createModel(); conn.getStatements(result, null, null).forEach(stateModel::add); assertFalse(stateModel.isEmpty()); assertTrue(stateModel.contains(result, VALUE_FACTORY.createIRI(RDF.TYPE.stringValue()), VALUE_FACTORY.createIRI(ApplicationState.TYPE))); assertTrue(stateModel.contains(result, VALUE_FACTORY.createIRI(State.forUser_IRI), user.getResource())); assertTrue(stateModel.contains(result, VALUE_FACTORY.createIRI(ApplicationState.application_IRI), application.getResource())); newState.forEach(statement -> { assertTrue(stateModel.contains(result, 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 storeApplicationStateWithUserThatDoesNotExistTest() { when(engineManager.retrieveUser(anyString())).thenReturn(Optional.empty()); manager.storeState(MODEL_FACTORY.createModel(), "error", "test"); }
@Test(expected = IllegalArgumentException.class) public void storeApplicationStateWithApplicationThatDoesNotExistTest() { when(applicationManager.getApplication(anyString())).thenReturn(Optional.empty()); manager.storeState(MODEL_FACTORY.createModel(), "test", "error"); }