### Question:
AcceptPolicyHandler implements ContextualRequestMessageHandler<Person> { @Override public MessageBody handleRequest(Person context, PlatformMessage msg) { if(context == null) { throw new NotFoundException(msg.getDestination()); } Errors.assertValidRequest( Objects.equal(msg.getActor(), msg.getDestination()), "Only the same user can accept new policy terms" ); MessageBody body = msg.getValue(); validate(body); String policyType = PersonCapability.AcceptPolicyRequest.getType(body); if(PersonCapability.AcceptPolicyRequest.TYPE_PRIVACY.equals(policyType)) { context.setPrivacyPolicyAgreed(new Date()); }else if(PersonCapability.AcceptPolicyRequest.TYPE_TERMS.equals(policyType)) { context.setTermsAgreed(new Date()); }else { throw new ErrorEventException(Errors.CODE_INVALID_REQUEST, "policy type " + policyType + " is not valid."); } personDao.update(context); return PersonCapability.AcceptPolicyResponse.instance(); } @Inject  AcceptPolicyHandler(
         PersonDAO personDao,
         BeanAttributesTransformer<Person> personTransformer,
         PlatformMessageBus bus); @Override String getMessageType(); @Override MessageBody handleRequest(Person context, PlatformMessage msg);  }### Answer:
@Test public void testRejectPolicyPrivacy() throws Exception { EasyMock .expect(mockPersonDao.update(person)) .andReturn(person); replay(); MessageBody request = PersonCapability.RejectPolicyRequest.builder() .withType(PersonCapability.RejectPolicyRequest.TYPE_PRIVACY) .build(); MessageBody response = handler.handleRequest(person, request(request)); assertEquals(MessageBody.emptyMessage(), response); assertEquals(REJECTED_DATE, person.getPrivacyPolicyAgreed()); assertEquals(ACCEPTANCE_DATE, person.getTermsAgreed()); verify(); }
@Test public void testRejectPolicyTerms() throws Exception { EasyMock .expect(mockPersonDao.update(person)) .andReturn(person); replay(); MessageBody request = PersonCapability.RejectPolicyRequest.builder() .withType(PersonCapability.RejectPolicyRequest.TYPE_TERMS) .build(); MessageBody response = handler.handleRequest(person, request(request)); assertEquals(MessageBody.emptyMessage(), response); assertEquals(ACCEPTANCE_DATE, person.getPrivacyPolicyAgreed()); assertEquals(REJECTED_DATE, person.getTermsAgreed()); verify(); }
@Test public void testMissingParam() throws Exception { replay(); MessageBody request = PersonCapability.RejectPolicyRequest.builder() .build(); try { handler.handleRequest(person, request(request)); fail(); } catch(ErrorEventException e) { assertEquals(Errors.CODE_MISSING_PARAM, e.getCode()); } verify(); }
@Test public void testWithMissingActor() throws Exception { replay(); MessageBody request = PersonCapability.RejectPolicyRequest.builder() .build(); PlatformMessage message = PlatformMessage .buildRequest(request, clientAddress, personAddress) .create(); try { handler.handleRequest(person, message); fail(); } catch(ErrorEventException e) { assertEquals(Errors.CODE_INVALID_REQUEST, e.getCode()); } verify(); }
@Test public void testWithWrongActor() throws Exception { replay(); MessageBody request = PersonCapability.RejectPolicyRequest.builder() .build(); PlatformMessage message = PlatformMessage .buildRequest(request, clientAddress, personAddress) .withActor(clientAddress) .create(); try { handler.handleRequest(person, message); fail(); } catch(ErrorEventException e) { assertEquals(Errors.CODE_INVALID_REQUEST, e.getCode()); } verify(); }
@Test @Ignore public void testInvalidParam() throws Exception { replay(); MessageBody request = PersonCapability.RejectPolicyRequest.builder() .withType("SomeBogusStuff") .build(); try { handler.handleRequest(person, request(request)); fail(); } catch(ErrorEventException e) { assertEquals(Errors.CODE_INVALID_PARAM, e.getCode()); } verify(); }