|
| 1 | +/* |
| 2 | + Copyright 2015 Red Hat, Inc. and/or its affiliates. |
| 3 | +
|
| 4 | + This file is part of lightblue. |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +package com.redhat.lightblue.crud.ldap; |
| 20 | + |
| 21 | +import static com.redhat.lightblue.util.JsonUtils.json; |
| 22 | +import static com.redhat.lightblue.util.test.AbstractJsonNodeTest.loadResource; |
| 23 | +import static org.junit.Assert.assertArrayEquals; |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | + |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.Date; |
| 29 | + |
| 30 | +import javax.xml.bind.DatatypeConverter; |
| 31 | + |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.junit.runners.Parameterized; |
| 35 | +import org.junit.runners.Parameterized.Parameters; |
| 36 | + |
| 37 | +import com.redhat.lightblue.common.ldap.LdapConstant; |
| 38 | +import com.redhat.lightblue.metadata.EntityMetadata; |
| 39 | +import com.redhat.lightblue.metadata.types.DateType; |
| 40 | +import com.redhat.lightblue.test.MetadataUtil; |
| 41 | +import com.redhat.lightblue.util.JsonDoc; |
| 42 | +import com.unboundid.ldap.sdk.Entry; |
| 43 | +import com.unboundid.util.StaticUtils; |
| 44 | + |
| 45 | +@RunWith(value = Parameterized.class) |
| 46 | +public class EntryBuilderTest { |
| 47 | + |
| 48 | + private final static Date now = new Date(); |
| 49 | + |
| 50 | + @Parameters(name= "{index}: {0}") |
| 51 | + public static Collection<Object[]> data() { |
| 52 | + return Arrays.asList(new Object[][] { |
| 53 | + {"{\"type\": \"string\"}", quote("teststring"), "teststring"}, |
| 54 | + {"{\"type\": \"integer\"}", "4", null}, |
| 55 | + {"{\"type\": \"boolean\"}", "true", null}, |
| 56 | + {"{\"type\": \"date\"}", quote(DateType.getDateFormat().format(now)), StaticUtils.encodeGeneralizedTime(now)}, |
| 57 | + {"{\"type\": \"binary\"}", quote(DatatypeConverter.printBase64Binary("test binary data".getBytes())), "test binary data"}, |
| 58 | + {"{\"type\": \"array\", \"items\": {\"type\": \"string\"}}", "[\"hello\",\"world\"]", new String[]{"hello", "world"}}, |
| 59 | + {"{\"type\": \"array\", \"items\": {\"type\": \"binary\"}}", |
| 60 | + "[" + quote(DatatypeConverter.printBase64Binary("hello".getBytes())) + "," |
| 61 | + + quote(DatatypeConverter.printBase64Binary("world".getBytes())) + "]", |
| 62 | + new String[]{"hello", "world"} |
| 63 | + }, |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + private static String quote(String text){ |
| 68 | + return '"' + text + '"'; |
| 69 | + } |
| 70 | + |
| 71 | + private final String fieldName = "testfield"; |
| 72 | + private final String metadataType; |
| 73 | + private final String crudValue; |
| 74 | + private final Object expectedValue; |
| 75 | + |
| 76 | + public EntryBuilderTest(String metadataType, String crudValue, Object expectedValue){ |
| 77 | + this.metadataType = metadataType; |
| 78 | + this.crudValue = crudValue; |
| 79 | + this.expectedValue = (expectedValue == null) ? crudValue : expectedValue; |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void test() throws Exception{ |
| 84 | + String metadata = loadResource("./metadata/entryBuilderTest-metadata-template.json") |
| 85 | + .replaceFirst("#fieldname", fieldName) |
| 86 | + .replaceFirst("#type", metadataType); |
| 87 | + String crud = loadResource("./crud/insert/entryBuilderTest-insert-template.json") |
| 88 | + .replaceFirst("#fieldname", fieldName) |
| 89 | + .replaceFirst("#value", crudValue); |
| 90 | + |
| 91 | + EntityMetadata md = MetadataUtil.createEntityMetadata(LdapConstant.BACKEND, json(metadata), null, null); |
| 92 | + EntryBuilder builder = new EntryBuilder(md); |
| 93 | + |
| 94 | + Entry entry = builder.build("uid=someuid,dc=example,dc=com", |
| 95 | + new JsonDoc(json(crud).get("data"))); |
| 96 | + |
| 97 | + if(expectedValue.getClass().isArray()){ |
| 98 | + assertArrayEquals((String[]) expectedValue, entry.getAttributeValues("testfield")); |
| 99 | + } |
| 100 | + else{ |
| 101 | + assertEquals(expectedValue, entry.getAttributeValue("testfield")); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments