RepositoryMethodArgumentResolver implements HandlerMethodArgumentResolver { @Override public Object resolveArgument(final MethodParameter parameter, final ModelAndViewContainer modelAndViewContainer, final NativeWebRequest nativeWebRequest, final WebDataBinderFactory webDataBinderFactory) throws MissingPathVariableException { final RepositoryMapping repositoryMapping = parameter.getParameterAnnotation(RepositoryMapping.class); final String storageVariableName = repositoryMapping.storageVariableName(); final String storageId = getRequiredPathVariable(parameter, nativeWebRequest, storageVariableName); final String repositoryVariableName = repositoryMapping.repositoryVariableName(); final String repositoryId = getRequiredPathVariable(parameter, nativeWebRequest, repositoryVariableName); Repository repository = (Repository) nativeWebRequest.getAttribute(REPOSITORY_REQUEST_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); if (repository != null && Objects.equals(repository.getId(), repositoryId) && Objects.equals(repository.getStorage().getId(), storageId)) { return repository; } final Storage storage = getStorage(storageId); if (storage == null) { final String message = String.format(NOT_FOUND_STORAGE_MESSAGE, storageId); throw new StorageNotFoundException(message); } repository = getRepository(storageId, repositoryId); if (repository == null) { final String message = String.format(NOT_FOUND_REPOSITORY_MESSAGE, storageId, repositoryId); throw new RepositoryNotFoundException(message); } if (!repository.isInService() && !repositoryMapping.allowOutOfServiceRepository()) { final String message = String.format(NOT_IN_SERVICE_REPOSITORY_MESSAGE, storageId, repositoryId); throw new ServiceUnavailableException(message); } return repository; }  @Override boolean supportsParameter(final MethodParameter parameter); @Override Object resolveArgument(final MethodParameter parameter,
                                  final ModelAndViewContainer modelAndViewContainer,
                                  final NativeWebRequest nativeWebRequest,
                                  final WebDataBinderFactory webDataBinderFactory); static final String NOT_FOUND_STORAGE_MESSAGE; static final String NOT_FOUND_REPOSITORY_MESSAGE; static final String NOT_IN_SERVICE_REPOSITORY_MESSAGE; }
@Test public void testResolveArgument() throws MissingPathVariableException { Map<String, String> uriTemplateVars = new HashMap<>(); uriTemplateVars.put("storageId", "storage-pypi"); uriTemplateVars.put("repositoryId", "pypi-releases"); nativeWebRequest.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars, RequestAttributes.SCOPE_REQUEST); mockStoragesAndRepositories(RepositoryStatusEnum.OUT_OF_SERVICE); MethodParameter uploadArtifactWithOutOfServiceRepositoryAllowedMethod = getMethodParameter("uploadArtifactWithOutOfServiceRepositoryAllowed", Repository.class); Repository repository = (Repository) repositoryMethodArgumentResolver.resolveArgument(uploadArtifactWithOutOfServiceRepositoryAllowedMethod, modelAndViewContainer, nativeWebRequest, webDataBinderFactory); assertNotNull(repository); mockStoragesAndRepositories(RepositoryStatusEnum.IN_SERVICE); repository = (Repository) repositoryMethodArgumentResolver.resolveArgument(uploadArtifactWithOutOfServiceRepositoryAllowedMethod, modelAndViewContainer, nativeWebRequest, webDataBinderFactory); assertNotNull(repository); mockStoragesAndRepositories(RepositoryStatusEnum.IN_SERVICE); MethodParameter uploadArtifactWithOutOfServiceRepositoryNotAllowedMethod = getMethodParameter("uploadArtifactWithOutOfServiceRepositoryNotAllowed", Repository.class); repository = (Repository) repositoryMethodArgumentResolver.resolveArgument(uploadArtifactWithOutOfServiceRepositoryNotAllowedMethod, modelAndViewContainer, nativeWebRequest, webDataBinderFactory); assertNotNull(repository); }
@Test public void testResolveArgumentThrowingServiceUnavailableException() throws MissingPathVariableException { Map<String, String> uriTemplateVars = new HashMap<>(); uriTemplateVars.put("storageId", "storage-pypi"); uriTemplateVars.put("repositoryId", "pypi-releases"); nativeWebRequest.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars, RequestAttributes.SCOPE_REQUEST); mockStoragesAndRepositories(RepositoryStatusEnum.OUT_OF_SERVICE); MethodParameter uploadArtifactWithOutOfServiceRepositoryNotAllowedMethod = getMethodParameter("uploadArtifactWithOutOfServiceRepositoryNotAllowed", Repository.class); ServiceUnavailableException serviceUnavailableException = Assertions.assertThrows(ServiceUnavailableException.class, () -> repositoryMethodArgumentResolver.resolveArgument(uploadArtifactWithOutOfServiceRepositoryNotAllowedMethod, modelAndViewContainer, nativeWebRequest, webDataBinderFactory)); assertEquals(String.format(RepositoryMethodArgumentResolver.NOT_IN_SERVICE_REPOSITORY_MESSAGE, uriTemplateVars.get("storageId"), uriTemplateVars.get("repositoryId")), serviceUnavailableException.getMessage()); }
@Test public void testResolveArgumentThrowingStorageNotFoundException() throws MissingPathVariableException { Map<String, String> uriTemplateVars = new HashMap<>(); uriTemplateVars.put("storageId", "storage-nuget"); uriTemplateVars.put("repositoryId", "pypi-releases"); nativeWebRequest.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars, RequestAttributes.SCOPE_REQUEST); mockStoragesAndRepositories(RepositoryStatusEnum.IN_SERVICE); MethodParameter uploadArtifactWithOutOfServiceRepositoryNotAllowedMethod = getMethodParameter("uploadArtifactWithOutOfServiceRepositoryNotAllowed", Repository.class); StorageNotFoundException storageNotFoundException = Assertions.assertThrows(StorageNotFoundException.class, () -> repositoryMethodArgumentResolver.resolveArgument(uploadArtifactWithOutOfServiceRepositoryNotAllowedMethod, modelAndViewContainer, nativeWebRequest, webDataBinderFactory)); assertEquals(String.format(RepositoryMethodArgumentResolver.NOT_FOUND_STORAGE_MESSAGE, uriTemplateVars.get("storageId")), storageNotFoundException.getMessage()); mockStoragesAndRepositories(RepositoryStatusEnum.OUT_OF_SERVICE); storageNotFoundException = Assertions.assertThrows(StorageNotFoundException.class, () -> repositoryMethodArgumentResolver.resolveArgument(uploadArtifactWithOutOfServiceRepositoryNotAllowedMethod, modelAndViewContainer, nativeWebRequest, webDataBinderFactory)); assertEquals(String.format(RepositoryMethodArgumentResolver.NOT_FOUND_STORAGE_MESSAGE, uriTemplateVars.get("storageId")), storageNotFoundException.getMessage()); }
@Test public void testResolveArgumentThrowingRepositoryNotFoundException() throws MissingPathVariableException { Map<String, String> uriTemplateVars = new HashMap<>(); uriTemplateVars.put("storageId", "storage-pypi"); uriTemplateVars.put("repositoryId", "maven-releases"); nativeWebRequest.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars, RequestAttributes.SCOPE_REQUEST); mockStoragesAndRepositories(RepositoryStatusEnum.IN_SERVICE); MethodParameter uploadArtifactWithOutOfServiceRepositoryNotAllowedMethod = getMethodParameter("uploadArtifactWithOutOfServiceRepositoryNotAllowed", Repository.class); RepositoryNotFoundException repositoryNotFoundException = Assertions.assertThrows(RepositoryNotFoundException.class, () -> repositoryMethodArgumentResolver.resolveArgument(uploadArtifactWithOutOfServiceRepositoryNotAllowedMethod, modelAndViewContainer, nativeWebRequest, webDataBinderFactory)); assertEquals(String.format(RepositoryMethodArgumentResolver.NOT_FOUND_REPOSITORY_MESSAGE, uriTemplateVars.get("storageId"), uriTemplateVars.get("repositoryId")), repositoryNotFoundException.getMessage()); mockStoragesAndRepositories(RepositoryStatusEnum.OUT_OF_SERVICE); repositoryNotFoundException = Assertions.assertThrows(RepositoryNotFoundException.class, () -> repositoryMethodArgumentResolver.resolveArgument(uploadArtifactWithOutOfServiceRepositoryNotAllowedMethod, modelAndViewContainer, nativeWebRequest, webDataBinderFactory)); assertEquals(String.format(RepositoryMethodArgumentResolver.NOT_FOUND_REPOSITORY_MESSAGE, uriTemplateVars.get("storageId"), uriTemplateVars.get("repositoryId")), repositoryNotFoundException.getMessage()); }