HttpDhis2Server implements Dhis2Server { @Override public AdxImportSummary postAdxReport(AdxType report) throws DHIS2ReportingException { log.debug("Posting A report"); AdxImportSummary summaries = null; StringWriter xmlReport = new StringWriter(); try { JAXBContext jaxbDataValueSetContext = JAXBContext .newInstance(AdxType.class); Marshaller adxTypeMarshaller = jaxbDataValueSetContext .createMarshaller(); adxTypeMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); adxTypeMarshaller.marshal(report, xmlReport); } catch (JAXBException ex) { throw new Dxf2Exception("Problem marshalling adxtype", ex); } String host = url.getHost(); int port = url.getPort(); HttpHost targetHost = new HttpHost(host, port, url.getProtocol()); DefaultHttpClient httpclient = new DefaultHttpClient(); BasicHttpContext localcontext = new BasicHttpContext(); try { HttpPost httpPost = new HttpPost(url.getPath() + DATAVALUESET_PATH); Credentials creds = new UsernamePasswordCredentials(username, password); Header bs = new BasicScheme().authenticate(creds, httpPost, localcontext); httpPost.addHeader("Authorization", bs.getValue()); httpPost.addHeader("Content-Type", "application/adx+xml"); httpPost.addHeader("Accept", "application/xml"); httpPost.setEntity(new StringEntity(xmlReport.toString())); HttpResponse response = httpclient.execute(targetHost, httpPost, localcontext); HttpEntity entity = response.getEntity(); if (response.getStatusLine().getStatusCode() != 200) { throw new Dhis2Exception(this, response.getStatusLine() .getReasonPhrase(), null); } if (entity != null) { JAXBContext jaxbImportSummaryContext = JAXBContext .newInstance(AdxImportSummary.class); Unmarshaller importSummaryUnMarshaller = jaxbImportSummaryContext .createUnmarshaller(); summaries = (AdxImportSummary) importSummaryUnMarshaller .unmarshal(entity.getContent()); } else { summaries = new AdxImportSummary(); } } catch (JAXBException ex) { throw new Dhis2Exception(this, "Problem unmarshalling AdxImportSummary", ex); } catch (AuthenticationException ex) { throw new Dhis2Exception(this, "Problem authenticating to DHIS2 server", ex); } catch (IOException ex) { throw new Dhis2Exception(this, "Problem accessing DHIS2 server", ex); } finally { httpclient.getConnectionManager().shutdown(); } return summaries; } HttpDhis2Server(); URL getUrl(); void setUrl(URL url); String getPassword(); void setPassword(String password); String getUsername(); void setUsername(String username); @Override boolean isConfigured(); @Override ImportSummary postReport(DataValueSet report); @Override AdxImportSummary postAdxReport(AdxType report); @Override ReportDefinition fetchReportTemplates(); static final String REPORTS_METADATA_PATH; static final String DATAVALUESET_PATH; }
@Test public void postAdxReportTest() throws DatatypeConfigurationException, MalformedURLException, DHIS2ReportingException { HttpDhis2Server server = mock(HttpDhis2Server.class); ObjectFactory of = new ObjectFactory(); AdxType adxt = of.createAdxType(); XMLGregorianCalendar date3 = null; date3 = DatatypeFactory.newInstance().newXMLGregorianCalendar( new GregorianCalendar(2016, 06, 14)); adxt.setExported(date3); GroupType group = of.createGroupType(); group.setOrgUnit("OU_559"); group.setPeriod("2015-06-01/P1M"); Map<QName, String> hm1 = group.getOtherAttributes(); QName qn2 = new QName("idScheme"); hm1.put(qn2, "Code"); List<GroupType> groups = adxt.getGroup(); List<DataValueType> dvt = group.getDataValue(); DataValueType dv = new DataValueType(); dv.setDataElement("DE_98454"); BigDecimal bd = new BigDecimal(32.0); dv.setValue(bd); dvt.add(dv); groups.add(group); URL url = new URL("http: server.setUrl(url); server.setPassword("district"); server.setUsername("admin2"); AdxImportSummary summary = new AdxImportSummary(); summary.setStatus("SUCCESS"); when(server.postAdxReport(adxt)).thenReturn(summary); AdxImportSummary summaries = server.postAdxReport(adxt); assertEquals(summaries.getStatus(), "SUCCESS"); }