View Javadoc

1   /*
2    * Copyright (C) 2007 ETH Zurich
3    *
4    * This file is part of Fosstrak (www.fosstrak.org).
5    *
6    * Fosstrak is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License version 2.1, as published by the Free Software Foundation.
9    *
10   * Fosstrak is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with Fosstrak; if not, write to the Free
17   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18   * Boston, MA  02110-1301  USA
19   */
20  
21  package org.fosstrak.epcis.repository.test;
22  
23  import java.io.FileInputStream;
24  import java.io.InputStream;
25  
26  import junit.framework.TestCase;
27  
28  import org.fosstrak.epcis.captureclient.CaptureClient;
29  import org.fosstrak.epcis.queryclient.QueryControlClient;
30  import org.fosstrak.epcis.soap.NoSuchSubscriptionExceptionResponse;
31  import org.fosstrak.epcis.utils.QueryCallbackListener;
32  
33  public class CallbackTriggerTest extends TestCase {
34      private static final String PATH = "src/test/resources/queries/webservice/";
35  
36      private static QueryControlClient client = new QueryControlClient();
37  
38      /**
39       * Tests if setting the initialRecordTime parameter has effect.
40       * 
41       * @throws Exception
42       *             Any exception, caught by the JUnit framework.
43       */
44      public void testSE75() throws Exception {
45  
46          // run first query
47          String query = "Test-EPCIS10-SE75-Request-1-Subscribe.xml";
48          InputStream fis = new FileInputStream(PATH + "requests/" + query);
49          client.subscribe(fis);
50          fis.close();
51  
52          new CaptureTrigger().start();
53  
54          // wait for response callback
55          QueryCallbackListener listener = QueryCallbackListener.getInstance();
56          if (!listener.isRunning()) {
57              listener.start();
58          }
59          System.out.println("waiting ...");
60          synchronized (listener) {
61              try {
62                  listener.wait(60 * 1000);
63              } catch (InterruptedException e) {
64                  e.printStackTrace();
65              }
66          }
67          String resp = listener.fetchResponse();
68          assertNotNull(resp);
69          assertTrue(resp.contains("urn:epc:id:sgtin:0614141.107340.1"));
70  
71          client.unsubscribe("QuerySE75");
72          listener.stopRunning();
73      }
74  
75      /**
76       * {@inheritDoc}
77       * 
78       * @see junit.framework.TestCase#tearDown()
79       */
80      @Override
81      protected void tearDown() throws Exception {
82          try {
83              client.unsubscribe("QuerySE75");
84          } catch (NoSuchSubscriptionExceptionResponse e) {
85          }
86          // reset the database
87          new CaptureClient().dbReset();
88      }
89  
90      private class CaptureTrigger extends Thread {
91  
92          private StringBuilder event = new StringBuilder();
93  
94          public CaptureTrigger() {
95              event.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
96              event.append("<epcis:EPCISDocument xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:epcis=\"urn:epcglobal:epcis:xsd:1\" xmlns:epcglobal=\"urn:epcglobal:xsd:1\" xsi:schemaLocation=\"urn:epcglobal:epcis:xsd:1 EPCglobal-epcis-1_0.xsd\" xmlns:hls=\"http://schema.hls.com/extension\" creationDate=\"2006-06-25T00:00:00Z\" schemaVersion=\"1.0\">");
97              event.append("<EPCISBody>");
98              event.append("<EventList>");
99              event.append("<ObjectEvent>");
100             event.append("<eventTime>2006-08-25T00:01:00Z</eventTime>");
101             event.append("<eventTimeZoneOffset>-06:00</eventTimeZoneOffset>");
102             event.append("<epcList>");
103             event.append("<epc>urn:epc:id:sgtin:0614141.107340.1</epc>");
104             event.append("</epcList>");
105             event.append("<action>OBSERVE</action>");
106             event.append("<bizStep>urn:epcglobal:hls:bizstep:commissioning</bizStep>");
107             event.append("<disposition>urn:epcglobal:hls:disp:active</disposition>");
108             event.append("<readPoint>");
109             event.append("<id>urn:epcglobal:fmcg:loc:0614141073467.RP-1</id>");
110             event.append("</readPoint>");
111             event.append("<bizLocation>");
112             event.append("<id>urn:epcglobal:fmcg:loc:0614141073467.1</id>");
113             event.append("</bizLocation>");
114             event.append("</ObjectEvent>");
115             event.append("</EventList>");
116             event.append("</EPCISBody>");
117             event.append("</epcis:EPCISDocument>");
118         }
119 
120         /*
121          * (non-Javadoc)
122          * 
123          * @see java.lang.Thread#run()
124          */
125         @Override
126         public void run() {
127             CaptureClient client = new CaptureClient();
128             try {
129                 sleep(10000);
130                 client.capture(event.toString());
131             } catch (InterruptedException e) {
132                 e.printStackTrace();
133             } catch (Exception e) {
134                 // TODO Auto-generated catch block
135                 e.printStackTrace();
136             }
137         }
138     }
139 }