Product Selector

Fusion 5.12
    Fusion 5.12

    Setting Up a Mock Platform Response

    Table of Contents

    In some cases it can be difficult to access a search platform. In these situations, it can be useful to work against a static response in the form of a file containing a typical search response (usually as XML).

    This is not a recommended method for developing a fully featured search application from start to finish, because it is impossible to emulate the dynamic nature of a live platform. However this is useful in a number of situations including:

    • Where networking or security considerations prevent access to the platform

    • Debugging issues in an application where it is necessary to recreate a specific type of response

    • Initial development phases where access to a live platform with realistic data is unavailable

    • Deterministic test conditions (this could be for unit tests as well as any other form of manual or automated testing)

    How to set up a mock platform

    A few simple steps are required to run an application against a 'mock' response:

    1. Create a mock response file by saving the XML from a query issued directly against the platform, or request one from someone who can access a platform instance. This should represent the type of response you want to mock. See this example of a Solr XML response:

      <?xml version="1.0" encoding="UTF-8"?>
      <response>
          <lst name="responseHeader">
            <int name="status">0</int>
            <int name="QTime">24</int>
            <lst name="params">
              <str name="spellcheck">true</str>
              <str name="facet">true</str>
              <str name="indent">true</str>
              <str name="facet.limit">10</str>
              <str name="wt">xmk</str>
              <str name="rows">5</str>
              <str name="fl">*,score</str>
              <str name="start">0</str>
              <arr name="q">
                <str>*:*</str>
              </arr>
              <str name="q.op">OR</str>
              <str name="facet.field">month</str>
              <str name="qt">/prodover</str>
              <str name="fq">category:Clothing</str>
            </lst>
          </lst>
          <result name="response" numFound="21111" start="0" maxScore="1.0">
            <doc>
              <str name="category">Clothing</str>
              <str name="p_image_path_1_s">/s/c/sc1402sep26pictga10015.jpg</str>
              <int name="p_id_i">356980</int>
              <date name="p_date">2014-10-07T00:00:00Z</date>
              <str name="p_name">Generic T-Shirt</str>
              <float name="p_price">552.63</float>
              <arr name="colors">
                <str>Black</str>
                <str>White</str>
                <str>Green</str>
              </arr>
              <long name="_version_">1486360119259168768</long>
              <float name="score">1.0</float>
            </doc>
            <doc>
              <str name="category">Clothing</str>
              <str name="p_image_path_1_s">/s/c/sc1402sep26pictga10015.jpg</str>
              <int name="p_id_i">356980</int>
              <date name="p_date">2014-10-07T00:00:00Z</date>
              <str name="p_name">Generic T-Shirt</str>
              <float name="p_price">552.63</float>
              <arr name="colors">
                <str>Black</str>
                <str>White</str>
                <str>Green</str>
              </arr>
              <long name="_version_">1486360119259168768</long>
              <float name="score">1.0</float>
            </doc>
            <doc>
              <str name="category">Clothing</str>
              <str name="p_image_path_1_s">/s/c/sc1402sep26pictga10015.jpg</str>
              <int name="p_id_i">356980</int>
              <date name="p_date">2014-10-07T00:00:00Z</date>
              <str name="p_name">Generic T-Shirt</str>
              <float name="p_price">552.63</float>
              <arr name="colors">
                <str>Black</str>
                <str>White</str>
                <str>Green</str>
              </arr>
              <long name="_version_">1486360119259168768</long>
              <float name="score">1.0</float>
            </doc>
            <doc>
              <str name="category">Clothing</str>
              <str name="p_image_path_1_s">/s/c/sc1402sep26pictga10015.jpg</str>
              <int name="p_id_i">356980</int>
              <date name="p_date">2014-10-07T00:00:00Z</date>
              <str name="p_name">Generic T-Shirt</str>
              <float name="p_price">552.63</float>
              <arr name="colors">
                <str>Black</str>
                <str>White</str>
                <str>Green</str>
              </arr>
              <long name="_version_">1486360119259168768</long>
              <float name="score">1.0</float>
            </doc>
            <doc>
              <str name="category">Clothing</str>
              <str name="p_image_path_1_s">/s/c/sc1402sep26pictga10015.jpg</str>
              <int name="p_id_i">356980</int>
              <date name="p_date">2014-10-07T00:00:00Z</date>
              <str name="p_name">Generic T-Shirt</str>
              <float name="p_price">552.63</float>
              <arr name="colors">
                <str>Black</str>
                <str>White</str>
                <str>Green</str>
              </arr>
              <long name="_version_">1486360119259168768</long>
              <float name="score">1.0</float>
            </doc>
          </result>
          <lst name="facet_counts">
            <lst name="facet_queries"/>
            <lst name="facet_fields">
              <lst name="month">
                <int name="November">9140</int>
                <int name="October">7936</int>
                <int name="September">7215</int>
                <int name="August">7088</int>
                <int name="July">6878</int>
                <int name="June">5238</int>
                <int name="May">4351</int>
                <int name="April">4207</int>
                <int name="March">3593</int>
                <int name="February">3002</int>
              </lst>
            </lst>
            <lst name="facet_dates"/>
            <lst name="facet_ranges"/>
            <lst name="facet_intervals"/>
          </lst>
      </response>
    2. Copy the XML file into your project to:

      src/main/webapp/WEB-INF/pages/mock-response.xml
    3. Modify the URL rules configuration to add a rule that emulates the path at which the search platform interface exists. For example:

          <rule>
              <from>^/login/</from>
              <to last="true">/login.jsp</to>
          </rule>
      
          <rule>
              <from>^/login</from>
              <to last="true">/login.jsp</to>
          </rule>
      
          <rule match-type="wildcard">
              <name>Ignore files on URL path.</name>
              <from>/**.*</from>
              <to last="true">/$0</to>
          </rule>
          <rule>
              <from>^/mock-response/</from>
              <to last="true">/WEB-INF/pages/mock-response.xml</to>
          </rule>
          <rule match-type="wildcard">
              <name>HTML 5 URL Rewrite</name>
              <from>/**</from>
              <to last="true">/index.jsp</to>
          </rule>
    4. Configure the platform host to point to the relevant URL:

      name: twigkit.search.solr.Solr
      result-id-field: url
      host: http://localhost:8080/mock-response
      defaultQuery: test

    The application is now configured to use this response XML file instead of a live platform whenever a query is issued. Obviously this will mean the same result set is retrieved no matter how the query is modified through the user interface. It is possible to mock different result sets by adding more complex URL rules to intercept the different parameters modified by interacting with Appkit UI components, however this is generally not recommended.