From 174ec2eeb4ae929aaa2ccea0e8541dad7c4c28f2 Mon Sep 17 00:00:00 2001 From: Youssef Chawki Date: Mon, 7 May 2018 20:08:31 +0200 Subject: Added UT auth-deforg, code cov 61% For those Unit tests, I added the library "mock-javamail" in order to test the "notify" function of DefaultOrg.java. Change-Id: I8894e2b6c264581be5d3c56b46600379f1d31f81 Issue-ID: AAF-239 Signed-off-by: Youssef Chawki --- .../src/main/java/org/onap/aaf/org/DefaultOrg.java | 82 ++++++++++++---------- .../java/org/onap/aaf/org/DefaultOrgIdentity.java | 11 +-- .../java/org/onap/aaf/org/DefaultOrgWarnings.java | 50 ++++++------- .../src/main/java/org/onap/aaf/org/Identities.java | 60 ++++++++-------- 4 files changed, 107 insertions(+), 96 deletions(-) (limited to 'auth/auth-deforg/src/main/java/org/onap') diff --git a/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrg.java b/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrg.java index ac2105f6..3d42b63c 100644 --- a/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrg.java +++ b/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrg.java @@ -7,9 +7,9 @@ * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at - * * + * * * * http://www.apache.org/licenses/LICENSE-2.0 - * * + * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -21,8 +21,7 @@ ******************************************************************************/ package org.onap.aaf.org; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.util.ArrayList; import java.util.Date; import java.util.GregorianCalendar; @@ -54,11 +53,13 @@ public class DefaultOrg implements Organization { final String domain; final String atDomain; final String realm; - + private final String NAME,mailHost,mailFrom; private final Set supportedRealms; + public DefaultOrg(Env env, String realm) throws OrganizationException { + this.realm = realm; supportedRealms=new HashSet(); supportedRealms.add(realm); @@ -74,7 +75,7 @@ public class DefaultOrg implements Organization { if(mailFrom==null) { throw new OrganizationException(s + PROPERTY_IS_REQUIRED); } - + System.getProperties().setProperty("mail.smtp.host",mailHost); System.getProperties().setProperty("mail.user", mailFrom); // Get the default Session object. @@ -90,6 +91,7 @@ public class DefaultOrg implements Organization { env.warn().log(defFile, " is not defined. Using default: ",temp+"/identities.dat"); File dir = new File(temp); fIdentities=new File(dir,"identities.dat"); + if(!fIdentities.exists()) { env.warn().log("No",fIdentities.getCanonicalPath(),"exists. Creating."); if(!dir.exists()) { @@ -107,7 +109,7 @@ public class DefaultOrg implements Organization { } } } - + if(fIdentities!=null && fIdentities.exists()) { identities = new Identities(fIdentities); } else { @@ -121,7 +123,7 @@ public class DefaultOrg implements Organization { throw new OrganizationException(e); } } - + // Implement your own Delegation System static final List NULL_DELEGATES = new ArrayList(); @@ -130,14 +132,14 @@ public class DefaultOrg implements Organization { private Session session; public enum Types {Employee, Contractor, Application, NotActive}; private final static Set typeSet; - + static { typeSet = new HashSet(); for(Types t : Types.values()) { typeSet.add(t.name()); } } - + private static final EmailWarnings emailWarnings = new DefaultOrgWarnings(); @Override @@ -158,10 +160,11 @@ public class DefaultOrg implements Organization { @Override public DefaultOrgIdentity getIdentity(AuthzTrans trans, String id) throws OrganizationException { int at = id.indexOf('@'); + String attt = at<0?id:id.substring(0, at); return new DefaultOrgIdentity(trans,at<0?id:id.substring(0, at),this); } - // Note: Return a null if found; return a String Message explaining why not found. + // Note: Return a null if found; return a String Message explaining why not found. @Override public String isValidID(final AuthzTrans trans, final String id) { try { @@ -185,18 +188,18 @@ public class DefaultOrg implements Organization { // if(!id.regionMatches(at+1, domain, 0, id.length()-at-1)) { // return false; // } - sid = id.substring(0,at); + sid = id.substring(0,at); } else { sid = id; } // We'll validate that it exists, rather than check patterns. - + return isValidID(trans, sid)==null; // Check Pattern (if checking existing is too long) // if(id.endsWith(SUFFIX) && ID_PATTERN.matcher(id).matches()) { // return true; // } - // return false; + // return false; } private static final String SPEC_CHARS = "!@#$%^*-+?/,:;."; @@ -327,6 +330,7 @@ public class DefaultOrg implements Organization { + "Please follow this link: \n\n\t" + url + "\n\n" + summary, urgent); } catch (Exception e) { + trans.error().log(e, "Failure to send Email"); return Response.ERR_NotificationFailure; } @@ -386,8 +390,9 @@ public class DefaultOrg implements Organization { @Override public int sendEmail(AuthzTrans trans, List toList, List ccList, String subject, String body, Boolean urgent) throws OrganizationException { + int status = 1; - + List to = new ArrayList(); for(String em : toList) { if(em.indexOf('@')<0) { @@ -396,11 +401,11 @@ public class DefaultOrg implements Organization { to.add(em); } } - + List cc = new ArrayList(); if(ccList!=null) { if(!ccList.isEmpty()) { - + for(String em : ccList) { if(em.indexOf('@')<0) { cc.add(new DefaultOrgIdentity(trans, em, this).email()); @@ -409,7 +414,7 @@ public class DefaultOrg implements Organization { } } } - + // for now, I want all emails so we can see what goes out. Remove later if (!ccList.contains(mailFrom)) { ccList.add(mailFrom); @@ -444,6 +449,7 @@ public class DefaultOrg implements Organization { // Now set the actual message message.setText(body); } else { + // override recipients message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(mailFrom)); @@ -480,11 +486,13 @@ public class DefaultOrg implements Organization { status = 0; } catch (MessagingException mex) { + System.out.println("Error messaging: "+ mex.getMessage()); + System.out.println("Error messaging: "+ mex.toString()); throw new OrganizationException("Exception send email message " + mex.getMessage()); } - return status; + return status; } /** @@ -507,8 +515,8 @@ public class DefaultOrg implements Organization { @Override public GregorianCalendar expiration(GregorianCalendar gc, Expiration exp, String... extra) { - GregorianCalendar now = new GregorianCalendar(); - GregorianCalendar rv = gc==null?now:(GregorianCalendar)gc.clone(); + GregorianCalendar now = new GregorianCalendar(); + GregorianCalendar rv = gc==null?now:(GregorianCalendar)gc.clone(); switch (exp) { case ExtendPassword: // Extending Password give 5 extra days, max 8 days from now @@ -573,7 +581,7 @@ public class DefaultOrg implements Organization { orgIdentitys.add(supervisor); } } - return orgIdentitys; + return orgIdentitys; } @Override @@ -590,7 +598,7 @@ public class DefaultOrg implements Organization { @Override public boolean canHaveMultipleCreds(String id) { // External entities are likely mono-password... if you change it, it is a global change. - // This is great for people, but horrible for Applications. + // This is great for people, but horrible for Applications. // // AAF's Password can have multiple Passwords, each with their own Expiration Date. // For Default Org, we'll assume true for all, but when you add your external @@ -621,13 +629,13 @@ public class DefaultOrg implements Organization { } } return null; - + case CREATE_MECHID_BY_PERM_ONLY: return getName() + " only allows sponsors to create MechIDs"; - + default: return policy.name() + " is unsupported at " + getName(); - } + } } @Override @@ -650,9 +658,9 @@ public class DefaultOrg implements Organization { return this.getAddresses(strAddress,";"); } /** - * Convert the delimiter String into Internet addresses with the + * Convert the delimiter String into Internet addresses with the * delimiter of provided - * @param strAddress + * @param strAddresses * @param delimiter * @return */ @@ -661,14 +669,14 @@ public class DefaultOrg implements Organization { int count = 0; for (String addr : strAddresses) { - try{ - addressArray[count] = new InternetAddress(addr); - count++; - }catch(Exception e){ - throw new OrganizationException("Failed to parse the email address "+ addr +": "+e.getMessage()); - } - } - return addressArray; + try{ + addressArray[count] = new InternetAddress(addr); + count++; + }catch(Exception e){ + throw new OrganizationException("Failed to parse the email address "+ addr +": "+e.getMessage()); + } + } + return addressArray; } private String extractRealm(final String r) { @@ -697,5 +705,5 @@ public class DefaultOrg implements Organization { public synchronized void addSupportedRealm(final String r) { supportedRealms.add(extractRealm(r)); } - + } diff --git a/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrgIdentity.java b/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrgIdentity.java index f2fe686f..7aa57fd7 100644 --- a/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrgIdentity.java +++ b/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrgIdentity.java @@ -43,10 +43,10 @@ public class DefaultOrgIdentity implements Identity { private static final String CONTRACTOR = "c"; private static final String EMPLOYEE = "e"; private static final String APPLICATION = "a"; - private static final String NON_ACTIVE = "n"; + private static final String NON_ACTIVE = "n"; private final static int TIMEOUT = Integer.parseInt(Config.AAF_CONN_TIMEOUT_DEF); - + private DefaultOrg org; //package on purpose Data identity; @@ -60,7 +60,7 @@ public class DefaultOrgIdentity implements Identity { org.identities.open(trans, TIMEOUT); try { Reuse r = org.identities.reuse(); - int at = key.indexOf(dorg.atDomain); + int at = key.indexOf(dorg.getDomain()); String search; if(at>=0) { search = key.substring(0,at); @@ -68,6 +68,9 @@ public class DefaultOrgIdentity implements Identity { search = key; } identity = org.identities.find(search, r); + + + if(identity==null) { identity = Identities.NO_DATA; } @@ -78,7 +81,7 @@ public class DefaultOrgIdentity implements Identity { throw new OrganizationException(e); } } - + @Override public boolean equals(Object b) { if(b instanceof DefaultOrgIdentity) { diff --git a/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrgWarnings.java b/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrgWarnings.java index c04707ee..97d0cef2 100644 --- a/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrgWarnings.java +++ b/auth/auth-deforg/src/main/java/org/onap/aaf/org/DefaultOrgWarnings.java @@ -26,38 +26,38 @@ import org.onap.aaf.auth.org.EmailWarnings; public class DefaultOrgWarnings implements EmailWarnings { @Override - public long credEmailInterval() - { - return 604800000L; // 7 days in millis 1000 * 86400 * 7 - } - + public long credEmailInterval() + { + return 604800000L; // 7 days in millis 1000 * 86400 * 7 + } + @Override - public long roleEmailInterval() - { - return 604800000L; // 7 days in millis 1000 * 86400 * 7 - } - + public long roleEmailInterval() + { + return 604800000L; // 7 days in millis 1000 * 86400 * 7 + } + @Override public long apprEmailInterval() { - return 259200000L; // 3 days in millis 1000 * 86400 * 3 + return 259200000L; // 3 days in millis 1000 * 86400 * 3 } - + @Override - public long credExpirationWarning() - { - return( 2592000000L ); // One month, in milliseconds 1000 * 86400 * 30 in milliseconds - } - + public long credExpirationWarning() + { + return( 2592000000L ); // One month, in milliseconds 1000 * 86400 * 30 in milliseconds + } + @Override - public long roleExpirationWarning() - { - return( 2592000000L ); // One month, in milliseconds 1000 * 86400 * 30 in milliseconds - } + public long roleExpirationWarning() + { + return( 2592000000L ); // One month, in milliseconds 1000 * 86400 * 30 in milliseconds + } @Override - public long emailUrgentWarning() - { - return( 1209600000L ); // Two weeks, in milliseconds 1000 * 86400 * 14 in milliseconds - } + public long emailUrgentWarning() + { + return( 1209600000L ); // Two weeks, in milliseconds 1000 * 86400 * 14 in milliseconds + } } diff --git a/auth/auth-deforg/src/main/java/org/onap/aaf/org/Identities.java b/auth/auth-deforg/src/main/java/org/onap/aaf/org/Identities.java index f3067fe6..344d0552 100644 --- a/auth/auth-deforg/src/main/java/org/onap/aaf/org/Identities.java +++ b/auth/auth-deforg/src/main/java/org/onap/aaf/org/Identities.java @@ -55,8 +55,8 @@ import org.onap.aaf.auth.local.DataFile.Token.Field; */ public class Identities extends AbsData { public final static Data NO_DATA = new Data(); - - public Identities(File users) { + + public Identities(File users) throws IOException { super(users,'|',512,0); } @@ -81,7 +81,7 @@ public class Identities extends AbsData { public final String email; public final String status; public final String responsibleTo; - + private Data(Field f) { f.reset(); id=f.next(); @@ -93,10 +93,10 @@ public class Identities extends AbsData { status=f.next(); responsibleTo =f.next(); } - + private Data() { id = name = fname = lname = - phone = email = status = responsibleTo + phone = email = status = responsibleTo = ""; } @@ -110,34 +110,34 @@ public class Identities extends AbsData { status + '|' + responsibleTo; } - + // Here, make up your own Methods which help you easily determine your Organization's structure // in your Organization Object - public boolean hasStatus(String possible) { - return possible.contains(status); - } + public boolean hasStatus(String possible) { + return possible.contains(status); + } + + public boolean isEmployee() { + return "e".equals(status); + } - public boolean isEmployee() { - return "e".equals(status); - } - - public boolean isContractor() { - return "c".equals(status); - } - - public boolean isApplication() { - return "a".equals(status); - } + public boolean isContractor() { + return "c".equals(status); + } + + public boolean isApplication() { + return "a".equals(status); + } } - - public Data find(Object key,Reuse r) throws IOException { - r.reset(); - // These are new, to allow for Thread Safety - int rec = ti.find(key,r,0); - if(rec<0) { - return null; - } - r.pos(rec); + + public Data find(Object key,Reuse r) throws IOException { + r.reset(); + // These are new, to allow for Thread Safety + int rec = ti.find(key,r,0); + if(rec<0) { + return null; + } + r.pos(rec); return new Data(r.getFieldData()); - } + } } -- cgit 1.2.3-korg