From 56e38b3c082d78a4fb1e34aef9014ee5afe7f8f0 Mon Sep 17 00:00:00 2001 From: Jake Mathews Date: Thu, 9 Apr 2020 12:57:26 -0400 Subject: [PATCH] refactor(git-parse): Moved GitConfigNode transformation to GitConfig to GitConfigNodeTransformer. Added license headers. --- .../git/parsing/GitParseExtractor.java | 7 ++- .../git/parsing/model/GitConfig.java | 47 +++++++-------- .../git/parsing/model/GitConfigBranch.java | 22 +++++++ .../git/parsing/model/GitConfigRemote.java | 22 +++++++ .../parse/GitConfigNodeTransformer.java | 57 +++++++++++++++++++ .../detectable/factory/DetectableFactory.java | 7 ++- ...java => GitConfigNodeTransformerTest.java} | 11 ++-- 7 files changed, 140 insertions(+), 33 deletions(-) create mode 100644 detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/parse/GitConfigNodeTransformer.java rename detectable/src/test/java/com/synopsys/integration/detectable/detectables/git/parsing/model/{GitConfigTest.java => GitConfigNodeTransformerTest.java} (89%) diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/GitParseExtractor.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/GitParseExtractor.java index fea7ca28c0..5249b36ca0 100644 --- a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/GitParseExtractor.java +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/GitParseExtractor.java @@ -34,6 +34,7 @@ import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfig; import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfigNode; import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitConfigExtractor; +import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitConfigNodeTransformer; import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitFileParser; import com.synopsys.integration.exception.IntegrationException; import com.synopsys.integration.log.IntLogger; @@ -45,10 +46,12 @@ public class GitParseExtractor { private final GitFileParser gitFileParser; private final GitConfigExtractor gitConfigExtractor; + private final GitConfigNodeTransformer gitConfigNodeTransformer; - public GitParseExtractor(final GitFileParser gitFileParser, final GitConfigExtractor gitConfigExtractor) { + public GitParseExtractor(final GitFileParser gitFileParser, final GitConfigExtractor gitConfigExtractor, final GitConfigNodeTransformer gitConfigNodeTransformer) { this.gitFileParser = gitFileParser; this.gitConfigExtractor = gitConfigExtractor; + this.gitConfigNodeTransformer = gitConfigNodeTransformer; } public final Extraction extract(final File gitConfigFile, final File gitHeadFile) { @@ -58,7 +61,7 @@ public final Extraction extract(final File gitConfigFile, final File gitHeadFile final List configFileContent = FileUtils.readLines(gitConfigFile, StandardCharsets.UTF_8); final List gitConfigNodes = gitFileParser.parseGitConfig(configFileContent); - final GitConfig gitConfig = GitConfig.fromGitConfigNodes(gitConfigNodes); + final GitConfig gitConfig = gitConfigNodeTransformer.createGitConfig(gitConfigNodes); final NameVersion projectNameVersion = gitConfigExtractor.extractProjectInfo(gitConfig, gitHead); diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfig.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfig.java index bdff2e4f23..81e35ae19f 100644 --- a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfig.java +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfig.java @@ -1,7 +1,28 @@ +/** + * detectable + * + * Copyright (c) 2020 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package com.synopsys.integration.detectable.detectables.git.parsing.model; import java.util.List; -import java.util.stream.Collectors; // A List is a rough parsing result. This class provides a better API. public class GitConfig { @@ -9,30 +30,6 @@ public class GitConfig { private final List gitConfigRemotes; private final List gitConfigBranches; - public static GitConfig fromGitConfigNodes(final List gitConfigNodes) { - final List gitConfigRemotes = gitConfigNodes.stream() - .filter(node -> node.getType().equals("remote")) - .map(node -> { - String remoteNodeName = node.getName().orElseThrow(() -> new IllegalArgumentException("Expected remote node to have a name.")); - String remoteNodeUrl = node.getProperty("url").orElseThrow(() -> new IllegalArgumentException("Expected remote node to have a url field.")); - String remoteNodeFetch = node.getProperty("fetch").orElseThrow(() -> new IllegalArgumentException("Expected remote node to have a fetch field.")); - return new GitConfigRemote(remoteNodeName, remoteNodeUrl, remoteNodeFetch); - }) - .collect(Collectors.toList()); - - final List gitConfigBranches = gitConfigNodes.stream() - .filter(node -> node.getType().equals("branch")) - .map(node -> { - String remoteNodeName = node.getName().orElseThrow(() -> new IllegalArgumentException("Expected branch node to have a name.")); - String remoteNodeRemote = node.getProperty("remote").orElseThrow(() -> new IllegalArgumentException("Expected branch node to have a remote field.")); - String remoteNodeMerge = node.getProperty("merge").orElseThrow(() -> new IllegalArgumentException("Expected branch node to have a fetch field.")); - return new GitConfigBranch(remoteNodeName, remoteNodeRemote, remoteNodeMerge); - }) - .collect(Collectors.toList()); - - return new GitConfig(gitConfigRemotes, gitConfigBranches); - } - public GitConfig(final List gitConfigRemotes, final List gitConfigBranches) { this.gitConfigRemotes = gitConfigRemotes; this.gitConfigBranches = gitConfigBranches; diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigBranch.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigBranch.java index 68a501d1d1..c532b4c793 100644 --- a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigBranch.java +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigBranch.java @@ -1,3 +1,25 @@ +/** + * detectable + * + * Copyright (c) 2020 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package com.synopsys.integration.detectable.detectables.git.parsing.model; import org.jetbrains.annotations.NotNull; diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigRemote.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigRemote.java index fe8481ccf7..75a6601542 100644 --- a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigRemote.java +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigRemote.java @@ -1,3 +1,25 @@ +/** + * detectable + * + * Copyright (c) 2020 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package com.synopsys.integration.detectable.detectables.git.parsing.model; import org.jetbrains.annotations.NotNull; diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/parse/GitConfigNodeTransformer.java b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/parse/GitConfigNodeTransformer.java new file mode 100644 index 0000000000..50a447b94f --- /dev/null +++ b/detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/parse/GitConfigNodeTransformer.java @@ -0,0 +1,57 @@ +/** + * detectable + * + * Copyright (c) 2020 Synopsys, Inc. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.synopsys.integration.detectable.detectables.git.parsing.parse; + +import java.util.List; +import java.util.stream.Collectors; + +import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfig; +import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfigBranch; +import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfigNode; +import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfigRemote; + +public class GitConfigNodeTransformer { + public GitConfig createGitConfig(final List gitConfigNodes) { + final List gitConfigRemotes = gitConfigNodes.stream() + .filter(node -> node.getType().equals("remote")) + .map(node -> { + String remoteNodeName = node.getName().orElseThrow(() -> new IllegalArgumentException("Expected remote node to have a name.")); + String remoteNodeUrl = node.getProperty("url").orElseThrow(() -> new IllegalArgumentException("Expected remote node to have a url field.")); + String remoteNodeFetch = node.getProperty("fetch").orElseThrow(() -> new IllegalArgumentException("Expected remote node to have a fetch field.")); + return new GitConfigRemote(remoteNodeName, remoteNodeUrl, remoteNodeFetch); + }) + .collect(Collectors.toList()); + + final List gitConfigBranches = gitConfigNodes.stream() + .filter(node -> node.getType().equals("branch")) + .map(node -> { + String remoteNodeName = node.getName().orElseThrow(() -> new IllegalArgumentException("Expected branch node to have a name.")); + String remoteNodeRemote = node.getProperty("remote").orElseThrow(() -> new IllegalArgumentException("Expected branch node to have a remote field.")); + String remoteNodeMerge = node.getProperty("merge").orElseThrow(() -> new IllegalArgumentException("Expected branch node to have a fetch field.")); + return new GitConfigBranch(remoteNodeName, remoteNodeRemote, remoteNodeMerge); + }) + .collect(Collectors.toList()); + + return new GitConfig(gitConfigRemotes, gitConfigBranches); + } +} diff --git a/detectable/src/main/java/com/synopsys/integration/detectable/factory/DetectableFactory.java b/detectable/src/main/java/com/synopsys/integration/detectable/factory/DetectableFactory.java index 65b44549f6..9a0e9b193c 100644 --- a/detectable/src/main/java/com/synopsys/integration/detectable/factory/DetectableFactory.java +++ b/detectable/src/main/java/com/synopsys/integration/detectable/factory/DetectableFactory.java @@ -104,6 +104,7 @@ import com.synopsys.integration.detectable.detectables.git.parsing.GitParseDetectable; import com.synopsys.integration.detectable.detectables.git.parsing.GitParseExtractor; import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitConfigExtractor; +import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitConfigNodeTransformer; import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitFileParser; import com.synopsys.integration.detectable.detectables.go.godep.GoDepExtractor; import com.synopsys.integration.detectable.detectables.go.godep.GoDepLockDetectable; @@ -447,8 +448,12 @@ private GitConfigExtractor gitConfigExtractor() { return new GitConfigExtractor(gitUrlParser()); } + private GitConfigNodeTransformer gitConfigNodeTransformer() { + return new GitConfigNodeTransformer(); + } + private GitParseExtractor gitParseExtractor() { - return new GitParseExtractor(gitFileParser(), gitConfigExtractor()); + return new GitParseExtractor(gitFileParser(), gitConfigExtractor(), gitConfigNodeTransformer()); } private GitUrlParser gitUrlParser() { diff --git a/detectable/src/test/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigTest.java b/detectable/src/test/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigNodeTransformerTest.java similarity index 89% rename from detectable/src/test/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigTest.java rename to detectable/src/test/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigNodeTransformerTest.java index a7f138531f..8ec79c3115 100644 --- a/detectable/src/test/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigTest.java +++ b/detectable/src/test/java/com/synopsys/integration/detectable/detectables/git/parsing/model/GitConfigNodeTransformerTest.java @@ -8,12 +8,13 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -class GitConfigTest { +import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitConfigNodeTransformer; + +class GitConfigNodeTransformerTest { @Test - void fromGitConfigNodes() { + void createGitConfig() { //#region Create GitConfigNodes - // The core node should be ignored by the transformation. final Map coreProperties = new HashMap<>(); coreProperties.put("repositoryformatversion", "0"); @@ -40,10 +41,10 @@ void fromGitConfigNodes() { gitConfigNodes.add(remoteNode); gitConfigNodes.add(branchNode); gitConfigNodes.add(anotherBranch); - //#endregion Create GitConfigNodes - final GitConfig gitConfig = GitConfig.fromGitConfigNodes(gitConfigNodes); + final GitConfigNodeTransformer gitConfigNodeTransformer = new GitConfigNodeTransformer(); + final GitConfig gitConfig = gitConfigNodeTransformer.createGitConfig(gitConfigNodes); Assertions.assertEquals(1, gitConfig.getGitConfigRemotes().size());