PageRenderTime 34ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/security/src/test/java/org/jboss/as/security/test/SubsystemParsingUnitTestCase.java

#
Java | 115 lines | 73 code | 18 blank | 24 comment | 4 complexity | 817bf30be5b472c4cc4a3daddb997c72 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1/*
  2 * JBoss, Home of Professional Open Source.
  3 * Copyright 2010, Red Hat, Inc., and individual contributors
  4 * as indicated by the @author tags. See the copyright.txt file in the
  5 * distribution for a full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22package org.jboss.as.security.test;
 23
 24import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
 25import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUBSYSTEM;
 26import static org.jboss.as.security.Constants.SECURITY_DOMAIN;
 27import static org.junit.Assert.assertEquals;
 28import static org.junit.Assert.assertNotNull;
 29import static org.junit.Assert.fail;
 30
 31import java.io.BufferedReader;
 32import java.io.IOException;
 33import java.io.InputStreamReader;
 34import java.net.URL;
 35import java.util.ArrayList;
 36import java.util.List;
 37
 38import javax.xml.namespace.QName;
 39import javax.xml.stream.XMLInputFactory;
 40import javax.xml.stream.XMLStreamException;
 41
 42import org.jboss.as.security.SecuritySubsystemParser;
 43import org.jboss.dmr.ModelNode;
 44import org.jboss.dmr.Property;
 45import org.jboss.staxmapper.XMLMapper;
 46import org.junit.Assert;
 47import org.junit.Test;
 48
 49/**
 50 * @author anil saldhana
 51 */
 52public class SubsystemParsingUnitTestCase {
 53    private static final String namespace = "urn:jboss:domain:security:1.0";
 54    private static final SecuritySubsystemParser parser = SecuritySubsystemParser.getInstance();
 55
 56    @Test
 57    public void test() throws Exception {
 58
 59        List<ModelNode> operations = parse("subsystem.xml");
 60        assertNotNull(operations);
 61        ModelNode props = operations.get(0);
 62        assertNotNull(props);
 63
 64
 65        ModelNode node = operations.get(1);
 66        assertNotNull(node);
 67        ModelNode address = node.get(OP_ADDR);
 68        List<Property> properties = address.asPropertyList();
 69        assertEquals(2, properties.size());
 70        for (Property prop : properties) {
 71            String propName = prop.getName();
 72            if (!(propName.equals(SUBSYSTEM) || propName.equals(SECURITY_DOMAIN)))
 73                fail("either subsystem or security-domain expected");
 74            if (propName.equals(SECURITY_DOMAIN)) {
 75                ModelNode securityDomainValue = prop.getValue();
 76                String value = securityDomainValue.asString();
 77                assertEquals("other", value);
 78            }
 79        }
 80
 81        node = operations.get(2);
 82        assertNotNull(node);
 83        address = node.get(OP_ADDR);
 84        properties = address.asPropertyList();
 85        assertEquals(3, properties.size());
 86        assertEquals("authentication", properties.get(2).getName());
 87        assertEquals("classic", properties.get(2).getValue().asString());
 88
 89        List<ModelNode> loginNodes = node.get("login-modules").asList();
 90        assertEquals(1, loginNodes.size());
 91
 92        ModelNode modelNode = loginNodes.get(0);
 93        ModelNode code = modelNode.get("code");
 94        assertEquals("UsersRoles", code.asString());
 95        ModelNode flag = modelNode.get("flag");
 96        assertEquals("required", flag.asString());
 97    }
 98
 99    List<ModelNode> parse(final String name) throws XMLStreamException, IOException {
100        final List<ModelNode> operations = new ArrayList<ModelNode>();
101
102        XMLMapper mapper = XMLMapper.Factory.create();
103        mapper.registerRootElement(new QName(namespace, "subsystem"), parser);
104
105        URL configURL = getClass().getResource(name);
106        Assert.assertNotNull(name + " url is not null", configURL);
107        System.out.println("configURL = " + configURL);
108
109        BufferedReader reader = new BufferedReader(new InputStreamReader(configURL.openStream()));
110        mapper.parseDocument(operations, XMLInputFactory.newInstance().createXMLStreamReader(reader));
111
112        return operations;
113    }
114
115}