use of com.emc.storageos.api.service.impl.resource.MeteringService in project coprhd-controller by CoprHD.
the class DbStatRetrieverTest method meteringXmlServiceDBRetrieverTest.
@Test
public void meteringXmlServiceDBRetrieverTest() throws WebApplicationException, IOException, JAXBException {
deleteIfExists(XmlTestOutputFile);
DummyDBClient dbClient = new DummyDBClient();
MeteringService statResource = new MeteringService();
// statResource.setDbClient(dbClient);
DbStatRetriever dummyDbStatRetriever = new DbStatRetriever();
dummyDbStatRetriever.setDbClient(dbClient);
statResource.setStatRetriever(dummyDbStatRetriever);
DummyHttpHeaders header = new DummyHttpHeaders(MediaType.APPLICATION_XML_TYPE);
Response r = statResource.getStats("2012-01-01T00:00", header);
Assert.assertNotNull(r);
Assert.assertEquals(Status.OK.getStatusCode(), r.getStatus());
Assert.assertTrue(r.getEntity() instanceof StreamingOutput);
StreamingOutput so = (StreamingOutput) r.getEntity();
File of = new File(XmlTestOutputFile);
OutputStream os = new FileOutputStream(of);
try {
so.write(os);
} finally {
os.close();
}
JAXBContext context = null;
Unmarshaller unmarshaller = null;
context = JAXBContext.newInstance(Stats.class);
unmarshaller = context.createUnmarshaller();
Object o = unmarshaller.unmarshal(new File(XmlTestOutputFile));
Assert.assertTrue(o instanceof Stats);
Stats stats = (Stats) o;
// expected number of stats unmarshaled
Assert.assertEquals(10, stats.stats.size());
deleteIfExists(XmlTestOutputFile);
}
use of com.emc.storageos.api.service.impl.resource.MeteringService in project coprhd-controller by CoprHD.
the class DbStatRetrieverTest method xmlStatIllegalTimeBucketArgumentTest.
@Test
public void xmlStatIllegalTimeBucketArgumentTest() throws WebApplicationException, IOException, JAXBException {
deleteIfExists(XmlTestOutputFile);
DummyDBClient dbClient = new DummyDBClient();
MeteringService statResource = new MeteringService();
statResource.setDbClient(dbClient);
DummyHttpHeaders header = new DummyHttpHeaders(MediaType.APPLICATION_XML_TYPE);
try {
statResource.getStats("xxxyyy", header);
Assert.fail("Expected a BadRequestException");
} catch (BadRequestException e) {
Assert.assertEquals(ServiceCode.API_PARAMETER_INVALID_TIME_FORMAT, e.getServiceCode());
}
}
use of com.emc.storageos.api.service.impl.resource.MeteringService in project coprhd-controller by CoprHD.
the class MeteringServiceTest method meteringServiceTestXML.
@Test
public void meteringServiceTestXML() throws WebApplicationException, IOException, JAXBException {
deleteIfExists(XmlTestOutputFile);
DummyStatRetriever dbStatRetriever = new DummyStatRetriever();
MeteringService statResource = new MeteringService();
statResource.setStatRetriever(dbStatRetriever);
DummyHttpHeaders header = new DummyHttpHeaders(MediaType.APPLICATION_XML_TYPE);
Response r = statResource.getStats("2012-08-08T00:00", header);
Assert.assertNotNull(r);
Assert.assertEquals(Status.OK.getStatusCode(), r.getStatus());
Assert.assertTrue(r.getEntity() instanceof StreamingOutput);
StreamingOutput so = (StreamingOutput) r.getEntity();
File of = new File(XmlTestOutputFile);
OutputStream os = new FileOutputStream(of);
so.write(os);
os.close();
JAXBContext context = null;
Unmarshaller unmarshaller = null;
context = JAXBContext.newInstance(Stats.class);
unmarshaller = context.createUnmarshaller();
Object o = unmarshaller.unmarshal(new File(XmlTestOutputFile));
Assert.assertTrue(o instanceof Stats);
Stats stats = (Stats) o;
// expected number of stats unmarshaled
Assert.assertEquals(100, stats.stats.size());
deleteIfExists(XmlTestOutputFile);
}
use of com.emc.storageos.api.service.impl.resource.MeteringService in project coprhd-controller by CoprHD.
the class MeteringServiceTest method testMeteringServiceJSON.
@Test
public void testMeteringServiceJSON() throws WebApplicationException, IOException, JsonParseException {
deleteIfExists(JsonTestOutputFile);
DummyStatRetriever dbStatRetriever = new DummyStatRetriever();
MeteringService statResource = new MeteringService();
statResource.setStatRetriever(dbStatRetriever);
DummyHttpHeaders header = new DummyHttpHeaders(MediaType.APPLICATION_JSON_TYPE);
Response r = statResource.getStats("2012-08-08T00", header);
Assert.assertNotNull(r);
Assert.assertEquals(Status.OK.getStatusCode(), r.getStatus());
Assert.assertTrue(r.getEntity() instanceof StreamingOutput);
StreamingOutput so = (StreamingOutput) r.getEntity();
File of = new File(JsonTestOutputFile);
OutputStream os = new FileOutputStream(of);
try {
so.write(os);
} finally {
os.close();
}
ObjectMapper mapper = null;
mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
mapper.getDeserializationConfig().withAnnotationIntrospector(introspector);
Stats stats = mapper.readValue(new File(JsonTestOutputFile), Stats.class);
Assert.assertEquals(100, stats.stats.size());
deleteIfExists(JsonTestOutputFile);
}
use of com.emc.storageos.api.service.impl.resource.MeteringService in project coprhd-controller by CoprHD.
the class DbStatRetrieverTest method statXmlServiceDBExceptionsTest.
@Test
public void statXmlServiceDBExceptionsTest() throws WebApplicationException, IOException, JAXBException {
deleteIfExists(XmlTestOutputFile);
DummyDBClient dbClient = new DummyDBClient();
MeteringService statResource = new MeteringService();
// statResource.setDbClient(dbClient);
DbStatRetriever dummyDbStatRetriever = new DbStatRetriever();
dummyDbStatRetriever.setDbClient(dbClient);
statResource.setStatRetriever(dummyDbStatRetriever);
DummyHttpHeaders header = new DummyHttpHeaders(MediaType.APPLICATION_XML_TYPE);
Response r = statResource.getStats("2012-01-02T00:00", header);
Assert.assertNotNull(r);
Assert.assertEquals(Status.OK.getStatusCode(), r.getStatus());
Assert.assertTrue(r.getEntity() instanceof StreamingOutput);
StreamingOutput so = (StreamingOutput) r.getEntity();
File of = new File(XmlTestOutputFile);
OutputStream os = new FileOutputStream(of);
try {
so.write(os);
} catch (Exception e) {
Assert.assertTrue(e.toString().contains("I/O"));
} finally {
os.close();
}
}
Aggregations