Skip to content

Commit

Permalink
Add a test for tech stats service
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraZizka committed Nov 5, 2016
1 parent 38d19d2 commit e0e2b3e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
@@ -1,9 +1,14 @@
package org.jboss.windup.rules.apps.javaee.model.stats;

import com.tinkerpop.blueprints.Vertex;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.service.GraphService;

import com.tinkerpop.frames.FramedGraphQuery;
import com.tinkerpop.pipes.filter.BackFilterPipe;
import com.tinkerpop.pipes.transform.OutPipe;
import com.tinkerpop.pipes.util.Pipeline;
import com.tinkerpop.pipes.util.StartPipe;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -52,15 +57,11 @@ public TechnologiesStatsModel computeStats()
{
TechnologiesStatsModel stats = this.create();
stats.setComputed(new Date());
/*
stats.setFilesStats(new HashMap<String, GeneralStatsItemModel>(){{
put("foo", item(123));
put("bar", item(234));
}});*/

// Files type share
Map<String, Integer> suffixToCount = countFilesBySuffix();
Map<String, Integer> fileTypeShares = countFilesShareBySuffix(suffixToCount);
// This will need to filter out archives.

stats.setStatsFilesByTypeJavaPercent(item(fileTypeShares.getOrDefault("class", 0) + fileTypeShares.getOrDefault("java", 0)));
stats.setStatsFilesByTypeJsPercent(item(fileTypeShares.getOrDefault("js", 0)));
Expand Down Expand Up @@ -95,7 +96,7 @@ public TechnologiesStatsModel computeStats()
stats.setStatsServerResourcesJndiTotalEntries(item(countByType(JNDIResourceModel.class)));

// Not sure how to get this number. Maybe JavaClassFileModel.getJavaClass() ?
stats.setStatsJavaClassesOriginal(item(countByType(JavaClassModel.class, JavaClassModel.DECOMPILED_SOURCE, null)));
stats.setStatsJavaClassesOriginal(item((int) countJavaClassesOriginal()));
stats.setStatsJavaClassesTotal(item(countByType(JavaClassModel.class)));
// We are not able to tell which of the jars are original. We can substract known opensource libs.
stats.setStatsJavaJarsOriginal(item(countByType(JarArchiveModel.class) - countByType(IdentifiedArchiveModel.class)));
Expand Down Expand Up @@ -126,19 +127,19 @@ private <T extends WindupVertexFrame> int countByType(Class<T> clazz, String pro
return (int) count;
}


private <T extends WindupVertexFrame> String getTypeValueForModel(Class<T> clazz)
{
return TypeAwareFramedGraphQuery.getTypeValue(clazz);
}

private Map<String, Integer> countFilesBySuffix()
{
Map<String, Integer> suffixToCount = new HashMap<>();
Iterable<FileModel> files = this.getGraphContext().getQuery().type(FileModel.class).vertices(FileModel.class);
/// TODO this just takes any file in the graph. Need to resctrict to project files.
Iterable<FileModel> files = this.getGraphContext().getQuery()
.type(FileModel.class)
.hasNot(FileModel.IS_DIRECTORY, true)
.vertices(FileModel.class);

// TODO this just takes any file in the graph. Need to resctrict to project files.
files.forEach( (FileModel file) -> {
String suffix = StringUtils.substringAfterLast(file.getFileName(), "");
String suffix = StringUtils.substringAfterLast(file.getFileName(), ".");
if (suffix.isEmpty())
return;
Integer val = suffixToCount.get(suffix);
if (val == null)
suffixToCount.put(suffix, 1);
Expand All @@ -150,9 +151,10 @@ private Map<String, Integer> countFilesBySuffix()

private static Map<String, Integer> countFilesShareBySuffix(Map<String, Integer> suffixToCount){
int sum = suffixToCount.entrySet().stream().mapToInt(e -> e.getValue()).sum();
System.out.println("SUM: " + sum);
Map<String, Integer> shares = suffixToCount.entrySet().stream().collect(Collectors.toMap(
e -> e.getKey(),
e -> e.getValue() / sum
e -> e.getValue() * 100 / sum
));
return shares;
}
Expand Down Expand Up @@ -195,4 +197,20 @@ private long countGraphVertices(Class<? extends WindupVertexFrame> clazz, Map<St
return count;
}



// Methods for individual statistic items

private long countJavaClassesOriginal()
{
//new Pipeline<Vertex, Vertex>().
Iterable<Vertex> startVertices = new TypeAwareFramedGraphQuery(this.getGraphContext().getFramed()).type(JavaClassModel.class).vertices();
Pipeline<Vertex, Vertex> pipeline = new Pipeline<Vertex, Vertex>();
pipeline.addPipe(new StartPipe(startVertices));
final OutPipe outPipe = new OutPipe(JavaClassModel.DECOMPILED_SOURCE);
// The BackFilterPipe needs to wrap all pipes which it "go back before".
// This means ...out(...).back(1);
pipeline.addPipe(new BackFilterPipe(outPipe));
return pipeline.count();
}
}
2 changes: 1 addition & 1 deletion tests/pom.xml
Expand Up @@ -120,7 +120,7 @@
<groupId>org.jboss.windup</groupId>
<artifactId>windup-test-harness</artifactId>
<scope>test</scope>
</dependency>
</dependency>

</dependencies>

Expand Down
@@ -1,6 +1,9 @@
package org.jboss.windup.tests.application;

import java.io.File;
import java.io.OutputStreamWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
Expand All @@ -13,6 +16,10 @@
import org.jboss.windup.graph.service.ArchiveService;
import org.jboss.windup.rules.apps.java.model.JavaClassModel;
import org.jboss.windup.rules.apps.java.service.JavaClassService;
import org.jboss.windup.rules.apps.javaee.model.stats.TechnologiesStatsModel;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -46,11 +53,34 @@ public void testRunWindupTiny() throws Exception
try (GraphContext context = createGraphContext())
{
super.runTest(context, "../test-files/Windup1x-javaee-example-tiny.war", false);
validateTechReportData(context);
validateArchiveHashes(context);
validateJavaClassModels(context);
}
}

private void validateTechReportData(GraphContext context)
{
TechnologiesStatsModel stats = context.service(TechnologiesStatsModel.class).getUnique();
/*try {
JSONWriter jsonWriter = new org.json.JSONWriter(new OutputStreamWriter(System.out));
JSONObject json = new JSONObject(stats);
jsonWriter.value(json);
}
catch (JSONException ex) {
throw new RuntimeException("Failed to JSONify: " + ex.getMessage(), ex);
}*/

//SUM: 106 txt = 1 java = 20 xml = 12 ear = 0 war = 0 MF = 5 jar = 5 class = 47 properties = 4
Assert.assertTrue(stats.getStatsFilesByTypeJavaPercent().getQuantity() >= 20);

Assert.assertTrue(stats.getStatsJavaClassesTotal().getQuantity() > 0);
//Assert.assertTrue(stats.getStatsJavaJarsTotal().getQuantity() > 0);
Assert.assertTrue(stats.getComputed() != null);
}



private void validateArchiveHashes(GraphContext context) throws Exception
{
ArchiveService archiveService = new ArchiveService(context);
Expand Down

0 comments on commit e0e2b3e

Please sign in to comment.