VersionPolicyHandler extends ComponentSupport implements Handler { @Nonnull @Override public Response handle(@Nonnull final Context context) throws Exception { final MavenPath path = context.getAttributes().require(MavenPath.class); final MavenFacet mavenFacet = context.getRepository().facet(MavenFacet.class); final VersionPolicy versionPolicy = mavenFacet.getVersionPolicy(); final Coordinates coordinates = path.getCoordinates(); if (coordinates != null && !versionPolicyValidator.validArtifactPath(versionPolicy, coordinates)) { return createResponse(context, "Repository version policy: " + versionPolicy + " does not allow version: " + coordinates.getVersion()); } if (!versionPolicyValidator.validMetadataPath(versionPolicy, path.main().getPath())) { return createResponse(context, "Repository version policy: " + versionPolicy + " does not allow metadata in path: " + path.getPath()); } return context.proceed(); } @Inject  VersionPolicyHandler(final VersionPolicyValidator versionPolicyValidator); @Nonnull @Override Response handle(@Nonnull final Context context);  }
@Test public void testScenario() throws Exception { when(context.getRequest()).thenReturn(request); when(request.getAction()).thenReturn(httpMethod); when(context.getRepository()).thenReturn(repository); when(repository.facet(MavenFacet.class)).thenReturn(mavenFacet); when(mavenFacet.getVersionPolicy()).thenReturn(policy); AttributesMap attributes = new AttributesMap(); attributes.set(MavenPath.class, mavenPathParser.parsePath(path)); when(context.getAttributes()).thenReturn(attributes); if (shouldProceed) { when(context.proceed()).thenReturn(proceeded); } Response response = underTest.handle(context); if (shouldProceed) { assertThat(response, is(proceeded)); } else { assertThat(response, not(proceeded)); assertThat(response.getStatus().getCode(), is(status)); } }