Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package org.wuspba.ctams.ws; import org.wuspba.ctams.util.IntegrationTestUtils; import org.wuspba.ctams.util.XMLUtils; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URI; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.junit.AfterClass; import static org.junit.Assert.*; import org.junit.BeforeClass; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.wuspba.ctams.model.CTAMSDocument; import org.wuspba.ctams.model.JudgePanelType; import org.wuspba.ctams.model.JudgeQualification; import org.wuspba.ctams.model.JudgeType; import org.wuspba.ctams.util.TestFixture; /** * * @author atrimble */ public class ITJudgeQualificationController { private static final Logger LOG = LoggerFactory.getLogger(ITJudgeQualificationController.class); protected static String PROTOCOL = "http"; protected static String HOST = "localhost"; protected static int PORT = 8081; protected static String PATH = "/judgequalification"; static { if (System.getProperties().containsKey("ctams.protocol")) { PROTOCOL = System.getProperty("ctams.protocol"); } if (System.getProperties().containsKey("ctams.host")) { HOST = System.getProperty("ctams.host"); } if (System.getProperties().containsKey("ctams.port")) { PORT = Integer.parseInt(System.getProperty("ctams.port")); } if (System.getProperties().containsKey("ctams.uri")) { PATH = System.getProperty("ctams.uri") + PATH; } } @BeforeClass static public void setup() throws Exception { add(); } @AfterClass static public void cleanup() throws Exception { delete(); } @Test public void testList() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); LOG.info("Connecting to " + uri.toString()); HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); assertEquals(doc.getJudgeQualifications().size(), 3); for (JudgeQualification q : doc.getJudgeQualifications()) { if (q.getType().equals(JudgeType.BAND_PIPING)) { testEquality(q, TestFixture.INSTANCE.pipingQual); } else if (q.getType().equals(JudgeType.BAND_DRUMMING)) { testEquality(q, TestFixture.INSTANCE.drummingQual); } else if (q.getType().equals(JudgeType.BAND_ENSEMBLE)) { testEquality(q, TestFixture.INSTANCE.ensembleQual); } else { fail(); } } EntityUtils.consume(entity); } } @Test public void testListType() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH) .setParameter("type", JudgeType.BAND_PIPING.toString()).build(); HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); assertEquals(doc.getJudgeQualifications().size(), 1); testEquality(doc.getJudgeQualifications().get(0), TestFixture.INSTANCE.pipingQual); EntityUtils.consume(entity); } uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH) .setParameter("type", JudgeType.PIOBAIREACHD.toString()).build(); httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); assertEquals(doc.getJudgeQualifications().size(), 0); EntityUtils.consume(entity); } } @Test public void testListPanel() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH) .setParameter("panel", JudgePanelType.A.toString()).build(); HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); assertEquals(doc.getJudgeQualifications().size(), 1); testEquality(doc.getJudgeQualifications().get(0), TestFixture.INSTANCE.pipingQual); EntityUtils.consume(entity); } uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH) .setParameter("panel", JudgePanelType.SHADOW.toString()).build(); httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); assertEquals(doc.getJudgeQualifications().size(), 0); EntityUtils.consume(entity); } } @Test public void testListPanelAndType() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH) .setParameter("panel", JudgePanelType.A.toString()) .setParameter("type", JudgeType.BAND_PIPING.toString()).build(); HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); assertEquals(doc.getJudgeQualifications().size(), 1); testEquality(doc.getJudgeQualifications().get(0), TestFixture.INSTANCE.pipingQual); EntityUtils.consume(entity); } uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH) .setParameter("panel", JudgePanelType.A.toString()) .setParameter("type", JudgeType.BAND_DRUMMING.toString()).build(); httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); assertEquals(doc.getJudgeQualifications().size(), 0); EntityUtils.consume(entity); } } @Test public void testDelete() throws Exception { delete(); CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); assertEquals(doc.getJudgeQualifications().size(), 0); EntityUtils.consume(entity); } add(); } public static JudgeQualification getQualification(JudgePanelType panel, JudgeType type) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); JudgeQualification ret; URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH) .setParameter("panel", panel.toString()).setParameter("type", type.toString()).build(); HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); assertEquals(doc.getJudgeQualifications().size(), 1); ret = doc.getJudgeQualifications().get(0); EntityUtils.consume(entity); } return ret; } protected static void add() throws Exception { CTAMSDocument doc = new CTAMSDocument(); doc.getJudgeQualifications().add(TestFixture.INSTANCE.drummingQual); doc.getJudgeQualifications().add(TestFixture.INSTANCE.ensembleQual); doc.getJudgeQualifications().add(TestFixture.INSTANCE.pipingQual); String xml = XMLUtils.marshal(doc); CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpPost httpPost = new HttpPost(uri); StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML); CloseableHttpResponse response = null; try { httpPost.setEntity(xmlEntity); response = httpclient.execute(httpPost); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); doc = IntegrationTestUtils.convertEntity(responseEntity); for (JudgeQualification q : doc.getJudgeQualifications()) { if (q.getPanel() == TestFixture.INSTANCE.pipingQual.getPanel() && q.getType() == TestFixture.INSTANCE.pipingQual.getType()) { TestFixture.INSTANCE.pipingQual.setId(q.getId()); } else if (q.getPanel() == TestFixture.INSTANCE.drummingQual.getPanel() && q.getType() == TestFixture.INSTANCE.drummingQual.getType()) { TestFixture.INSTANCE.drummingQual.setId(q.getId()); } else if (q.getPanel() == TestFixture.INSTANCE.ensembleQual.getPanel() && q.getType() == TestFixture.INSTANCE.ensembleQual.getType()) { TestFixture.INSTANCE.ensembleQual.setId(q.getId()); } } EntityUtils.consume(responseEntity); } catch (UnsupportedEncodingException ex) { LOG.error("Unsupported coding", ex); } catch (IOException ioex) { LOG.error("IOException", ioex); } finally { if (response != null) { try { response.close(); } catch (IOException ex) { LOG.error("Could not close response", ex); } } } } protected static void delete() throws Exception { List<String> ids = new ArrayList<>(); CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); for (JudgeQualification q : doc.getJudgeQualifications()) { ids.add(q.getId()); } EntityUtils.consume(entity); } for (String id : ids) { httpclient = HttpClients.createDefault(); uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH) .setParameter("id", id).build(); HttpDelete httpDelete = new HttpDelete(uri); CloseableHttpResponse response = null; try { response = httpclient.execute(httpDelete); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); EntityUtils.consume(responseEntity); } catch (UnsupportedEncodingException ex) { LOG.error("Unsupported coding", ex); } catch (IOException ioex) { LOG.error("IOException", ioex); } finally { if (response != null) { try { response.close(); } catch (IOException ex) { LOG.error("Could not close response", ex); } } } } } private void testEquality(JudgeQualification q1, JudgeQualification q2) { assertEquals(q1.getId(), q2.getId()); assertEquals(q1.getPanel(), q2.getPanel()); assertEquals(q1.getType(), q2.getType()); } }