/** * Test entry. * * @throws Exception If the test failed unexpectedly. */ @Test public void testEntryToAndFromDatabase() throws Exception { ensureServerIsUpAndRunning(); // Convert the test LDIF string to a byte array byte[] originalLDIFBytes = StaticUtils.getBytes(ldifString); try (final LDIFReader reader = new LDIFReader(new LDIFImportConfig(new ByteArrayInputStream(originalLDIFBytes)))) { Entry entryBefore, entryAfter; while ((entryBefore = reader.readEntry(false)) != null) { ByteString bytes = ID2Entry.entryToDatabase(entryBefore, new DataConfig(false, false, null)); entryAfter = ID2Entry.entryFromDatabase(bytes, DirectoryServer.getDefaultCompressedSchema()); // check DN and number of attributes assertEquals(entryBefore.getAttributes().size(), entryAfter.getAttributes().size()); assertEquals(entryBefore.getName(), entryAfter.getName()); // check the object classes were not changed for (String ocBefore : entryBefore.getObjectClasses().values()) { ObjectClass objectClass = DirectoryServer.getObjectClass(ocBefore.toLowerCase()); if (objectClass == null) { objectClass = DirectoryServer.getDefaultObjectClass(ocBefore); } String ocAfter = entryAfter.getObjectClasses().get(objectClass); assertEquals(ocBefore, ocAfter); } // check the user attributes were not changed for (AttributeType attrType : entryBefore.getUserAttributes().keySet()) { List<Attribute> listBefore = entryBefore.getAttribute(attrType); List<Attribute> listAfter = entryAfter.getAttribute(attrType); assertThat(listBefore).hasSameSizeAs(listAfter); for (Attribute attrBefore : listBefore) { boolean found = false; for (Attribute attrAfter : listAfter) { if (attrAfter.optionsEqual(attrBefore.getOptions())) { // Found the corresponding attribute assertEquals(attrBefore, attrAfter); found = true; } } assertTrue(found); } } } } }
/** * The attributes will be encoded as a sequence of: - A UTF-8 byte representation of the attribute * name. - A zero delimiter - A one-to-five byte number of values for the attribute - A sequence * of: - A one-to-five byte length for the value - A UTF-8 byte representation for the value */ private void append(ByteStringBuilder buffer, Map<AttributeType, List<Attribute>> attributes) { for (List<Attribute> attrList : attributes.values()) { for (Attribute a : attrList) { byte[] nameBytes = getBytes(a.getNameWithOptions()); buffer.appendBytes(nameBytes); buffer.appendByte(0x00); buffer.appendBERLength(a.size()); for (ByteString v : a) { buffer.appendBERLength(v.length()); buffer.appendBytes(v); } } } }
private void encodeV1Attributes( ByteStringBuilder buffer, Map<AttributeType, List<Attribute>> attributes) { int numAttributes = 0; // First count how many attributes are there to encode. for (List<Attribute> attrList : attributes.values()) { for (Attribute a : attrList) { if (a.isVirtual() || a.isEmpty()) { continue; } numAttributes++; } } // Encoded one-to-five byte number of attributes buffer.appendBERLength(numAttributes); append(buffer, attributes); }
private void encodeV2Attributes( ByteStringBuilder buffer, Map<AttributeType, List<Attribute>> attributes, EntryEncodeConfig config) throws DirectoryException { int numAttributes = 0; // First count how many attributes are there to encode. for (List<Attribute> attrList : attributes.values()) { for (Attribute a : attrList) { if (a.isVirtual() || a.isEmpty()) { continue; } numAttributes++; } } // Encoded one-to-five byte number of attributes buffer.appendBERLength(numAttributes); if (config.compressAttributeDescriptions()) { for (List<Attribute> attrList : attributes.values()) { for (Attribute a : attrList) { if (a.isVirtual() || a.isEmpty()) { continue; } ByteStringBuilder bsb = new ByteStringBuilder(); config.getCompressedSchema().encodeAttribute(bsb, a); buffer.appendBERLength(bsb.length()); buffer.appendBytes(bsb); } } } else { append(buffer, attributes); } }