### Question:
ComposerHostedDownloadHandler implements Handler { @Nonnull @Override public Response handle(@Nonnull final Context context) throws Exception { Repository repository = context.getRepository(); ComposerHostedFacet hostedFacet = repository.facet(ComposerHostedFacet.class); AssetKind assetKind = context.getAttributes().require(AssetKind.class); switch (assetKind) { case PACKAGES: return HttpResponses.ok(hostedFacet.getPackagesJson()); case LIST: throw new IllegalStateException("Unsupported assetKind: " + assetKind); case PROVIDER: return HttpResponses.ok(hostedFacet.getProviderJson(getVendorToken(context), getProjectToken(context))); case ZIPBALL: return responseFor(hostedFacet.getZipball(buildZipballPath(context))); default: throw new IllegalStateException("Unexpected assetKind: " + assetKind); } }  @Nonnull @Override Response handle(@Nonnull final Context context);  }### Answer:
@Test public void testHandlePackages() throws Exception { when(attributes.require(AssetKind.class)).thenReturn(PACKAGES); when(composerHostedFacet.getPackagesJson()).thenReturn(content); Response response = underTest.handle(context); assertThat(response.getStatus().getCode(), is(200)); assertThat(response.getPayload(), is(content)); }
@Test public void testHandleProvider() throws Exception { when(attributes.require(AssetKind.class)).thenReturn(PROVIDER); when(tokens.get(VENDOR_TOKEN)).thenReturn(VENDOR); when(tokens.get(PROJECT_TOKEN)).thenReturn(PROJECT); when(composerHostedFacet.getProviderJson(VENDOR, PROJECT)).thenReturn(content); Response response = underTest.handle(context); assertThat(response.getStatus().getCode(), is(200)); assertThat(response.getPayload(), is(content)); }
@Test public void testHandleList() throws Exception { exception.expect(IllegalStateException.class); exception.expectMessage("Unsupported assetKind: " + LIST); when(attributes.require(AssetKind.class)).thenReturn(LIST); underTest.handle(context); }
@Test public void testHandleZipballPresent() throws Exception { when(attributes.require(AssetKind.class)).thenReturn(ZIPBALL); when(tokens.get(VENDOR_TOKEN)).thenReturn(VENDOR); when(tokens.get(PROJECT_TOKEN)).thenReturn(PROJECT); when(tokens.get(VERSION_TOKEN)).thenReturn(VERSION); when(tokens.get(NAME_TOKEN)).thenReturn(NAME); when(composerHostedFacet.getZipball(ZIPBALL_PATH)).thenReturn(content); Response response = underTest.handle(context); assertThat(response.getStatus().getCode(), is(200)); assertThat(response.getPayload(), is(content)); }
@Test public void testHandleZipballAbsent() throws Exception { when(attributes.require(AssetKind.class)).thenReturn(ZIPBALL); when(tokens.get(VENDOR_TOKEN)).thenReturn(VENDOR); when(tokens.get(PROJECT_TOKEN)).thenReturn(PROJECT); when(tokens.get(VERSION_TOKEN)).thenReturn(VERSION); when(tokens.get(NAME_TOKEN)).thenReturn(NAME); when(composerHostedFacet.getZipball(ZIPBALL_PATH)).thenReturn(null); Response response = underTest.handle(context); assertThat(response.getStatus().getCode(), is(404)); assertThat(response.getPayload(), is(nullValue())); }