URLFactoryBuilder { public Optional<FactoryDto> createFactoryFromDevfile( RemoteFactoryUrl remoteFactoryUrl, FileContentProvider fileContentProvider, Map<String, String> overrideProperties) throws BadRequestException { for (DevfileLocation location : remoteFactoryUrl.devfileFileLocations()) { String devfileYamlContent = urlFetcher.fetchSafely(location.location()); if (isNullOrEmpty(devfileYamlContent)) { continue; } try { DevfileImpl devfile = devfileParser.parseYaml(devfileYamlContent, overrideProperties); devfileParser.resolveReference(devfile, fileContentProvider); devfile = ensureToUseGenerateName(devfile); FactoryDto factoryDto = newDto(FactoryDto.class) .withV(CURRENT_VERSION) .withDevfile(DtoConverter.asDto(devfile)) .withSource(location.filename().isPresent() ? location.filename().get() : null); return Optional.of(factoryDto); } catch (DevfileException | OverrideParameterException e) { throw new BadRequestException( "Error occurred during creation a workspace from devfile located at `" + location.location() + "`. Cause: " + e.getMessage()); } } return Optional.empty(); } @Inject  URLFactoryBuilder(
      @Named("che.factory.default_editor") String defaultCheEditor,
      @Named("che.factory.default_plugins") String defaultChePlugins,
      URLFetcher urlFetcher,
      DevfileParser devfileParser); Optional<FactoryDto> createFactoryFromJson(RemoteFactoryUrl remoteFactoryUrl); Optional<FactoryDto> createFactoryFromDevfile(
      RemoteFactoryUrl remoteFactoryUrl,
      FileContentProvider fileContentProvider,
      Map<String, String> overrideProperties); WorkspaceConfigDto buildDefaultWorkspaceConfig(String name); DevfileDto buildDefaultDevfile(String name);  }
@Test public void checkWithCustomDevfileAndRecipe() throws Exception { DevfileImpl devfile = new DevfileImpl(); WorkspaceConfigImpl workspaceConfigImpl = new WorkspaceConfigImpl(); String myLocation = "http: RecipeImpl expectedRecipe = new RecipeImpl(KUBERNETES_COMPONENT_TYPE, "application/x-yaml", "content", ""); EnvironmentImpl expectedEnv = new EnvironmentImpl(expectedRecipe, emptyMap()); workspaceConfigImpl.setEnvironments(singletonMap("name", expectedEnv)); workspaceConfigImpl.setDefaultEnv("name"); when(urlFetcher.fetchSafely(anyString())).thenReturn("random_content"); when(devfileParser.parseYaml(anyString(), anyMap())).thenReturn(devfile); FactoryDto factory = urlFactoryBuilder .createFactoryFromDevfile( new DefaultFactoryUrl().withDevfileFileLocation(myLocation), s -> myLocation + ".list", emptyMap()) .get(); assertNotNull(factory); assertNull(factory.getSource()); }
@Test(dataProvider = "devfiles") public void checkThatDtoHasCorrectNames(DevfileImpl devfile, String expectedGenerateName) throws BadRequestException, ServerException, DevfileException, IOException, OverrideParameterException { DefaultFactoryUrl defaultFactoryUrl = mock(DefaultFactoryUrl.class); FileContentProvider fileContentProvider = mock(FileContentProvider.class); when(defaultFactoryUrl.devfileFileLocations()) .thenReturn( singletonList( new DevfileLocation() { @Override public Optional<String> filename() { return Optional.empty(); } @Override public String location() { return "http: } })); when(devfileParser.parseYaml(anyString(), anyMap())).thenReturn(devfile); when(urlFetcher.fetchSafely(anyString())).thenReturn("anything"); FactoryDto factory = urlFactoryBuilder .createFactoryFromDevfile(defaultFactoryUrl, fileContentProvider, emptyMap()) .get(); assertNull(factory.getDevfile().getMetadata().getName()); assertEquals(factory.getDevfile().getMetadata().getGenerateName(), expectedGenerateName); }