CAS Unit Tests
Clover coverage report - CAS Unit Tests
Coverage timestamp: Mon Jul 4 2005 18:13:17 CDT
file stats: LOC: 130   Methods: 3
NCLOC: 88   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PopulateUserData.java 0% 0% 0% 0%
coverage
 1    /*
 2    * Portions of this file Copyright 1999-2005 University of Chicago
 3    * Portions of this file Copyright 1999-2005 The University of Southern California.
 4    *
 5    * This file or a portion of this file is licensed under the
 6    * terms of the Globus Toolkit Public License, found at
 7    * http://www.globus.org/toolkit/download/license.html.
 8    * If you redistribute this file, with or without
 9    * modifications, you must include this notice in the file.
 10    */
 11    package org.globus.cas.impl.bootstrap;
 12   
 13    import java.io.File;
 14    import java.io.FileInputStream;
 15   
 16    import java.util.Properties;
 17   
 18    import org.globus.cas.impl.CasConstants;
 19   
 20    import org.globus.cas.types.UserData;
 21    import org.globus.cas.types.PolicyData;
 22    import org.globus.cas.types.UserGroupData;
 23    import org.globus.cas.types.TrustAnchorData;
 24    import org.globus.cas.types.ArrayOfString;
 25   
 26    import org.globus.cas.impl.databaseAccess.UserDataHandler;
 27    import org.globus.cas.impl.databaseAccess.PolicyDataHandler;
 28    import org.globus.cas.impl.databaseAccess.ObjectDataHandler;
 29    import org.globus.cas.impl.databaseAccess.UserGroupDataHandler;
 30    import org.globus.cas.impl.databaseAccess.TrustAnchorDataHandler;
 31    import org.globus.cas.impl.databaseAccess.ServiceTypeActionHandler;
 32   
 33    // Code now expects that a trust anchor, user group and user are added
 34    // using this. Does not support scenario where only trust anchor or
 35    // user needs to be added.
 36    public class PopulateUserData {
 37   
 38  0 public static void populateDb(String bootstrapFilename) throws Exception {
 39   
 40  0 File bootstrapFile = new File(bootstrapFilename);
 41  0 if (!bootstrapFile.exists()) {
 42  0 throw new Exception("bootstrap file \"" + bootstrapFilename
 43    + "\" does not exist ");
 44    }
 45  0 Properties prop = new Properties();
 46  0 prop.load(new FileInputStream(bootstrapFilename));
 47   
 48  0 String taName = prop.getProperty("ta-name");
 49  0 String taAuthMethod = prop.getProperty("ta-authMethod");
 50  0 String taAuthData = prop.getProperty("ta-authData");
 51    // To store trust anchor data, none of this shld be null
 52  0 emptyStringPruning(taName, "Trust anchor name");
 53  0 emptyStringPruning(taAuthMethod, "Trust anchor authentication method");
 54  0 emptyStringPruning(taAuthData, "Trust anchor authentication data");
 55   
 56  0 String userName = prop.getProperty("user-name");
 57  0 String userSubject = prop.getProperty("user-subject");
 58  0 emptyStringPruning(userName, "User name");
 59  0 emptyStringPruning(userSubject, "User subject");
 60   
 61  0 String userGroup = prop.getProperty("userGroupname");
 62  0 emptyStringPruning(userGroup, "User group name");
 63   
 64  0 TrustAnchorData trustAnchor = new TrustAnchorData();
 65  0 trustAnchor.setNickname(taName.trim());
 66  0 trustAnchor.setAuthMethod(taAuthMethod.trim());
 67  0 trustAnchor.setAuthData(taAuthData.trim());
 68  0 TrustAnchorDataHandler.storeObject(trustAnchor);
 69   
 70  0 UserData user = new UserData();
 71  0 user.setNickname(userName.trim());
 72  0 user.setSubjectName(userSubject.trim());
 73  0 user.setTrustAnchorName(taName.trim());
 74  0 UserDataHandler.storeObject(user);
 75   
 76    // Add user group
 77  0 UserGroupData userGpData = new UserGroupData();
 78  0 userGpData.setGroupName(userGroup.trim());
 79  0 UserGroupDataHandler.storeObject(userGpData);
 80   
 81    // Add user to group
 82  0 UserGroupDataHandler.addGroupMember(userGroup.trim(),
 83    userName.trim());
 84   
 85    // Get CAS object id
 86  0 int objectId =
 87    ObjectDataHandler.getObjectId(CasConstants.OBJECT_SELF,
 88    CasConstants.NAMESPACE_SELF);
 89  0 if (objectId == -1) {
 90    // policy cannot be stored, so return
 91  0 throw new Exception("Implicit object initialization has not been "
 92    + " done can't find cas server object");
 93    }
 94   
 95    // Set super user permissions on this user
 96  0 int serviceActionId = ServiceTypeActionHandler
 97    .getServiceActionId(CasConstants.SERVICETYPE_CAS,
 98    CasConstants.ACTION_SU);
 99   
 100  0 if (serviceActionId == -1) {
 101    // policy cannot be stored, so return
 102  0 throw new Exception("Implicit object initialization has not been "
 103    + " done can't find cas server object");
 104    }
 105   
 106  0 PolicyData policyData = new PolicyData();
 107  0 policyData.setUserGroupName(userGroup.trim());
 108  0 policyData.setObjectSpec(Integer.toString(objectId));
 109  0 policyData.setObjectSpecDesc("object");
 110  0 policyData.setActionSpec(Integer.toString(serviceActionId));
 111  0 policyData.setActionSpecDesc("serviceAction");
 112  0 PolicyDataHandler.storeObject(policyData);
 113    }
 114   
 115  0 private static boolean stringInArray(String[] userNames, String userName) {
 116  0 for (int i=0; i<userNames.length; i++) {
 117  0 if (userNames[i].equals(userName))
 118  0 return true;
 119    }
 120  0 return false;
 121    }
 122   
 123  0 private static void emptyStringPruning(String str, String strName)
 124    throws Exception {
 125   
 126  0 if ((str == null) || (str.equals(""))) {
 127  0 throw new Exception(strName + " cannot be null or empty");
 128    }
 129    }
 130    }