public void Init()
 {
     _graphClient = new GraphClient(new Uri("http://localhost:7475/db/data"), "neo4j", "extra");
     _graphClient.Connect();
     _graphClient.BeginTransaction();
     _dataGenerator = new DbTestDataGenerator(_graphClient);
 }
    public void CreateRootNode()
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans        = graphClient.BeginTransaction();
        var          membersQuery = graphClient.Cypher
                                    .Match("(n:Root {name :'FIP', Creator : 'Coskun Kucuk'})")
                                    .Return(n => n.As <GetroRoot>()).Results;

        if (membersQuery.Count() == 0)
        {
            graphClient.Cypher.Create("(n:Root {name :'FIP', Creator : 'Coskun Kucuk'})").ExecuteWithoutResults();
            trans.Commit();
        }
    }
    private void CreateRelationCoordinateLatitude(Geocode_Node GeoNode, GeoLatitude lat)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans      = graphClient.BeginTransaction();
        var          NodeQuery2 = graphClient.Cypher
                                  .Match("(lt:Latitude   {Geo_lat : '" + lat.Geo_lat + "'})")
                                  .Return(lt => lt.As <GetroCoordinate>()).Results;

        if (NodeQuery2.Count() == 0)
        {
            graphClient.Cypher
            .Match("(geo:Geocode { name: '" + GeoNode.name + "'})")
            .Create("geo-[:latitude]->(lt:Latitude{ LatitudeData })")
            .WithParam("LatitudeData", lat)
            .ExecuteWithoutResults();
        }
        trans.Commit();
    }
    public void CreateRelationCompany_Facebook(Facebook_Node FbNode, Company_Node CompNode)
    {
        GraphClient graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans     = graphClient.BeginTransaction();
        var          NodeQuery = graphClient.Cypher
                                 .Match("(f:Facebook {name : '" + FbNode.name + "',fb_url : '" + FbNode.fb_url + "'})")
                                 .Return(f => f.As <GetroSocialMedia>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(c:" + CompNode.name.Replace(" ", "") + ")")
            .Create("c-[:Facebook]->(f:Facebook { FacebookData })")
            .WithParam("FacebookData", FbNode)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationCompany_Twitter(Twitter_Node TwNode, Company_Node CompNode)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(t:Twitter {name : '" + TwNode.name + "',tw_url : '" + TwNode.tw_url + "'})")
                        .Return(t => t.As <GetroSocialMedia>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(c:" + CompNode.name.Replace(" ", "") + ")")
            .Create("c-[:Twitter]->(t:Twitter { TwitterData })")
            .WithParam("TwitterData", TwNode)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationShodan_CountryName(Shodan_Node ShNode, Shodan_Node.SmallNodes_CountryName CountryName)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(sn:country_name {country_name : '" + CountryName.country_name + "'})")
                        .Return(sn => sn.As <Getro>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(sh:Shodan {name: '" + ShNode.name + "'})")
            .Create("sh-[:COUNTRY]->(sn:CountryName{ CountryName })")
            .WithParam("CountryName", CountryName)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationShodan_LastUpdate(Shodan_Node ShNode, Shodan_Node.SmallNodes_LastUp LastUp)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(sn:LastUpdate {last_update : '" + LastUp.last_update + "'})")
                        .Return(sn => sn.As <Getro>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(sh:Shodan {name:'" + ShNode.name + "'})")
            .Create("sh-[:LastUpdate]->(sn:LastUp{ LastUp })")
            .WithParam("LastUp", LastUp)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationShodan_Isp(Shodan_Node ShNode, Shodan_Node.SmallNodes_Isp isp)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(sn:Isp {isp : '" + isp.isp + "'})")
                        .Return(sn => sn.As <Getro>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(sh: Shodan {name:'" + ShNode.name + "'})")
            .Create("sh-[:ISP]->(sn:Isp{ ispData })")
            .WithParam("ispData", isp)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationShodan_org(Shodan_Node ShNode, Shodan_Node.SmallNodes_Org org)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(sn:Org {org : '" + org.org + "'})")
                        .Return(sn => sn.As <Getro>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(sh: Shodan{name : '" + ShNode.name + "'})")
            .Create("sh-[:ORG]->(sn:Org{ OrgData })")
            .WithParam("OrgData", org)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationRoot_Company(Company_Node CompNode)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var membersQuery = graphClient.Cypher
                           .Match("(c:" + CompNode.name.Replace(" ", "") + "{comp_host: '" + CompNode.comp_host + "'})")
                           .Return(c => c.As <GetroRoot>()).Results;

        if (membersQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(n:Root)")
            .Create("n-[:Company]->(c:" + CompNode.name.Replace(" ", "") + " { CompanyData })")
            .WithParam("CompanyData", CompNode)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationCompany_Whois(Whois_Node WiNode, Company_Node CompNode)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(wh:Whois  {WhoisText : '" + WiNode.WhoisText + "'})")
                        .Return(wh => wh.As <Getro>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(c:" + CompNode.name.Replace(" ", "") + ")")
            .Create("c-[:Whois]->(wh:Whois{ WhoisData })")
            .WithParam("WhoisData", WiNode)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationCompany_Wikipedia(Wikipedia_Node WikiNode, Company_Node CompNode)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(w:Wikipedia  {name : '" + WikiNode.name + "',description : '" + WikiNode.description + "'})")
                        .Return(w => w.As <GetroSocialMedia>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(c:" + CompNode.name.Replace(" ", "") + ")")
            .Create("c-[:Wikipedia]->(w:Wikipedia { WikipediaData })")
            .WithParam("WikipediaData", WikiNode)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationCompany_GoogleNews(GoogleNews_Node GnNode, Company_Node CompNode)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(gn:GoogleNews  {name : '" + GnNode.name + "',gn_url : '" + GnNode.gn_url + "'})")
                        .Return(gn => gn.As <GetroSocialMedia>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(c:" + CompNode.name.Replace(" ", "") + ")")
            .Create("c-[:GoogleNews]->(gn:GoogleNews { GoogleNewsData })")
            .WithParam("GoogleNewsData", GnNode)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationCompany_Reddit(Reddit_Node RedNode, Company_Node CompNode)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(r:Reddit  {name : '" + RedNode.name + "',red_url : '" + RedNode.red_url + "'})")
                        .Return(r => r.As <GetroSocialMedia>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(c:" + CompNode.name.Replace(" ", "") + ")")
            .Create("c-[:Reddit]->(r:Reddit { RedditData })")
            .WithParam("RedditData", RedNode)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationCompany_Linkedin(Linkedin_Node LinNode, Company_Node CompNode)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(l:Linkedin {name : '" + LinNode.name + "',lin_url : '" + LinNode.lin_url + "'})")
                        .Return(l => l.As <GetroSocialMedia>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(c:" + CompNode.name.Replace(" ", "") + ")")
            .Create("c-[:Linkedin]->(l:Linkedin { LinkedinData })")
            .WithParam("LinkedinData", LinNode)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationShodan_ASN(Shodan_Node ShNode, Shodan_Node.SmallNodes_ASN ASN)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(sn:asn {asn : '" + ASN.asn + "'})")
                        .Return(sn => sn.As <Getro>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(sh:Shodan {name:'" + ShNode.name + "'})")
            .Create("sh-[:ASN]->(sn:asn{ ASN })")
            .WithParam("ASN", ASN)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationShodan_Hosts(Shodan_Node ShNode, Shodan_Node.SmallNodes_Hosts hosts)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(sn:hosts {hosts : '" + hosts.hosts + "'})")
                        .Return(sn => sn.As <Getro>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(sh:Shodan {name:'" + ShNode.name + "'})")
            .Create("sh-[:HOST]->(sn:hosts{ hosts })")
            .WithParam("hosts", hosts)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationCompany_Shodan(Shodan_Node ShNode, Company_Node CompNode)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(sh:Shodan  {name : '" + ShNode.name + "'})")
                        .Return(sh => sh.As <Getro>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(c:" + CompNode.name.Replace(" ", "") + ")")
            .Create("c-[:Shodan]->(sh:Shodan{ ShodanData })")
            .WithParam("ShodanData", ShNode)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
    public void CreateRelationShodan_IPsTR(Shodan_Node ShNode, Shodan_Node.SmallNodes_IPsTR ipStr)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(sn:ip_str {ip_str : '" + ipStr.ip_str + "'})")
                        .Return(sn => sn.As <Getro>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(sh: Shodan {name :'" + ShNode.name + "'})")
            .Create("sh-[:IPSTR]->(sn:ip_str{ ipStr })")
            .WithParam("ipStr", ipStr)
            .ExecuteWithoutResults();
            trans.Commit();
        }
    }
Exemplo n.º 20
0
        public void SerializationError()
        {
            _client.Connect();
            _client.BeginTransaction();
            var data = new SomeType()
            {
                Date = DateTime.Today, Name = "Yolo"
            };

            _client
            .Cypher
            .Create("(n:SomeType {data})")
            .WithParam("data", data)
            .ExecuteWithoutResults();

            var actual = _client.Cypher.Match("(n:SomeType {Name:'Yolo'})").Return(n => n.As <SomeType>()).Results.First();

            Assert.AreEqual(actual, data);

            _client.EndTransaction();
        }
    public void CreateRelationCompany_Geocode(Geocode_Node GeoNode, Company_Node CompNode, GeoLongitude lon, GeoLatitude lat)
    {
        var graphClient = new GraphClient(new Uri(uri), Db_id, DB_pass);

        graphClient.Connect();
        ITransaction trans = graphClient.BeginTransaction();

        var NodeQuery = graphClient.Cypher
                        .Match("(geo:Geocode   {name : '" + GeoNode.name + "'})")
                        .Return(geo => geo.As <GetroSocialMedia>()).Results;

        if (NodeQuery.Count() == 0)
        {
            graphClient.Cypher
            .Match("(c:" + CompNode.name.Replace(" ", "") + ")")
            .Create("c-[:Geocode]->(geo:Geocode { GeocodeData })")
            .WithParam("GeocodeData", GeoNode)
            .ExecuteWithoutResults();
        }
        trans.Commit();
        CreateRelationCoordinateLongitude(GeoNode, lon, lat);
    }
 public void BeginTransactionShouldFailWithoutConnectingFirst()
 {
     var client = new GraphClient(new Uri("http://foo/db/data"), null);
     Assert.That(() => client.BeginTransaction(), Throws.InvalidOperationException);
 }
        public async Task BeginTransactionShouldFailWithoutConnectingFirst()
        {
            var client = new GraphClient(new Uri("http://foo/db/data"), null);

            Assert.Throws <InvalidOperationException>(() => client.BeginTransaction());
        }
        public void ExecuteMultipleStatementInOneRequestHttpRequest()
        {
            const string headerName = "MyTestHeader";
            const string headerValue = "myTestHeaderValue";
            var customHeaders = new NameValueCollection { {headerName, headerValue} };
            

            var initTransactionRequest = MockRequest.PostJson("/transaction", @"{
                'statements': [{'statement': 'MATCH n\r\nRETURN count(n)', 'resultDataContents':[], 'parameters': {}}, {'statement': 'MATCH t\r\nRETURN count(t)', 'resultDataContents':[], 'parameters': {}}]}");
            var commitRequest = MockRequest.PostJson("/transaction/1/commit", @"{'statements': []}");
            using (var testHarness = new RestTestHarness
            {
                {
                    initTransactionRequest,
                    MockResponse.Json(201, TransactionRestResponseHelper.GenerateInitTransactionResponse(1), "http://foo/db/data/transaction/1")
                },
                {
                    commitRequest, MockResponse.Json(200, @"{'results':[], 'errors':[] }")
                }
            })
            {
                var response = MockResponse.NeoRoot20();
                testHarness.Add(MockRequest.Get(""), response);
                var httpClient = testHarness.GenerateHttpClient("http://foo/db/data");
                testHarness.CreateAndConnectTransactionalGraphClient();
                ITransactionalGraphClient client = new GraphClient(new Uri("http://foo/db/data"), httpClient);
                client.Connect();
                using (var transaction = client.BeginTransaction())
                {
                    // dummy query to generate request
                    var rawClient = client as IRawGraphClient;
                    if (rawClient == null)
                    {
                        Assert.Fail("ITransactionalGraphClient is not IRawGraphClient");
                    }

                    var queries = new List<CypherQuery>()
                    {
                        client.Cypher
                            .Match("n")
                            .Return(n => n.Count())
                            .Query,
                        client.Cypher
                            .Match("t")
                            .Return(t => t.Count())
                            .Query
                    };
                    httpClient.ClearReceivedCalls();
                    rawClient.ExecuteMultipleCypherQueriesInTransaction(queries, customHeaders);
                    transaction.Commit();

                    var calls = httpClient.ReceivedCalls().ToList();
                    Assert.IsNotEmpty(calls);

                    HttpRequestMessage requestMessage = null;

                    foreach (var call in calls)
                    {
                        if (call.GetArguments().Single().GetType() == typeof (HttpRequestMessage))
                        {
                            requestMessage = (HttpRequestMessage) call.GetArguments().Single();
                        }
                    }

                    Assert.IsNotNull(requestMessage);

                    var customHeader = requestMessage.Headers.Single(h => h.Key == headerName);
                    Assert.IsNotNull(customHeader);
                    Assert.AreEqual(headerValue, customHeader.Value.Single());
                }
            }
        }
Exemplo n.º 25
0
        public void ExecuteMultipleStatementInOneRequestHttpRequest()
        {
            const string headerName    = "MyTestHeader";
            const string headerValue   = "myTestHeaderValue";
            var          customHeaders = new NameValueCollection {
                { headerName, headerValue }
            };


            var initTransactionRequest = MockRequest.PostJson("/transaction", @"{
                'statements': [{'statement': 'MATCH n\r\nRETURN count(n)', 'resultDataContents':[], 'parameters': {}}, {'statement': 'MATCH t\r\nRETURN count(t)', 'resultDataContents':[], 'parameters': {}}]}");
            var commitRequest          = MockRequest.PostJson("/transaction/1/commit", @"{'statements': []}");

            using (var testHarness = new RestTestHarness
            {
                {
                    initTransactionRequest,
                    MockResponse.Json(201, TransactionRestResponseHelper.GenerateInitTransactionResponse(1), "http://foo/db/data/transaction/1")
                },
                {
                    commitRequest, MockResponse.Json(200, @"{'results':[], 'errors':[] }")
                }
            })
            {
                var response = MockResponse.NeoRoot20();
                testHarness.Add(MockRequest.Get(""), response);
                var httpClient = testHarness.GenerateHttpClient("http://foo/db/data");
                testHarness.CreateAndConnectTransactionalGraphClient();
                ITransactionalGraphClient client = new GraphClient(new Uri("http://foo/db/data"), httpClient);
                client.Connect();
                using (var transaction = client.BeginTransaction())
                {
                    // dummy query to generate request
                    var rawClient = client as IRawGraphClient;
                    if (rawClient == null)
                    {
                        Assert.Fail("ITransactionalGraphClient is not IRawGraphClient");
                    }

                    var queries = new List <CypherQuery>()
                    {
                        client.Cypher
                        .Match("n")
                        .Return(n => n.Count())
                        .Query,
                        client.Cypher
                        .Match("t")
                        .Return(t => t.Count())
                        .Query
                    };
                    httpClient.ClearReceivedCalls();
                    rawClient.ExecuteMultipleCypherQueriesInTransaction(queries, customHeaders);
                    transaction.Commit();

                    var calls = httpClient.ReceivedCalls().ToList();
                    Assert.IsNotEmpty(calls);

                    HttpRequestMessage requestMessage = null;

                    foreach (var call in calls)
                    {
                        if (call.GetArguments().Single().GetType() == typeof(HttpRequestMessage))
                        {
                            requestMessage = (HttpRequestMessage)call.GetArguments().Single();
                        }
                    }

                    Assert.IsNotNull(requestMessage);

                    var customHeader = requestMessage.Headers.Single(h => h.Key == headerName);
                    Assert.IsNotNull(customHeader);
                    Assert.AreEqual(headerValue, customHeader.Value.Single());
                }
            }
        }
        public void BeginTransactionShouldFailWithoutConnectingFirst()
        {
            var client = new GraphClient(new Uri("http://foo/db/data"), null);

            client.BeginTransaction();
        }
 public void BeginTransactionShouldFailWithoutConnectingFirst()
 {
     var client = new GraphClient(new Uri("http://foo/db/data"), null);
     client.BeginTransaction();
 }
Exemplo n.º 28
0
 public ITransaction BeginTransaction()
 {
     return(_graphClient.BeginTransaction());
 }