diff --git a/examples/mapping/gitlab_mapping_example_1_0.xml b/examples/mapping/gitlab_mapping_example_1_0.xml new file mode 100644 index 00000000..b21c86cf --- /dev/null +++ b/examples/mapping/gitlab_mapping_example_1_0.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/examples/mapping/gitlab_mapping_example_2_0.xml b/examples/mapping/gitlab_mapping_example_2_0.xml new file mode 100644 index 00000000..63a99a2b --- /dev/null +++ b/examples/mapping/gitlab_mapping_example_2_0.xml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sources/codemetropolis-toolchain-converter/pom.xml b/sources/codemetropolis-toolchain-converter/pom.xml index 3fc4dfe9..cea8abaa 100644 --- a/sources/codemetropolis-toolchain-converter/pom.xml +++ b/sources/codemetropolis-toolchain-converter/pom.xml @@ -54,6 +54,12 @@ codemetropolis-toolchain-commons 1.4.0 + + junit + junit + 4.11 + test + args4j args4j @@ -64,5 +70,10 @@ graphlib 1.0 + + org.gitlab4j + gitlab4j-api + 4.9.19 + diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/control/ConverterLoader.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/control/ConverterLoader.java index 39579620..d2b1cda8 100644 --- a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/control/ConverterLoader.java +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/control/ConverterLoader.java @@ -3,6 +3,7 @@ import java.util.Map; import codemetropolis.toolchain.commons.cdf.converter.CdfConverter; +import codemetropolis.toolchain.converter.gitlab.GitLabConverter; import codemetropolis.toolchain.converter.sonarqube.SonarQubeConverter; import codemetropolis.toolchain.converter.sourcemeter.GraphConverter; @@ -12,6 +13,8 @@ private ConverterLoader() {} public static CdfConverter load(ConverterType converterType, Map params) { switch(converterType) { + case GITLAB: + return new GitLabConverter(params); case SOURCEMETER: return new GraphConverter(params); case SONARQUBE: diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/control/ConverterType.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/control/ConverterType.java index da4a69ad..f6877cf8 100644 --- a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/control/ConverterType.java +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/control/ConverterType.java @@ -1,6 +1,7 @@ package codemetropolis.toolchain.converter.control; public enum ConverterType { + GITLAB, SOURCEMETER, SONARQUBE } diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabClient.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabClient.java new file mode 100644 index 00000000..b52cf109 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabClient.java @@ -0,0 +1,49 @@ +package codemetropolis.toolchain.converter.gitlab; + +import codemetropolis.toolchain.commons.cdf.CdfElement; +import codemetropolis.toolchain.commons.util.Resources; +import org.gitlab4j.api.*; +import org.gitlab4j.api.models.*; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Base64; +import java.util.List; + +public class GitLabClient { + + private final String EXCEPTION_MESSAGE = "Invalid project, group, username or password! (-i hostUrl (eg.: http://gitlab-okt.sed.hu) parameters are: -p username=... password=... group=... project=...)"; + + private String hostUrl; + private String username; + private String password; + + public GitLabClient(String hostUrl, String username, String password) { + this.hostUrl = hostUrl; + this.username = username; + this.password = password; + } + + public String getHostUrl() { + return hostUrl; + } + + public void authentication() throws GitLabConnectException + { + GitLabApi gitLabApi = null; + + try { + gitLabApi = GitLabApi.login(hostUrl, username, password); + } catch (GitLabApiException e) { + throw new GitLabConnectException(EXCEPTION_MESSAGE); + } + + if(gitLabApi != null) { + GitLabElement.setGitLabApi(gitLabApi); + } + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabConnectException.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabConnectException.java new file mode 100644 index 00000000..9b261074 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabConnectException.java @@ -0,0 +1,8 @@ +package codemetropolis.toolchain.converter.gitlab; + +import codemetropolis.toolchain.commons.exceptions.CodeMetropolisException; + +public class GitLabConnectException extends CodeMetropolisException { + public GitLabConnectException() { super(); } + public GitLabConnectException(String message) { super(message); } +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabConverter.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabConverter.java new file mode 100644 index 00000000..7cfc99a2 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabConverter.java @@ -0,0 +1,117 @@ +package codemetropolis.toolchain.converter.gitlab; + +import codemetropolis.toolchain.commons.cdf.CdfElement; +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import codemetropolis.toolchain.commons.cdf.CdfTree; +import codemetropolis.toolchain.commons.cdf.converter.CdfConverter; +import codemetropolis.toolchain.commons.exceptions.CodeMetropolisException; +import org.gitlab4j.api.GitLabApi; +import org.gitlab4j.api.GitLabApiException; +import org.gitlab4j.api.models.Project; + +import java.util.List; +import java.util.Map; + +//to run: -t gitlab -i hostUrl (e.g.: http://gitlab-okt.sed.hu) -o output_dir -p username= password= group= project= + +public class GitLabConverter extends CdfConverter { + + public final String USER_NAME_PARAM = "username"; + public final String PASSWORD_PARAM = "password"; + public final String GROUP_PARAM = "group"; + public final String PROJECT_PARAM = "project"; + + private String hostUrl; + private String projectName; + private String groupName; + + private Project p; + + public String getHostUrl() { + return hostUrl; + } + + public void setHostUrl(String hostUrl) { + this.hostUrl = hostUrl; + } + + public String getProjectName() { + return projectName; + } + + public void setProjectName(String projectName) { + this.projectName = projectName; + } + + public String getGroupName() { + return groupName; + } + + public void setGroupName(String groupName) { + this.groupName = groupName; + } + + public Project getP() { + return p; + } + + public void setP(Project p) { + this.p = p; + } + + public String createFullUrl() { + return hostUrl + "/" + groupName + "/" + projectName; + } + + public GitLabConverter() {super(null);} + + public GitLabConverter(Map params) { + super(params); + } + + public void authentication() throws GitLabConnectException + { + projectName = getParameter(PROJECT_PARAM); + groupName = getParameter(GROUP_PARAM); + + GitLabClient glc = new GitLabClient(hostUrl, getParameter(USER_NAME_PARAM), getParameter(PASSWORD_PARAM)); + glc.authentication(); + } + + @Override + public CdfTree createElements(String source) throws CodeMetropolisException { + hostUrl = source; + + try { + authentication(); + + List pList; + pList = GitLabElement.gitLabApi.getProjectApi().getProjects(); + + for(Project pr : pList) + { + if(pr.getName().equalsIgnoreCase(projectName)) { + p = pr; + GitLabElement.setProjectID(p.getId()); + } + } + } catch (GitLabConnectException e) { + throw new CodeMetropolisException(e.getMessage()); + } catch (GitLabApiException e) { + throw new CodeMetropolisException(e.getMessage()); + } + + CdfTree tree = new CdfTree(); + CdfElement root = null; + + try { + root = GitLabResource.getElement(Type.PROJECT, Integer.toString(GitLabElement.projectId), Integer.toString(GitLabElement.projectId)); + } catch (GitLabException e) { + throw new CodeMetropolisException(e.getMessage()); + } + + tree.setRoot(root); + + return tree; + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabElement.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabElement.java new file mode 100644 index 00000000..b68b1c1a --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabElement.java @@ -0,0 +1,105 @@ +package codemetropolis.toolchain.converter.gitlab; + +import codemetropolis.toolchain.commons.cdf.CdfElement; +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import org.gitlab4j.api.GitLabApi; + +import java.util.ArrayList; +import java.util.List; + +public abstract class GitLabElement { + + private List properties; + protected List children; + private List parentIds; + + protected static Integer projectId; + protected static GitLabApi gitLabApi; + + protected Type type; + protected String ID; + + public String getID() { + return ID; + } + + protected CdfElement element; + + public List getParentIds() { + return parentIds; + } + + public void addChild(Pair child) { + children.add(child); + } + + public static void setProjectID(int project) { + projectId = project; + } + + public static void setGitLabApi(GitLabApi gitLabA) { + gitLabApi = gitLabA; + } + + public void addParentID(String parentId) { + parentIds.add(parentId); + } + + public GitLabElement() { + this.parentIds=new ArrayList<>(); + this.children=new ArrayList<>(); + } + + public void makeProperties() { + properties = createProperties(); + } + + public void makeChildren() { + children = createChildren(); + } + + private String currParentId; + + public CdfElement getElement() throws GitLabException { + + if (GitLabResource.consistsOf(type, ID)) { + GitLabResource.addParentID(type, ID, currParentId); + return GitLabResource.getElement(type, ID).element; + } + + CdfElement element = new CdfElement(ID, type.toString().toLowerCase()); + makeChildren(); + + for(Pair ch: children) { + GitLabElement el=GitLabResource.receiveElement(ch.getType()); + el.addParentID(ID); + el.currParentId=ID; + el.setID(ch.getID()); + el.setType(ch.getType()); + element.addChildElement(el.getElement()); + } + + makeProperties(); + + for (CdfProperty property : properties) { + element.addProperty(property.getName(), property.getValue(), property.getType()); + } + + + this.element=element; + GitLabResource.addElement(this); + + return element; + } + + public void setID(String id) { + this.ID = id; + } + + public void setType(Type type) {this.type = type;} + + public abstract List createProperties(); + + public abstract List createChildren(); + +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabException.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabException.java new file mode 100644 index 00000000..7f5f99d8 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabException.java @@ -0,0 +1,23 @@ +package codemetropolis.toolchain.converter.gitlab; + +public class GitLabException extends Exception{ + public GitLabException() { + super(); + } + + public GitLabException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + public GitLabException(String message, Throwable cause) { + super(message, cause); + } + + public GitLabException(String message) { + super(message); + } + + public GitLabException(Throwable cause) { + super(cause); + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabResource.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabResource.java new file mode 100644 index 00000000..04d6b36c --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/GitLabResource.java @@ -0,0 +1,121 @@ +package codemetropolis.toolchain.converter.gitlab; + +import codemetropolis.toolchain.commons.cdf.CdfElement; +import codemetropolis.toolchain.converter.gitlab.model.*; + +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Map; + +public class GitLabResource { + + private static Map> mainStorage = new EnumMap>(Type.class); + + public static GitLabElement receiveElement(Type t) throws GitLabException { + switch(t) { + case PROJECT: + return new Project(); + case COMMIT: + return new Commit(); + case TREE: + return new Tree(); + case ISSUE: + return new Issue(); + case BRANCH: + return new Branch(); + case MILESTONE: + return new Milestone(); + case USER: + return new User(); + default: + throw new GitLabException("Illegal type"); + } + + } + + public static void clearMainStorage() {mainStorage.clear();} + + public static Map> getMainStorage() { + return mainStorage; + } + + public static boolean consistsOf(Type type, String ID) { + return mainStorage.containsKey(type) && mainStorage.get(type).containsKey(ID); + } + + public static boolean addElement(GitLabElement element) { + if (mainStorage.containsKey(element.type)) { + if (mainStorage.get(element.type).containsKey(element.ID)) + return false; + + mainStorage.get(element.type).put(element.ID, element); + + return true; + } + + mainStorage.put(element.type, new HashMap<>()); + mainStorage.get(element.type).put(element.ID, element); + + return true; + } + + public static void addParentID(Type type, String ID, String parentID) { + if (consistsOf(type, ID)) { + GitLabElement element=mainStorage.get(type).get(ID); + element.addParentID(parentID); + mainStorage.get(type).put(ID, element); + } + } + + public static Map getAllByType(Type type) { + return mainStorage.get(type); + } + + public static GitLabElement getElement(Type type, String ID) { + return mainStorage.get(type).get(ID); + } + + public static GitLabElement getElement(Pair pair) { + return getElement(pair.getType(), pair.getID()); + } + + public static CdfElement getElement(Type type, String ID, String parentId) throws GitLabException { + if (ID==null) return new CdfElement(); + + try { + if (mainStorage.containsKey(type)) { + if (mainStorage.get(type).containsKey(ID)) + return mainStorage.get(type).get(ID).element; + + GitLabElement gle = createGitLabElement(type, ID, parentId); + mainStorage.get(type).put(ID, gle); + + return gle.element; + } + + Map m=new HashMap<>(); + GitLabElement gle = createGitLabElement(type, ID, parentId); + + CdfElement out=gle.getElement(); + m.put(ID, gle); + + mainStorage.put(type, m); + + return out; + } catch (Exception e) { + throw new GitLabException("Illegal argument added"); + } + + } + + private static GitLabElement createGitLabElement(Type type, String ID, String parentId) throws GitLabException { + GitLabElement gle = receiveElement(type); + gle.addParentID(parentId); + gle.setType(type); + gle.setID(ID); + gle.makeProperties(); + + return gle; + } + +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/Pair.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/Pair.java new file mode 100644 index 00000000..226f5c9e --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/Pair.java @@ -0,0 +1,19 @@ +package codemetropolis.toolchain.converter.gitlab; + +public class Pair { + private String ID; + private Type type; + + public Pair(String ID, Type type) { + this.ID = ID; + this.type = type; + } + + public String getID() { + return ID; + } + + public Type getType() { + return type; + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/Type.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/Type.java new file mode 100644 index 00000000..3ecd6d04 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/Type.java @@ -0,0 +1,5 @@ +package codemetropolis.toolchain.converter.gitlab; + +public enum Type { + BRANCH, PROJECT, ISSUE, USER, COMMIT, TREE, MILESTONE +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Branch.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Branch.java new file mode 100644 index 00000000..bf6bd789 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Branch.java @@ -0,0 +1,118 @@ +package codemetropolis.toolchain.converter.gitlab.model; + +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import codemetropolis.toolchain.converter.gitlab.GitLabElement; +import codemetropolis.toolchain.converter.gitlab.GitLabResource; +import codemetropolis.toolchain.converter.gitlab.Pair; +import codemetropolis.toolchain.converter.gitlab.Type; +import org.gitlab4j.api.*; +import org.gitlab4j.api.models.Commit; +import org.gitlab4j.api.models.TreeItem; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class Branch extends GitLabElement { + private String name; + + private int isMerged; + private int developersCanPush; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getIsMerged() { + return isMerged; + } + + public void setIsMerged(int isMerged) { + this.isMerged = isMerged; + } + + public int getDevelopersCanPush() { + return developersCanPush; + } + + public void setDevelopersCanPush(int developersCanPush) { + this.developersCanPush = developersCanPush; + } + + public static int convertBooleanToInteger(boolean value) { + if (value == true) + return 1; + else + return 0; + } + + public void addProperties(List list) { + list.add(new CdfProperty("isMerged", isMerged + "", CdfProperty.Type.INT)); + list.add(new CdfProperty("developersCanPush", developersCanPush + "", CdfProperty.Type.INT)); + } + + @Override + public List createProperties() { + RepositoryApi rapi = new RepositoryApi(gitLabApi); + List list = new ArrayList<>(); + + try { + org.gitlab4j.api.models.Branch b = rapi.getBranch(projectId, ID); + + name = b.getName(); + + isMerged = convertBooleanToInteger(b.getMerged()); + developersCanPush = convertBooleanToInteger(b.getDevelopersCanPush()); + addProperties(list); + + } catch (GitLabApiException e) { + e.printStackTrace(); + } + + return list; + } + + @Override + public List createChildren() { + RepositoryApi rapi = new RepositoryApi(gitLabApi); + Pair child; + List list = new ArrayList<>(); + List items = null; + org.gitlab4j.api.models.Branch b=null; + + try { + items = rapi.getTree(projectId); + b = rapi.getBranch(projectId, ID); + + Queue commits = new LinkedList<>(); + commits.add(b.getCommit()); + CommitsApi capi=gitLabApi.getCommitsApi(); + + while(!commits.isEmpty()) { + Commit commit=commits.remove(); + list.add(new Pair(commit.getId(), Type.COMMIT)); + + for(String commit1: commit.getParentIds()) { + if (!GitLabResource.consistsOf(Type.COMMIT, commit1)) + commits.add(capi.getCommit(projectId, commit1)); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + for(TreeItem item: items) { + if(item.getType() == TreeItem.Type.TREE) { + child = new Pair(item.getPath()+"---"+ID, Type.TREE); + list.add(child); + } + } + + return list; + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Commit.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Commit.java new file mode 100644 index 00000000..eee0d6ce --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Commit.java @@ -0,0 +1,90 @@ +package codemetropolis.toolchain.converter.gitlab.model; + +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import codemetropolis.toolchain.converter.gitlab.GitLabElement; +import codemetropolis.toolchain.converter.gitlab.Pair; +import org.gitlab4j.api.CommitsApi; +import org.gitlab4j.api.GitLabApiException; + +import java.util.ArrayList; +import java.util.List; + +public class Commit extends GitLabElement { + private int messageLength; + private int addition; + private int deletions; + private int total; + private int numberOfComments; + + private String status; + private String message; + + public void setTotal(int total) { + this.total = total; + } + + public void setStatus(String status) { + this.status = status; + } + + public void setDeletions(int deletions) { + this.deletions = deletions; + } + + public void setAddition(int addition) { + this.addition = addition; + } + + public void setMessage(String message) { + this.message = message; + } + + public void setNumberOfComments(int numberOfComments) { + this.numberOfComments = numberOfComments; + } + + @Override + public List createProperties() { + CommitsApi capi = new CommitsApi(gitLabApi); + List list = new ArrayList<>(); + org.gitlab4j.api.models.Commit c=null; + + try { + c= capi.getCommit(projectId, ID); + if (c!=null) { + message=c.getMessage(); + addition = c.getStats().getAdditions(); + deletions = c.getStats().getDeletions(); + total = c.getStats().getTotal(); + status = c.getStatus(); + numberOfComments = capi.getComments(projectId, ID).size(); + } + } catch (GitLabApiException e) { + //e.printStackTrace(); + } + catch (Exception e) { + e.printStackTrace(); + } + + if (message!=null) + messageLength=message.length(); + if (ID!=null) { + list.add(new CdfProperty("messageLength", Integer.toString(messageLength), CdfProperty.Type.INT)); + list.add(new CdfProperty("addition", Integer.toString(addition), CdfProperty.Type.INT)); + list.add(new CdfProperty("deletion", Integer.toString(deletions), CdfProperty.Type.INT)); + list.add(new CdfProperty("total", Integer.toString(total), CdfProperty.Type.INT)); + list.add(new CdfProperty("status", status, CdfProperty.Type.STRING)); + list.add(new CdfProperty("numberOfComments", numberOfComments + "", CdfProperty.Type.INT)); + list.add(new CdfProperty("message", message, CdfProperty.Type.STRING)); + } + + return list; + } + + @Override + public List createChildren() { + List list=new ArrayList<>(); + + return list; + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Issue.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Issue.java new file mode 100644 index 00000000..78917b31 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Issue.java @@ -0,0 +1,105 @@ +package codemetropolis.toolchain.converter.gitlab.model; + +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import codemetropolis.toolchain.converter.gitlab.GitLabElement; +import codemetropolis.toolchain.converter.gitlab.Pair; +import org.gitlab4j.api.IssuesApi; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class Issue extends GitLabElement { + + private String state; + + private int userNotesCount; + private int weight; + private int timeEstimate; + private int totalTime; + + private float timeRatio; + + public void setState(String state) { + this.state = state; + } + + public void setTimeRatio(float timeRatio) { + this.timeRatio = timeRatio; + } + + public void setTimeEstimate(int timeEstimate) { + this.timeEstimate = timeEstimate; + } + + public void setTotalTime(int totalTime) { + this.totalTime = totalTime; + } + + public void setUserNotesCount(int userNotesCount) { + this.userNotesCount = userNotesCount; + } + + public void setWeight(int weight) { + this.weight = weight; + } + + @Override + public List createProperties() { + + List properties=new ArrayList<>(); + + if (ID==null) return properties; + + IssuesApi iapi=null; + + if (gitLabApi!=null) + iapi=new IssuesApi(gitLabApi); + + org.gitlab4j.api.models.Issue issue=null; + + if (iapi!=null) { + Optional opt = iapi.getOptionalIssue(projectId, Integer.parseInt(ID)); + issue = opt.orElse(null); + } + + if (issue!=null && issue.getId()!=null) { + if (issue.getState()!=null) + state = issue.getState().toString(); + + if (issue.getUserNotesCount()!=null) + userNotesCount = issue.getUserNotesCount(); + + if (issue.getWeight()!=null) + weight = issue.getWeight(); + + if (issue.getTimeStats()!=null) { + timeEstimate = issue.getTimeStats().getTimeEstimate(); + totalTime = issue.getTimeStats().getTimeEstimate(); + } + } + + if (totalTime==0) + timeRatio=0; + else + timeRatio = (float) (timeEstimate / (float) totalTime); + + if (state!=null) + properties.add(new CdfProperty("state", state, CdfProperty.Type.STRING)); + + properties.add(new CdfProperty("userNoteCount", Integer.toString(userNotesCount), CdfProperty.Type.INT)); + properties.add(new CdfProperty("weight", Integer.toString(weight), CdfProperty.Type.INT)); + properties.add(new CdfProperty("timeEstimate", Integer.toString(timeEstimate), CdfProperty.Type.INT)); + properties.add(new CdfProperty("totalTime", Integer.toString(totalTime), CdfProperty.Type.INT)); + properties.add(new CdfProperty("timeRatio", Float.toString(timeRatio), CdfProperty.Type.FLOAT)); + + return properties; + } + + @Override + public List createChildren() { + List children=new ArrayList<>(); + + return children; + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Milestone.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Milestone.java new file mode 100644 index 00000000..311f5ed9 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Milestone.java @@ -0,0 +1,100 @@ +package codemetropolis.toolchain.converter.gitlab.model; + +import codemetropolis.toolchain.commons.cdf.CdfElement; +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import codemetropolis.toolchain.converter.gitlab.*; +import org.gitlab4j.api.GitLabApiException; +import org.gitlab4j.api.MilestonesApi; +import org.gitlab4j.api.models.Issue; + +import java.util.ArrayList; +import java.util.List; + + +public class Milestone extends GitLabElement { + + private String title; + private String state; + + private double closedRatio; + private double openedRatio; + + public void setTitle(String title) { + this.title = title; + } + + public void setState(String state) { + this.state = state; + } + + @Override + public List createProperties() { + MilestonesApi mapi=null; + + if (gitLabApi!=null) + mapi=new MilestonesApi(gitLabApi); + + List properties=new ArrayList<>(); + + double all=children.size(); + + try { + org.gitlab4j.api.models.Milestone milestone=null; + if (mapi!=null) + milestone=mapi.getMilestone(projectId, Integer.parseInt(ID)); + + if (milestone!=null) { + title = milestone.getTitle(); + state = milestone.getState(); + } + + if (ID!=null) { + properties.add(new CdfProperty("title", title, CdfProperty.Type.STRING)); + properties.add(new CdfProperty("state", state, CdfProperty.Type.STRING)); + + for (Pair child : children) { + CdfElement element = GitLabResource.getElement(child).getElement(); + if (element.getPropertyValue("state").equals("closed")) closedRatio++; + else openedRatio++; + } + } + + } catch (GitLabApiException e) { + e.printStackTrace(); + } catch (GitLabException e) { + e.printStackTrace(); + } + + if (all!=0.0) { + closedRatio/=all; + openedRatio/=all; + } + + if (ID!=null) { + properties.add(new CdfProperty("closedRatio", closedRatio + "", CdfProperty.Type.FLOAT)); + properties.add(new CdfProperty("openedRatio", openedRatio + "", CdfProperty.Type.FLOAT)); + } + + return properties; + } + + @Override + public List createChildren() { + if (ID==null) return new ArrayList<>(); + + MilestonesApi mapi=new MilestonesApi(gitLabApi); + List children=new ArrayList<>(); + + try { + List issues=mapi.getIssues(projectId, Integer.parseInt(ID)); + for(Issue issue: issues) { + children.add(new Pair(issue.getIid().toString(), Type.ISSUE)); + } + + } catch (GitLabApiException e) { + e.printStackTrace(); + } + + return children; + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Project.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Project.java new file mode 100644 index 00000000..61dba349 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Project.java @@ -0,0 +1,183 @@ +package codemetropolis.toolchain.converter.gitlab.model; + +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import codemetropolis.toolchain.converter.gitlab.GitLabElement; +import codemetropolis.toolchain.converter.gitlab.GitLabResource; +import codemetropolis.toolchain.converter.gitlab.Pair; +import codemetropolis.toolchain.converter.gitlab.Type; +import org.gitlab4j.api.*; +import org.gitlab4j.api.models.*; +import org.gitlab4j.api.models.Commit; +import org.gitlab4j.api.models.Issue; +import org.gitlab4j.api.models.Milestone; + +import java.util.*; + +public class Project extends GitLabElement { + + private String name; + + private int commitCount; + private int jobArtifactsSize; + private int lfsObjectSize; + private int storageSize; + private int approvalsNumber = 0; + private int forksCount = 0; + + public Project() {} + + public int getCommitCount() { + return commitCount; + } + + public void setCommitCount(int commitCount) { + this.commitCount = commitCount; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getJobArtifactsSize() { + return jobArtifactsSize; + } + + public void setJobArtifactsSize(int jobArtifactsSize) { + this.jobArtifactsSize = jobArtifactsSize; + } + + public int getLfsObjectSize() { + return lfsObjectSize; + } + + public void setLfsObjectSize(int lfsObjectSize) { + this.lfsObjectSize = lfsObjectSize; + } + + public int getStorageSize() { + return storageSize; + } + + public void setStorageSize(int storageSize) { + this.storageSize = storageSize; + } + + public int getApprovalsNumber() { + return approvalsNumber; + } + + public void setApprovalsNumber(int approvalsNumber) { + this.approvalsNumber = approvalsNumber; + } + + public int getForksCount() { + return forksCount; + } + + public void setForksCount(int forksCount) { + this.forksCount = forksCount; + } + private class BranchHolder implements Comparable { + String branchName; + + Date commitDate; + + public BranchHolder(String branchName, Date commitDate) { + this.branchName=branchName; + this.commitDate=commitDate; + } + @Override + public int compareTo(Object o) { + return this.commitDate.compareTo(((BranchHolder)o).commitDate); + } + + } + + public void addProperties(List properties) { + properties.add(new CdfProperty("commitCount", Integer.toString(commitCount), CdfProperty.Type.INT)); + properties.add(new CdfProperty("jobArtifactsSize", Integer.toString(jobArtifactsSize), CdfProperty.Type.INT)); + properties.add(new CdfProperty("lfsObjectSize", Integer.toString(lfsObjectSize), CdfProperty.Type.INT)); + properties.add(new CdfProperty("storageSize", Integer.toString(storageSize), CdfProperty.Type.INT)); + properties.add(new CdfProperty("approvalsNumber", Integer.toString(approvalsNumber), CdfProperty.Type.INT)); + properties.add(new CdfProperty("forksCount", Integer.toString(forksCount), CdfProperty.Type.INT)); + properties.add(new CdfProperty("name", name, CdfProperty.Type.STRING)); + } + + @Override + public List createProperties() { + + List properties = new ArrayList<>(); + + if(gitLabApi != null) { + ProjectApi papi = new ProjectApi(gitLabApi); + + try { + org.gitlab4j.api.models.Project proj = papi.getProject(ID, true); + ProjectStatistics projectStatistics = proj.getStatistics(); + + if (proj.getApprovalsBeforeMerge() != null) + approvalsNumber=proj.getApprovalsBeforeMerge(); + + name = proj.getName(); + + if (proj.getForksCount() != null) + forksCount=proj.getForksCount(); + + storageSize = (int) projectStatistics.getStorageSize(); + lfsObjectSize = (int) projectStatistics.getLfsObjectSize(); + jobArtifactsSize = (int) projectStatistics.getJobArtifactsSize(); + commitCount = (int) projectStatistics.getCommitCount(); + + addProperties(properties); + + } catch (GitLabApiException e) { + e.printStackTrace(); + } + } + + return properties; + } + + @Override + public List createChildren() { + List children = new ArrayList<>(); + + if(gitLabApi != null) { + try { + MilestonesApi mapi = gitLabApi.getMilestonesApi(); + List milestoneList = mapi.getMilestones(Integer.parseInt(ID)); + + for (org.gitlab4j.api.models.Milestone milestone: milestoneList) { + children.add(new Pair(milestone.getId().toString(), Type.MILESTONE)); + } + + RepositoryApi rapi=new RepositoryApi(gitLabApi); + + List branches=rapi.getBranches(Integer.parseInt(ID)); + + Set holders=new TreeSet<>(); + + for(org.gitlab4j.api.models.Branch branch: branches) { + holders.add(new BranchHolder(branch.getName(), branch.getCommit().getCommittedDate())); + } + + for(BranchHolder holder: holders) { + children.add(new Pair(holder.branchName, Type.BRANCH)); + } + + for(Contributor contributor: rapi.getContributors(Integer.parseInt(ID))) { + children.add(new Pair(contributor.getEmail(), Type.USER)); + } + } catch (GitLabApiException e) { + e.printStackTrace(); + } + + } + + return children; + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Tree.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Tree.java new file mode 100644 index 00000000..7e8275d8 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/Tree.java @@ -0,0 +1,86 @@ +package codemetropolis.toolchain.converter.gitlab.model; + +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import codemetropolis.toolchain.converter.gitlab.GitLabElement; +import codemetropolis.toolchain.converter.gitlab.Pair; +import codemetropolis.toolchain.converter.gitlab.Type; +import org.gitlab4j.api.CommitsApi; +import org.gitlab4j.api.GitLabApiException; +import org.gitlab4j.api.RepositoryApi; +import org.gitlab4j.api.models.Commit; +import org.gitlab4j.api.models.TreeItem; + +import java.util.ArrayList; +import java.util.List; + +public class Tree extends GitLabElement { + + private int numberOfChildren; + + private String branchName; + private String name; + + public String getBranchName() { + return branchName; + } + public String getName() { + return name; + } + + @Override + public List createProperties() { + List properties=new ArrayList<>(); + + if (ID==null) return properties; + + RepositoryApi repository=null; + + if (gitLabApi!=null) { + repository=new RepositoryApi(gitLabApi); + } + + String names[] = ID.split("---"); + branchName = names[names.length-1]; + List out=new ArrayList<>(); + name=names[0]; + + if (repository!=null) { + try { + List list = repository.getTree(projectId, name, branchName, true); + numberOfChildren = list.size(); + } catch (GitLabApiException e) { + e.printStackTrace(); + } + } + + properties.add(new CdfProperty("numberOfChildren", numberOfChildren + "", CdfProperty.Type.INT)); + + return properties; + } + + @Override + public List createChildren() { + if (ID==null) return new ArrayList<>(); + RepositoryApi repository=new RepositoryApi(gitLabApi); + String names[] = ID.split("---"); + String branchName = names[names.length-1]; + List out=new ArrayList<>(); + String name=names[0]; + + try { + List list=repository.getTree(projectId, name, branchName); + + for(TreeItem treeItem: list) { + if (treeItem.getType()==TreeItem.Type.TREE) { + out.add(new Pair(treeItem.getPath()+"---"+branchName, Type.TREE)); + } + + } + } catch (GitLabApiException e) { + e.printStackTrace(); + } + + + return out; + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/User.java b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/User.java new file mode 100644 index 00000000..e74ddc06 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/main/java/codemetropolis/toolchain/converter/gitlab/model/User.java @@ -0,0 +1,119 @@ +package codemetropolis.toolchain.converter.gitlab.model; + +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import codemetropolis.toolchain.converter.gitlab.GitLabElement; +import codemetropolis.toolchain.converter.gitlab.GitLabResource; +import codemetropolis.toolchain.converter.gitlab.Pair; +import codemetropolis.toolchain.converter.gitlab.Type; +import org.gitlab4j.api.*; +import org.gitlab4j.api.models.Assignee; +import org.gitlab4j.api.models.Commit; +import org.gitlab4j.api.models.CustomAttribute; +import org.gitlab4j.api.models.Issue; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class User extends GitLabElement { + private String username; + + private int isAdmin; + private int commitNumber; + private int deletions; + private int additions; + private int total; + + private float deletionRatio; + private float addingRatio; + + public User() {} + + public float getDeletionRatio() { + return deletionRatio; + } + public float getAddingRatio() { return addingRatio; } + + public void setDeletionRatio(int deletion, int total) { + if (total == 0) + deletionRatio = 0.0f; + else + deletionRatio = ((float)deletion / (float)total); + } + + public void setAddingRatio(int additions, int total) { + if(total == 0) + addingRatio = 0.0f; + else + addingRatio = ((float)additions/ (float)total); + } + + @Override + public List createProperties() { + List list = new ArrayList<>(); + UserApi uapi = gitLabApi.getUserApi(); + RepositoryApi rapi=gitLabApi.getRepositoryApi(); + + commitNumber=0; + additions=0; + deletions=0; + + try { + List currentUsers = uapi.findUsers(ID); + + Map typeCommit=GitLabResource.getAllByType(Type.COMMIT); + CommitsApi commitsApi=gitLabApi.getCommitsApi(); + + for(GitLabElement gle: typeCommit.values()) { + Commit commit=commitsApi.getCommit(projectId, gle.getID()); + + if ( commit.getCommitterEmail().equals(ID)) { + commitNumber++; + additions+=commit.getStats().getAdditions(); + deletions+=commit.getStats().getDeletions(); + } + + } + + total = deletions + additions; + + if (currentUsers.size()>0) { + org.gitlab4j.api.models.User currentUser = currentUsers.get(0); + List attributeLIst = currentUser.getCustomAttributes(); + + username = currentUser.getUsername(); + Boolean admin = currentUser.getIsAdmin(); + + if (admin != null && currentUser.getIsAdmin()) { + isAdmin = 1; + } else { + isAdmin = 0; + } + + list.add(new CdfProperty("username", username, CdfProperty.Type.STRING)); + list.add(new CdfProperty("admin", isAdmin + "", CdfProperty.Type.INT)); + } + + setDeletionRatio(deletions, total); + setAddingRatio(additions, total); + + list.add(new CdfProperty("numberOfCommits", commitNumber + "", CdfProperty.Type.INT)); + list.add(new CdfProperty("deletions", deletions + "", CdfProperty.Type.INT)); + list.add(new CdfProperty("additions", additions + "", CdfProperty.Type.INT)); + list.add(new CdfProperty("deletionRatio", deletionRatio + "", CdfProperty.Type.FLOAT)); + list.add(new CdfProperty("additionRatio", addingRatio + "", CdfProperty.Type.FLOAT)); + + } catch (GitLabApiException e) { + e.printStackTrace(); + } + + return list; + } + + @Override + public List createChildren() { + List list = new ArrayList<>(); + + return list; + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/test/BranchTest.java b/sources/codemetropolis-toolchain-converter/src/test/BranchTest.java new file mode 100644 index 00000000..93cdaf66 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/test/BranchTest.java @@ -0,0 +1,34 @@ +import static org.junit.Assert.*; + +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import org.junit.BeforeClass; +import org.junit.Test; + +import codemetropolis.toolchain.converter.gitlab.model.Branch; + +import java.util.ArrayList; +import java.util.List; + +public class BranchTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @Test + public void testCreateProperties() { + Branch b = new Branch(); + List list = new ArrayList(); + + b.setIsMerged(0); + b.setDevelopersCanPush(1); + b.addProperties(list); + assertEquals(2, list.size()); + } + + @Test + public void testConvertBooleanToInteger() { + assertEquals(0, Branch.convertBooleanToInteger(false)); + assertEquals(1, Branch.convertBooleanToInteger(true)); + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/test/CommitTest.java b/sources/codemetropolis-toolchain-converter/src/test/CommitTest.java new file mode 100644 index 00000000..1b83ded2 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/test/CommitTest.java @@ -0,0 +1,83 @@ +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import codemetropolis.toolchain.converter.gitlab.GitLabElement; +import codemetropolis.toolchain.converter.gitlab.Pair; +import codemetropolis.toolchain.converter.gitlab.model.Commit; +import org.gitlab4j.api.GitLabApi; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + + +public class CommitTest { + + private Commit commit; + + private int COMMIT_ADDITION=10; + private int COMMIT_DELETIONS=15; + private int COMMIT_TOTAL=20; + private int projectID=10; + + private String COMMIT_MESSAGE="Commit branch"; + private String COMMIT_STATUS="success"; + private String ID="aAAAa"; + + @Before + public void setup() { + commit=new Commit(); + GitLabElement.setGitLabApi(new GitLabApi("", "")); + GitLabElement.setProjectID(10); + commit.setID(ID); + commit.setAddition(COMMIT_ADDITION); + commit.setDeletions(COMMIT_DELETIONS); + commit.setStatus(COMMIT_STATUS); + commit.setTotal(COMMIT_TOTAL); + commit.setMessage(COMMIT_MESSAGE); + commit.makeProperties(); + } + + @Test + public void testIfPropertiesConsistsElements() { + + List properties=commit.createProperties(); + Assert.assertTrue(properties.stream().anyMatch(item->(COMMIT_MESSAGE.equals(item.getValue())) + && "message".equals(item.getName()) + && CdfProperty.Type.STRING.equals(item.getType()))); + + } + + @Test + public void testCommitMessageLength() { + + Commit commit=new Commit(); + commit.setID(ID); + commit.setMessage(COMMIT_MESSAGE); + List properties= commit.createProperties(); + Assert.assertTrue(properties.stream().anyMatch(item->("13".equals(item.getValue())) + && "messageLength".equals(item.getName()) && + CdfProperty.Type.INT.equals(item.getType()))); + } + + + + @Test + public void testArraySize() { + List properties=commit.createProperties(); + Assert.assertEquals(7, properties.size()); + } + + @Test + public void testChildren() { + List children= commit.createChildren(); + Assert.assertEquals(0, children.size()); + } + + @Test + public void testIfCommitIDIsNull() { + commit.setID(null); + Assert.assertEquals(true, commit.createProperties().isEmpty()); + + } + +} diff --git a/sources/codemetropolis-toolchain-converter/src/test/GitLabClientTest.java b/sources/codemetropolis-toolchain-converter/src/test/GitLabClientTest.java new file mode 100644 index 00000000..b66b3806 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/test/GitLabClientTest.java @@ -0,0 +1,19 @@ +import codemetropolis.toolchain.converter.gitlab.GitLabClient; +import codemetropolis.toolchain.converter.gitlab.GitLabConnectException; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GitLabClientTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @Test(expected = GitLabConnectException.class) + public void testAuthentication() throws GitLabConnectException + { + GitLabClient glc = new GitLabClient("mock", "mock", "mock"); + glc.authentication(); + } + +} diff --git a/sources/codemetropolis-toolchain-converter/src/test/GitLabConverterTest.java b/sources/codemetropolis-toolchain-converter/src/test/GitLabConverterTest.java new file mode 100644 index 00000000..ab9d2605 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/test/GitLabConverterTest.java @@ -0,0 +1,34 @@ +import static org.junit.Assert.*; + +import codemetropolis.toolchain.converter.gitlab.GitLabConnectException; +import codemetropolis.toolchain.converter.gitlab.GitLabConverter; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GitLabConverterTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @Test + public void testGetFullUrl() { + GitLabConverter glc = new GitLabConverter(); + + final String url = "myHostUrl/myGroup/myProject"; + glc.setGroupName("myGroup"); + glc.setHostUrl("myHostUrl"); + glc.setProjectName("myProject"); + + assertEquals(true, glc.createFullUrl().equals(url)); + } + + @Test(expected = java.lang.NullPointerException.class) + public void testAuthentication() throws GitLabConnectException { + GitLabConverter glc = new GitLabConverter(); + glc.setProjectName("simple_project"); + glc.setGroupName("simple_group"); + glc.authentication(); + } + +} diff --git a/sources/codemetropolis-toolchain-converter/src/test/GitLabElementTest.java b/sources/codemetropolis-toolchain-converter/src/test/GitLabElementTest.java new file mode 100644 index 00000000..1a1696d3 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/test/GitLabElementTest.java @@ -0,0 +1,62 @@ +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import codemetropolis.toolchain.converter.gitlab.*; +import codemetropolis.toolchain.converter.gitlab.model.Issue; +import codemetropolis.toolchain.converter.gitlab.model.Milestone; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class GitLabElementTest { + + private static GitLabElement element; + + private String ID="12"; + private String MILESTONE_ID="11"; + + private Issue issue; + private Milestone milestone; + + @BeforeClass + public static void setUp() { + element = new GitLabElement() { + @Override + public List createProperties() { + return new ArrayList<>(); + } + + @Override + public List createChildren() { + return new ArrayList<>(); + } + }; + } + + @Before + public void afterSetUp() { + issue=new Issue(); + issue.setID(ID); + issue.setType(Type.ISSUE); + milestone=new Milestone(); + milestone.setID(MILESTONE_ID); + } + + @Test + public void testIfConsistsGitLabResource() { + GitLabResource.addElement(issue); + element.setType(Type.ISSUE); + element.setID(ID); + + try { + Assert.assertEquals(element.getElement(), issue.getElement()); + } catch (GitLabException e) { + e.printStackTrace(); + } + + GitLabResource.clearMainStorage(); + } + +} diff --git a/sources/codemetropolis-toolchain-converter/src/test/GitLabResourceTest.java b/sources/codemetropolis-toolchain-converter/src/test/GitLabResourceTest.java new file mode 100644 index 00000000..5cc6169f --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/test/GitLabResourceTest.java @@ -0,0 +1,118 @@ +import codemetropolis.toolchain.commons.cdf.CdfElement; +import codemetropolis.toolchain.converter.gitlab.GitLabElement; +import codemetropolis.toolchain.converter.gitlab.GitLabException; +import codemetropolis.toolchain.converter.gitlab.GitLabResource; +import codemetropolis.toolchain.converter.gitlab.Type; +import codemetropolis.toolchain.converter.gitlab.model.Commit; +import org.gitlab4j.api.GitLabApi; +import org.junit.*; +import org.junit.rules.ExpectedException; +import codemetropolis.toolchain.converter.gitlab.model.Branch; +import codemetropolis.toolchain.converter.gitlab.model.Commit; +import codemetropolis.toolchain.converter.gitlab.model.Project; + +import static org.junit.Assert.assertEquals; + +public class GitLabResourceTest { + + private static Commit commit; + + private String COMMIT_MESSAGE="Commit branch"; + private String COMMIT_STATUS="success"; + private String ID = "aAAAa"; + + private int COMMIT_ADDITION=10; + private int COMMIT_DELETIONS=15; + private int COMMIT_TOTAL=20; + private int projectID=10; + + @BeforeClass + public static void setUp() { + commit=new Commit(); + GitLabElement.setGitLabApi(new GitLabApi("", "")); + GitLabElement.setProjectID(10); + } + + @Before + public void afterSetUp() { + GitLabResource.clearMainStorage(); + commit.setID(ID); + commit.setAddition(COMMIT_ADDITION); + commit.setDeletions(COMMIT_DELETIONS); + commit.setStatus(COMMIT_STATUS); + commit.setTotal(COMMIT_TOTAL); + commit.setMessage(COMMIT_MESSAGE); + } + + @Test + public void testConsistsOf() { + GitLabElement branch = new Branch(); + branch.setID("myBelovedBranch"); + branch.setType(Type.BRANCH); + + assertEquals(false, GitLabResource.consistsOf(Type.BRANCH, "myBelovedBranch")); + GitLabResource.addElement(branch); + + assertEquals(false, GitLabResource.consistsOf(Type.COMMIT, "non-existing-commit")); + assertEquals(true, GitLabResource.consistsOf(Type.BRANCH, "myBelovedBranch")); + } + + @Test + public void testAddElement() { + GitLabElement project = new Project(); + project.setID("root"); + project.setType(Type.PROJECT); + + GitLabElement project2 = new Project(); + project2.setID("root"); + project2.setType(Type.PROJECT); + + + assertEquals(true, GitLabResource.addElement(project)); + assertEquals(false, GitLabResource.addElement(project2)); + } + + @Test + public void testAddParentID() { + GitLabElement commit = new Commit(); + commit.setID("c1"); + commit.addParentID("parent_c1"); + assertEquals(true, commit.getParentIds().contains("parent_c1")); + } + + @Test + public void testIfReturnsTheSameValue() { + CdfElement element=new CdfElement(); + CdfElement element2=new CdfElement(); + + try { + element = GitLabResource.getElement(Type.COMMIT, ID, "aaa"); + element2 = GitLabResource.getElement(Type.COMMIT, ID, "aaa"); + } catch (GitLabException e) { + e.printStackTrace(); + } + + Assert.assertEquals(element2, element); + + GitLabResource.clearMainStorage(); + } + + @Rule + public ExpectedException thrown=ExpectedException.none(); + + @Test + public void testIfTypeNull() throws GitLabException { + thrown.expect(GitLabException.class); + thrown.expectMessage("Illegal argument added"); + CdfElement element = GitLabResource.getElement(null, ID, "aaa"); + } + + @Test + public void testIfNullIDIsInserted() throws GitLabException { + GitLabResource.clearMainStorage(); + CdfElement element= GitLabResource.getElement(Type.COMMIT, null, "aaa"); + Assert.assertEquals(true, GitLabResource.getMainStorage().isEmpty()); + GitLabResource.clearMainStorage(); + } + +} diff --git a/sources/codemetropolis-toolchain-converter/src/test/IssueTest.java b/sources/codemetropolis-toolchain-converter/src/test/IssueTest.java new file mode 100644 index 00000000..a17ea2c4 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/test/IssueTest.java @@ -0,0 +1,73 @@ +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import codemetropolis.toolchain.converter.gitlab.GitLabElement; +import codemetropolis.toolchain.converter.gitlab.Type; +import codemetropolis.toolchain.converter.gitlab.model.Issue; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +public class IssueTest { + + private Issue issue; + + private static String ISSUE_STATE="opened"; + private static int ISSUE_USER_NOTES_COUNT=10; + private static int ISSUE_WEIGHT=2; + private static int ISSUE_TIME_ESTIMATE=4; + private static int ISSUE_TOTAL_TIME=11; + + @Before + public void setUp() { + issue=new Issue(); + issue.setType(Type.ISSUE); + issue.setState(ISSUE_STATE); + issue.setTimeEstimate(ISSUE_TIME_ESTIMATE); + issue.setTotalTime(ISSUE_TOTAL_TIME); + issue.setUserNotesCount(ISSUE_USER_NOTES_COUNT); + issue.setWeight(ISSUE_WEIGHT); + issue.setID("12"); + GitLabElement.setGitLabApi(null); + } + + @Test + public void testArraySize() { + List properties=issue.createProperties(); + Assert.assertEquals(6, properties.size()); + } + + @Test + public void testIfPropertiesConsistsElements() { + List properties=issue.createProperties(); + Assert.assertTrue(properties.stream().anyMatch(item->( + ISSUE_STATE.equals(item.getValue()) && + "state".equals(item.getName())&& + CdfProperty.Type.STRING.equals(item.getType()) + ))); + } + + @Test + public void testIfIssueIDIsNull() { + issue.setID(null); + Assert.assertEquals(true, issue.createProperties().isEmpty()); + } + + @Test + public void testTimeRatio() { + float t=ISSUE_TIME_ESTIMATE/(float)ISSUE_TOTAL_TIME; + String ISSUE_RATIO=Float.toString(t); + List properties=issue.createProperties(); + Assert.assertTrue(properties.stream().anyMatch(item-> + ISSUE_RATIO.equals(item.getValue()) && + "timeRatio".equals(item.getName()) && + CdfProperty.Type.FLOAT.equals(item.getType()) + )); + } + + @Test + public void testChildren() { + Assert.assertEquals(0, issue.createChildren().size()); + } + +} diff --git a/sources/codemetropolis-toolchain-converter/src/test/MilestoneTest.java b/sources/codemetropolis-toolchain-converter/src/test/MilestoneTest.java new file mode 100644 index 00000000..4d851724 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/test/MilestoneTest.java @@ -0,0 +1,89 @@ +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import codemetropolis.toolchain.converter.gitlab.*; +import codemetropolis.toolchain.converter.gitlab.model.Issue; +import codemetropolis.toolchain.converter.gitlab.model.Milestone; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +public class MilestoneTest { + + private Milestone milestone; + private static String MILESTONE_TITLE = "title"; + private static String MILESTONE_STATE = "state"; + + @Before + public void setUp() { + milestone = new Milestone(); + milestone.setState(MILESTONE_STATE); + milestone.setTitle(MILESTONE_TITLE); + milestone.setID("15"); + GitLabElement.setGitLabApi(null); + } + + @Test + public void testArraySize() { + List properties = milestone.createProperties(); + Assert.assertEquals(4, properties.size()); + } + + @Test + public void testIfPropertiesConsistsElements() { + List properties = milestone.createProperties(); + Assert.assertTrue(properties.stream().anyMatch(item->(MILESTONE_STATE.equals(item.getValue())) + && "state".equals(item.getName()) + && CdfProperty.Type.STRING.equals(item.getType()))); + } + + @Test + public void testIfMilestoneIDIsNull() { + milestone.setID(null); + Assert.assertEquals(true, milestone.createProperties().isEmpty()); + + } + + private Milestone addPair(Pair pair, String state) { + milestone.addChild(pair); + Issue issue = new Issue(); + issue.setID(pair.getID()); + issue.setState(state); + issue.setType(Type.ISSUE); + + try { + issue.getElement(); + } catch (GitLabException e) { + e.printStackTrace(); + } + + GitLabResource.addElement(issue); + + return milestone; + } + + @Test + public void testChildrenIssues() { + + addPair(new Pair("11", Type.ISSUE), "opened"); + addPair(new Pair("12", Type.ISSUE), "closed"); + + List properties=milestone.createProperties(); + + Assert.assertTrue(properties.stream().anyMatch(item->("0.5".equals(item.getValue())) + && "openedRatio".equals(item.getName()) && + CdfProperty.Type.FLOAT.equals(item.getType()))); + + Assert.assertTrue(properties.stream().anyMatch(item->("0.5".equals(item.getValue())) + && "closedRatio".equals(item.getName()) && + CdfProperty.Type.FLOAT.equals(item.getType()))); + + GitLabResource.clearMainStorage(); + } + + @Test + public void testChildrenMethodIfIDIsNull() { + milestone.setID(null); + Assert.assertEquals(0, milestone.createChildren().size()); + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/test/ProjectTest.java b/sources/codemetropolis-toolchain-converter/src/test/ProjectTest.java new file mode 100644 index 00000000..588d5e81 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/test/ProjectTest.java @@ -0,0 +1,52 @@ +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import codemetropolis.toolchain.converter.gitlab.GitLabElement; +import codemetropolis.toolchain.converter.gitlab.Pair; +import org.junit.BeforeClass; +import org.junit.Test; + +import codemetropolis.toolchain.commons.cdf.CdfProperty; +import codemetropolis.toolchain.converter.gitlab.model.Project; + +public class ProjectTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @Test + public void testCreateProperties() { + GitLabElement project = new Project(); + List l = project.createProperties(); + assertEquals(true, l.size() == 0); + } + + @Test + public void testAddProperties() { + Project p = new Project(); + List list = new ArrayList<>(); + + p.setCommitCount(33); + p.setJobArtifactsSize(3); + p.setJobArtifactsSize(10); + p.setLfsObjectSize(4); + p.setStorageSize(10); + p.setApprovalsNumber(3); + p.setForksCount(4); + p.setName("namae"); + + p.addProperties(list); + + assertEquals(true, list.size() == 7); + } + + @Test + public void testCreateChildren() { + GitLabElement project = new Project(); + List pairList = project.createChildren(); + assertEquals(false, pairList.size() != 0); + } +} diff --git a/sources/codemetropolis-toolchain-converter/src/test/TreeTest.java b/sources/codemetropolis-toolchain-converter/src/test/TreeTest.java new file mode 100644 index 00000000..04f5ca00 --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/test/TreeTest.java @@ -0,0 +1,38 @@ +import codemetropolis.toolchain.converter.gitlab.GitLabElement; +import codemetropolis.toolchain.converter.gitlab.model.Tree; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TreeTest { + + private static Tree tree; + private static String ID = "doc---master"; + + @Before + public void setUp() { + GitLabElement.setGitLabApi(null); + tree = new Tree(); + tree.setID(ID); + } + + @Test + public void branchAndNameHasBeenSplitted() { + tree.createProperties(); + Assert.assertEquals("master", tree.getBranchName()); + Assert.assertEquals("doc", tree.getName()); + } + + @Test + public void ifIDIsNullPropertyIsEmpty() { + tree.setID(null); + Assert.assertEquals(true, tree.createProperties().isEmpty()); + } + + @Test + public void ifIDisNullPChildrenIsEmpty() { + tree.setID(null); + Assert.assertEquals(true, tree.createChildren().isEmpty()); + } + +} diff --git a/sources/codemetropolis-toolchain-converter/src/test/UserTest.java b/sources/codemetropolis-toolchain-converter/src/test/UserTest.java new file mode 100644 index 00000000..5dd3445e --- /dev/null +++ b/sources/codemetropolis-toolchain-converter/src/test/UserTest.java @@ -0,0 +1,39 @@ +import static org.junit.Assert.*; + +import codemetropolis.toolchain.converter.gitlab.GitLabElement; +import org.junit.BeforeClass; +import org.junit.Test; + +import codemetropolis.toolchain.converter.gitlab.model.User; + +public class UserTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @Test + public void testAddingRatio() { + User user = new User(); + user.setAddingRatio(3, 0); + final float epsilon = 0.01f; + + assertEquals(true, user.getAddingRatio() == 0); + } + + @Test + public void testSetDeletionRatio() { + User user = new User(); + user.setDeletionRatio(3, 10); + final float epsilon = 0.01f; + + assertEquals(true, 0.3 - epsilon <= user.getDeletionRatio() && 0.3 + epsilon >= user.getDeletionRatio()); + } + + @Test + public void testCreateChildren() { + GitLabElement user = new User(); + assertEquals(0, user.createChildren().size()); + } + +}