11package O7_DataCreationHttp ;
22
3- import O4_TableView .data .ProjectTables ;
3+ import O7_DataCreationHttp .data .BaseServiceNowDataTest ;
4+ import O7_DataCreationHttp .data .ProjectTables ;
45import O7_DataCreationHttp .data .incident .Incident ;
56import O7_DataCreationHttp .data .incident .IncidentRepository ;
6- import O7_DataCreationHttp .data .user . User ;
7- import O7_DataCreationHttp .data .user .UserRepository ;
7+ import O7_DataCreationHttp .data .incident . IncidentRepositoryFactory ;
8+ import O7_DataCreationHttp .data .user .UserRepositoryFactory ;
89import org .junit .jupiter .api .Assertions ;
910import org .junit .jupiter .api .Test ;
10- import solutions .bellatrix .core .configuration .ConfigurationService ;
11- import solutions .bellatrix .core .utilities .TimestampBuilder ;
12- import solutions .bellatrix .data .http .infrastructure .QueryParameter ;
13- import solutions .bellatrix .servicenow .baseTest .ServiceNowBaseTest ;
14- import solutions .bellatrix .servicenow .infrastructure .configuration .ServiceNowProjectSettings ;
1511
12+ import java .lang .reflect .InvocationTargetException ;
1613import java .util .List ;
1714
18- public class DataCreationTests extends ServiceNowBaseTest {
19- User currentUser ;
15+ public class DataCreationTests extends BaseServiceNowDataTest {
16+ protected IncidentRepositoryFactory incidentFactory ;
17+ protected UserRepositoryFactory userFactory ;
2018
2119 @ Override
2220 protected void beforeEach () throws Exception {
2321 super .beforeEach ();
24- UserRepository userRepository = new UserRepository ();
25- currentUser = userRepository .getEntitiesByParameters (List .of (new QueryParameter ("user_name" , ConfigurationService .get (ServiceNowProjectSettings .class ).getUserName ()))).get (0 );
22+
23+ incidentFactory = new IncidentRepositoryFactory ();
24+ userFactory = new UserRepositoryFactory ();
25+
26+ registerRepositoriesAndFactories ();
2627 }
2728
2829 @ Test
29- public void createEntity () {
30- IncidentRepository incidentRepository = new IncidentRepository ();
30+ public void createEntityWithDependencyDefaultTest () {
31+ // Build incident with dependencies using factory
32+ Incident incident = incidentFactory .buildDefault ();
33+ incident .createWithDependencies ();
3134
32- Incident incident = incidentRepository .create (Incident .builder ()
33- .caller (currentUser .getSysId ())
34- .shortDescription (String .format ("Incident Report %s" , TimestampBuilder .buildUniqueTextByPrefix ("au" )))
35- .build ());
35+ Assertions .assertNotNull (incident .getSysId ());
36+ Assertions .assertNotNull (incident .getCaller ().getSysId ());
3637
37- var entityCreated = incidentRepository .getById (incident );
38-
39- Assertions .assertEquals (incident .getCaller (), entityCreated .getCaller ());
38+ incident .deleteDependenciesAndSelf ();
4039 }
4140
4241 @ Test
43- public void updateEntity () {
44- IncidentRepository incidentRepository = new IncidentRepository ();
42+ public void createEntityWithDependencyCustomizedTest () throws NoSuchFieldException , IllegalAccessException , InvocationTargetException , NoSuchMethodException , InstantiationException {
43+ String expectedIncidentDescription = "Custom Description " + System .currentTimeMillis ();
44+ String expectedUserName = "Custom User " + System .currentTimeMillis ();
4545
46- Incident incident = incidentRepository .create (Incident .builder ()
47- .caller (currentUser .getSysId ())
48- .shortDescription (String .format ("Incident Report %s" , TimestampBuilder .buildUniqueTextByPrefix ("au" )))
49- .build ());
46+ // Build incident with dependencies using factory
47+ Incident incident = incidentFactory .buildDefaultWithDependencies ();
5048
49+ incident .setShortDescription (expectedIncidentDescription );
50+ incident .getCaller ().setUserName (expectedUserName );
5151
52- var updatedIncident = Incident .builder ().sysId (incident .getSysId ()).shortDescription ("Description updated" ).build ();
53- incidentRepository .update (updatedIncident );
52+ incident .createWithDependencies ();
5453
55- var entityFromAPI = incidentRepository .getById (incident );
54+ Incident defaultIncident = incidentFactory .buildDefault ();
55+ defaultIncident .setIdentifier (incident .getSysId ());
5656
57- Assertions .assertEquals (updatedIncident .getShortDescription (), entityFromAPI .getShortDescription ());
58- }
57+ var entityFromAPI = defaultIncident .getWithDependencies ();
5958
60- @ Test
61- public void deleteEntity () {
62- IncidentRepository incidentRepository = new IncidentRepository ();
63-
64- Incident incident = incidentRepository .create (Incident .builder ()
65- .caller (currentUser .getSysId ())
66- .shortDescription (String .format ("Incident Report %s" , TimestampBuilder .buildUniqueTextByPrefix ("au" )))
67- .build ());
59+ Assertions .assertEquals (entityFromAPI .getShortDescription (), expectedIncidentDescription );
60+ Assertions .assertEquals (entityFromAPI .getCaller ().getUserName (), expectedUserName );
6861
69- incidentRepository .delete (incident );
70-
71- incidentRepository .validateEntityDoesNotExist (incident );
62+ incident .deleteDependenciesAndSelf ();
7263 }
7364
7465 @ Test
75- public void getEntity () {
66+ public void deleteEntity () throws NoSuchFieldException , InvocationTargetException , IllegalAccessException , NoSuchMethodException , InstantiationException {
67+ // Build incident with dependencies using factory
7668 IncidentRepository incidentRepository = new IncidentRepository ();
7769
78- Incident incident = incidentRepository .create (Incident .builder ()
79- .caller (currentUser .getSysId ())
80- .shortDescription (String .format ("Incident Report %s" , TimestampBuilder .buildUniqueTextByPrefix ("au" )))
81- .build ());
70+ Incident incident = incidentFactory .buildDefault ();
71+ incident .createWithDependencies ();
72+
73+ List <Incident > incidents = incidentRepository .getAll ().stream ().filter (i -> i .getSysId ().equals (incident .getSysId ())).toList ();
74+ Assertions .assertTrue (incidents .size ()==1 );
8275
83- var incidentRegistered = incidentRepository . getById ( incident );
76+ incident . deleteDependenciesAndSelf ( );
8477
85- Assertions .assertEquals (incident .getShortDescription (), incidentRegistered .getShortDescription ());
78+ incidents = incidentRepository .getAll ().stream ().filter (i -> i .getSysId ().equals (incident .getSysId ())).toList ();
79+ Assertions .assertFalse (incidents .size ()==1 );
8680 }
8781
8882 @ Test
@@ -95,4 +89,29 @@ public void getAllEntities() {
9589
9690 Assertions .assertEquals (serviceNowTableViewPage .map ().totalRowsInfo ().getText (), String .valueOf (incidents .size ()));
9791 }
92+
93+ @ Test
94+ public void updateEntityWithDependencyCustomizedTest () throws NoSuchFieldException , IllegalAccessException , InvocationTargetException , NoSuchMethodException , InstantiationException {
95+ // Build incident with dependencies using factory
96+ Incident incident = incidentFactory .buildDefaultWithDependencies ();
97+ incident .createWithDependencies ();
98+
99+ String expectedIncidentDescription = "Custom Description " + System .currentTimeMillis ();
100+ String expectedUserName = "Custom User " + System .currentTimeMillis ();
101+
102+ incident .setShortDescription (expectedIncidentDescription );
103+ incident .getCaller ().setUserName (expectedUserName );
104+
105+ incident .updateWithDependencies ();
106+
107+ Incident defaultIncident = incidentFactory .buildDefault ();
108+ defaultIncident .setIdentifier (incident .getSysId ());
109+
110+ var entityFromAPI = defaultIncident .getWithDependencies ();
111+
112+ Assertions .assertEquals (entityFromAPI .getShortDescription (), expectedIncidentDescription );
113+ Assertions .assertEquals (entityFromAPI .getCaller ().getUserName (), expectedUserName );
114+
115+ incident .deleteDependenciesAndSelf ();
116+ }
98117}
0 commit comments