1010import io .elastest .ece .persistance .QueryHelper ;
1111import junit .framework .TestCase ;
1212import junit .framework .TestSuite ;
13+ import org .hibernate .Query ;
1314import org .hibernate .cfg .Configuration ;
1415import org .junit .Before ;
1516import org .junit .Test ;
@@ -71,6 +72,16 @@ public void setup(){
7172 logger .info ("Setting up the Database connection." );
7273 checkAndConfigureHibernate ();
7374 hibernateClient = HibernateClient .getInstance ();
75+ List <CostModel > oldCostModels = hibernateClient .executeQuery (QueryHelper .createListQuery (CostModel .class ));
76+ for (CostModel costModel : oldCostModels ){
77+ hibernateClient .deleteObject (costModel );
78+ }
79+
80+ List <TJob > oldTjobs = hibernateClient .executeQuery (QueryHelper .createListQuery (TJob .class ));
81+ for (TJob tJob : oldTjobs ){
82+ hibernateClient .deleteObject (tJob );
83+ }
84+
7485 logger .info ("Creating Demo T-Job Values." );
7586 HashMap <String , String > test0Map = new HashMap <>();
7687 test0Map .put ("cpus" , "8.0" );
@@ -82,13 +93,13 @@ public void setup(){
8293
8394 logger .info ("Creating Demo Cost Model Values." );
8495 HashMap varCosts = new HashMap ();
85- varCosts .put ("cpus" , 50 .0 );
86- varCosts .put ("memory" , 10 .0 );
87- varCosts .put ("disk" , 1 .0 );
96+ varCosts .put ("cpus" , 0 .0 );
97+ varCosts .put ("memory" , 0 .0 );
98+ varCosts .put ("disk" , 0 .0 );
8899 HashMap fixCost = new HashMap ();
89- fixCost .put ("deployment" , 10 .0 );
100+ fixCost .put ("deployment" , 0 .0 );
90101
91- this .costModel = new CostModel ("On Demand 10 + Charges " , "ONDEMAND" , fixCost , varCosts , null , "On Demand 5 per deployment, 50 per core, 10 per GB ram and 1 per GB disk " );
102+ this .costModel = new CostModel ("Free Cost Model " , "ONDEMAND" , fixCost , varCosts , null , "Everything for free, test purposes " );
92103 this .hibernateClient .persistObject (costModel );
93104 }
94105
@@ -118,26 +129,60 @@ public void testAddCostModel() {
118129 assertTrue (costModels .contains (costModel ));
119130 }
120131
121- // @Test
122- // public void testGetCostModel() {
123- // setup();
124- // apiController = new APIController();
125- // String name = costModel.getName();
126- // String description = costModel.getDescription();
127- // String fixName = (String) costModel.getFix_cost().keySet().toArray()[0];
128- // Double fixValue = costModel.getFix_cost().get(fixName);
129- // double cpus = costModel.getVar_rate().get("cpus");
130- // double memory = costModel.getVar_rate().get("memory");
131- // double disk = costModel.getVar_rate().get("disk");
132- //
133- // apiController.addCostModel(name, description, fixName, fixValue, cpus, memory, disk, new ExtendedModelMap());
134- // List<CostModel> costModels = hibernateClient.executeQuery(QueryHelper.createListQuery(CostModel.class));
135- // int index = costModels.indexOf(costModel);
136- // Long id = costModels.get(index).getId();
137- // CostModel obtained = (CostModel) hibernateClient.getObject(CostModel.class, id);
138- //
139- // assertEquals(obtained, costModel);
140- // }
132+ @ Test
133+ public void testGetCostModel () {
134+ setup ();
135+ apiController = new APIController ();
136+ String name = costModel .getName ();
137+ String description = costModel .getDescription ();
138+ String fixName = (String ) costModel .getFix_cost ().keySet ().toArray ()[0 ];
139+ Double fixValue = costModel .getFix_cost ().get (fixName );
140+ double cpus = costModel .getVar_rate ().get ("cpus" );
141+ double memory = costModel .getVar_rate ().get ("memory" );
142+ double disk = costModel .getVar_rate ().get ("disk" );
143+
144+ apiController .addCostModel (name , description , fixName , fixValue , cpus , memory , disk , new ExtendedModelMap ());
145+ List <CostModel > costModels = hibernateClient .executeQuery (QueryHelper .createListQuery (CostModel .class ));
146+ int index = costModels .indexOf (costModel );
147+ Long id = costModels .get (index ).getId ();
148+ CostModel obtained = (CostModel ) hibernateClient .getObject (CostModel .class , id );
149+
150+ assertEquals (obtained , costModel );
151+ }
152+
153+ @ Test
154+ public void testDeleteCostModel () {
155+ setup ();
156+ apiController = new APIController ();
157+ List <CostModel > costModels = hibernateClient .executeQuery (QueryHelper .createListQuery (CostModel .class ));
158+ int index = costModels .indexOf (costModel );
159+ Long id = costModels .get (index ).getId ();
160+
161+ apiController .deleteCostModel (String .valueOf (id ));
162+
163+ assertEquals (null , hibernateClient .getObject (CostModel .class , id ));
164+ }
165+
166+ @ Test
167+ public void testEstimatePost () {
168+ setup ();
169+ apiController = new APIController ();
170+
171+ List <CostModel > costModels = hibernateClient .executeQuery (QueryHelper .createListQuery (CostModel .class ));
172+ int costModelIndex = costModels .indexOf (costModel );
173+ Long costModelId = costModels .get (costModelIndex ).getId ();
174+
175+ List <TJob > tJobs = hibernateClient .executeQuery (QueryHelper .createListQuery (TJob .class ));
176+ int tJobIndex = tJobs .indexOf (tJob );
177+ Long tJobId = tJobs .get (tJobIndex ).getId ();
178+
179+ ExtendedModelMap model = new ExtendedModelMap ();
180+ apiController .estimatePost (String .valueOf (tJobId ), String .valueOf (costModelId ), model );
181+
182+ Double cost = (Double ) model .get ("estimate" );
183+
184+ assertEquals (0d , cost );
185+ }
141186
142187 /**
143188 * Check and configure Hibernate
0 commit comments