11package com .marklogic .appdeployer .command .forests ;
22
33import com .marklogic .appdeployer .AppConfig ;
4+ import com .marklogic .appdeployer .DefaultAppConfigFactory ;
45import com .marklogic .appdeployer .command .CommandContext ;
5- import static org .junit .jupiter .api .Assertions .*;
6+ import com .marklogic .mgmt .api .forest .Forest ;
7+ import com .marklogic .mgmt .util .SimplePropertySource ;
68import org .junit .jupiter .api .Test ;
79
8- import java .util .ArrayList ;
9- import java .util .HashSet ;
10- import java .util .List ;
11- import java .util .Set ;
10+ import java .util .*;
1211
13- public class CreateForestsOnOneHostTest {
12+ import static org .junit .jupiter .api .Assertions .assertEquals ;
13+
14+ public class CreateForestsOnOneHostTest {
1415
1516 @ Test
1617 public void test () {
@@ -19,34 +20,83 @@ public void test() {
1920
2021 DeployForestsCommand command = new DeployForestsCommand ("test-db" );
2122
22- List <String > fakeHostNames = new ArrayList <>();
23- fakeHostNames .add ("host1" );
24- fakeHostNames .add ("host2" );
25- fakeHostNames .add ("host3" );
26-
2723 HostCalculator hostCalculator = new DefaultHostCalculator (new TestHostNameProvider ("host1" , "host2" , "host3" ));
2824 command .setHostCalculator (hostCalculator );
2925
30- ForestHostNames hostNames = command .determineHostNamesForForest (context , fakeHostNames );
26+ ForestHostNames hostNames = command .determineHostNamesForForest (context , new ArrayList <>() );
3127 assertEquals (3 , hostNames .getPrimaryForestHostNames ().size ());
3228
3329 command .setCreateForestsOnEachHost (false );
34- hostNames = command .determineHostNamesForForest (context , fakeHostNames );
30+ hostNames = command .determineHostNamesForForest (context , new ArrayList <>() );
3531 assertEquals (1 , hostNames .getPrimaryForestHostNames ().size ());
3632 assertEquals ("host1" , hostNames .getPrimaryForestHostNames ().get (0 ));
3733 assertEquals (3 , hostNames .getReplicaForestHostNames ().size (),
3834 "When forests aren't created on each host, all hosts should still be available for replica forests, " +
3935 "with the expectation that the primary forest host will still not be used" );
4036
4137 command .setCreateForestsOnEachHost (true );
42- hostNames = command .determineHostNamesForForest (context , fakeHostNames );
38+ hostNames = command .determineHostNamesForForest (context , new ArrayList <>() );
4339 assertEquals (3 , hostNames .getPrimaryForestHostNames ().size ());
4440
4541 Set <String > names = new HashSet <>();
4642 names .add ("test-db" );
4743 appConfig .setDatabasesWithForestsOnOneHost (names );
48- hostNames = command .determineHostNamesForForest (context , fakeHostNames );
44+ hostNames = command .determineHostNamesForForest (context , new ArrayList <>() );
4945 assertEquals (1 , hostNames .getPrimaryForestHostNames ().size ());
5046 assertEquals (3 , hostNames .getReplicaForestHostNames ().size ());
5147 }
48+
49+ /**
50+ * Added for ticket #439, where the wrong host was being selected when forests already exist on a host that isn'
51+ * the first one in the list.
52+ */
53+ @ Test
54+ void secondHostAlreadyHasForests () {
55+ DeployForestsCommand command = new DeployForestsCommand ("test-db" );
56+ command .setHostCalculator (new DefaultHostCalculator (new TestHostNameProvider ("host1" , "host2" , "host3" )));
57+
58+ AppConfig appConfig = new DefaultAppConfigFactory (new SimplePropertySource (
59+ "mlDatabasesWithForestsOnOneHost" , "test-db" ,
60+ "mlForestsPerHost" , "test-db,2"
61+ )).newAppConfig ();
62+
63+ CommandContext context = new CommandContext (appConfig , null , null );
64+
65+ final List <Forest > existingForests = Arrays .asList (new Forest ("host2" , "test-db-1" ));
66+ ForestHostNames hostNames = command .determineHostNamesForForest (context , existingForests );
67+
68+ assertEquals (1 , hostNames .getPrimaryForestHostNames ().size (), "Only one host name should exist since the " +
69+ "database should only have forests on one host" );
70+ assertEquals ("host2" , hostNames .getPrimaryForestHostNames ().get (0 ), "Because there's already a forest on " +
71+ "host2, that should be the primary forest host name, even though host1 is first in the list" );
72+
73+ List <Forest > forestsToCreate = command .buildForests (context , false , existingForests );
74+ assertEquals (1 , forestsToCreate .size (), "Because forests per host is 2 and host2 already has 1 forest, 1 more " +
75+ "forest needs to be created" );
76+ assertEquals ("host2" , forestsToCreate .get (0 ).getHost ());
77+ assertEquals ("test-db-2" , forestsToCreate .get (0 ).getForestName ());
78+ }
79+
80+ @ Test
81+ void forestsShouldGoOnHostInSecondGroup () {
82+ DeployForestsCommand command = new DeployForestsCommand ("test-db" );
83+ TestHostNameProvider hostNameProvider = new TestHostNameProvider ();
84+ hostNameProvider .addGroupHostNames ("group1" , "host1-1" , "host1-2" );
85+ hostNameProvider .addGroupHostNames ("group2" , "host2-1" , "host2-2" );
86+ command .setHostCalculator (new DefaultHostCalculator (hostNameProvider ));
87+
88+ AppConfig appConfig = new DefaultAppConfigFactory (new SimplePropertySource (
89+ "mlDatabasesWithForestsOnOneHost" , "test-db" ,
90+ "mlDatabaseGroups" , "test-db,group2"
91+ )).newAppConfig ();
92+
93+ CommandContext context = new CommandContext (appConfig , null , null );
94+
95+ ForestHostNames hostNames = command .determineHostNamesForForest (context , new ArrayList <>());
96+ assertEquals (1 , hostNames .getPrimaryForestHostNames ().size ());
97+ assertEquals ("host2-1" , hostNames .getPrimaryForestHostNames ().get (0 ), "mlDatabaseGroups says that the " +
98+ "database should only have forests on hosts in group2, so host2-1 - the first host in group2 - should " +
99+ "be selected" );
100+ }
101+
52102}
0 commit comments