View Javadoc
1   package org.bremersee.ldaptive.serializable;
2   
3   import com.fasterxml.jackson.core.JsonProcessingException;
4   import com.fasterxml.jackson.databind.ObjectMapper;
5   import java.nio.charset.StandardCharsets;
6   import org.assertj.core.api.SoftAssertions;
7   import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
8   import org.bremersee.ldaptive.LdaptiveObjectMapperModule;
9   import org.junit.jupiter.api.BeforeAll;
10  import org.junit.jupiter.api.Test;
11  import org.junit.jupiter.api.extension.ExtendWith;
12  import org.ldaptive.LdapAttribute;
13  import org.ldaptive.LdapEntry;
14  
15  /**
16   * The serializable ldap entry test.
17   */
18  @ExtendWith({SoftAssertionsExtension.class})
19  class SerLdapEntryTest {
20  
21    private static ObjectMapper objectMapper;
22  
23    /**
24     * Init object mapper.
25     */
26    @BeforeAll
27    static void initObjectMapper() {
28      objectMapper = new ObjectMapper();
29      objectMapper.registerModule(new LdaptiveObjectMapperModule());
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    * @throws JsonProcessingException the json processing exception
108    */
109   @Test
110   void json(SoftAssertions softly) throws JsonProcessingException {
111     LdapAttribute la0 = new LdapAttribute("say", "Hello world!", "How are you?");
112     LdapAttribute la1 = new LdapAttribute("bin");
113     la1.setBinary(true);
114     la1.addBinaryValues("I'm a jpeg photo ;-)".getBytes(StandardCharsets.UTF_8));
115     LdapEntry le0 = new LdapEntry();
116     le0.setDn("dc=junit");
117     le0.addAttributes(la0, la1);
118 
119     String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(le0);
120     LdapEntry readEntry = objectMapper.readValue(json, LdapEntry.class);
121     softly
122         .assertThat(readEntry)
123         .isEqualTo(le0);
124 
125     SerLdapEntry expected = new SerLdapEntry(le0);
126     json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(expected);
127     SerLdapEntry actual = objectMapper.readValue(json, SerLdapEntry.class);
128     softly
129         .assertThat(actual).isEqualTo(expected);
130   }
131 
132 }