PublicUserHandler extends BaseHandler { public Optional<UserDto> registerAnonymously() { String path = String.format("/client/user/anonymous"); String hal = HalHmacResourceFactory.INSTANCE .newClientResource(locale, path) .post(String.class); ReadableRepresentation rr = unmarshalHal(hal); Map<String, Object> properties = rr.getProperties(); UserDto clientUser = new UserDto(); String apiKey = (String) properties.get("api_key"); String secretKey = (String) properties.get("secret_key"); if ("".equals(apiKey) || "".equals(secretKey)) { return Optional.absent(); } clientUser.setApiKey(apiKey); clientUser.setSecretKey(secretKey); clientUser.setCachedAuthorities(new Authority[]{Authority.ROLE_PUBLIC}); return Optional.of(clientUser); } PublicUserHandler(Locale locale); Optional<UserDto> registerWithWebForm(WebFormClientRegistration registration); Optional<UserDto> registerAnonymously();  }
@Test public void registerAnonymously() throws Exception { URI expectedUri = URI.create("http: when(client.resource(expectedUri)).thenReturn(webResource); when(webResource.post(String.class)).thenReturn( FixtureAsserts.jsonFixture("/fixtures/hal/user/expected-client-register-anonymous-user.json") ); Optional<UserDto> publicUser = PublicMerchantClient .newInstance(locale) .user() .registerAnonymously(); assertTrue("Expected user", publicUser.isPresent()); assertEquals("bob123",publicUser.get().getApiKey()); assertEquals("bob456",publicUser.get().getSecretKey()); }