public void testDetectsUnknownTemplates() throws Exception { final EntityManager entityManager = context.mock(EntityManager.class); final Query q = context.mock(Query.class); final List<MeasurementDefinition> allDefs = new ArrayList<MeasurementDefinition>(); context.checking( new Expectations() { { // make the entity manager return the test data allowing(entityManager).createQuery(with(any(String.class))); will(returnValue(q)); allowing(q).getResultList(); will(returnValue(allDefs)); } }); // add some test data allDefs.add(createMeasurementDefinition("m1", false)); allDefs.add(createMeasurementDefinition("m1", true)); allDefs.add(createMeasurementDefinition("m2", false)); allDefs.add(createMeasurementDefinition("m3", false)); MetricTemplateValidator validator = new MetricTemplateValidator(); validator.initialize(null, entityManager); validator.initializeExportedStateValidation(null); // check that all the defined measurement defs pass for (MeasurementDefinition md : allDefs) { validator.validateExportedEntity(new MetricTemplate(md)); } // now let's create an "unknown" metric template MetricTemplate unknown = new MetricTemplate(); unknown.setMetricName("unknown"); unknown.setResourceTypeName("some"); unknown.setResourceTypePlugin("other"); try { validator.validateExportedEntity(unknown); fail("Validating a metric template " + unknown + " should have failed."); } catch (ValidationException e) { // expected } }
public void testDetectsDuplicitEntries() throws Exception { final EntityManager entityManager = context.mock(EntityManager.class); final Query q = context.mock(Query.class); final List<MeasurementDefinition> allDefs = new ArrayList<MeasurementDefinition>(); context.checking( new Expectations() { { // make the entity manager return the test data allowing(entityManager).createQuery(with(any(String.class))); will(returnValue(q)); allowing(q).getResultList(); will(returnValue(allDefs)); } }); // add some test data allDefs.add(createMeasurementDefinition("m1", false)); allDefs.add(createMeasurementDefinition("m1", true)); allDefs.add(createMeasurementDefinition("m2", false)); allDefs.add(createMeasurementDefinition("m3", false)); MetricTemplateValidator validator = new MetricTemplateValidator(); validator.initialize(null, entityManager); validator.initializeExportedStateValidation(null); // check that all the defined measurement defs pass for (MeasurementDefinition md : allDefs) { validator.validateExportedEntity(new MetricTemplate(md)); } // now let's try and validate the first test data again. // this simulates the duplicate entries in the export file try { MetricTemplate duplicate = new MetricTemplate(allDefs.get(0)); validator.validateExportedEntity(duplicate); fail("Validating a duplicate metric template " + duplicate + " should have failed."); } catch (ValidationException e) { // expected } }