DefaultFactoryParameterResolver implements FactoryParametersResolver { @Override public FactoryDto createFactory(@NotNull final Map<String, String> factoryParameters) throws BadRequestException, ServerException { String devfileLocation = factoryParameters.get(URL_PARAMETER_NAME); URI devfileURI; try { devfileURI = new URL(devfileLocation).toURI(); } catch (MalformedURLException | URISyntaxException e) { throw new BadRequestException( format( "Unable to process provided factory URL. Please check its validity and try again. Parser message: %s", e.getMessage())); } return urlFactoryBuilder .createFactoryFromDevfile( new DefaultFactoryUrl().withDevfileFileLocation(devfileLocation), new URLFileContentProvider(devfileURI, urlFetcher), extractOverrideParams(factoryParameters)) .orElse(null); } @Inject  DefaultFactoryParameterResolver(
      URLFactoryBuilder urlFactoryBuilder, URLFetcher urlFetcher); @Override boolean accept(Map<String, String> factoryParameters); @Override FactoryDto createFactory(@NotNull final Map<String, String> factoryParameters);  }
@Test public void shouldResolveRelativeFiles() throws Exception { DevfileSchemaValidator validator = new DevfileSchemaValidator(new DevfileSchemaProvider()); Map<String, ComponentIntegrityValidator> validators = new HashMap<>(); validators.put(EDITOR_COMPONENT_TYPE, new NoopComponentIntegrityValidator()); validators.put(PLUGIN_COMPONENT_TYPE, new NoopComponentIntegrityValidator()); validators.put(KUBERNETES_COMPONENT_TYPE, new NoopComponentIntegrityValidator()); validators.put(OPENSHIFT_COMPONENT_TYPE, new NoopComponentIntegrityValidator()); DevfileIntegrityValidator integrityValidator = new DevfileIntegrityValidator(validators); DevfileParser devfileParser = new DevfileParser(validator, integrityValidator); URLFactoryBuilder factoryBuilder = new URLFactoryBuilder("editor", "plugin", urlFetcher, devfileParser); DefaultFactoryParameterResolver res = new DefaultFactoryParameterResolver(factoryBuilder, urlFetcher); Map<String, String> factoryParameters = new HashMap<>(); factoryParameters.put(URL_PARAMETER_NAME, "http: doReturn(DEVFILE).when(urlFetcher).fetchSafely(eq("http: doReturn("localfile").when(urlFetcher).fetch("http: res.createFactory(factoryParameters); verify(urlFetcher).fetch(eq("http: }
@Test @SuppressWarnings("unchecked") public void shouldFilterAndProvideOverrideParameters() throws Exception { URLFactoryBuilder urlFactoryBuilder = mock(URLFactoryBuilder.class); URLFetcher urlFetcher = mock(URLFetcher.class); DefaultFactoryParameterResolver res = new DefaultFactoryParameterResolver(urlFactoryBuilder, urlFetcher); Map<String, String> factoryParameters = new HashMap<>(); factoryParameters.put(URL_PARAMETER_NAME, "http: factoryParameters.put("override.param.foo", "bar"); factoryParameters.put("override.param.bar", "foo"); factoryParameters.put("ignored.non-override.property", "baz"); ArgumentCaptor<Map<String, String>> captor = ArgumentCaptor.forClass(Map.class); res.createFactory(factoryParameters); verify(urlFactoryBuilder) .createFactoryFromDevfile( any(RemoteFactoryUrl.class), any(URLFileContentProvider.class), captor.capture()); Map<String, String> filteredOverrides = captor.getValue(); assertEquals(2, filteredOverrides.size()); assertEquals("bar", filteredOverrides.get("param.foo")); assertEquals("foo", filteredOverrides.get("param.bar")); assertFalse(filteredOverrides.containsKey("ignored.non-override.property")); }
@Test(dataProvider = "invalidURLsProvider") public void shouldThrowExceptionOnInvalidURL(String url, String message) throws Exception { URLFactoryBuilder urlFactoryBuilder = mock(URLFactoryBuilder.class); URLFetcher urlFetcher = mock(URLFetcher.class); DefaultFactoryParameterResolver res = new DefaultFactoryParameterResolver(urlFactoryBuilder, urlFetcher); Map<String, String> factoryParameters = new HashMap<>(); factoryParameters.put(URL_PARAMETER_NAME, url); try { res.createFactory(factoryParameters); fail("Exception is expected"); } catch (BadRequestException e) { assertEquals( e.getMessage(), format( "Unable to process provided factory URL. Please check its validity and try again. Parser message: %s", message)); } }