|
| 1 | +/* |
| 2 | + * TestEventClient.java |
| 3 | + * Created: 21/03/2014 |
| 4 | + * |
| 5 | + * Copyright 2014 Systemic Pty Ltd |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 14 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 15 | + * or implied. |
| 16 | + * See the License for the specific language governing permissions and limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package sif3.test.infra.rest.client; |
| 20 | + |
| 21 | +import java.net.URI; |
| 22 | + |
| 23 | +import javax.ws.rs.core.MediaType; |
| 24 | + |
| 25 | +import sif.dd.au30.conversion.DataModelMarshalFactory; |
| 26 | +import sif.dd.au30.conversion.DataModelUnmarshalFactory; |
| 27 | +import sif.dd.au30.model.StudentPersonalCollectionType; |
| 28 | +import sif3.common.conversion.MarshalFactory; |
| 29 | +import sif3.common.conversion.UnmarshalFactory; |
| 30 | +import sif3.common.exception.ServiceInvokationException; |
| 31 | +import sif3.common.header.HeaderProperties; |
| 32 | +import sif3.common.header.HeaderValues.EventAction; |
| 33 | +import sif3.common.header.RequestHeaderConstants; |
| 34 | +import sif3.common.model.SIFEvent; |
| 35 | +import sif3.infra.common.env.mgr.BrokeredProviderEnvironmentManager; |
| 36 | +import sif3.infra.common.env.types.AdapterEnvironmentStore; |
| 37 | +import sif3.infra.common.env.types.ConsumerEnvironment.ConnectorName; |
| 38 | +import sif3.infra.rest.client.EventClient; |
| 39 | +import systemic.sif3.demo.rest.conversion.CSVMarshaller; |
| 40 | +import au.com.systemic.framework.utils.FileReaderWriter; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Joerg Huber |
| 44 | + * |
| 45 | + */ |
| 46 | +public class TestEventClient |
| 47 | +{ |
| 48 | + private final static String MULTI_STUDENT_FILE_NAME = "C:/Development/GitHubRepositories/SIF3InfraRest/SIF3InfraREST/TestData/xml/input/StudentPersonals5.xml"; |
| 49 | +// private static final String PROP_FILE_NAME="BrokeredSISProvider"; |
| 50 | + private static final String PROP_FILE_NAME="StudentProvider"; |
| 51 | + |
| 52 | + // Broker |
| 53 | +// private static final String EVENT_CONNECTOR_URI = "https://australia.hostedzone.com/svcs/systemicDemo/events"; |
| 54 | +// private static final String SESSION_TOKEN = "1a47dae9-579b-4aa5-8048-608b06c611cb"; |
| 55 | +// private static final String PWD="---"; |
| 56 | +// private static final String ZONE="demo"; |
| 57 | + |
| 58 | +// private static final String EVENT_CONNECTOR_URI = "https://australia.hostedzone.com/svcs/systemicDemo/events"; |
| 59 | +// private static final String SESSION_TOKEN = "be06a10e-1771-47c3-99bc-52a30b34711b"; |
| 60 | +// private static final String PWD="---"; |
| 61 | +// private static final String ZONE="loadZone"; |
| 62 | + |
| 63 | + // Local host setup |
| 64 | + private static final String EVENT_CONNECTOR_URI = "http://localhost:9080/SIF3InfraREST/sif3/eventsConnector"; |
| 65 | + private static final String SESSION_TOKEN = "25745c93-07d3-4ca8-a28a-389084719cba"; |
| 66 | + private static final String PWD="Password1"; |
| 67 | + private static final String ZONE="auSchoolTestingZone"; |
| 68 | + |
| 69 | + |
| 70 | + private MarshalFactory marshaller = new DataModelMarshalFactory(); |
| 71 | + private UnmarshalFactory unmarshaller = new DataModelUnmarshalFactory(); |
| 72 | + private AdapterEnvironmentStore store = new AdapterEnvironmentStore(PROP_FILE_NAME); |
| 73 | + |
| 74 | + |
| 75 | + private StudentPersonalCollectionType getStudents() |
| 76 | + { |
| 77 | + String inputEnvXML = FileReaderWriter.getFileContent(MULTI_STUDENT_FILE_NAME); |
| 78 | + //System.out.println("File content:\n" + inputEnvXML); |
| 79 | + try |
| 80 | + { |
| 81 | + return (StudentPersonalCollectionType)unmarshaller.unmarshalFromXML(inputEnvXML, StudentPersonalCollectionType.class); |
| 82 | + } |
| 83 | + catch (Exception ex) |
| 84 | + { |
| 85 | + ex.printStackTrace(); |
| 86 | + return null; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | +// private SIFEvent<StudentCollectionType> getEvents() |
| 91 | +// { |
| 92 | +// SIFEvent<StudentCollectionType> events = new SIFEvent<StudentCollectionType>(); |
| 93 | +// events.setEventAction(EventAction.UPDATE); |
| 94 | +// //events.setUpdateType(UpdateType.FULL); // If this is not set then the value from the property file should be used. |
| 95 | +// events.setSIFObjectList(getStudents()); |
| 96 | +// events.setListSize(events.getSIFObjectList().getStudentPersonal().size()); |
| 97 | +// |
| 98 | +// return events; |
| 99 | +// } |
| 100 | + |
| 101 | + private SIFEvent<String> getEvents() |
| 102 | + { |
| 103 | + SIFEvent<String> events = new SIFEvent<String>(); |
| 104 | + events.setEventAction(EventAction.UPDATE); |
| 105 | + //events.setUpdateType(UpdateType.FULL); // If this is not set then the value from the property file should be used. |
| 106 | + events.setSIFObjectList(new String("123,Peter,Hydon,1\n456,Dan,Carter,1\n890,Monica,Saladin,2")); |
| 107 | + events.setListSize(3); |
| 108 | + |
| 109 | + return events; |
| 110 | + } |
| 111 | + |
| 112 | + private URI getEventConnectorURI() |
| 113 | + { |
| 114 | + try |
| 115 | + { |
| 116 | + return new URI(EVENT_CONNECTOR_URI); |
| 117 | + } |
| 118 | + catch (Exception ex) |
| 119 | + { |
| 120 | + return null; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /* |
| 125 | + * Just fake a SIF3Session. |
| 126 | + */ |
| 127 | +// private SIF3Session getSIF3Session() |
| 128 | +// { |
| 129 | +// SIF3Session session = new SIF3Session(); |
| 130 | +// session.setSessionToken(SESSION_TOKEN); |
| 131 | +// session.setPassword(PWD); |
| 132 | +// session.setDefaultZone(new SIFZone(ZONE, true)); |
| 133 | +// |
| 134 | +// return session; |
| 135 | +// } |
| 136 | + |
| 137 | + private EventClient getEventClient() |
| 138 | + { |
| 139 | +// store.getEnvironment().addConnectorBaseURI(ConnectorName.eventsConnector, getEventConnectorURI()); |
| 140 | +// ProviderEnvironment env = (ProviderEnvironment)store.getEnvironment(); |
| 141 | + |
| 142 | + BrokeredProviderEnvironmentManager.getInstance().getEnvironmentInfo().addConnectorBaseURI(ConnectorName.eventsConnector, getEventConnectorURI()); |
| 143 | + return new EventClient(BrokeredProviderEnvironmentManager.getInstance(), MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_PLAIN_TYPE, "CSVStudents", new CSVMarshaller(), false); |
| 144 | +// return new EventClient(env, env.getMediaType(), env.getMediaType(), getSIF3Session(), "StudentPersonals", marshaller); |
| 145 | + } |
| 146 | + |
| 147 | + private void sendEvent(EventClient evtClt) throws ServiceInvokationException |
| 148 | + { |
| 149 | + System.out.println(evtClt.sendEvents(getEvents(), null, null, getOverrideHeaderProperties())); |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + private HeaderProperties getOverrideHeaderProperties() |
| 154 | + { |
| 155 | + HeaderProperties hdrProps = new HeaderProperties(); |
| 156 | + hdrProps.setHeaderProperty(RequestHeaderConstants.HDR_GENERATOR_ID, "TestEventGenerator"); |
| 157 | + hdrProps.setHeaderProperty(RequestHeaderConstants.HDR_APPLICATION_KEY, "TestEventClient"); |
| 158 | + hdrProps.setHeaderProperty(RequestHeaderConstants.HDR_AUTHENTICATED_USER, ""); |
| 159 | + |
| 160 | + return hdrProps; |
| 161 | + } |
| 162 | + |
| 163 | + public static void main(String[] args) |
| 164 | + { |
| 165 | + try |
| 166 | + { |
| 167 | + TestEventClient tester = new TestEventClient(); |
| 168 | + |
| 169 | + System.out.println("Start Testing TestEventClient..."); |
| 170 | + |
| 171 | + EventClient evtClt = tester.getEventClient(); |
| 172 | + for (int i=0; i<5; i++) |
| 173 | + { |
| 174 | + tester.sendEvent(evtClt); |
| 175 | + } |
| 176 | + |
| 177 | + System.out.println("End Testing TestEventClient."); |
| 178 | + } |
| 179 | + catch (Exception ex) |
| 180 | + { |
| 181 | + ex.printStackTrace(); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments