Permalink
Browse files

added NUnit test for 7.x ozasmt load/save

added sample ozasmt file,
  • Loading branch information...
1 parent e903722 commit 56f609c7cb44abeb47dfacd39ce64bbbca3b8aa5 @DinisCruz DinisCruz committed Nov 25, 2012
@@ -1,16 +1,32 @@
// This file is part of the OWASP O2 Platform (http://www.owasp.org/index.php/OWASP_O2_Platform) and is released under the Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0)
using System;
+using System.Linq;
using System.Collections.Generic;
using O2.DotNetWrappers.DotNet;
using O2.Interfaces.O2Findings;
using O2.DotNetWrappers.ExtensionMethods;
using O2.Kernel;
using O2.XRules.ThirdPary.IBM;
//O2File:xsd_Ozasmt_OunceV7_0.cs
-//___O2File:OzasmtUtils_OunceV6.cs
+
+//O2File:Findings_ExtensionMethods.cs
+using O2.XRules.Database.Findings;
namespace O2.XRules.ThirdPary.IBM
{
+ public class O2AssessmentSave_OunceV7_test
+ {
+ public static void test()
+ {
+ var testFile = @"E:\_Work\IBM\8.6_files\8.6_files\AltoroJ_2.5 _Callbacks.ozasmt";
+
+ var findings = "cachedFindings".o2Cache(()=>testFile.loadO2Findings());
+ var assessmentSave = new O2AssessmentSave_OunceV7();
+
+ var savedFile = assessmentSave.save(findings);
+ }
+
+ }
public class O2AssessmentSave_OunceV7 : IO2AssessmentSave
{
public AssessmentRun assessmentRun {get;set;}
@@ -39,30 +55,33 @@ public bool save(List<IO2Finding> o2Findings, string sPathToSaveAssessment)
public bool save(string assessmentName, IEnumerable<IO2Finding> o2Findings, string sPathToSaveAssessment)
{
createAssessmentRunObject(assessmentName, o2Findings);
- return assessmentRun.saveAs(sPathToSaveAssessment);
- //return OzasmtUtils_OunceV6.SaveAssessmentRun(assessmentRun, sPathToSaveAssessment);
- return false;
- }
-
- private void createAssessmentRunObject(string assessmentName, IEnumerable<IO2Finding> o2Findings)
+ return assessmentRun.saveAs(sPathToSaveAssessment);
+ }
+ public void createAssessmentRunObject(IEnumerable<IO2Finding> o2Findings)
+ {
+ createAssessmentRunObject(assessmentRun.name, o2Findings);
+ }
+ public void createAssessmentRunObject(string assessmentName, IEnumerable<IO2Finding> o2Findings)
{
- assessmentRun.name = assessmentName ?? "";
- //assessmentRun.Assessment.owner_name = assessmentRun.name;
- assessmentRun.Assessment.assessee_name = assessmentRun.name;
- //assessmentRun.Assessment.owner_type = "Application";
- //addO2FindingsToAssessmentRunObject(o2Findings);
+ assessmentRun.name = assessmentName ?? "";
+ assessmentRun.Assessment.assessee_file = "";
+ assessmentRun.Assessment.assessee_name = assessmentRun.name;
+ assessmentRun.Assessment.assessee_type = "Application";
+ addO2FindingsToAssessmentRunObject(o2Findings);
}
-/*
- private AssessmentRun createAssessmentRunObject(IO2Assessment o2Assessment)
+
+ public AssessmentRun createAssessmentRunObject(IO2Assessment o2Assessment)
{
createAssessmentRunObject(o2Assessment.name, o2Assessment.o2Findings);
return assessmentRun;
}
public void addO2FindingsToAssessmentRunObject(IEnumerable<IO2Finding> o2Findings)
{
- Dictionary<string, List<AssessmentAssessmentFileFinding>> filesMappedToO2Findings =
- getFilesToO2FindingMappings(o2Findings);
+ assessmentRun.FilePool = getFilePool(o2Findings);
+ //Dictionary<string, List<AssessmentRunFile>> filesMappedToO2Findings = getFilePool(o2Findings);
+
+ /*
var assessmentFiles = new List<AssessmentAssessmentFile>();
foreach (string file in filesMappedToO2Findings.Keys)
{
@@ -74,8 +93,29 @@ public void addO2FindingsToAssessmentRunObject(IEnumerable<IO2Finding> o2Finding
assessmentFiles.Add(assessmentFile);
}
assessmentRun.Assessment.Assessment[0].AssessmentFile = assessmentFiles.ToArray();
+ */
}
-
+
+ public AssessmentRunFile[] getFilePool(IEnumerable<IO2Finding> o2Findings)
+ {
+
+ var uniqueFiles = (from o2Finding in o2Findings
+ where o2Finding.file != null
+ select o2Finding.file).distinct();
+
+ var filePool = new List<AssessmentRunFile>();
+
+ filePool.add(new AssessmentRunFile() { id=1 });
+ UInt32 id = 2;
+ foreach(var uniqueFile in uniqueFiles)
+ filePool.add(new AssessmentRunFile() { id = id++ , value = uniqueFile});
+
+ // filePool.show_In_ListView().makeColumnWidthMatchCellWidth();
+
+ return filePool.ToArray();
+
+ }
+/*
public Dictionary<String, List<AssessmentAssessmentFileFinding>> getFilesToO2FindingMappings(IEnumerable<IO2Finding> o2Findings)
{
var filesMappedToO2Findings = new Dictionary<string, List<AssessmentAssessmentFileFinding>>();
@@ -148,10 +188,22 @@ public bool addAssessmentStatsFromSourceToO2AssessmentAndSaveIt(string ozasmtSou
return OzasmtUtils_OunceV6.SaveAssessmentRun(assessmentRun, savedCreatedOzasmtAs);
}*/
}
-
-
+
public class O2Assessment_OunceV7_Utils
{
+ public static AssessmentRun getVersionFromDirectLoad(string ozasmtFile)
+ {
+ return OzasmtUtils_OunceV7_0.getAssessmentRunObjectFromXmlFile(ozasmtFile);
+ }
+
+ public static AssessmentRun getVersionFromSaveEngine(string ozasmtFile)
+ {
+ var findings = ozasmtFile.loadO2Findings();
+ var assessmentSave = new O2AssessmentSave_OunceV7();
+ assessmentSave.createAssessmentRunObject(findings);
+ return assessmentSave.assessmentRun;
+ }
+
public static AssessmentRun getDefaultAssessmentRunObject()
{
// this is what we need to create a default assessment
@@ -12,8 +12,9 @@ namespace O2.ImportExport.OunceLabs.Ozasmt_OunceV6_1
{
public class O2AssessmentLoad_OunceV6_1 : IO2AssessmentLoad
{
- public string engineName { get; set; }
-
+ public string engineName { get; set; }
+ public bool ShowErrorOnLoadFail { get; set; }
+
public O2AssessmentLoad_OunceV6_1()
{
engineName = "O2AssessmentLoad_OunceV6_1";
@@ -28,9 +29,9 @@ public bool canLoadFile(string fileToTryToLoad)
"Engine {0} can load file {1}".info(engineName, fileToTryToLoad);
return true;
}
-
- //PublicDI.log.debug("in {0} engine, could not load file {1} since the root element value didnt match the Regex: {2}!={3}",
- // engineName, fileToTryToLoad, rootElementText, expectedRootElementRegEx);
+ if(this.ShowErrorOnLoadFail)
+ "in {0} engine, could not load file {1} since the root element value didnt match the Regex: {2}!={3}".error(
+ engineName, fileToTryToLoad, rootElementText, expectedRootElementRegEx);
return false;
}
@@ -0,0 +1,87 @@
+// This file is part of the OWASP O2 Platform (http://www.owasp.org/index.php/OWASP_O2_Platform) and is released under the Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0)
+using System;
+using System.IO;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Text;
+using NUnit.Framework;
+using O2.Interfaces.O2Findings;
+using O2.DotNetWrappers.O2Findings;
+using O2.DotNetWrappers.ExtensionMethods;
+using O2.ImportExport.OunceLabs.Ozasmt_OunceV6;
+using O2.ImportExport.OunceLabs.Ozasmt_OunceV6_1;
+using O2.XRules.ThirdPary.IBM;
+using O2.XRules.Database.Findings;
+
+//O2File:O2AssessmentSave_OunceV7_0.cs
+//O2File:Findings_ExtensionMethods.cs
+//O2File:Findings_ExtensionMethods.cs
+
+//O2Ref:nunit.framework.dll
+
+namespace O2.XRules.Database.UnitTests
+{
+ [TestFixture]
+ public class LoadAndSave_Ozasmt8
+ {
+ public string TestFile { get; set; }
+ public List<IO2Finding> Findings_Load { get; set; }
+
+ public LoadAndSave_Ozasmt8()
+ {
+ TestFile = "JpetStore_8.6.ozasmt".local();
+ Findings_Load = TestFile.loadO2Findings();
+ }
+
+ [Test]
+ public void IsTestFileOk()
+ {
+ Assert.That(TestFile.fileExists(),"Couldn't find test file");
+ Assert.That(Findings_Load.size()>0, "There where no findings loaded");
+ }
+
+ [Test]
+ public void SaveAndLoad_Work()
+ {
+ // save findings using new O2AssessmentSave_OunceV7 engine
+ var savedFile = new O2AssessmentSave_OunceV7().save(Findings_Load);
+
+ // check that it exists
+ Assert.That(TestFile.fileExists(),"Couldn't find O2AssessmentSave_OunceV7 saved file");
+
+ // check that we can load the saved file with 7x
+ var o2Assessment = new O2AssessmentLoad_OunceV7_0().loadFile(savedFile);
+ Assert.That(o2Assessment.notNull(), "O2AssessmentLoad_OunceV7_0 failed to load");
+
+ // check that we CAN'T load the saved file with 6.1
+ o2Assessment = new O2AssessmentLoad_OunceV6_1().loadFile(savedFile);
+ Assert.That(o2Assessment.isNull(), "O2AssessmentLoad_OunceV6_1 failed to load");
+
+ // check that we CAN'T load the saved file with 6.0
+ o2Assessment = new O2AssessmentLoad_OunceV6().loadFile(savedFile);
+ Assert.That(o2Assessment.isNull(), "O2AssessmentLoad_OunceV6_0 failed to load");
+ }
+
+ [Test]
+ public void CheckAssessmentRunData_AfterLoad_and_InMemorySave()
+ {
+ var directlyLoaded_Assessment = O2Assessment_OunceV7_Utils.getVersionFromDirectLoad(TestFile);
+ var fromSaveEngine_Assessment = O2Assessment_OunceV7_Utils.getVersionFromSaveEngine(TestFile);
+
+ Assert.That(directlyLoaded_Assessment.name == fromSaveEngine_Assessment.name ,"name didn't match");
+ }
+
+
+ [Test]
+ public void SaveAndLoad_FindingsCountIsTheSame()
+ {
+ var savedFile = new O2AssessmentSave_OunceV7().save(Findings_Load);
+ var savedFindings = savedFile.loadO2Findings();
+ Assert.That(savedFindings.size() > 0 , "There where no findings loaded");
+ Assert.That(savedFindings.size() == Findings_Load.size() , "Finding's size doesn't match");
+ }
+// var findingsSaved = savedFile.loadO2Findings();
+// Assert.That(findingsSaved.size()>0, "There where no saved Findings loaded");
+// }
+ }
+}
@@ -1,47 +1,52 @@
<?xml version="1.0"?>
<H2>
- <SourceCode>
-//panel.clear();
-//var topPanel = panel.add_Panel();
+ <SourceCode>//var topPanel = panel.add_Panel(true);
var topPanel = O2Gui.open&lt;Panel&gt;("Write and Execute Unit Tests", 1000,600);
var codeEditor = topPanel.add_SourceCodeEditor();
var unitTest = codeEditor.insert_Right&lt;ascx_XRules_UnitTests&gt;(300);
unitTest.insert_Below&lt;Panel&gt;(130)
.add_LogViewer();
+
Action&lt;string&gt; loadFileAndCompile =
(file)=&gt;{
codeEditor.open(file);
codeEditor.compileSourceCode();
};
-
-var textBox = topPanel.insert_Above&lt;Panel&gt;(25)
- .add_LabelAndTextAndButton("Unit Test file","","load", loadFileAndCompile)
- .controls&lt;TextBox&gt;();
+TextBox textBox_LoadFile = null;
+Button button_LoadFile = null;
+topPanel.insert_Above&lt;Panel&gt;(25).add_LabelAndTextAndButton("Unit Test file","","load", ref textBox_LoadFile, ref button_LoadFile, loadFileAndCompile);
Action&lt;string&gt; loadFile =
(file)=&gt;{
- textBox.set_Text(file);
+ textBox_LoadFile.set_Text(file);
loadFileAndCompile(file);
};
-textBox.onDrop(
- (text)=&gt; loadFile(text));
+textBox_LoadFile.onDrop((text)=&gt; loadFile(text));
- //unitTest.loadFile(text));
+codeEditor.onCompile(
+ (assembly)=&gt;{
+ unitTest.loadFile(assembly.Location,true);
+ if(unitTest.control&lt;CheckBox&gt;(true).@checked())
+ unitTest.invoke("executeAllLoadedTests");
+ unitTest.XRulesTreeView_ExpandAll();
+ });
//
-
-//Default data
-textBox.set_Text("DefaultUnitTest.cs".local().fileContents().saveWithExtension(".cs"));
-textBox.sendEnter();
-
+//Default data
+var tempFile = "DefaultUnitTest.cs".local().fileContents().saveWithExtension(".cs");
+textBox_LoadFile.set_Text(tempFile);
+button_LoadFile.click();
+
return loadFile;
//return unitTest.type().methods();
//using O2.Core.XRules.Ascx
//O2Ref:O2_Core_XRules.dll
-//O2Ref:O2_External_O2Mono.dll</SourceCode>
+//O2Ref:O2_External_O2Mono.dll
+
+//O2File:_Extra_methods_To_Add_to_Main_CodeBase.cs</SourceCode>
<ReferencedAssemblies />
</H2>
@@ -268,10 +268,16 @@ public static Label bold(this Label label)
return label.font_bold();
}
}
- public static class Extra_compile_SplitContainer
+ public static class Extra_compile_Misc
{
-
-
+ // do this propely when adding to main O2 code base since this will not work as expected when there are other textboxes and buttons on the same 'control' object
+ public static T add_LabelAndTextAndButton<T>(this T control, string labelText, string textBoxText, string buttonText,ref TextBox textBox, ref Button button, Action<string> onButtonClick) where T : Control
+ {
+ control.add_LabelAndTextAndButton(labelText,textBoxText,buttonText, onButtonClick);
+ textBox = control.control<TextBox>();
+ button = control.control<Button>();
+ return control;
+ }
}
public static class Extra_compile_TextBox
Oops, something went wrong.

0 comments on commit 56f609c

Please sign in to comment.