public CompleteBean buid() { CompleteBean bean = new CompleteBean(); bean.setId(id); bean.setName(name); bean.setLabel(label); bean.setAge(age); bean.setFriends(friends); bean.setFollowers(followers); bean.setPreferences(preferences); return bean; }
@Test public void should_throw_exception_when_error_initializing() throws Exception { CompleteBean bean = new CompleteBean() { public Long getId() { throw new RuntimeException(); } }; PropertyMeta pm = new PropertyMeta(); pm.setType(PropertyType.LAZY_SIMPLE); pm.setGetter(bean.getClass().getMethod("getId")); EntityMeta entityMeta = new EntityMeta(); entityMeta.setPropertyMetas(ImmutableMap.<String, PropertyMeta>of("id", pm)); entityMeta.setGetterMetas(ImmutableMap.<Method, PropertyMeta>of(pm.getGetter(), pm)); when(interceptor.getAlreadyLoaded()).thenReturn(new HashSet<Method>()); exception.expect(AchillesException.class); initializer.initializeEntity(bean, entityMeta, interceptor); }
@Test public void should_initialize_entity() throws Exception { Class<? extends CompleteBean> beanClass = bean.getClass(); PropertyMeta nameMeta = new PropertyMeta(); nameMeta.setEntityClassName("beanClass"); nameMeta.setType(SIMPLE); nameMeta.setGetter(beanClass.getMethod("getName")); PropertyMeta friendsMeta = new PropertyMeta(); friendsMeta.setEntityClassName("beanClass"); friendsMeta.setType(LAZY_LIST); friendsMeta.setGetter(beanClass.getMethod("getFriends")); PropertyMeta followersMeta = new PropertyMeta(); followersMeta.setEntityClassName("beanClass"); followersMeta.setType(LAZY_SET); followersMeta.setGetter(beanClass.getMethod("getFollowers")); Set<Method> alreadyLoaded = Sets.newHashSet(friendsMeta.getGetter(), nameMeta.getGetter()); Map<Method, PropertyMeta> getterMetas = ImmutableMap.<Method, PropertyMeta>of( nameMeta.getGetter(), nameMeta, friendsMeta.getGetter(), friendsMeta, followersMeta.getGetter(), followersMeta); Map<String, PropertyMeta> allMetas = ImmutableMap.<String, PropertyMeta>of( "name", nameMeta, "friends", friendsMeta, "followers", followersMeta); EntityMeta entityMeta = new EntityMeta(); entityMeta.setPropertyMetas(allMetas); entityMeta.setGetterMetas(getterMetas); when(interceptor.getAlreadyLoaded()).thenReturn(alreadyLoaded); initializer.initializeEntity(bean, entityMeta, interceptor); assertThat(calledMethods).containsExactly("getFollowers"); }
@Test public void should_deserialize_from_json() throws Exception { // Given pmf.configContext = configContext; ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); when(configContext.getMapperFor(CompleteBean.class)).thenReturn(mapper); // When final CompleteBean actual = pmf.deserializeJson(CompleteBean.class, "{\"id\":10,\"name\":\"name\"}"); // Then assertThat(actual.getId()).isEqualTo(10L); assertThat(actual.getName()).isEqualTo("name"); assertThat(actual.getFriends()).isNull(); assertThat(actual.getFollowers()).isNull(); assertThat(actual.getPreferences()).isNull(); }