directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From build...@apache.org
Subject svn commit: r1036684 - in /websites/staging/directory/trunk/content: ./ apacheds/advanced-ug/7-embedding-apacheds.html
Date Thu, 01 Nov 2018 12:26:27 GMT
Author: buildbot
Date: Thu Nov  1 12:26:27 2018
New Revision: 1036684

Log:
Staging update by buildbot for directory

Modified:
    websites/staging/directory/trunk/content/   (props changed)
    websites/staging/directory/trunk/content/apacheds/advanced-ug/7-embedding-apacheds.html

Propchange: websites/staging/directory/trunk/content/
------------------------------------------------------------------------------
--- cms:source-revision (original)
+++ cms:source-revision Thu Nov  1 12:26:27 2018
@@ -1 +1 @@
-1842054
+1845459

Modified: websites/staging/directory/trunk/content/apacheds/advanced-ug/7-embedding-apacheds.html
==============================================================================
--- websites/staging/directory/trunk/content/apacheds/advanced-ug/7-embedding-apacheds.html
(original)
+++ websites/staging/directory/trunk/content/apacheds/advanced-ug/7-embedding-apacheds.html
Thu Nov  1 12:26:27 2018
@@ -181,7 +181,126 @@
 }
 h2:hover > .headerlink, h3:hover > .headerlink, h1:hover > .headerlink, h6:hover
> .headerlink, h4:hover > .headerlink, h5:hover > .headerlink, dt:hover > .elementid-permalink
{ visibility: visible }</style>
 <h1 id="7-embedding-apacheds-in-java">7 - Embedding ApacheDS in Java<a class="headerlink"
href="#7-embedding-apacheds-in-java" title="Permanent link">&para;</a></h1>
-<p>TODO...</p>
+<h2 id="using-apacheds-for-unit-tests">Using ApacheDS for unit tests<a class="headerlink"
href="#using-apacheds-for-unit-tests" title="Permanent link">&para;</a></h2>
+<p>The idea is to use ADS as an embedded server for Ldap junit tests.
+We will build an environment in which it will be convenient to test
+Ldap applications.</p>
+<p>We also want to avoid launching the server for every test, as it's
+an expensive operation. We have built ApacheDS so that you can start
+a server, inject some data, launch a test, then revert the data and
+go on to another test. At the end of the tests, the server is stopped.</p>
+<h3 id="prerequicites">Prerequicites<a class="headerlink" href="#prerequicites"
title="Permanent link">&para;</a></h3>
+<p>We will assume you have <strong>Maven</strong> project and using <strong>JUNIT</strong>
for testing</p>
+<h3 id="first-steps">First steps<a class="headerlink" href="#first-steps" title="Permanent
link">&para;</a></h3>
+<h4 id="maven">Maven<a class="headerlink" href="#maven" title="Permanent link">&para;</a></h4>
+<p>You need to add following dependency to your <code>pom.xml</code>:</p>
+<div class="codehilite"><pre><span class="nt">&lt;dependency&gt;</span>
+    <span class="nt">&lt;groupId&gt;</span>org.apache.directory.server<span
class="nt">&lt;/groupId&gt;</span>
+    <span class="nt">&lt;artifactId&gt;</span>apacheds-test-framework<span
class="nt">&lt;/artifactId&gt;</span>
+    <span class="nt">&lt;version&gt;</span>2.0.0.AM25<span class="nt">&lt;/version&gt;</span>
+    <span class="nt">&lt;scope&gt;</span>test<span class="nt">&lt;/scope&gt;</span>
+<span class="nt">&lt;/dependency&gt;</span>
+</pre></div>
+
+
+<p>Please NOTE: you need to exclude <code>org.apache.directory.api:api-ldap-schema-data</code>
+artifact from above dependency in case your project uses <code>org.apache.directory.api:api-all</code></p>
+<h4 id="java">Java<a class="headerlink" href="#java" title="Permanent link">&para;</a></h4>
+<p>Maven set-up was easy enough, let's create our first unit test:</p>
+<h5 id="resources">Resources<a class="headerlink" href="#resources" title="Permanent
link">&para;</a></h5>
+<p>Lets describe your LDAP structure in external LDIF file</p>
+<p>Create file <code>src/test/resources/users.ldif</code> with following
text:</p>
+<div class="codehilite"><pre><span class="n">version</span><span
class="o">:</span> <span class="mi">1</span>
+<span class="n">dn</span><span class="o">:</span> <span class="n">dc</span><span
class="o">=</span><span class="n">myorg</span><span class="o">,</span><span
class="n">dc</span><span class="o">=</span><span class="n">com</span>
+<span class="n">objectClass</span><span class="o">:</span> <span
class="n">domain</span>
+<span class="n">objectClass</span><span class="o">:</span> <span
class="n">top</span>
+<span class="n">dc</span><span class="o">:</span> <span class="n">myorg</span>
+
+<span class="n">dn</span><span class="o">:</span> <span class="n">ou</span><span
class="o">=</span><span class="n">Users</span><span class="o">,</span><span
class="n">dc</span><span class="o">=</span><span class="n">myorg</span><span
class="o">,</span><span class="n">dc</span><span class="o">=</span><span
class="n">com</span>
+<span class="n">objectClass</span><span class="o">:</span> <span
class="n">organizationalUnit</span>
+<span class="n">objectClass</span><span class="o">:</span> <span
class="n">top</span>
+<span class="n">ou</span><span class="o">:</span> <span class="n">Users</span>
+
+<span class="n">dn</span><span class="o">:</span> <span class="n">ou</span><span
class="o">=</span><span class="n">Groups</span><span class="o">,</span><span
class="n">dc</span><span class="o">=</span><span class="n">myorg</span><span
class="o">,</span><span class="n">dc</span><span class="o">=</span><span
class="n">com</span>
+<span class="n">objectClass</span><span class="o">:</span> <span
class="n">organizationalUnit</span>
+<span class="n">objectClass</span><span class="o">:</span> <span
class="n">top</span>
+<span class="n">ou</span><span class="o">:</span> <span class="n">Groups</span>
+
+<span class="n">dn</span><span class="o">:</span> <span class="n">cn</span><span
class="o">=</span><span class="n">Test1</span> <span class="n">Ldap</span><span
class="o">,</span><span class="n">ou</span><span class="o">=</span><span
class="n">Users</span><span class="o">,</span><span class="n">dc</span><span
class="o">=</span><span class="n">myorg</span><span class="o">,</span><span
class="n">dc</span><span class="o">=</span><span class="n">com</span>
+<span class="n">objectClass</span><span class="o">:</span> <span
class="n">inetOrgPerson</span>
+<span class="n">objectClass</span><span class="o">:</span> <span
class="n">organizationalPerson</span>
+<span class="n">objectClass</span><span class="o">:</span> <span
class="n">person</span>
+<span class="n">objectClass</span><span class="o">:</span> <span
class="n">top</span>
+<span class="n">cn</span><span class="o">:</span> <span class="n">Test1</span>
<span class="n">Ldap</span>
+<span class="n">sn</span><span class="o">:</span> <span class="n">Ldap</span>
+<span class="n">uid</span><span class="o">:</span> <span class="n">ldaptest1</span>
+<span class="n">userPassword</span><span class="o">:</span> <span
class="mi">12345</span>
+</pre></div>
+
+
+<h5 id="basic-test">Basic test<a class="headerlink" href="#basic-test" title="Permanent
link">&para;</a></h5>
+<div class="codehilite"><pre><span class="n">import</span> <span
class="n">org</span><span class="p">.</span><span class="n">apache</span><span
class="p">.</span><span class="n">directory</span><span class="p">.</span><span
class="n">server</span><span class="p">.</span><span class="n">core</span><span
class="p">.</span><span class="n">integ</span><span class="p">.</span><span
class="n">AbstractLdapTestUnit</span><span class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">apache</span><span class="p">.</span><span
class="n">directory</span><span class="p">.</span><span class="n">server</span><span
class="p">.</span><span class="n">core</span><span class="p">.</span><span
class="n">integ</span><span class="p">.</span><span class="n">FrameworkRunner</span><span
class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">apache</span><span class="p">.</span><span
class="n">directory</span><span class="p">.</span><span class="n">server</span><span
class="p">.</span><span class="n">core</span><span class="p">.</span><span
class="n">annotations</span><span class="p">.</span><span class="n">CreateDS</span><span
class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">apache</span><span class="p">.</span><span
class="n">directory</span><span class="p">.</span><span class="n">server</span><span
class="p">.</span><span class="n">core</span><span class="p">.</span><span
class="n">annotations</span><span class="p">.</span><span class="n">CreatePartition</span><span
class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">apache</span><span class="p">.</span><span
class="n">directory</span><span class="p">.</span><span class="n">server</span><span
class="p">.</span><span class="n">annotations</span><span class="p">.</span><span
class="n">CreateLdapServer</span><span class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">apache</span><span class="p">.</span><span
class="n">directory</span><span class="p">.</span><span class="n">server</span><span
class="p">.</span><span class="n">annotations</span><span class="p">.</span><span
class="n">CreateTransport</span><span class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">apache</span><span class="p">.</span><span
class="n">directory</span><span class="p">.</span><span class="n">server</span><span
class="p">.</span><span class="n">core</span><span class="p">.</span><span
class="n">annotations</span><span class="p">.</span><span class="n">ApplyLdifFiles</span><span
class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">junit</span><span class="p">.</span><span
class="n">runner</span><span class="p">.</span><span class="n">RunWith</span><span
class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">junit</span><span class="p">.</span><span
class="n">Test</span><span class="p">;</span>
+
+<span class="p">@</span><span class="n">RunWith</span><span class="p">(</span><span
class="n">FrameworkRunner</span><span class="p">.</span><span class="n">class</span><span
class="p">)</span>
+<span class="p">@</span><span class="n">CreateDS</span><span class="p">(</span><span
class="n">name</span> <span class="p">=</span> &quot;<span class="n">myDS</span>&quot;<span
class="p">,</span>
+    <span class="n">partitions</span> <span class="p">=</span> <span
class="p">{</span>
+        <span class="p">@</span><span class="n">CreatePartition</span><span
class="p">(</span><span class="n">name</span> <span class="p">=</span>
&quot;<span class="n">test</span>&quot;<span class="p">,</span>
<span class="n">suffix</span> <span class="p">=</span> &quot;<span
class="n">dc</span><span class="p">=</span><span class="n">myorg</span><span
class="p">,</span><span class="n">dc</span><span class="p">=</span><span
class="n">com</span>&quot;<span class="p">)</span>
+    <span class="p">})</span>
+<span class="p">@</span><span class="n">CreateLdapServer</span><span
class="p">(</span><span class="n">transports</span> <span class="p">=</span>
<span class="p">{</span> <span class="p">@</span><span class="n">CreateTransport</span><span
class="p">(</span><span class="n">protocol</span> <span class="p">=</span>
&quot;<span class="n">LDAP</span>&quot;<span class="p">,</span>
<span class="n">address</span> <span class="p">=</span> &quot;<span
class="n">localhost</span>&quot;<span class="p">)})</span>
+<span class="p">@</span><span class="n">ApplyLdifFiles</span><span
class="p">({</span>&quot;<span class="n">users</span><span class="p">.</span><span
class="n">ldif</span>&quot;<span class="p">})</span>
+<span class="n">public</span> <span class="n">class</span> <span
class="n">TestLdap</span> <span class="n">extends</span> <span class="n">AbstractLdapTestUnit</span>
<span class="p">{</span>
+    <span class="p">@</span><span class="n">Test</span>
+    <span class="n">public</span> <span class="n">void</span> <span
class="n">test</span><span class="p">()</span> <span class="p">{</span>
+        <span class="o">//</span><span class="n">do</span> <span
class="n">whatever</span> <span class="n">you</span> <span class="n">need</span>
<span class="n">with</span> `<span class="n">ldapServer</span>`
+    <span class="p">}</span>
+<span class="p">}</span>
+</pre></div>
+
+
+<p>As a result you can access running ApacheDS server at <code>ldap://localhost:&lt;random
port&gt;</code>
+inside your test, or access server internals via <code>static ldapServer</code>.</p>
+<h5 id="advanced-usage">Advanced usage<a class="headerlink" href="#advanced-usage"
title="Permanent link">&para;</a></h5>
+<p>Often you need to add LDAP unit tests to already created tests, in this case you
might
+not be allowed to <code>extend AbstractLdapTestUnit</code> and/or to <code>@RunWith(FrameworkRunner.class)</code>
+In this case you can use <code>@ClassRule</code>.
+Here is the example:</p>
+<div class="codehilite"><pre><span class="n">import</span> <span
class="n">org</span><span class="p">.</span><span class="n">apache</span><span
class="p">.</span><span class="n">directory</span><span class="p">.</span><span
class="n">server</span><span class="p">.</span><span class="n">core</span><span
class="p">.</span><span class="n">annotations</span><span class="p">.</span><span
class="n">CreateDS</span><span class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">apache</span><span class="p">.</span><span
class="n">directory</span><span class="p">.</span><span class="n">server</span><span
class="p">.</span><span class="n">core</span><span class="p">.</span><span
class="n">annotations</span><span class="p">.</span><span class="n">CreatePartition</span><span
class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">apache</span><span class="p">.</span><span
class="n">directory</span><span class="p">.</span><span class="n">server</span><span
class="p">.</span><span class="n">annotations</span><span class="p">.</span><span
class="n">CreateLdapServer</span><span class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">apache</span><span class="p">.</span><span
class="n">directory</span><span class="p">.</span><span class="n">server</span><span
class="p">.</span><span class="n">annotations</span><span class="p">.</span><span
class="n">CreateTransport</span><span class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">apache</span><span class="p">.</span><span
class="n">directory</span><span class="p">.</span><span class="n">server</span><span
class="p">.</span><span class="n">core</span><span class="p">.</span><span
class="n">annotations</span><span class="p">.</span><span class="n">ApplyLdifFiles</span><span
class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">apache</span><span class="p">.</span><span
class="n">directory</span><span class="p">.</span><span class="n">server</span><span
class="p">.</span><span class="n">core</span><span class="p">.</span><span
class="n">integ</span><span class="p">.</span><span class="n">CreateLdapServerRule</span><span
class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">junit</span><span class="p">.</span><span
class="n">ClassRule</span><span class="p">;</span>
+<span class="n">import</span> <span class="n">org</span><span
class="p">.</span><span class="n">junit</span><span class="p">.</span><span
class="n">Test</span><span class="p">;</span>
+
+<span class="p">@</span><span class="n">CreateDS</span><span class="p">(</span><span
class="n">name</span> <span class="p">=</span> &quot;<span class="n">myDS</span>&quot;<span
class="p">,</span>
+    <span class="n">partitions</span> <span class="p">=</span> <span
class="p">{</span>
+        <span class="p">@</span><span class="n">CreatePartition</span><span
class="p">(</span><span class="n">name</span> <span class="p">=</span>
&quot;<span class="n">test</span>&quot;<span class="p">,</span>
<span class="n">suffix</span> <span class="p">=</span> &quot;<span
class="n">dc</span><span class="p">=</span><span class="n">myorg</span><span
class="p">,</span><span class="n">dc</span><span class="p">=</span><span
class="n">com</span>&quot;<span class="p">)</span>
+    <span class="p">})</span>
+<span class="p">@</span><span class="n">CreateLdapServer</span><span
class="p">(</span><span class="n">transports</span> <span class="p">=</span>
<span class="p">{</span> <span class="p">@</span><span class="n">CreateTransport</span><span
class="p">(</span><span class="n">protocol</span> <span class="p">=</span>
&quot;<span class="n">LDAP</span>&quot;<span class="p">,</span>
<span class="n">address</span> <span class="p">=</span> &quot;<span
class="n">localhost</span>&quot;<span class="p">)})</span>
+<span class="p">@</span><span class="n">ApplyLdifFiles</span><span
class="p">({</span>&quot;<span class="n">users</span><span class="p">.</span><span
class="n">ldif</span>&quot;<span class="p">})</span>
+<span class="n">public</span> <span class="n">class</span> <span
class="n">TestLdap</span> <span class="p">{</span>
+    <span class="p">@</span><span class="n">ClassRule</span>
+    <span class="n">public</span> <span class="n">static</span> <span
class="n">CreateLdapServerRule</span> <span class="n">serverRule</span>
<span class="p">=</span> <span class="n">new</span> <span class="n">CreateLdapServerRule</span><span
class="p">();</span>
+
+    <span class="p">@</span><span class="n">Test</span>
+    <span class="n">public</span> <span class="n">void</span> <span
class="n">test</span><span class="p">()</span> <span class="p">{</span>
+        <span class="o">//</span><span class="n">do</span> <span
class="n">whatever</span> <span class="n">you</span> <span class="n">need</span>
<span class="n">with</span> `<span class="n">serverRule</span><span
class="p">.</span><span class="n">getLdapServer</span><span class="p">()</span>`
+    <span class="p">}</span>
+<span class="p">}</span>
+</pre></div>
+
+
+<p>As you can see the code if very much the same, the only difference: you are working
with
+<code>serverRule.getLdapServer()</code> and not with <code>ldapServer</code></p>
 
 
     <div class="nav">



Mime
View raw message