InMemoryPolicyEvaluationCache extends AbstractPolicyEvaluationCache { @Override void set(final String key, final String value) { if (isPolicySetChangedKey(key) || isResourceChangedKey(key) || isSubjectChangedKey(key) || isPolicyEvalResultKey(key)) { this.evalCache.put(key, value); } else { throw new IllegalArgumentException("Unsupported key format."); } }    }
@SuppressWarnings("unchecked") @Test public void testSetPolicyEvalResult() throws Exception { PolicyEvaluationRequestV1 request = new PolicyEvaluationRequestV1(); request.setAction(ACTION_GET); request.setSubjectIdentifier(AGENT_MULDER); request.setResourceIdentifier(XFILES_ID); PolicyEvaluationRequestCacheKey key = new PolicyEvaluationRequestCacheKey.Builder().zoneId(ZONE_NAME) .request(request).build(); PolicyEvaluationResult result = new PolicyEvaluationResult(Effect.PERMIT); String value = OBJECT_MAPPER.writeValueAsString(result); this.cache.set(key.toDecisionKey(), value); Map<String, String> evalCache = (Map<String, String>) getInternalState(this.cache, "evalCache"); assertEquals(evalCache.size(), 1); }
@SuppressWarnings("unchecked") @Test public void testSetPolicySetChangedTimestamp() throws Exception { String key = AbstractPolicyEvaluationCache.policySetKey(ZONE_NAME, "testSetPolicyPolicySetChangedTimestamp"); String value = OBJECT_MAPPER.writeValueAsString(new DateTime()); this.cache.set(key, value); Map<String, String> evalCache = (Map<String, String>) getInternalState(this.cache, "evalCache"); assertEquals(evalCache.size(), 1); }
@SuppressWarnings("unchecked") @Test public void testSetPolicyResourceChangedTimestamp() throws Exception { String key = AbstractPolicyEvaluationCache.resourceKey(ZONE_NAME, XFILES_ID); String value = OBJECT_MAPPER.writeValueAsString(new DateTime()); this.cache.set(key, value); Map<String, String> evalCache = (Map<String, String>) getInternalState(this.cache, "evalCache"); assertEquals(evalCache.size(), 1); }
@SuppressWarnings("unchecked") @Test public void testSetPolicySubjectChangedTimestamp() throws Exception { String key = AbstractPolicyEvaluationCache.subjectKey(ZONE_NAME, AGENT_MULDER); String value = OBJECT_MAPPER.writeValueAsString(new DateTime()); this.cache.set(key, value); Map<String, String> evalCache = (Map<String, String>) getInternalState(this.cache, "evalCache"); assertEquals(evalCache.size(), 1); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testSetUnsupportedKeyFormat() { this.cache.set("key", ""); }
@SuppressWarnings("unchecked") @Test public void testSetPolicyEvalResult() throws Exception { PolicyEvaluationRequestV1 request = new PolicyEvaluationRequestV1(); request.setAction(ACTION_GET); request.setSubjectIdentifier(AGENT_MULDER); request.setResourceIdentifier(XFILES_ID); PolicyEvaluationRequestCacheKey key = new PolicyEvaluationRequestCacheKey(request, ZONE_NAME); PolicyEvaluationResult result = new PolicyEvaluationResult(Effect.PERMIT); String value = OBJECT_MAPPER.writeValueAsString(result); this.cache.set(key.toDecisionKey(), value); Map<String, String> evalCache = (Map<String, String>) getInternalState(this.cache, "evalCache"); assertEquals(evalCache.size(), 1); }