### Question:
JCPermissionFactory implements PermissionFactory { @Override public Permission buildFromMask(int mask) { Permission permission = jtalksPermissionFactory.buildFromMask(mask); if (permission == null) { permission = pluginPermissionFactory.buildFromMask(mask); } if (permission == null) { permission = NullPermission.NULL_PERMISSION; } return permission; } JCPermissionFactory(JtalksPermissionFactory jtalksPermissionFactory, PluginsPermissionFactory pluginPermissionFactory); @Override Permission buildFromMask(int mask); @Override Permission buildFromName(String name); @Override List<Permission> buildFromNames(List<String> names);  }### Answer:
@Test public void buildFromMaskShouldReturnPermissionFromCommons() { Permission targetPermission = BranchPermission.VIEW_TOPICS; when(jtalksPermissionFactory.buildFromMask(targetPermission.getMask())).thenReturn(targetPermission); JCPermissionFactory permissionFactory = new JCPermissionFactory(jtalksPermissionFactory,pluginsPermissionFactory); Permission result = permissionFactory.buildFromMask(targetPermission.getMask()); assertEquals(result, targetPermission); verify(pluginsPermissionFactory, never()).buildFromMask(anyInt()); }
@Test public void buildFromMaskShouldReturnPluginPermissionWhenCommonNotFound() { Permission targetPermission = BranchPermission.VIEW_TOPICS; when(jtalksPermissionFactory.buildFromMask(anyInt())).thenReturn(null); when(pluginsPermissionFactory.buildFromMask(targetPermission.getMask())).thenReturn(targetPermission); JCPermissionFactory permissionFactory = new JCPermissionFactory(jtalksPermissionFactory,pluginsPermissionFactory); Permission result = permissionFactory.buildFromMask(targetPermission.getMask()); assertEquals(result, targetPermission); verify(jtalksPermissionFactory).buildFromMask(targetPermission.getMask()); }
@Test public void buildFromMaskShouldReturnAnyNotNullDefaultValueWhenNoPermissionFound() { Permission targetPermission = BranchPermission.VIEW_TOPICS; when(jtalksPermissionFactory.buildFromMask(anyInt())).thenReturn(null); when(pluginsPermissionFactory.buildFromMask(anyInt())).thenReturn(null); JCPermissionFactory permissionFactory = new JCPermissionFactory(jtalksPermissionFactory,pluginsPermissionFactory); Permission result = permissionFactory.buildFromMask(targetPermission.getMask()); assertNotNull(result); verify(jtalksPermissionFactory).buildFromMask(targetPermission.getMask()); verify(pluginsPermissionFactory).buildFromMask(targetPermission.getMask()); }