Skip to content

Commit

Permalink
Add net conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Jul 25, 2023
1 parent 0c8c48c commit e87d963
Show file tree
Hide file tree
Showing 16 changed files with 253 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ jobs:
test-net:
runs-on: ubuntu-latest
if: ${{ false }} # disable for now
#if: ${{ false }} # disable for now
permissions:
checks: write
defaults:
Expand Down
31 changes: 31 additions & 0 deletions net/QACover/Giis.Tdrules.Model.IO/ModelJsonSerializer.N.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Newtonsoft.Json;
using System;

namespace Giis.Tdrules.Model.IO
{
// Temporal location, to be moved to tdrules-model
public class ModelJsonSerializer
{
/**
* Recommended serialization for models (exclude null and empty attributes)
*/
public string Serialize(object model, bool prettyPrint)
{
JsonSerializerSettings settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
JsonSerializer serializer = new JsonSerializer();
string result = JsonConvert.SerializeObject(model, prettyPrint ? Formatting.Indented : Formatting.None, settings);
return result;
}

/**
* Recommended deserialization for models
*/
public object Deserialize(string json, Type clazz)
{
return JsonConvert.DeserializeObject(json, clazz);
}
}
}
14 changes: 14 additions & 0 deletions net/QACover/Translated/Giis.Qacover.Core.Services/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class Configuration

private string storeRulesLocation;

private string cacheRulesLocation;

private string storeReportsLocation;

private string fpcServiceUrl;
Expand Down Expand Up @@ -240,6 +242,18 @@ public virtual void SetStoreRulesLocation(string location)
storeRulesLocation = location;
}

public virtual string GetCacheRulesLocation()
{
return cacheRulesLocation;
}

public virtual Giis.Qacover.Core.Services.Configuration SetCacheRulesLocation(string location)
{
// Ensures that valid location is relative to project root
cacheRulesLocation = location != null && !string.Empty.Equals(location) ? FileUtil.GetPath(Parameters.GetProjectRoot(), location) : string.Empty;
return this;
}

public virtual string GetStoreReportsLocation()
{
return storeReportsLocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Giis.Portable.Util;
using Giis.Qacover.Model;
using Giis.Qacover.Portable;
using Giis.Tdrules.Client;
using Giis.Tdrules.Client.Rdb;
using Giis.Tdrules.Model;
using Giis.Tdrules.Model.IO;
Expand Down Expand Up @@ -71,7 +70,7 @@ public virtual string GetRulesInput(string sql, SchemaModel schemaModel, string
public virtual string[] GetAllTableNames(string sql)
{
this.SetErrorContext("Get query table names");
SqlTableListBody model = GetApi().SqlTablesPost(sql);
SqlTableListBody model = GetApi().GetTables(sql);
InjectFaultIfNeeded(model);
if (!string.Empty.Equals(model.GetError()))
{
Expand All @@ -83,7 +82,7 @@ public virtual string[] GetAllTableNames(string sql)
public virtual QueryWithParameters InferQueryWithParameters(string sql)
{
this.SetErrorContext("Infer query parameters");
SqlParametersBody model = GetApi().SqlParametersPost(sql);
SqlParametersBody model = GetApi().GetParameters(sql);
InjectFaultIfNeeded(model);
if (!string.Empty.Equals(model.GetError()))
{
Expand Down
89 changes: 89 additions & 0 deletions net/QACover/Translated/Giis.Qacover.Core.Services/TdRulesApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/////////////////////////////////////////////////////////////////////////////////////////////
/////// THIS FILE HAS BEEN AUTOMATICALLY CONVERTED FROM THE JAVA SOURCES. DO NOT EDIT ///////
/////////////////////////////////////////////////////////////////////////////////////////////
using Giis.Tdrules.Openapi.Model;


namespace Giis.Qacover.Core.Services
{
/// <summary>Temporal implementation of the client api, to be moved to tdrules-client</summary>
public class TdRulesApi : Giis.Tdrules.Client.TdRulesApi
{
private bool useCache;

private string cacheLocation;

public TdRulesApi(string uri)
: base(uri)
{
this.cacheLocation = Giis.Qacover.Core.Services.Configuration.GetInstance().GetCacheRulesLocation();
this.useCache = !string.Empty.Equals(Coalesce(cacheLocation));
}

/// <summary>Gets the fpc rules for a query executed under the specified schema</summary>
public SqlRules GetRules(DbSchema schema, string query, string options)

Check warning on line 24 in net/QACover/Translated/Giis.Qacover.Core.Services/TdRulesApi.cs

View workflow job for this annotation

GitHub Actions / test-net

'TdRulesApi.GetRules(DbSchema, string, string)' hides inherited member 'TdRulesApi.GetRules(DbSchema, string, string)'. Use the new keyword if hiding was intended.

Check warning on line 24 in net/QACover/Translated/Giis.Qacover.Core.Services/TdRulesApi.cs

View workflow job for this annotation

GitHub Actions / test-net

'TdRulesApi.GetRules(DbSchema, string, string)' hides inherited member 'TdRulesApi.GetRules(DbSchema, string, string)'. Use the new keyword if hiding was intended.

Check warning on line 24 in net/QACover/Translated/Giis.Qacover.Core.Services/TdRulesApi.cs

View workflow job for this annotation

GitHub Actions / test-net

'TdRulesApi.GetRules(DbSchema, string, string)' hides inherited member 'TdRulesApi.GetRules(DbSchema, string, string)'. Use the new keyword if hiding was intended.
{
SqlRulesBody request = new SqlRulesBody();
request.SetSchema(schema);
request.SetSql(query);
request.SetOptions(Coalesce(options));
TdRulesCache cache = GetCache("rulesPost", request);
if (UseCache() && cache.Hit())
{
return (SqlRules)cache.GetPayload(typeof(SqlRules));
}
SqlRules result = base.RulesPost(request);
if (UseCache())
{
cache.PutPayload(result);
}
return result;
}

public virtual SqlTableListBody GetTables(string sql)
{
TdRulesCache cache = GetCache("sqlTablesPost", sql);
if (UseCache() && cache.Hit())
{
return (SqlTableListBody)cache.GetPayload(typeof(SqlTableListBody));
}
SqlTableListBody result = base.SqlTablesPost(sql);
if (UseCache())
{
cache.PutPayload(result);
}
return result;
}

public virtual SqlParametersBody GetParameters(string sql)
{
TdRulesCache cache = GetCache("sqlParametersPost", sql);
if (UseCache() && cache.Hit())
{
return (SqlParametersBody)cache.GetPayload(typeof(SqlParametersBody));
}
SqlParametersBody result = base.SqlParametersPost(sql);
if (UseCache())
{
cache.PutPayload(result);
}
return result;
}

// Cache management
private bool UseCache()
{
return this.useCache;
}

private TdRulesCache GetCache(string endpoint, object request)
{
return UseCache() ? new TdRulesCache(this.cacheLocation, endpoint, request) : null;
}

private string Coalesce(string value)
{
return value == null ? string.Empty : value;
}
}
}
75 changes: 75 additions & 0 deletions net/QACover/Translated/Giis.Qacover.Core.Services/TdRulesCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/////////////////////////////////////////////////////////////////////////////////////////////
/////// THIS FILE HAS BEEN AUTOMATICALLY CONVERTED FROM THE JAVA SOURCES. DO NOT EDIT ///////
/////////////////////////////////////////////////////////////////////////////////////////////
using System;
using Giis.Portable.Util;
using Giis.Tdrules.Model.IO;
using NLog;


namespace Giis.Qacover.Core.Services
{
/// <summary>
/// Temporal implementation of a local storage cache for payloads sent to
/// TdRules, to be moved to tdrules-client
/// </summary>
public class TdRulesCache
{
private static readonly Logger log = NLogUtil.GetLogger(typeof(Giis.Qacover.Core.Services.TdRulesCache));

private ModelJsonSerializer serializer;

internal string endpoint;

internal string payload;

internal string hash;

internal string cacheFile;

internal string hit;

public TdRulesCache(string cacheFolder, string endpoint, object request)
{
serializer = new ModelJsonSerializer();
this.endpoint = endpoint;
this.payload = serializer.Serialize(request, true);
this.hash = JavaCs.GetHash(payload);
this.EnsureCacheFolder(cacheFolder, endpoint);
this.cacheFile = GetCacheFile(cacheFolder, endpoint, hash);
this.hit = FileUtil.FileRead(cacheFile, false);
log.Debug("Cache {} {} hit: {}", endpoint, hash, this.hit != null);
}

/// <summary>
/// Gets the payload of a given request from cache, if does not exists returns
/// null
/// </summary>
public virtual bool Hit()
{
return this.hit != null;
}

public virtual object GetPayload(Type clazz)
{
return serializer.Deserialize(hit, clazz);
}

/// <summary>Saves the response payload of a given request to cache</summary>
public virtual void PutPayload(object result)
{
FileUtil.FileWrite(cacheFile, serializer.Serialize(result, true));
log.Debug("Cache {} {} update: {}", endpoint, hash, result);
}

private void EnsureCacheFolder(string cacheFolder, string endpoint)
{
FileUtil.CreateDirectory(FileUtil.GetPath(cacheFolder, endpoint));
}

private string GetCacheFile(string cacheFolder, string endpoint, string hash)
{
return FileUtil.GetPath(cacheFolder, endpoint, hash + ".json");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Java.Util;
using NUnit.Framework;

using Test4giis.Qacover;
using Test4giis.Qacoverapp;

namespace Test4giis.Qacover.Model
Expand All @@ -20,13 +21,13 @@ public class TestUtil
[NUnit.Framework.SetUp]
public virtual void SetUp()
{
Configuration.GetInstance().Reset();
Base.ConfigureTestOptions();
}

[NUnit.Framework.TearDown]
public virtual void TearDown()
{
Configuration.GetInstance().Reset();
Base.ConfigureTestOptions();
}

/// <exception cref="Java.Sql.SQLException"/>
Expand Down
8 changes: 7 additions & 1 deletion net/QACoverTest/Translated/Test4giis.Qacover/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ public virtual void SetUp()
variant = GetVariant();
log.Info("*** CURRENT DBMS - " + variant.GetSgbdName());
rs = null;
options = Configuration.GetInstance().Reset().SetName("qacovertest");
options = ConfigureTestOptions();
new StoreService(options).DropRules().DropLast();
QueryStatement.SetFaultInjector(null);
//Locale.SetDefault(new Locale("en", "US"));
}

/// <summary>Default configuration for tests</summary>
public static Configuration ConfigureTestOptions()
{
return Configuration.GetInstance().Reset().SetName("qacovertest").SetCacheRulesLocation(".tdrules-cache");
}

/// <exception cref="Java.Sql.SQLException"/>
[NUnit.Framework.TearDown]
public virtual void TearDown()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public virtual void TestEvalInferParametersNegative()
[Test]
public virtual void TestEvalFpcOptions()
{
Configuration.GetInstance().Reset().SetFpcServiceOptions("noboundaries");
ConfigureTestOptions().SetFpcServiceOptions("noboundaries");
rs = app.QueryNoParameters1Condition(-1);
AssertEvalResults("select id,num,text from test where num>=-1", "1 0 abc\n2 99 xyz\n3 99 NULL", SqlUtil.ResultSet2csv(rs, " "), "UNCOVERED SELECT id , num , text FROM test WHERE NOT(num >= -1)\n" + "COVERED SELECT id , num , text FROM test WHERE (num >= -1)");
}
Expand Down Expand Up @@ -145,7 +145,7 @@ public virtual void TestEvalNoConditions()
[Test]
public virtual void TestEvalNoConditionsInferParams()
{
Configuration.GetInstance().Reset().SetInferQueryParameters(true);
Base.ConfigureTestOptions().SetInferQueryParameters(true);
rs = app.QueryNoConditions();
AssertEvalResults("SELECT id , num , text FROM test", "1 0 abc\n2 99 xyz\n3 99 NULL", SqlUtil.ResultSet2csv(rs, " "), string.Empty);
}
Expand Down
3 changes: 2 additions & 1 deletion net/QACoverTest/Translated/Test4giis.Qacover/TestFaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ private bool Contains(string text, string substring)
[Test]
public virtual void TestFaultConnectingService()
{
options.SetFpcServiceUrl("http://giis.uniovi.es/noexiste.xml");
options.SetFpcServiceUrl("http://giis.uniovi.es/noexiste.xml").SetCacheRulesLocation(string.Empty);
// disable cache to run the actual service
rs = app.ExecuteQuery("select id,num,text from test where num<9");
AssertExceptionMessage(new Variability().IsJava() ? "Error at Get query table names: ApiException" : "Error at Get query table names: Giis.Tdrules.Openapi.Client.ApiException: Error calling SqlTablesPost", StoreService.GetLast().GetLastGenStatus());
}
Expand Down
10 changes: 5 additions & 5 deletions net/QACoverTest/Translated/Test4giis.Qacover/TestStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ public virtual void TestSameQueriesDifferentLine()
public virtual void TestAbortByTableExclusion()
{
// full match (implies an abort on the rule evaluation)
Configuration.GetInstance().Reset().SetFpcServiceOptions("noboundaries").AddTableExclusionExact("test");
Base.ConfigureTestOptions().SetFpcServiceOptions("noboundaries").AddTableExclusionExact("test");
rs = app.QueryNoParameters1Condition(-1);
AssertAbort("{}");
// approximate match (no abort)
Configuration.GetInstance().Reset().SetFpcServiceOptions("noboundaries").AddTableExclusionExact("tes");
Base.ConfigureTestOptions().SetFpcServiceOptions("noboundaries").AddTableExclusionExact("tes");
rs = app.QueryNoParameters1Condition(-1);
AssertNoAbort();
}
Expand All @@ -243,15 +243,15 @@ public virtual void TestAbortByTableExclusion()
public virtual void TestAbortByClassExclusion()
{
// full match (abort)
Configuration.GetInstance().Reset().SetFpcServiceOptions("noboundaries").AddClassExclusion("test4giis.qacoverapp.AppSimpleJdbc");
Base.ConfigureTestOptions().SetFpcServiceOptions("noboundaries").AddClassExclusion("test4giis.qacoverapp.AppSimpleJdbc");
rs = app.QueryNoParameters1Condition(-1);
AssertAbort(string.Empty);
// partial match
Configuration.GetInstance().Reset().SetFpcServiceOptions("noboundaries").AddClassExclusion("test4giis.qacoverapp.AppSimpleJdb");
Base.ConfigureTestOptions().SetFpcServiceOptions("noboundaries").AddClassExclusion("test4giis.qacoverapp.AppSimpleJdb");
rs = app.QueryNoParameters1Condition(-1);
AssertAbort(string.Empty);
// no match, normal processing
Configuration.GetInstance().Reset().SetFpcServiceOptions("noboundaries").AddClassExclusion("test4giis.qacoverapp.AppSimpleJdbx");
Base.ConfigureTestOptions().SetFpcServiceOptions("noboundaries").AddClassExclusion("test4giis.qacoverapp.AppSimpleJdbx");
rs = app.QueryNoParameters1Condition(-1);
AssertNoAbort();
}
Expand Down
9 changes: 7 additions & 2 deletions net/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,13 @@
<concatfilter prepend="sharpen-autogenerated-note.txt"/>
</filterchain>
</copy>

<!-- IMPORTANT: En java el p6spy se configura con un fichero de propiedades, en .net con una variable de entorno.
<!-- temporal until caches be moved to tdrules-client -->
<replace file="QACover/Translated/Giis.Qacover.Core.Services/TdRulesApi.cs"
token=" Configuration.GetInstance()" value=" Giis.Qacover.Core.Services.Configuration.GetInstance()" />
<replace file="QACover/Translated/Giis.Qacover.Core.Services/TdRulesApi.cs"
token="public override SqlRules GetRules" value="public SqlRules GetRules" />

<!-- IMPORTANT: En java el p6spy se configura con un fichero de propiedades, en .net con una variable de entorno.
Para tests se cambia directamente una variable -->
<replace file="QACoverTest/Translated/Test4giis.Qacover/Base.cs" token="// PLACEHOLDER FOR .NET STATIC INITIALIZATION - DO NOT CHANGE"
value="static Base() { Giis.Qacover.Driver.EventTrigger.SetListenerClassName(&quot;Giis.Qacover.Driver.EventListener&quot;); }" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ public String getCacheRulesLocation() {
return cacheRulesLocation;
}
public Configuration setCacheRulesLocation(String location) {
cacheRulesLocation = location;
// Ensures that valid location is relative to project root
cacheRulesLocation = location != null && !"".equals(location) ? FileUtil.getPath(Parameters.getProjectRoot(), location) : "";
return this;
}
public String getStoreReportsLocation() {
Expand Down
Loading

0 comments on commit e87d963

Please sign in to comment.