PermissionModelDataMapper { public Permission transformInverse(PermissionModel permissionModel) { if (permissionModel == null) { throw new IllegalArgumentException("Cannot transform a null PermissionModel object."); } return Permission.builder() .permission(permissionModel.permission()) .rationaleTitle(permissionModel.rationaleTitle()) .rationaleMessage(permissionModel.rationaleMessage()) .deniedMessage(permissionModel.deniedMessage()) .build(); } @Inject  PermissionModelDataMapper(); PermissionModel transform(Permission permission); Collection<PermissionModel> transform(Collection<Permission> permissionCollection); Permission transformInverse(PermissionModel permissionModel); Collection<Permission> transformInverse(Collection<PermissionModel> permissionModels);  }
@Test public void shouldProperlyMapPermissionModelCollectionIntoPermissionCollection() throws Exception { PermissionModel permissionModel1 = PermissionModel.builder() .permission("CAMERA") .rationaleTitle("Permission Request") .rationaleMessage("You should allow it") .deniedMessage("Allow it!") .build(); PermissionModel permissionModel2 = PermissionModel.builder() .permission("CONTACTS") .rationaleTitle("Permission Request") .rationaleMessage("You should allow it, please") .deniedMessage("ALLOW IT!!") .build(); ArrayList<PermissionModel> permissionModels = new ArrayList<>(); permissionModels.add(permissionModel1); permissionModels.add(permissionModel2); Collection<Permission> permissions = permissionModelDataMapper.transformInverse(permissionModels); assertThat(permissions, notNullValue()); assertThat(permissions.size(), is(2)); Permission permission1 = (Permission) permissions.toArray()[0]; assertThat(permission1, notNullValue()); assertThat(permission1, instanceOf(Permission.class)); assertThat(permission1.permission(), notNullValue()); assertThat(permission1.permission(), is("CAMERA")); Permission permission2 = (Permission) permissions.toArray()[1]; assertThat(permission2, notNullValue()); assertThat(permission2, instanceOf(Permission.class)); assertThat(permission2.permission(), notNullValue()); assertThat(permission2.permission(), is("CONTACTS")); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionGivenNullPermissionModel() throws Exception { PermissionModel permissionModel = null; permissionModelDataMapper.transformInverse(permissionModel); }
@Test public void shouldProperlyMapPermissionModelIntoPermission() throws Exception { PermissionModel permissionModel = PermissionModel.builder() .permission("CAMERA") .rationaleTitle("Permission Request") .rationaleMessage("You should allow it") .deniedMessage("Allow it!") .build(); Permission permission = permissionModelDataMapper.transformInverse(permissionModel); assertThat(permission, notNullValue()); assertThat(permission, instanceOf(Permission.class)); assertThat(permission.permission(), notNullValue()); assertThat(permission.permission(), is("CAMERA")); assertThat(permission.rationaleTitle(), notNullValue()); assertThat(permission.rationaleTitle(), is("Permission Request")); assertThat(permission.rationaleMessage(), notNullValue()); assertThat(permission.rationaleMessage(), is("You should allow it")); assertThat(permission.deniedMessage(), notNullValue()); assertThat(permission.deniedMessage(), is("Allow it!")); }