Search in sources :

Example 1 with DnFieldValueImpl

use of ee.ria.xroad.common.certificateprofile.impl.DnFieldValueImpl in project X-Road by nordic-institute.

the class DnFieldHelperTest method processDnParameters.

@Test
public void processDnParameters() throws Exception {
    DnFieldDescription field1ReadOnly = new DnFieldDescriptionImpl(FIELD_1, "x", FIELD_1_DEFAULT).setReadOnly(true);
    DnFieldDescription field2Editable = new DnFieldDescriptionImpl(FIELD_2, "x", FIELD_2_DEFAULT).setReadOnly(false);
    // read only
    // no param
    List<DnFieldValue> values = helper.processDnParameters(new DnFieldTestCertificateProfileInfo(field1ReadOnly, true), new HashMap<>());
    assertTrue(values.size() == 1);
    assertEquals(new DnFieldValueImpl(FIELD_1, FIELD_1_DEFAULT), values.iterator().next());
    // attempt to set param is ignored
    values = helper.processDnParameters(new DnFieldTestCertificateProfileInfo(field1ReadOnly, true), ImmutableMap.of(FIELD_1, "bar"));
    assertTrue(values.size() == 1);
    assertEquals(new DnFieldValueImpl(FIELD_1, FIELD_1_DEFAULT), values.iterator().next());
    // extra param
    try {
        helper.processDnParameters(new DnFieldTestCertificateProfileInfo(field1ReadOnly, true), ImmutableMap.of("foo", "bar"));
        fail("should throw exception");
    } catch (DnFieldHelper.InvalidDnParameterException expected) {
    }
    // no param
    try {
        helper.processDnParameters(new DnFieldTestCertificateProfileInfo(field2Editable, true), new HashMap<>());
        fail("should throw exception");
    } catch (DnFieldHelper.InvalidDnParameterException expected) {
    }
    // set param
    values = helper.processDnParameters(new DnFieldTestCertificateProfileInfo(field2Editable, true), ImmutableMap.of(FIELD_2, "bar"));
    assertTrue(values.size() == 1);
    assertEquals(new DnFieldValueImpl(FIELD_2, "bar"), values.iterator().next());
    // extra param 1
    try {
        helper.processDnParameters(new DnFieldTestCertificateProfileInfo(field2Editable, true), ImmutableMap.of("foo", "bar"));
        fail("should throw exception");
    } catch (DnFieldHelper.InvalidDnParameterException expected) {
    }
    // extra param 2
    try {
        helper.processDnParameters(new DnFieldTestCertificateProfileInfo(field2Editable, true), ImmutableMap.of(FIELD_2, "bar", "foo", "bar2"));
        fail("should throw exception");
    } catch (DnFieldHelper.InvalidDnParameterException expected) {
    }
    // invalid param
    try {
        values = helper.processDnParameters(new DnFieldTestCertificateProfileInfo(field2Editable, false), ImmutableMap.of(FIELD_2, "bar"));
        fail("should throw exception");
    } catch (DnFieldHelper.InvalidDnParameterException expected) {
    }
}
Also used : DnFieldValue(ee.ria.xroad.common.certificateprofile.DnFieldValue) DnFieldDescription(ee.ria.xroad.common.certificateprofile.DnFieldDescription) DnFieldValueImpl(ee.ria.xroad.common.certificateprofile.impl.DnFieldValueImpl) DnFieldDescriptionImpl(ee.ria.xroad.common.certificateprofile.impl.DnFieldDescriptionImpl) Test(org.junit.Test)

Example 2 with DnFieldValueImpl

use of ee.ria.xroad.common.certificateprofile.impl.DnFieldValueImpl in project X-Road by nordic-institute.

the class DnFieldHelper method processDnParameters.

/**
 * Read dn parameters from dnParameters map, match them to DnFieldDescription
 * definitions (consider readOnly, required, etc) and validate that all parameters
 * are fine.
 * @return valid DnFieldValue objects
 * @throws InvalidDnParameterException if there were invalid parameters
 */
public List<DnFieldValue> processDnParameters(CertificateProfileInfo profile, Map<String, String> dnParameters) throws InvalidDnParameterException {
    Set<String> unprocessedParameters = new HashSet<>(dnParameters.keySet());
    List<DnFieldValue> dnValues = new ArrayList<>();
    // match all dn fields with either default values or actual parameters
    for (DnFieldDescription description : profile.getSubjectFields()) {
        String fieldValue = null;
        boolean parameterIsMissing = StringUtils.isBlank(dnParameters.get(description.getId()));
        if (description.isRequired() && (!description.isReadOnly()) && parameterIsMissing) {
            throw new InvalidDnParameterException("missing parameter: " + description.getId());
        }
        if (description.isReadOnly() || parameterIsMissing) {
            fieldValue = description.getDefaultValue();
        } else {
            fieldValue = dnParameters.get(description.getId());
        }
        dnValues.add(new DnFieldValueImpl(description.getId(), fieldValue));
        unprocessedParameters.remove(description.getId());
    }
    if (!unprocessedParameters.isEmpty()) {
        throw new InvalidDnParameterException("extraneous parameters: " + unprocessedParameters);
    }
    // validate
    for (DnFieldValue dnValue : dnValues) {
        try {
            profile.validateSubjectField(dnValue);
        } catch (Exception e) {
            throw new InvalidDnParameterException(e);
        }
    }
    return dnValues;
}
Also used : DnFieldValue(ee.ria.xroad.common.certificateprofile.DnFieldValue) DnFieldDescription(ee.ria.xroad.common.certificateprofile.DnFieldDescription) DnFieldValueImpl(ee.ria.xroad.common.certificateprofile.impl.DnFieldValueImpl) ArrayList(java.util.ArrayList) ServiceException(org.niis.xroad.restapi.service.ServiceException) HashSet(java.util.HashSet)

Example 3 with DnFieldValueImpl

use of ee.ria.xroad.common.certificateprofile.impl.DnFieldValueImpl in project X-Road by nordic-institute.

the class DnFieldHelperTest method createSubjectName.

@Test
public void createSubjectName() {
    List<DnFieldValue> fieldValues = Arrays.asList(new DnFieldValueImpl("O", "foo"), new DnFieldValueImpl("CN", "bar"));
    assertEquals("O=foo, CN=bar", helper.createSubjectName(fieldValues));
}
Also used : DnFieldValue(ee.ria.xroad.common.certificateprofile.DnFieldValue) DnFieldValueImpl(ee.ria.xroad.common.certificateprofile.impl.DnFieldValueImpl) Test(org.junit.Test)

Aggregations

DnFieldValue (ee.ria.xroad.common.certificateprofile.DnFieldValue)3 DnFieldValueImpl (ee.ria.xroad.common.certificateprofile.impl.DnFieldValueImpl)3 DnFieldDescription (ee.ria.xroad.common.certificateprofile.DnFieldDescription)2 Test (org.junit.Test)2 DnFieldDescriptionImpl (ee.ria.xroad.common.certificateprofile.impl.DnFieldDescriptionImpl)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ServiceException (org.niis.xroad.restapi.service.ServiceException)1