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