View Javadoc
1   package org.bremersee.ldaptive.serializable;
2   
3   import java.nio.charset.StandardCharsets;
4   import org.assertj.core.api.SoftAssertions;
5   import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
6   import org.bremersee.ldaptive.LdaptiveObjectMapperModule;
7   import org.junit.jupiter.api.BeforeAll;
8   import org.junit.jupiter.api.Test;
9   import org.junit.jupiter.api.extension.ExtendWith;
10  import org.ldaptive.LdapAttribute;
11  import org.ldaptive.LdapEntry;
12  import tools.jackson.databind.json.JsonMapper;
13  
14  /**
15   * The serializable ldap entry test.
16   */
17  @ExtendWith({SoftAssertionsExtension.class})
18  class SerLdapEntryTest {
19  
20    private static JsonMapper objectMapper;
21  
22    /**
23     * Init object mapper.
24     */
25    @BeforeAll
26    static void initObjectMapper() {
27      objectMapper = JsonMapper.builder()
28          .addModules(new LdaptiveObjectMapperModule())
29          .build();
30    }
31  
32    /**
33     * Gets dn.
34     *
35     * @param softly the softly
36     */
37    @Test
38    void getDn(SoftAssertions softly) {
39      SerLdapEntry e0 = new SerLdapEntry(null);
40      SerLdapEntry e1 = new SerLdapEntry(null);
41      softly
42          .assertThat(e0)
43          .isEqualTo(e1);
44      softly
45          .assertThat(e0.hashCode())
46          .isEqualTo(e1.hashCode());
47      softly
48          .assertThat(e0.toString())
49          .contains("null");
50      softly
51          .assertThat(e0.getDn())
52          .isNull();
53  
54      LdapEntry le0 = new LdapEntry();
55      le0.setDn("dc=junit");
56      e0 = new SerLdapEntry(le0);
57      e1 = new SerLdapEntry(le0);
58      softly
59          .assertThat(e0)
60          .isEqualTo(e1);
61      softly
62          .assertThat(e0.hashCode())
63          .isEqualTo(e1.hashCode());
64      softly
65          .assertThat(e0.toString())
66          .contains("dc=junit");
67      softly
68          .assertThat(e0.getDn())
69          .isEqualTo("dc=junit");
70    }
71  
72    /**
73     * Gets attributes.
74     *
75     * @param softly the softly
76     */
77    @Test
78    void getAttributes(SoftAssertions softly) {
79      SerLdapEntry e0 = new SerLdapEntry(null);
80      softly
81          .assertThat(e0.getAttributes())
82          .isEmpty();
83  
84      LdapAttribute la0 = new LdapAttribute("say", " Hello world!");
85      LdapEntry le0 = new LdapEntry();
86      le0.setDn("dc=junit");
87      le0.addAttributes(la0);
88      e0 = new SerLdapEntry(le0);
89      softly
90          .assertThat(e0.getAttributes())
91          .containsExactly((new SerLdapAttr(la0)));
92  
93      softly
94          .assertThat(e0.findAttribute("SAY"))
95          .hasValue((new SerLdapAttr(la0)));
96  
97      LdapEntry le1 = e0.toLdapEntry();
98      softly
99          .assertThat(le1)
100         .isEqualTo(le0);
101   }
102 
103   /**
104    * Json.
105    *
106    * @param softly the softly
107    */
108   @Test
109   void json(SoftAssertions softly) {
110     LdapAttribute la0 = new LdapAttribute("say", "Hello world!", "How are you?");
111     LdapAttribute la1 = new LdapAttribute("bin");
112     la1.setBinary(true);
113     la1.addBinaryValues("I'm a jpeg photo ;-)".getBytes(StandardCharsets.UTF_8));
114     LdapEntry le0 = new LdapEntry();
115     le0.setDn("dc=junit");
116     le0.addAttributes(la0, la1);
117 
118     String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(le0);
119     LdapEntry readEntry = objectMapper.readValue(json, LdapEntry.class);
120     softly
121         .assertThat(readEntry)
122         .isEqualTo(le0);
123 
124     SerLdapEntry expected = new SerLdapEntry(le0);
125     json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
126     SerLdapEntry actual = objectMapper.readValue(json, SerLdapEntry.class);
127     softly
128         .assertThat(actual).isEqualTo(expected);
129   }
130 
131 }