Book a Demo

Author Topic: Cannot find the Element created from Test  (Read 6207 times)

integrationsmaster

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Cannot find the Element created from Test
« on: June 13, 2020, 12:41:34 am »
Hello everyone,
I am writing some MS tests for a service i wrote, in which i create elements and add them to a given package. The same code works from an addin.

In my Test, i am trying to test my connector creation, in the test buildup, i create 2 elements of type "Requirement" and then i create a connector between them. During the creation of the elements i create(if not exists) a diagram and add diagram-object  for each element.

Whats happening is when i create the second Element, the first element is unlinked from the package and the diagram during the created during creation of Element 1 also disappears.

If i open project in EA15 and search for all elements i can see the Element1 along with Element2 but i cant locate the Element1 in the project browser.

And because of this when i try to create a connector between the 2 Elements i get "System.Runtime.InteropServices.COMException (0x800403E9): Cannot update connector as either the Start or the End object is NULL"

When i am printing the ID, Name, other props of both the elements during creation of connector, i can can see they look all correct.

The code below is my create Element method. i call this twice to make the 2 elements
Code: [Select]
public bool CreateElement(...) {

parentPkg = CreateOrGetParentPkg(inputEle);
         
string eleType = inputEle.Type;
// name and type are passed are args to method
ele = parentPkg.Elements.AddNew(eleName, eleType);

// creating diagram
Diagram rootDiagram = CreateOrGetDiagram(parentPkg, parentPkg.Name);
parentPkg.Elements.Refresh();
parentPkg.Update();

// Add element to diagram - autogenerate position
DiagramObject diagramObject = (DiagramObject)rootDiagram.DiagramObjects.AddNew(GetDiagramObjectPosition(ele.Name.Length), "");
diagramObject.ElementID = ele.ElementID;
diagramObject.Update();
rootDiagram.Update();
rootDiagram.DiagramObjects.Refresh();
parentPkg.Update();
parentPkg.Diagrams.Refresh();
ele.Update();
}

Any pointers in figuring out why this is happening will be highly appreciated, thanks

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Cannot find the Element created from Test
« Reply #1 on: June 13, 2020, 01:09:29 am »
Besides the fact that you're doing lots of superfluous updates and refreshes and use unknown functions there's nothing which creates a connector.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Cannot find the Element created from Test
« Reply #2 on: June 13, 2020, 01:30:04 am »
Something like this should work bettter:

Code: [Select]
public bool CreateElement(...) {

parentPkg = CreateOrGetParentPkg(inputEle);
         
string eleType = inputEle.Type;
// name and type are passed are args to method
ele = parentPkg.Elements.AddNew(eleName, eleType);
ele.Update(); //save here to make sure it's in the database before messing with the other stuff

// creating diagram
Diagram rootDiagram = CreateOrGetDiagram(parentPkg, parentPkg.Name);

// Add element to diagram - autogenerate position
DiagramObject diagramObject = (DiagramObject)rootDiagram.DiagramObjects.AddNew(GetDiagramObjectPosition(ele.Name.Length), "");
diagramObject.ElementID = ele.ElementID;
diagramObject.Update();

//if you want to see the changes in the project browser you can do a reload here
Repository.ReloadPackage (parentPkg.PackageID) ; //check the exact syntax
}

Geert

integrationsmaster

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Re: Cannot find the Element created from Test
« Reply #3 on: June 13, 2020, 11:42:40 pm »
Besides the fact that you're doing lots of superfluous updates and refreshes and use unknown functions there's nothing which creates a connector.

q.
I did not post the code for the connector creation because the issue seems to be with creation, even though the creation is successful the element cant be found

Code: [Select]
// link is an arg, ele and otherSide are the 2 requirements
Connector connector = ele.Connectors.AddNew(link.Name, link.LinkType);
connector.SupplierID = otherSide.ElementID;
connector.Update();


integrationsmaster

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Re: Cannot find the Element created from Test
« Reply #4 on: June 13, 2020, 11:45:56 pm »
Something like this should work bettter:

Code: [Select]
public bool CreateElement(...) {

parentPkg = CreateOrGetParentPkg(inputEle);
         
string eleType = inputEle.Type;
// name and type are passed are args to method
ele = parentPkg.Elements.AddNew(eleName, eleType);
ele.Update(); //save here to make sure it's in the database before messing with the other stuff

// creating diagram
Diagram rootDiagram = CreateOrGetDiagram(parentPkg, parentPkg.Name);

// Add element to diagram - autogenerate position
DiagramObject diagramObject = (DiagramObject)rootDiagram.DiagramObjects.AddNew(GetDiagramObjectPosition(ele.Name.Length), "");
diagramObject.ElementID = ele.ElementID;
diagramObject.Update();

//if you want to see the changes in the project browser you can do a reload here
Repository.ReloadPackage (parentPkg.PackageID) ; //check the exact syntax
}

Geert

Thanks for the corrections, my issue is after the first Element create, i can see the element in Project Browser, once the second create happens, the first one disappears. do you see anything wrong with my code.

 After the second create, i have the connector create which also fails.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Cannot find the Element created from Test
« Reply #5 on: June 14, 2020, 04:19:07 pm »
The code you shared seems fine. I guess you must be doing something wrong in between the two creates.

Would be best to create a minimal reproducible example.
Strip all the code you don't really need to reproduce the issue. Chances are that you are going to discover the problem like that yourself.

If not you can post the code here and we can have a look.

Geert

integrationsmaster

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Re: Cannot find the Element created from Test
« Reply #6 on: June 15, 2020, 06:35:34 pm »
I only running some asserts between the creates

Code: [Select]
      ElementService ElementService = new ElementService(repo);
      // create parent requirement
      RequirementObj reqObj = GetResource<RequirementObj>("requirement.json");
      Element parent = ElementService.ProcessJson(reqObj);
      Assert.IsNotNull(parent);
      Assert.AreEqual(parent.Name, "Parent");

      // create child requirement - performance
      reqObj = GetResource<RequirementObj>("child_req.json");
      Assert.IsNotNull(reqObj);
      Element child = ElementService.ProcessJson(reqObj);
      Assert.IsNotNull(child);
      Assert.AreEqual(child.Name, "Child");

      // relate
      reqObj = GetResource<RequirementObj>("relation.json");
      Assert.IsNotNull(reqObj);
      Element updatedChild = ElementService.ProcessJson(reqObj);

      Assert.AreEqual(child.ElementGUID, updatedChild.ElementGUID);
      Assert.AreEqual(child.Name, updatedChild.Name);



Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Cannot find the Element created from Test
« Reply #7 on: June 15, 2020, 06:45:36 pm »
I don't see the relation between this code and the previous.
There are lots of calls to methods we don't know. Something might have gone wrong in any of them.

You'll really have to strip it down to its bare essentials to discover the issue.

Geert

integrationsmaster

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Re: Cannot find the Element created from Test
« Reply #8 on: June 16, 2020, 06:31:49 pm »
Sorry, the "ElementService " is the class which contains the "CreateElement", "CreateConnection", "ProcessJson" methods and i call the "ProcessJson" from the test which internally calls "CreateElement".

Code: [Select]
     ElementService elementService = new ElementService(repo);
      // create parent requirement
      RequirementObj reqObj = GetResource<RequirementObj>("requirement.json");
      Element parent = elementService.[b]ProcessJson[/b](reqObj);
      Assert.IsNotNull(parent);
      Assert.AreEqual(parent.Name, "Parent");

      // create child requirement - performance
      reqObj = GetResource<RequirementObj>("child_req.json");
      Assert.IsNotNull(reqObj);
      Element child = elementService.ProcessJson(reqObj);
      Assert.IsNotNull(child);
      Assert.AreEqual(child.Name, "Child");

      // relate
      reqObj = GetResource<RequirementObj>("relation.json");
      Assert.IsNotNull(reqObj);
      Element updatedChild = elementService.ProcessJson(reqObj);

      Assert.AreEqual(child.ElementGUID, updatedChild.ElementGUID);
      Assert.AreEqual(child.Name, updatedChild.Name);