|
| 1 | +package sif3.test.infra.rest.consumer; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import javax.ws.rs.core.MediaType; |
| 8 | + |
| 9 | +import sif3.common.header.HeaderValues.QueryIntention; |
| 10 | +import sif3.common.header.HeaderValues.RequestType; |
| 11 | +import sif3.common.model.PagingInfo; |
| 12 | +import sif3.common.model.SIFContext; |
| 13 | +import sif3.common.model.SIFZone; |
| 14 | +import sif3.common.model.ZoneContextInfo; |
| 15 | +import sif3.common.ws.Response; |
| 16 | +import sif3.infra.common.env.mgr.ConsumerEnvironmentManager; |
| 17 | +import sif3.infra.rest.consumer.ConsumerLoader; |
| 18 | +import systemic.sif3.demo.rest.consumer.namedquery.StudentsInYearConsumer; |
| 19 | + |
| 20 | +public class TestStudentsInYearConsumer |
| 21 | +{ |
| 22 | + private static final String CONSUMER_ID = "StudentConsumer"; |
| 23 | + private static final RequestType REQUEST_TYPE = RequestType.IMMEDIATE; |
| 24 | + |
| 25 | + private StudentsInYearConsumer getConsumer() |
| 26 | + { |
| 27 | + return new StudentsInYearConsumer(); |
| 28 | + } |
| 29 | + |
| 30 | + private void printResponses(List<Response> responses) |
| 31 | + { |
| 32 | + if (responses != null) |
| 33 | + { |
| 34 | + for (Response response : responses) |
| 35 | + { |
| 36 | + printResponse(response); |
| 37 | + } |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + System.out.println("Responses from attempt to execute a Named Query Service: null"); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private void printResponse(Response response) |
| 46 | + { |
| 47 | + try |
| 48 | + { |
| 49 | + if (response != null) |
| 50 | + { |
| 51 | + System.out.println("Response:\n"+response); |
| 52 | + if (response.hasError()) |
| 53 | + { |
| 54 | + System.out.println("Error for Response: "+response.getError()); |
| 55 | + } |
| 56 | + else // We may have data |
| 57 | + { |
| 58 | + if (response.getHasEntity()) |
| 59 | + { |
| 60 | + // Check if response type is String |
| 61 | + if (response.getDataObjectType() != String.class) // This is for phase response |
| 62 | + { |
| 63 | + System.out.println("Unexpectded Data Type retrieved in Response ("+response.getDataObjectType()+")! Cannot unmarshal"); |
| 64 | + } |
| 65 | + } |
| 66 | + else // in delayed we may have delayed receipt |
| 67 | + { |
| 68 | + System.out.println("No Data Returned. Response Status = "+response.getStatus()+" ("+response.getStatusMessage()+")"); |
| 69 | + System.out.println("Delayed Receipt: "+response.getDelayedReceipt()); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + System.out.println("Responses from attempt to execute a Named Query Service: null"); |
| 76 | + } |
| 77 | + } |
| 78 | + catch (Exception ex) |
| 79 | + { |
| 80 | + ex.printStackTrace(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private void getStudentsInYear(StudentsInYearConsumer consumer) |
| 85 | + { |
| 86 | + System.out.println("Start 'Get Students In Year' ..."); |
| 87 | + try |
| 88 | + { |
| 89 | + List<ZoneContextInfo> zoneCtxList = new ArrayList<ZoneContextInfo>(); |
| 90 | + zoneCtxList.add(new ZoneContextInfo((SIFZone)null, (SIFContext)null)); // Default zone and context |
| 91 | +// zoneCtxList.add(new ZoneContextInfo(new SIFZone("auRolloverTestingZone"), null)); |
| 92 | + |
| 93 | + HashMap<String, String> namedQueryParameters = new HashMap<String, String>(); |
| 94 | + namedQueryParameters.put("Year", "Yr7"); |
| 95 | + namedQueryParameters.put("SchoolCode", "4001"); |
| 96 | + |
| 97 | + List<Response> responses = consumer.retrieveDataFromNamedQuery(namedQueryParameters, MediaType.APPLICATION_XML_TYPE, new PagingInfo(5, 1), zoneCtxList, REQUEST_TYPE, QueryIntention.ALL, null); |
| 98 | + |
| 99 | + System.out.println("Responses from attempt to Get Job:"); |
| 100 | + printResponses(responses); |
| 101 | + } |
| 102 | + catch (Exception ex) |
| 103 | + { |
| 104 | + ex.printStackTrace(); |
| 105 | + } |
| 106 | + System.out.println("Finished 'Get Students in Year'."); |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + public static void main(String[] args) |
| 111 | + { |
| 112 | + TestStudentsInYearConsumer tester = new TestStudentsInYearConsumer(); |
| 113 | + System.out.println("Start Testing StudentsInYearConsumer..."); |
| 114 | + |
| 115 | + if (ConsumerLoader.initialise(CONSUMER_ID)) |
| 116 | + { |
| 117 | + System.out.println("Consumer loaded successfully. Environment Data:\n"+ConsumerEnvironmentManager.getInstance().getEnvironmentInfo()); |
| 118 | + |
| 119 | + StudentsInYearConsumer consumer = tester.getConsumer(); |
| 120 | + StudentsInYearConsumer consumer2 = tester.getConsumer(); |
| 121 | + |
| 122 | + // |
| 123 | + // Get Students |
| 124 | + // |
| 125 | + tester.getStudentsInYear(consumer); |
| 126 | + tester.getStudentsInYear(consumer2); |
| 127 | + |
| 128 | + // Put this agent to a blocking wait..... |
| 129 | + if (true) |
| 130 | + { |
| 131 | + try |
| 132 | + { |
| 133 | + Object semaphore = new Object(); |
| 134 | + synchronized (semaphore) // this will block until CTRL-C is pressed. |
| 135 | + { |
| 136 | + System.out.println("==================================================\nTestStudentsInYearConsumer is running (Press Ctrl-C to stop)\n=================================================="); |
| 137 | + semaphore.wait(); |
| 138 | + } |
| 139 | + } |
| 140 | + catch (Exception ex) |
| 141 | + { |
| 142 | + System.out.println("Blocking wait in TestStudentsInYearConsumer interrupted: " + ex.getMessage()); |
| 143 | + ex.printStackTrace(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + System.out.println("Finalise Consumer (i.e. disconnect and remove environment)."); |
| 148 | + consumer.finalise(); |
| 149 | + } |
| 150 | + |
| 151 | + ConsumerLoader.shutdown(); |
| 152 | + System.out.println("End Testing StudentsInYearConsumer."); |
| 153 | + } |
| 154 | +} |
0 commit comments