diff options
29 files changed, 3154 insertions, 322 deletions
diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/actions/EmailPrint.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/actions/EmailPrint.java index 3ab481f5..2ae86994 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/actions/EmailPrint.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/actions/EmailPrint.java @@ -3,6 +3,8 @@ * org.onap.aaf * =========================================================================== * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright © 2018 IBM. * =========================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,7 +46,9 @@ public class EmailPrint extends Email { boolean first = true; out.print("To: "); for (String s: toList) { - if (first) {first = false;} + if (first) { + first = false; + } else {out.print(',');} out.print(s); } @@ -53,7 +57,9 @@ public class EmailPrint extends Email { first = true; out.print("CC: "); for (String s: ccList) { - if (first) {first = false;} + if (first) { + first = false; + } else {out.print(',');} out.print(s); } diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/ExpireRange.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/ExpireRange.java index a985640b..c459dc66 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/ExpireRange.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/ExpireRange.java @@ -3,6 +3,8 @@ * org.onap.aaf * =========================================================================== * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright © 2018 IBM. * =========================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,20 +32,19 @@ import java.util.Map; import java.util.Set; import org.onap.aaf.cadi.Access; -import org.onap.aaf.cadi.PropAccess; public class ExpireRange { private static final String AAF_BATCH_RANGE = "aaf_batch_range."; public Map<String,List<Range>> ranges; public final Date now; + public String rangeOneMonth = "OneMonth"; public ExpireRange(final Access access) { now = new Date(); ranges = new HashMap<>(); int i=0; String prop = access.getProperty(AAF_BATCH_RANGE + i,null); - if(prop==null) { - if(i==0) { + if(prop==null && i==0) { List<Range> lcred = getRangeList("cred"); List<Range> lur = getRangeList("ur"); List<Range> lx509 = getRangeList("x509"); @@ -55,14 +56,13 @@ public class ExpireRange { lcred.add(new Range("CredOneWeek",3,1,0,0,GregorianCalendar.WEEK_OF_MONTH,1)); lcred.add(new Range("CredTwoWeek",2,1,GregorianCalendar.WEEK_OF_MONTH,1,GregorianCalendar.WEEK_OF_MONTH,2)); - lcred.add(new Range("OneMonth",1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1)); + lcred.add(new Range(rangeOneMonth,1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1)); lcred.add(new Range("TwoMonth",1,0,GregorianCalendar.MONTH,1,GregorianCalendar.MONTH,2)); - lur.add(new Range("OneMonth",1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1)); + lur.add(new Range(rangeOneMonth,1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1)); - lx509.add(new Range("OneMonth",1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1)); + lx509.add(new Range(rangeOneMonth,1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1)); } - } } public Set<String> names() { diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/MiscID.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/MiscID.java index 45875a20..e30336fb 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/MiscID.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/MiscID.java @@ -4,6 +4,8 @@ * =========================================================================== * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. * =========================================================================== + * Modifications Copyright (C) 2018 IBM. + * =========================================================================== * 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 @@ -47,9 +49,12 @@ public class MiscID { CREATE_DATE - Date when MiscID was created LAST_RENEWAL_DATE - Date when MiscID Sponsorship was last renewed */ - public String id,sponsor,created,renewal; + public String id; + public String sponsor; + public String created; + public String renewal; - private static final String fieldString = "id,created,sponsor,renewal"; + private static final String FIELD_STRING = "id,created,sponsor,renewal"; /** * Load a Row of Strings (from CSV file). @@ -63,8 +68,10 @@ public class MiscID { * @throws IllegalAccessException * @throws IllegalArgumentException */ - public void set(String row []) throws BatchException { - if (row.length<4) {throw new BatchException("Row of MiscID_XRef is too short");} + public void set(String[] row ) throws BatchException { + if (row.length<4) { + throw new BatchException("Row of MiscID_XRef is too short"); + } id = row[0]; sponsor = row[1]; created = row[2]; @@ -80,15 +87,15 @@ public class MiscID { public static void load(Trans trans, Session session ) { - load(trans, session,"SELECT " + fieldString + " FROM authz.miscid;",data); + load(trans, session,"SELECT " + FIELD_STRING + " FROM authz.miscid;",data); } public static void load(Trans trans, Session session, Map<String,MiscID> map ) { - load(trans, session,"SELECT " + fieldString + " FROM authz.miscid;",map); + load(trans, session,"SELECT " + FIELD_STRING + " FROM authz.miscid;",map); } public static void loadOne(Trans trans, Session session, String id ) { - load(trans, session,"SELECT " + fieldString + " FROM authz.miscid WHERE id ='" + id + "';", data); + load(trans, session,"SELECT " + FIELD_STRING + " FROM authz.miscid WHERE id ='" + id + "';", data); } public static void load(Trans trans, Session session, String query, Map<String,MiscID> map) { @@ -141,7 +148,7 @@ public class MiscID { public StringBuilder insertStmt() throws IllegalArgumentException, IllegalAccessException { StringBuilder sb = new StringBuilder("INSERT INTO authz.miscid ("); - sb.append(fieldString); + sb.append(FIELD_STRING); sb.append(") VALUES ('"); sb.append(id); sb.append("','"); diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Perm.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Perm.java index 21bd71ee..acf54274 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Perm.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Perm.java @@ -3,6 +3,8 @@ * org.onap.aaf * =========================================================================== * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright © 2018 IBM. * =========================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,9 +44,24 @@ public class Perm implements Comparable<Perm> { public static final TreeMap<String,Perm> keys = new TreeMap<>(); private static List<Perm> deletePerms = new ArrayList<>(); - public final String ns, type, instance, action,description; - private String fullType = null, fullPerm = null, encode = null; + public final String ns; + public final String type; + public final String instance; + public final String action; + public final String description; + private String fullType = null; + private String fullPerm = null; + private String encode = null; public final Set<String> roles; + + public Perm(String ns, String type, String instance, String action, String description, Set<String> roles) { + this.ns = ns; + this.type = type; + this.instance = instance; + this.action = action; + this.description = description; + this.roles = roles; + } public String encode() { if (encode == null) { @@ -66,17 +83,6 @@ public class Perm implements Comparable<Perm> { } return fullPerm; } - - public Perm(String ns, String type, String instance, String action, String description, Set<String> roles) { - this.ns = ns; - this.type = type; - this.instance = instance; - this.action = action; - this.description = description; - // 2.0.11 -// this.full = encode();//ns+'.'+type+'|'+instance+'|'+action; - this.roles = roles; - } public static void load(Trans trans, Session session) { load(trans, session, "select ns, type, instance, action, description, roles from authz.perm;"); diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/X509.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/X509.java index 0ffaa8f5..3cbf90fa 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/X509.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/X509.java @@ -3,6 +3,8 @@ * org.onap.aaf * =========================================================================== * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright © 2018 IBM. * =========================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +23,6 @@ package org.onap.aaf.auth.batch.helpers; -import java.io.IOException; import java.nio.ByteBuffer; import java.security.cert.X509Certificate; import java.util.Iterator; @@ -42,7 +43,10 @@ import com.datastax.driver.core.SimpleStatement; import com.datastax.driver.core.Statement; public class X509 { - public final String ca,id,x500,x509; + public final String ca; + public final String id; + public final String x500; + public final String x509; public ByteBuffer serial; public X509(String ca, String id, String x500, String x509, ByteBuffer serial) { @@ -104,12 +108,12 @@ public class X509 { } - public void row(CSV.Writer cw, X509Certificate x509Cert) throws IOException { + public void row(CSV.Writer cw, X509Certificate x509Cert) { cw.row("x509",ca,Hash.toHex(serial.array()),Chrono.dateOnlyStamp(x509Cert.getNotAfter()),x500); } - public static void row(StringBuilder sb, List<String> row) throws IOException { + public static void row(StringBuilder sb, List<String> row) { sb.append("DELETE from authz.x509 WHERE ca='"); sb.append(row.get(1)); sb.append("' AND serial="); diff --git a/auth/auth-cass/src/main/java/org/onap/aaf/auth/dao/hl/Question.java b/auth/auth-cass/src/main/java/org/onap/aaf/auth/dao/hl/Question.java index 4a307693..a513da0a 100644 --- a/auth/auth-cass/src/main/java/org/onap/aaf/auth/dao/hl/Question.java +++ b/auth/auth-cass/src/main/java/org/onap/aaf/auth/dao/hl/Question.java @@ -132,9 +132,9 @@ public class Question { public final HistoryDAO historyDAO; public final CachedNSDAO nsDAO; - public final CachedRoleDAO roleDAO; + public CachedRoleDAO roleDAO; public final CachedPermDAO permDAO; - public final CachedUserRoleDAO userRoleDAO; + public CachedUserRoleDAO userRoleDAO; public final CachedCredDAO credDAO; public final CachedCertDAO certDAO; public final DelegateDAO delegateDAO; @@ -145,6 +145,7 @@ public class Question { public Question(AuthzTrans trans, Cluster cluster, String keyspace, boolean startClean) throws APIException, IOException { PERMS = trans.slot("USER_PERMS"); + System.out.println(trans.init()); trans.init().log("Instantiating DAOs"); long expiresIn = Long.parseLong(trans.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF)); historyDAO = new HistoryDAO(trans, cluster, keyspace); diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedPermDAOTest.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedPermDAOTest.java new file mode 100644 index 00000000..66c32a7b --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedPermDAOTest.java @@ -0,0 +1,166 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.dao.cached; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.dao.CIDAO; +import org.onap.aaf.auth.dao.cass.PermDAO; +import org.onap.aaf.auth.dao.cass.PermDAO.Data; +import org.onap.aaf.auth.dao.cass.RoleDAO; +import org.onap.aaf.auth.dao.cass.Status; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.env.AuthzTransImpl; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.misc.env.LogTarget; + +public class JU_CachedPermDAOTest { + + @Mock + private CIDAO<AuthzTrans> info; + @Mock + private PermDAO dao; + + @Mock + RoleDAO.Data role; + + @Mock + private PermDAO.Data perm; + + @Mock + private AuthzTrans trans; + @Mock + private Result<List<PermDAO.Data>> value; + + @Before + public void setUp() throws Exception { + initMocks(this); + + when(dao.readNS(trans, "ns")).thenReturn(value); + when(trans.debug()).thenReturn(new LogTarget() { + + @Override + public void printf(String fmt, Object... vars) {} + + @Override + public void log(Throwable e, Object... msgs) { + e.getMessage(); + e.printStackTrace(); + msgs.toString(); + + } + + @Override + public void log(Object... msgs) { + } + + @Override + public boolean isLoggable() { + + return true; + } + }); + } + + @Test + public void testReadNS() { + when(value.isOKhasData()).thenReturn(true); + when(value.isOK()).thenReturn(false); + CachedPermDAO ccDao = new CachedPermDAO(dao, info, 100l); + + Result<List<Data>> result = ccDao.readNS(trans, "ns"); + + assertEquals(result, value); + + when(value.isOKhasData()).thenReturn(false); + + result = ccDao.readNS(trans, "ns"); + + assertEquals(result.status, Status.ERR_PermissionNotFound); + + ccDao.readChildren(trans, "ns", "type"); + + verify(dao).readChildren(trans, "ns", "type"); + } + + @Test + public void testReadByTypeSuccess() { + CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"test4",new String[0]); + Mockito.doReturn(retVal1).when(dao).readByType(trans, "test4",""); + Result<List<Data>> retVal = roleDaoObj.readByType(trans, "test4",""); +// System.out.println(retVal.status); + //retVal.status = 0; + assertEquals("1", Integer.toString(retVal.status)); + } + + @Test + public void testReadByTypeFailure() { + CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test3123",new String[0]); + Mockito.doReturn(retVal1).when(dao).readByType(trans, "test3",""); + Result<List<Data>> retVal = roleDaoObj.readByType(trans, "test3",""); + //System.out.println(retVal.status); + assertEquals("23", Integer.toString(retVal.status)); + } + + @Test + public void testAddRole() { + CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10); + Result<Void> retVal1 = new Result<Void>(null,0,"testAddRole",new String[0]); + Mockito.doReturn(retVal1).when(info).touch(trans, null,null); + Mockito.doReturn(retVal1).when(dao).addRole(trans, perm,null); + Result<Void> retVal = roleDaoObj.addRole(trans, perm, role); +// System.out.println("ret value is::"+retVal); + assertEquals("testAddRole", retVal.toString()); + } + + @Test + public void testDelRole() { + CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10); + Result<Void> retVal1 = new Result<Void>(null,0,"testAddRole",new String[0]); + Mockito.doReturn(retVal1).when(info).touch(trans, null,null); + Mockito.doReturn(retVal1).when(dao).delRole(trans, perm,null); + Result<Void> retVal = roleDaoObj.delRole(trans, perm, role); +// System.out.println(retVal); + assertEquals("testAddRole", retVal.toString()); + } + + @Test + public void testAddDescription() { + CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result<Void> retVal1 = new Result<Void>(null,0,"test1",new String[0]); + Mockito.doReturn(retVal1).when(dao).addDescription(trans, "","","","",""); + Result<Void> retVal = roleDaoObj.addDescription(trans, "", "","","",""); + //System.out.println(retVal.status); + assertEquals("0", Integer.toString(retVal.status)); + } + +} diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedRoleDAO.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedRoleDAO.java deleted file mode 100644 index 2e27bfd9..00000000 --- a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedRoleDAO.java +++ /dev/null @@ -1,174 +0,0 @@ -/** - * ============LICENSE_START==================================================== - * org.onap.aaf - * =========================================================================== - * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. - * =========================================================================== - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END==================================================== - * - */ -package org.onap.aaf.auth.dao.cached; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.when; -import static org.mockito.MockitoAnnotations.initMocks; - -import java.util.List; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; -import org.onap.aaf.auth.dao.CIDAO; -import org.onap.aaf.auth.dao.cass.PermDAO; -import org.onap.aaf.auth.dao.cass.RoleDAO; -import org.onap.aaf.auth.dao.cass.RoleDAO.Data; -import org.onap.aaf.auth.env.AuthzTrans; -import org.onap.aaf.auth.env.AuthzTransImpl; -import org.onap.aaf.auth.layer.Result; -import org.onap.aaf.misc.env.LogTarget; - - -@RunWith(MockitoJUnitRunner.class) -public class JU_CachedRoleDAO { - - @Mock - RoleDAO dao; - - @Mock - CIDAO<AuthzTrans> info; - - @Mock - AuthzTransImpl trans; - - @Mock - RoleDAO.Data data; - - @Mock - PermDAO.Data permData; - - @Before - public void setUp() throws Exception { - initMocks(this); - when(trans.debug()).thenReturn(new LogTarget() { - - @Override - public void printf(String fmt, Object... vars) {} - - @Override - public void log(Throwable e, Object... msgs) { - e.getMessage(); - e.printStackTrace(); - msgs.toString(); - - } - - @Override - public void log(Object... msgs) { - } - - @Override - public boolean isLoggable() { - - return true; - } - }); - } - - @Test - public void testReadNameSuccess() { - CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// - Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"test4",new String[0]); - Mockito.doReturn(retVal1).when(dao).readName(trans, "test4"); -// Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1); - Result<List<Data>> retVal = roleDaoObj.readName(trans, "test4"); -// System.out.println(retVal.status); - //retVal.status = 0; - assertEquals("1", Integer.toString(retVal.status)); - } - - @Test - public void testReadNameFailure() { - CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// - Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test3123",new String[0]); - Mockito.doReturn(retVal1).when(dao).readName(trans, "test3"); -// Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1); - Result<List<Data>> retVal = roleDaoObj.readName(trans, "test3"); -// System.out.println(retVal.status); - assertEquals("22", Integer.toString(retVal.status)); - } - @Test - public void testReadNSSuccess() { - CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// - Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"test",new String[0]); - Mockito.doReturn(retVal1).when(dao).readNS(trans, ""); -// Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1); - Result<List<Data>> retVal = roleDaoObj.readNS(trans, ""); -// System.out.println(retVal.status); - assertEquals("1", Integer.toString(retVal.status)); - } - @Test - public void testReadNSFailure() { - CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// - Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test1",new String[0]); - Mockito.doReturn(retVal1).when(dao).readNS(trans, ""); -// Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1); - Result<List<Data>> retVal = roleDaoObj.readNS(trans, ""); -// System.out.println(retVal.status); - assertEquals("22", Integer.toString(retVal.status)); - } - - @Test - public void testReadChildren() { - CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// - Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test1",new String[0]); - Mockito.doReturn(retVal1).when(dao).readChildren(trans, "",""); - Result<List<Data>> retVal = roleDaoObj.readChildren(trans, "", ""); - //System.out.println(retVal.status); - assertEquals("0", Integer.toString(retVal.status)); - } - - @Test - public void testAddPerm() { - CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10); - Result<Void> retVal1 = new Result<Void>(null,0,"testAddPerm",new String[0]); - Mockito.doReturn(retVal1).when(info).touch(trans, null,null); - Mockito.doReturn(retVal1).when(dao).addPerm(trans, data,permData); - Result<Void> retVal = roleDaoObj.addPerm(trans, data, permData); - assertEquals("testAddPerm", retVal.toString()); - } - - @Test - public void testDelPerm() { - CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10); - Result<Void> retVal1 = new Result<Void>(null,0,"testAddPerm",new String[0]); - Mockito.doReturn(retVal1).when(info).touch(trans, null,null); - Mockito.doReturn(retVal1).when(dao).delPerm(trans, data,permData); - Result<Void> retVal = roleDaoObj.delPerm(trans, data, permData); - System.out.println(retVal); - assertEquals("testAddPerm", retVal.toString()); - } - - @Test - public void testAddDescription() { - CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// - Result<Void> retVal1 = new Result<Void>(null,0,"test1",new String[0]); - Mockito.doReturn(retVal1).when(dao).addDescription(trans, "","",""); - Result<Void> retVal = roleDaoObj.addDescription(trans, "", "",""); - //System.out.println(retVal.status); - assertEquals("0", Integer.toString(retVal.status)); - } -}
\ No newline at end of file diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedUserRoleDAO.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedUserRoleDAO.java new file mode 100644 index 00000000..8dcf8e44 --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedUserRoleDAO.java @@ -0,0 +1,180 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.dao.cached; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.dao.CIDAO; +import org.onap.aaf.auth.dao.cass.PermDAO; +import org.onap.aaf.auth.dao.cass.RoleDAO; +import org.onap.aaf.auth.dao.cass.UserRoleDAO; +import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data; +import org.onap.aaf.auth.env.AuthzEnv; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.env.AuthzTransImpl; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.principal.TaggedPrincipal; +import org.onap.aaf.misc.env.LogTarget; +import org.onap.aaf.misc.env.Slot; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class JU_CachedUserRoleDAO { + + @Mock + UserRoleDAO dao; + + @Mock + CIDAO<AuthzTrans> info; + + @Mock + AuthzTransImpl trans; + + @Mock + RoleDAO.Data data; + + @Mock + PermDAO.Data permData; + + @Before + public void setUp() throws Exception { + initMocks(this); + when(trans.debug()).thenReturn(new LogTarget() { + + @Override + public void printf(String fmt, Object... vars) {} + + @Override + public void log(Throwable e, Object... msgs) { + e.getMessage(); + e.printStackTrace(); + msgs.toString(); + + } + + @Override + public void log(Object... msgs) { + } + + @Override + public boolean isLoggable() { + + return true; + } + }); + } + + private class TaggedPrincipalStub extends TaggedPrincipal { + String name="TaggedPrincipalStub"; + public TaggedPrincipalStub() { super(); } + public TaggedPrincipalStub(final TagLookup tl) { super(tl); } + @Override public String getName() { return name; } + @Override public String tag() { return null; } + } + + @Test + public void testReadName() { + CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10L); + Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test4",new String[0]); + Mockito.doReturn(retVal1).when(dao).readByUser(trans, "test4"); +// Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1); + Result<List<Data>> retVal = roleDaoObj.readByUser(trans, "test4"); + //System.out.println(retVal.status); + //retVal.status = 0; + assertEquals("25", Integer.toString(retVal.status)); + } + + @Test + public void testReadNameUser() { + CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10L); + Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"TaggedPrincipalStub",new String[0]); + AuthzEnv env = Mockito.mock(AuthzEnv.class); + AuthzTransImpl transTemp = new AuthzTransImpl(env) { + @Override + public<T> T get(Slot slot, T deflt) { + Object o=null; + return (T)o; + } + + }; + transTemp.setUser(new TaggedPrincipalStub()); + Mockito.doReturn(retVal1).when(info).touch(trans, null,null); + Mockito.doReturn(retVal1).when(dao).readByUser(transTemp, "TaggedPrincipalStub"); + roleDaoObj.invalidate("TaggedPrincipalStub"); + Result<List<Data>> retVal = roleDaoObj.readByUser(transTemp, "TaggedPrincipalStub"); +// System.out.println(retVal.status); + assertEquals("1", Integer.toString(retVal.status)); + } + + @Test + public void testReadByRoleSuccess() { + CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 0);//Mockito.mock(CachedRoleDAO.class);// + Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal1).when(dao).readByRole(trans, ""); + roleDaoObj.invalidate(""); + Result<List<Data>> retVal = roleDaoObj.readByRole(trans, ""); + //System.out.println(retVal.status); + assertEquals("1", Integer.toString(retVal.status)); + } + @Test + public void testReadByRoleFailure() { + CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 0);//Mockito.mock(CachedRoleDAO.class);// + Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test1",new String[0]); + Mockito.doReturn(retVal1).when(dao).readByRole(trans, ""); + roleDaoObj.invalidate(""); + Result<List<Data>> retVal = roleDaoObj.readByRole(trans, ""); + //System.out.println(retVal.status); + assertEquals("25", Integer.toString(retVal.status)); + } + + @Test + public void testReadUserInRole() { + CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"TaggedPrincipalStub",new String[0]); + AuthzEnv env = Mockito.mock(AuthzEnv.class); + AuthzTransImpl transTemp = new AuthzTransImpl(env) { + @Override + public<T> T get(Slot slot, T deflt) { + Object o=null; + return (T)o; + } + + }; + transTemp.setUser(new TaggedPrincipalStub()); + Mockito.doReturn(retVal1).when(info).touch(trans, null,null); + Mockito.doReturn(retVal1).when(dao).readByUserRole(transTemp, "",""); + Mockito.doReturn(retVal1).when(dao).readByUser(transTemp, "TaggedPrincipalStub"); + Result<List<Data>> retVal = roleDaoObj.readUserInRole(transTemp, "TaggedPrincipalStub",""); + //System.out.println(retVal.status); + assertEquals("25", Integer.toString(retVal.status)); + } + + +}
\ No newline at end of file diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CassExecutor.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CassExecutor.java new file mode 100644 index 00000000..5dd33c7e --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CassExecutor.java @@ -0,0 +1,154 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.dao.hl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; +import org.onap.aaf.auth.common.Define; +import org.onap.aaf.auth.dao.cass.NsSplit; +import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data; +import org.onap.aaf.auth.env.AuthzTransImpl; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.CadiException; + + +@RunWith(MockitoJUnitRunner.class) +public class JU_CassExecutor { + + + + @Mock + AuthzTransImpl trans; + + @Mock + Question q; + + @Mock + Access access; + + Function f; + + @Before + public void setUp() throws Exception { + initMocks(this); + try { + Mockito.doReturn("0.0").when(access).getProperty("aaf_root_ns","org.osaaf.aaf"); + Mockito.doReturn(new Properties()).when(access).getProperties(); + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + f =new Function(trans, q); + } + + @Test + public void testHasPermission() { + + CassExecutor cassExecutorObj =new CassExecutor(trans, f); + Mockito.doReturn(false).when(q).isGranted(trans, "","","","",""); + boolean retVal = cassExecutorObj.hasPermission("", "", "", "", ""); +// System.out.println(retVal); + assertFalse(retVal); + } + + @Test + public void testInRole() { + + CassExecutor cassExecutorObj =new CassExecutor(trans, f); + Result<NsSplit> retVal1 = new Result<NsSplit>(null,1,"",new String[0]); + Mockito.doReturn(retVal1).when(q).deriveNsSplit(trans, "test"); + + boolean retVal = cassExecutorObj.inRole("test"); +// System.out.println(retVal); + assertFalse(retVal); + } + + @Test + public void testNamespace() { + f =new Function(trans, q); + CassExecutor cassExecutorObj =new CassExecutor(trans, f); + Result<Data> retVal1 = new Result<Data>(null,1,"",new String[0]); + Mockito.doReturn(retVal1).when(q).validNSOfDomain(trans, null); + + String retVal=""; + try { + retVal = cassExecutorObj.namespace(); + } catch (Exception e) { + System.out.println(e.getMessage()); + assertEquals("33", e.getMessage()); + } + System.out.println(retVal); +// assertFalse(retVal); + } + + @Test + public void testId() { + Mockito.doReturn("").when(trans).user(); + CassExecutor cassExecutorObj =new CassExecutor(trans, f); + String retVal = cassExecutorObj.id(); + assertEquals("", retVal); + } + + @Test + public void testNamespaceSuccess() { + Mockito.doAnswer(new Answer() { + private int count = 0; + + public Object answer(InvocationOnMock invocation) { + if (count++ == 1) + return "test@test.com"; + + return null; + } + }).when(trans).user(); + f =new Function(trans, q); + CassExecutor cassExecutorObj =new CassExecutor(trans, f); + Result<Data> retVal1 = new Result<Data>(null,0,"",new String[0]); + Mockito.doReturn(retVal1).when(q).validNSOfDomain(trans, null); + + + String retVal=""; + try { + retVal = cassExecutorObj.namespace(); + } catch (Exception e) { + e.printStackTrace(); + System.out.println(e.getMessage()); +// assertNull( e.getMessage()); + } +// System.out.println(retVal); +// assertFalse(retVal); + } + +}
\ No newline at end of file diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_PermLookup.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_PermLookup.java new file mode 100644 index 00000000..e63d4b52 --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_PermLookup.java @@ -0,0 +1,383 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.dao.hl; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; +import java.util.Set; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.aaf.auth.common.Define; +import org.onap.aaf.auth.dao.cached.CachedRoleDAO; +import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO; +import org.onap.aaf.auth.dao.cass.PermDAO.Data; +import org.onap.aaf.auth.dao.cass.RoleDAO; +import org.onap.aaf.auth.dao.cass.UserRoleDAO; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.misc.env.LogTarget; + + +@RunWith(MockitoJUnitRunner.class) +public class JU_PermLookup { + + + + @Mock + AuthzTrans trans; + + @Mock + Question q; + + @Mock + Access access; + + Function f; + + @Before + public void setUp() throws Exception { + initMocks(this); + try { + Mockito.doReturn("0.0").when(access).getProperty("aaf_root_ns","org.osaaf.aaf"); + Mockito.doReturn(new Properties()).when(access).getProperties(); + Define.set(access); + + when(trans.error()).thenReturn(new LogTarget() { + + @Override + public void printf(String fmt, Object... vars) {} + + @Override + public void log(Throwable e, Object... msgs) { + e.getMessage(); + e.printStackTrace(); + msgs.toString(); + + } + + @Override + public void log(Object... msgs) { + } + + @Override + public boolean isLoggable() { + + return true; + } + }); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + f =new Function(trans, q); + } + + + @Test + public void testPerm() { + + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + +// System.out.println(cassExecutorObj); +// assertFalse(retVal); + } + + @Test + public void testGetUserRole() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result<List<UserRoleDAO.Data>> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList<UserRoleDAO.Data>(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + dataObj.expires = new Date(); + + retVal1.value.add(dataObj); + Mockito.doReturn(true).when(retVal1).isOKhasData(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result<List<UserRoleDAO.Data>> userRoles = cassExecutorObj.getUserRoles(); + + //System.out.println(""+userRoles.status); + assertEquals(24,userRoles.status); + } + + @Test + public void testGetUserRolesFirstIf() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result<List<UserRoleDAO.Data>> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList<UserRoleDAO.Data>(); + + Mockito.doReturn(false).when(retVal1).isOKhasData(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result<List<UserRoleDAO.Data>> userRoles = cassExecutorObj.getUserRoles(); + +// System.out.println("output is"+userRoles.status); + assertEquals(0,userRoles.status); + } + + @Test + public void testGetUserRolesSecondIf() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result<List<UserRoleDAO.Data>> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList<UserRoleDAO.Data>(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + Date dt = new Date(); + Calendar c = Calendar.getInstance(); + c.setTime(dt); + c.add(Calendar.DATE, 1); + dataObj.expires = c.getTime(); + + retVal1.value.add(dataObj); + Mockito.doReturn(true).when(retVal1).isOKhasData(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result<List<UserRoleDAO.Data>> userRoles = cassExecutorObj.getUserRoles(); + + //System.out.println(userRoles.status); + assertEquals("Success",userRoles.details); + Result<List<UserRoleDAO.Data>> userRoles1 = cassExecutorObj.getUserRoles(); + + //System.out.println(userRoles1.status); + assertEquals(0, userRoles1.status); + } + + @Test + public void testGetRole() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result<List<UserRoleDAO.Data>> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList<UserRoleDAO.Data>(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + dataObj.expires = new Date(); + + retVal1.value.add(dataObj); + Mockito.doReturn(false).when(retVal1).isOKhasData(); + Mockito.doReturn(true).when(retVal1).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result<List<RoleDAO.Data>> userRoles = cassExecutorObj.getRoles(); + + //System.out.println(""+userRoles.status); + assertEquals(4,userRoles.status); + } + + @Test + public void testGetRoleFirstIf() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + q.roleDAO = Mockito.mock(CachedRoleDAO.class); + Result<List<UserRoleDAO.Data>> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList<UserRoleDAO.Data>(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + dataObj.expires = new Date(); + dataObj.ns=""; + dataObj.rname=""; + + retVal1.value.add(dataObj); + Mockito.doReturn(false).when(retVal1).isOKhasData(); + Mockito.doReturn(false).when(retVal1).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + Mockito.doReturn(retVal1).when(q.roleDAO).read(trans,"",""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result<List<RoleDAO.Data>> userRoles = cassExecutorObj.getRoles(); + +// System.out.println(""+userRoles.status); + assertEquals(0,userRoles.status); + } + + @Test + public void testGetRoleSecondIf() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + q.roleDAO = Mockito.mock(CachedRoleDAO.class); + Result<List<UserRoleDAO.Data>> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList<UserRoleDAO.Data>(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + dataObj.expires = new Date(); + dataObj.ns=""; + dataObj.rname=""; + + retVal1.value.add(dataObj); + Mockito.doReturn(false).when(retVal1).isOKhasData(); + Mockito.doReturn(true).when(retVal1).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + Mockito.doReturn(retVal1).when(q.roleDAO).read(trans,"",""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result<List<RoleDAO.Data>> userRoles = cassExecutorObj.getRoles(); + userRoles = cassExecutorObj.getRoles(); + +// System.out.println(""+userRoles.status); + assertEquals(0,userRoles.status); + } + @Test + public void testGetPerms() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result<List<UserRoleDAO.Data>> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList<UserRoleDAO.Data>(); + Mockito.doReturn(false).when(retVal1).isOKhasData(); + Mockito.doReturn(true).when(retVal1).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result<Set<String>> userRoles = cassExecutorObj.getPermNames(); + userRoles = cassExecutorObj.getPermNames(); + + //System.out.println(""+userRoles.status); + assertEquals(0,userRoles.status); + } + @Test + public void testGetPermsRrldOk() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result<List<UserRoleDAO.Data>> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList<UserRoleDAO.Data>(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + dataObj.expires = new Date(); + + retVal1.value.add(dataObj); + Mockito.doReturn(false).when(retVal1).isOKhasData(); + Mockito.doReturn(true).when(retVal1).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result<Set<String>> userRoles = cassExecutorObj.getPermNames(); + + //System.out.println(""+userRoles.status); + assertEquals(4,userRoles.status); + } + + + @Test + public void testGetPerm() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + q.roleDAO = Mockito.mock(CachedRoleDAO.class); + Result<List<UserRoleDAO.Data>> retVal1 = Mockito.mock(Result.class); + Result<List<RoleDAO.Data>> retVal2 = Mockito.mock(Result.class); + + retVal1.value = new ArrayList<UserRoleDAO.Data>(); + retVal2.value = new ArrayList<RoleDAO.Data>(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + Date dt = new Date(); + Calendar c = Calendar.getInstance(); + c.setTime(dt); + c.add(Calendar.DATE, 1); + dataObj.expires = c.getTime(); + dataObj.ns = ""; + dataObj.rname=""; + + RoleDAO.Data dataObj1 = Mockito.mock( RoleDAO.Data.class); + Set<String> permSet = new HashSet<String>(); + permSet.add("test"); + Mockito.doReturn(permSet).when(dataObj1).perms(false); + + dt = new Date(); + c = Calendar.getInstance(); + c.setTime(dt); + c.add(Calendar.DATE, 1); + dataObj1.ns = "test"; + dataObj1.perms = permSet; + + retVal1.value.add(dataObj); + retVal2.value.add(dataObj1); + Mockito.doReturn(true).when(retVal1).isOKhasData(); + Mockito.doReturn(true).when(retVal1).isOK(); + Mockito.doReturn(true).when(retVal2).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + Mockito.doReturn(retVal2).when(q.roleDAO).read(trans,"",""); + + + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); +// +// Mockito.doReturn(retVal2).when(cassExecutorObj).getPermNames(); + Result<List<Data>> userRoles = cassExecutorObj.getPerms(true); +// userRoles = cassExecutorObj.getPerms(false); + +// System.out.println(""+userRoles.status); + assertEquals(0,userRoles.status); + } + + @Test + public void testGetPermFalse() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + q.roleDAO = Mockito.mock(CachedRoleDAO.class); + Result<List<UserRoleDAO.Data>> retVal1 = Mockito.mock(Result.class); + Result<List<RoleDAO.Data>> retVal2 = Mockito.mock(Result.class); + + retVal1.value = new ArrayList<UserRoleDAO.Data>(); + retVal2.value = new ArrayList<RoleDAO.Data>(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + Date dt = new Date(); + Calendar c = Calendar.getInstance(); + c.setTime(dt); + c.add(Calendar.DATE, 1); + dataObj.expires = c.getTime(); + dataObj.ns = ""; + dataObj.rname=""; + + RoleDAO.Data dataObj1 = Mockito.mock( RoleDAO.Data.class); + Set<String> permSet = new HashSet<String>(); + permSet.add("test"); + Mockito.doReturn(permSet).when(dataObj1).perms(false); + + dt = new Date(); + c = Calendar.getInstance(); + c.setTime(dt); + c.add(Calendar.DATE, 1); + dataObj1.ns = "test"; + dataObj1.perms = permSet; + + retVal1.value.add(dataObj); + retVal2.value.add(dataObj1); + Mockito.doReturn(true).when(retVal1).isOKhasData(); + Mockito.doReturn(true).when(retVal1).isOK(); + Mockito.doReturn(true).when(retVal2).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + Mockito.doReturn(retVal2).when(q.roleDAO).read(trans,"",""); + + + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); +// +// Mockito.doReturn(retVal2).when(cassExecutorObj).getPermNames(); + Result<List<Data>> userRoles = cassExecutorObj.getPerms(false); + userRoles = cassExecutorObj.getPerms(false); + +// System.out.println(""+userRoles.status); + assertEquals(0,userRoles.status); + } + +}
\ No newline at end of file diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedPermDAOTest.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedPermDAOTest.java deleted file mode 100644 index 972e366c..00000000 --- a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedPermDAOTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/** - * ============LICENSE_START==================================================== - * org.onap.aaf - * =========================================================================== - * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. - * =========================================================================== - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END==================================================== - * - */ -package org.onap.aaf.auth.dao.cached; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.mockito.MockitoAnnotations.initMocks; - -import java.util.List; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.onap.aaf.auth.dao.CIDAO; -import org.onap.aaf.auth.dao.cass.PermDAO; -import org.onap.aaf.auth.dao.cass.PermDAO.Data; -import org.onap.aaf.auth.dao.cass.Status; -import org.onap.aaf.auth.env.AuthzTrans; -import org.onap.aaf.auth.layer.Result; - -public class JU_CachedPermDAOTest { - - @Mock - private CIDAO<AuthzTrans> info; - @Mock - private PermDAO dao; - private AuthzTrans trans; - @Mock - private Result<List<PermDAO.Data>> value; - - @Before - public void setUp() throws Exception { - initMocks(this); - - when(dao.readNS(trans, "ns")).thenReturn(value); - } - - @Test - public void testReadNS() { - when(value.isOKhasData()).thenReturn(true); - when(value.isOK()).thenReturn(false); - CachedPermDAO ccDao = new CachedPermDAO(dao, info, 100l); - - Result<List<Data>> result = ccDao.readNS(trans, "ns"); - - assertEquals(result, value); - - when(value.isOKhasData()).thenReturn(false); - - result = ccDao.readNS(trans, "ns"); - - assertEquals(result.status, Status.ERR_PermissionNotFound); - - ccDao.readChildren(trans, "ns", "type"); - - verify(dao).readChildren(trans, "ns", "type"); - } - -} diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/direct/test/JU_DirectAAFLocator.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/direct/test/JU_DirectAAFLocator.java new file mode 100644 index 00000000..cf850587 --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/direct/test/JU_DirectAAFLocator.java @@ -0,0 +1,152 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.direct.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.aaf.auth.dao.cass.LocateDAO; +import org.onap.aaf.auth.dao.cass.LocateDAO.Data; +import org.onap.aaf.auth.direct.DirectAAFLocator; +import org.onap.aaf.auth.env.AuthzEnv; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.LocatorException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; + + +@RunWith(MockitoJUnitRunner.class) +public class JU_DirectAAFLocator { + + @Mock + LocateDAO ldao; + + @Mock + AuthzEnv env; + + @Mock + AuthzTrans trans; + + @Mock + Access access; + + @Before + public void setUp() throws Exception { + initMocks(this); + } + + @Test + public void testConstructorExcpetion() { + + PropAccess access = Mockito.mock(PropAccess.class); + Mockito.doReturn(access).when(env).access(); + Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null); + Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null); + try { + DirectAAFLocator aafLocatorObj=new DirectAAFLocator(env, ldao,"test","test"); + } catch (LocatorException e) { +// System.out.println(e.getMessage()); + assertEquals("Invalid Version String: test", e.getMessage()); + } + } + + @Test + public void testConstructorUriExcpetion() { + + PropAccess access = Mockito.mock(PropAccess.class); + Mockito.doReturn(access).when(env).access(); + Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null); + Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null); + try { + DirectAAFLocator aafLocatorObj=new DirectAAFLocator(env, ldao," test","3.2"); + } catch (LocatorException e) { +// System.out.println(e.getMessage()); + assertTrue(e.getMessage().contains("Illegal character in path at index")); + } + } + @Test + public void testRefresh() { + + DirectAAFLocator aafLocatorObj=null; + PropAccess access = Mockito.mock(PropAccess.class); + Mockito.doReturn(access).when(env).access(); + Mockito.doReturn(trans).when(env).newTransNoAvg(); + Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null); + Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null); + try { + aafLocatorObj = new DirectAAFLocator(env, ldao,"test","30.20.30.30"); + } catch (LocatorException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"",new String[0]); + + Data data= new Data(); + data.major=30; + data.minor=30; + data.patch=30; + data.pkg=30; + retVal1.value = new ArrayList<Data>(); + retVal1.value.add(data); + + Mockito.doReturn(retVal1).when(ldao).readByName(trans,"test"); + boolean retVal = aafLocatorObj.refresh(); +// System.out.println(retVal); + assertTrue(retVal); + } + + @Test + public void testRefreshNOK() { + + DirectAAFLocator aafLocatorObj=null; + PropAccess access = Mockito.mock(PropAccess.class); + Mockito.doReturn(access).when(env).access(); + Mockito.doReturn(trans).when(env).newTransNoAvg(); + Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null); + Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null); + try { + aafLocatorObj = new DirectAAFLocator(env, ldao,"test","30.20.30.30"); + } catch (LocatorException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"",new String[0]); + + Mockito.doReturn(retVal1).when(ldao).readByName(trans,"test"); + boolean retVal = aafLocatorObj.refresh(); +// System.out.println(retVal); + assertFalse(retVal); + } + +}
\ No newline at end of file diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/direct/test/JU_DirectAAFUserPass.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/direct/test/JU_DirectAAFUserPass.java new file mode 100644 index 00000000..ca0a8917 --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/direct/test/JU_DirectAAFUserPass.java @@ -0,0 +1,223 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.direct.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.Date; + +import javax.servlet.http.HttpServletRequest; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.aaf.auth.dao.DAOException; +import org.onap.aaf.auth.dao.hl.Question; +import org.onap.aaf.auth.direct.DirectAAFUserPass; +import org.onap.aaf.auth.env.AuthzEnv; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.misc.env.LogTarget; + + +@RunWith(MockitoJUnitRunner.class) +public class JU_DirectAAFUserPass { + + @Mock + Question question; + + @Mock + AuthzEnv env; + + @Mock + AuthzTrans trans; + + @Mock + HttpServletRequest request; + + + @Before + public void setUp() throws Exception { + initMocks(this); + when(env.warn()).thenReturn(new LogTarget() { + + @Override + public void printf(String fmt, Object... vars) {} + + @Override + public void log(Throwable e, Object... msgs) { + e.getMessage(); + e.printStackTrace(); + msgs.toString(); + + } + + @Override + public void log(Object... msgs) { + } + + @Override + public boolean isLoggable() { + + return true; + } + }); + when(env.error()).thenReturn(new LogTarget() { + + @Override + public void printf(String fmt, Object... vars) {} + + @Override + public void log(Throwable e, Object... msgs) { + e.getMessage(); + e.printStackTrace(); + msgs.toString(); + + } + + @Override + public void log(Object... msgs) { + } + + @Override + public boolean isLoggable() { + + return true; + } + }); + } + + @Test + public void testUserPass() { + + DirectAAFUserPass aafLocatorObj=null; + aafLocatorObj = new DirectAAFUserPass(env, question); + Result<Date> retVal1 = new Result<Date>(null,0,"",new String[0]); + Mockito.doReturn(trans).when(env).newTransNoAvg(); + try { + Mockito.doReturn(retVal1).when(question).doesUserCredMatch(trans, null, null); + } catch (DAOException e) { + e.printStackTrace(); + } + boolean retVal = aafLocatorObj.validate(null, null, null, null); + + assertTrue(retVal); + } + + @Test + public void testUserPassStateisRequest() { + + DirectAAFUserPass aafLocatorObj=null; + aafLocatorObj = new DirectAAFUserPass(env, question); + Result<Date> retVal1 = new Result<Date>(null,1,"",new String[0]); + Mockito.doReturn(trans).when(env).newTransNoAvg(); + try { + Mockito.doReturn(retVal1).when(question).doesUserCredMatch(trans, null, null); + } catch (DAOException e) { + e.printStackTrace(); + } + boolean retVal = aafLocatorObj.validate(null, null, null, request); + +// System.out.println(retVal); + assertFalse(retVal); + } + + @Test + public void testUserPassStateNotNull() { + + DirectAAFUserPass aafLocatorObj=null; + aafLocatorObj = new DirectAAFUserPass(env, question); + Result<Date> retVal1 = new Result<Date>(null,1,"",new String[0]); + Mockito.doReturn(trans).when(env).newTransNoAvg(); + try { + Mockito.doReturn(retVal1).when(question).doesUserCredMatch(trans, null, null); + } catch (DAOException e) { + e.printStackTrace(); + } + boolean retVal = aafLocatorObj.validate(null, null, null, "test"); + +// System.out.println(retVal); + assertFalse(retVal); + } + + @Test + public void testUserPassTransChk() { + + DirectAAFUserPass aafLocatorObj=null; + aafLocatorObj = new DirectAAFUserPass(env, question); + Result<Date> retVal1 = new Result<Date>(null,1,"",new String[0]); + Mockito.doReturn(trans).when(env).newTransNoAvg(); + try { + Mockito.doReturn(retVal1).when(question).doesUserCredMatch(trans, null, null); + } catch (DAOException e) { + e.printStackTrace(); + } + boolean retVal = aafLocatorObj.validate(null, null, null, trans); + +// System.out.println(retVal); + assertFalse(retVal); + } + + @Test + public void testUserPassTransIpNotNull() { + + DirectAAFUserPass aafLocatorObj=null; + aafLocatorObj = new DirectAAFUserPass(env, question); + Result<Date> retVal1 = new Result<Date>(null,1,"",new String[0]); + Mockito.doReturn("test").when(trans).ip(); + Mockito.doReturn(trans).when(env).newTransNoAvg(); + try { + Mockito.doReturn(retVal1).when(question).doesUserCredMatch(trans, null, null); + } catch (DAOException e) { + e.printStackTrace(); + } + boolean retVal = aafLocatorObj.validate(null, null, null, trans); + +// System.out.println(retVal); + assertFalse(retVal); + } + + @Test + public void testUserExceptionChk() { + + DirectAAFUserPass aafLocatorObj=null; + aafLocatorObj = new DirectAAFUserPass(env, question); + Result<Date> retVal1 = new Result<Date>(null,1,"",new String[0]); + Mockito.doReturn(trans).when(env).newTransNoAvg(); + try { + Mockito.doThrow(DAOException.class).when(question).doesUserCredMatch(trans, null, null); + } catch (DAOException e) { + // TODO Auto-generated catch block +// e.printStackTrace(); + } + boolean retVal = aafLocatorObj.validate(null, null, null, trans); + +// System.out.println(retVal); + assertFalse(retVal); + } + +}
\ No newline at end of file diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/direct/test/JU_DirectLocatorCreateor.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/direct/test/JU_DirectLocatorCreateor.java new file mode 100644 index 00000000..d026500c --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/direct/test/JU_DirectLocatorCreateor.java @@ -0,0 +1,86 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.direct.test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.MockitoAnnotations.initMocks; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.aaf.auth.dao.cass.LocateDAO; +import org.onap.aaf.auth.direct.DirectLocatorCreator; +import org.onap.aaf.auth.env.AuthzEnv; +import org.onap.aaf.cadi.LocatorException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; + + +@RunWith(MockitoJUnitRunner.class) +public class JU_DirectLocatorCreateor { + + @Mock + LocateDAO ldao; + + @Mock + AuthzEnv env; + + @Before + public void setUp() throws Exception { + initMocks(this); + } + + @Test + public void testCreate() { + PropAccess access = Mockito.mock(PropAccess.class); + Mockito.doReturn(access).when(env).access(); + Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null); + Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null); + DirectLocatorCreator aafLocatorObj=new DirectLocatorCreator(env, ldao); + try { + aafLocatorObj.setSelf("test", 9080); + aafLocatorObj.create("test","30.20.30.30"); + } catch (LocatorException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testCreateHostnameNull() { + PropAccess access = Mockito.mock(PropAccess.class); + Mockito.doReturn(access).when(env).access(); + Mockito.doReturn("20").when(access).getProperty(Config.CADI_LATITUDE,null); + Mockito.doReturn("20").when(access).getProperty(Config.CADI_LONGITUDE,null); + DirectLocatorCreator aafLocatorObj=new DirectLocatorCreator(env, ldao); + try { + aafLocatorObj.create("test","30.20.30.30"); + } catch (LocatorException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + +}
\ No newline at end of file diff --git a/auth/auth-certman/src/main/java/org/onap/aaf/auth/cm/validation/CertmanValidator.java b/auth/auth-certman/src/main/java/org/onap/aaf/auth/cm/validation/CertmanValidator.java index 1a27b41f..bb157a2e 100644 --- a/auth/auth-certman/src/main/java/org/onap/aaf/auth/cm/validation/CertmanValidator.java +++ b/auth/auth-certman/src/main/java/org/onap/aaf/auth/cm/validation/CertmanValidator.java @@ -78,22 +78,6 @@ public class CertmanValidator extends Validator{ return this; } - public CertmanValidator artisKeys(List<ArtiDAO.Data> list, int min) { - if (list==null) { - msg(ARTIFACT_LIST_IS_NULL); - } else { - if (list.size()<min) { - msg(ARTIFACTS_MUST_HAVE_AT_LEAST + min + ENTR + (min==1?Y:IES)); - } else { - for (ArtiDAO.Data a : list) { - keys(a); - } - } - } - return this; - } - - public CertmanValidator keys(ArtiDAO.Data add) { if (add==null) { msg("Artifact is null."); diff --git a/auth/auth-certman/src/test/java/org/onap/aaf/auth/cm/validation/JU_CertmanValidator.java b/auth/auth-certman/src/test/java/org/onap/aaf/auth/cm/validation/JU_CertmanValidator.java new file mode 100644 index 00000000..4aa3d6d3 --- /dev/null +++ b/auth/auth-certman/src/test/java/org/onap/aaf/auth/cm/validation/JU_CertmanValidator.java @@ -0,0 +1,114 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.cm.validation; + +import org.junit.Test; +import org.onap.aaf.auth.dao.cass.ArtiDAO; + +import static com.google.common.collect.Lists.newArrayList; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class JU_CertmanValidator { + + private static final String COLLECTION_NAME = "collection_name"; + private static final int MIN_SIZE = 3; + private CertmanValidator certmanValidator = new CertmanValidator(); + + + @Test + public void nullBlankMin_shouldReportErrorWhenListIsNull() { + + certmanValidator.nullBlankMin(COLLECTION_NAME, null, MIN_SIZE); + assertEquals(COLLECTION_NAME + " is null.\n", certmanValidator.errs()); + } + + @Test + public void nullBlankMin_shouldReportErrorWhenListHasNotEnoughElements() { + + certmanValidator.nullBlankMin(COLLECTION_NAME, newArrayList("one", "two"), MIN_SIZE); + assertEquals(COLLECTION_NAME + " must have at least " + MIN_SIZE + " entries.\n", certmanValidator.errs()); + } + + @Test + public void nullBlankMin_shouldReportErrorWhenListContainsNullOrEmptyElements() { + + certmanValidator.nullBlankMin(COLLECTION_NAME, newArrayList("one", "", "three"), MIN_SIZE); + assertEquals("List Item is blank.\n", certmanValidator.errs()); + } + + @Test + public void nullBlankMin_shouldPassValidation() { + + certmanValidator.nullBlankMin(COLLECTION_NAME, newArrayList("one", "two", "three"), MIN_SIZE); + assertFalse(certmanValidator.err()); + } + + @Test + public void artisRequired_shouldReportErrorWhenListIsNull() { + + certmanValidator.artisRequired(null, MIN_SIZE); + assertEquals("Artifact List is null.\n", certmanValidator.errs()); + } + + @Test + public void artisRequired_shouldReportErrorWhenListHasNotEnoughElements() { + + certmanValidator.artisRequired(newArrayList(newArtifactData(), newArtifactData()), MIN_SIZE); + assertEquals("Artifacts must have at least " + MIN_SIZE + " entries.\n", certmanValidator.errs()); + } + + @Test + public void artisRequired_shouldReportErrorWhenArtifactDoesNotHaveAllRequiredFields() { + + certmanValidator.artisRequired(newArrayList(newArtifactData("id", "", "ca", "dir", "user")), 1); + assertEquals("machine is blank.\n", certmanValidator.errs()); + } + + @Test + public void keys_shouldReportErrorWhenArtifactIsNull() { + + certmanValidator.keys(null); + assertEquals("Artifact is null.\n", certmanValidator.errs()); + } + + @Test + public void keys_shouldReportErrorWhenArtifactDoesNotHaveAllRequiredFields() { + + certmanValidator.keys(newArtifactData("", "", "ca", "dir", "user")); + assertEquals("mechid is blank.\n" + "machine is blank.\n", certmanValidator.errs()); + } + + private ArtiDAO.Data newArtifactData() { + return new ArtiDAO.Data(); + } + + private ArtiDAO.Data newArtifactData(String mechId, String machine, String ca, String dir, String user) { + ArtiDAO.Data artifact = new ArtiDAO.Data(); + artifact.mechid = mechId; + artifact.machine = machine; + artifact.ca = ca; + artifact.dir = dir; + artifact.os_user = user; + return artifact; + + } +}
\ No newline at end of file diff --git a/auth/auth-core/src/main/java/org/onap/aaf/auth/layer/Result.java b/auth/auth-core/src/main/java/org/onap/aaf/auth/layer/Result.java index a3abb2bb..3e68e3ab 100644 --- a/auth/auth-core/src/main/java/org/onap/aaf/auth/layer/Result.java +++ b/auth/auth-core/src/main/java/org/onap/aaf/auth/layer/Result.java @@ -48,7 +48,7 @@ public class Result<RV> { ERR_Backend = 9, ERR_General = 20; - public final RV value; + public RV value; public final int status; public final String details; public final String[] variables; @@ -275,6 +275,8 @@ public class Result<RV> { * @return */ public boolean isOKhasData() { + System.out.println("specialCondition:"+specialCondition); + System.out.println("specialCondition:"+(specialCondition & EMPTY_LIST)); return status == OK && (specialCondition & EMPTY_LIST) != EMPTY_LIST; } diff --git a/cadi/core/src/test/resources/keystore.p12 b/cadi/core/src/test/resources/keystore.p12 Binary files differnew file mode 100644 index 00000000..51273d5d --- /dev/null +++ b/cadi/core/src/test/resources/keystore.p12 diff --git a/cadi/core/src/test/resources/output_key b/cadi/core/src/test/resources/output_key new file mode 100644 index 00000000..9d94dcbd --- /dev/null +++ b/cadi/core/src/test/resources/output_key @@ -0,0 +1 @@ +QRSTUVWXYZabcdef
\ No newline at end of file diff --git a/cadi/core/src/test/resources/truststore.jks b/cadi/core/src/test/resources/truststore.jks Binary files differnew file mode 100644 index 00000000..9ee3dcf6 --- /dev/null +++ b/cadi/core/src/test/resources/truststore.jks diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/Api.java b/misc/env/src/test/java/org/onap/aaf/misc/env/Api.java new file mode 100644 index 00000000..688e11b8 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/Api.java @@ -0,0 +1,373 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +// +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.5-2 +// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> +// Any modifications to this file will be lost upon recompilation of the source schema. +// Generated on: 2018.12.09 at 10:14:23 PM IST +// + + +package org.onap.aaf.misc.env; + +import java.util.ArrayList; +import java.util.List; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + + +/** + * <p>Java class for anonymous complex type. + * + * <p>The following schema fragment specifies the expected content contained within this class. + * + * <pre> + * <complexType> + * <complexContent> + * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> + * <sequence> + * <element name="route" maxOccurs="unbounded" minOccurs="0"> + * <complexType> + * <complexContent> + * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> + * <sequence> + * <element name="meth" type="{http://www.w3.org/2001/XMLSchema}string"/> + * <element name="path" type="{http://www.w3.org/2001/XMLSchema}string"/> + * <element name="param" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/> + * <element name="desc" type="{http://www.w3.org/2001/XMLSchema}string"/> + * <element name="comments" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/> + * <element name="contentType" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/> + * <element name="expected" type="{http://www.w3.org/2001/XMLSchema}int"/> + * <element name="explicitErr" type="{http://www.w3.org/2001/XMLSchema}int" maxOccurs="unbounded" minOccurs="0"/> + * </sequence> + * </restriction> + * </complexContent> + * </complexType> + * </element> + * </sequence> + * </restriction> + * </complexContent> + * </complexType> + * </pre> + * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "", propOrder = { + "route" +}) +@XmlRootElement(name = "api") +public class Api { + + protected List<Api.Route> route; + + /** + * Gets the value of the route property. + * + * <p> + * This accessor method returns a reference to the live list, + * not a snapshot. Therefore any modification you make to the + * returned list will be present inside the JAXB object. + * This is why there is not a <CODE>set</CODE> method for the route property. + * + * <p> + * For example, to add a new item, do as follows: + * <pre> + * getRoute().add(newItem); + * </pre> + * + * + * <p> + * Objects of the following type(s) are allowed in the list + * {@link Api.Route } + * + * + */ + public List<Api.Route> getRoute() { + if (route == null) { + route = new ArrayList<Api.Route>(); + } + return this.route; + } + + + /** + * <p>Java class for anonymous complex type. + * + * <p>The following schema fragment specifies the expected content contained within this class. + * + * <pre> + * <complexType> + * <complexContent> + * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> + * <sequence> + * <element name="meth" type="{http://www.w3.org/2001/XMLSchema}string"/> + * <element name="path" type="{http://www.w3.org/2001/XMLSchema}string"/> + * <element name="param" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/> + * <element name="desc" type="{http://www.w3.org/2001/XMLSchema}string"/> + * <element name="comments" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/> + * <element name="contentType" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/> + * <element name="expected" type="{http://www.w3.org/2001/XMLSchema}int"/> + * <element name="explicitErr" type="{http://www.w3.org/2001/XMLSchema}int" maxOccurs="unbounded" minOccurs="0"/> + * </sequence> + * </restriction> + * </complexContent> + * </complexType> + * </pre> + * + * + */ + @XmlAccessorType(XmlAccessType.FIELD) + @XmlType(name = "", propOrder = { + "meth", + "path", + "param", + "desc", + "comments", + "contentType", + "expected", + "explicitErr" + }) + public static class Route { + + @XmlElement(required = true) + protected String meth; + @XmlElement(required = true) + protected String path; + protected List<String> param; + @XmlElement(required = true) + protected String desc; + protected List<String> comments; + protected List<String> contentType; + protected int expected; + @XmlElement(type = Integer.class) + protected List<Integer> explicitErr; + + /** + * Gets the value of the meth property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getMeth() { + return meth; + } + + /** + * Sets the value of the meth property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setMeth(String value) { + this.meth = value; + } + + /** + * Gets the value of the path property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getPath() { + return path; + } + + /** + * Sets the value of the path property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setPath(String value) { + this.path = value; + } + + /** + * Gets the value of the param property. + * + * <p> + * This accessor method returns a reference to the live list, + * not a snapshot. Therefore any modification you make to the + * returned list will be present inside the JAXB object. + * This is why there is not a <CODE>set</CODE> method for the param property. + * + * <p> + * For example, to add a new item, do as follows: + * <pre> + * getParam().add(newItem); + * </pre> + * + * + * <p> + * Objects of the following type(s) are allowed in the list + * {@link String } + * + * + */ + public List<String> getParam() { + if (param == null) { + param = new ArrayList<String>(); + } + return this.param; + } + + /** + * Gets the value of the desc property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getDesc() { + return desc; + } + + /** + * Sets the value of the desc property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setDesc(String value) { + this.desc = value; + } + + /** + * Gets the value of the comments property. + * + * <p> + * This accessor method returns a reference to the live list, + * not a snapshot. Therefore any modification you make to the + * returned list will be present inside the JAXB object. + * This is why there is not a <CODE>set</CODE> method for the comments property. + * + * <p> + * For example, to add a new item, do as follows: + * <pre> + * getComments().add(newItem); + * </pre> + * + * + * <p> + * Objects of the following type(s) are allowed in the list + * {@link String } + * + * + */ + public List<String> getComments() { + if (comments == null) { + comments = new ArrayList<String>(); + } + return this.comments; + } + + /** + * Gets the value of the contentType property. + * + * <p> + * This accessor method returns a reference to the live list, + * not a snapshot. Therefore any modification you make to the + * returned list will be present inside the JAXB object. + * This is why there is not a <CODE>set</CODE> method for the contentType property. + * + * <p> + * For example, to add a new item, do as follows: + * <pre> + * getContentType().add(newItem); + * </pre> + * + * + * <p> + * Objects of the following type(s) are allowed in the list + * {@link String } + * + * + */ + public List<String> getContentType() { + if (contentType == null) { + contentType = new ArrayList<String>(); + } + return this.contentType; + } + + /** + * Gets the value of the expected property. + * + */ + public int getExpected() { + return expected; + } + + /** + * Sets the value of the expected property. + * + */ + public void setExpected(int value) { + this.expected = value; + } + + /** + * Gets the value of the explicitErr property. + * + * <p> + * This accessor method returns a reference to the live list, + * not a snapshot. Therefore any modification you make to the + * returned list will be present inside the JAXB object. + * This is why there is not a <CODE>set</CODE> method for the explicitErr property. + * + * <p> + * For example, to add a new item, do as follows: + * <pre> + * getExplicitErr().add(newItem); + * </pre> + * + * + * <p> + * Objects of the following type(s) are allowed in the list + * {@link Integer } + * + * + */ + public List<Integer> getExplicitErr() { + if (explicitErr == null) { + explicitErr = new ArrayList<Integer>(); + } + return this.explicitErr; + } + + } + +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/JU_BaseDataFactory.java b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_BaseDataFactory.java new file mode 100644 index 00000000..0c21bda3 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_BaseDataFactory.java @@ -0,0 +1,82 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.misc.env; + +import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.aaf.misc.env.impl.EnvFactory; + +public class JU_BaseDataFactory { + + @Before + public void setUp() throws Exception { + initMocks(this); + } + + @Test + public void testGenSchemaException() { + Store env = Mockito.mock(Store.class); + Mockito.doReturn("testdir").when(env).get(null, EnvFactory.DEFAULT_SCHEMA_DIR); + try { + BaseDataFactory.genSchema(env, new String[] {}); + } catch (APIException e) { + assertTrue(e.getLocalizedMessage().contains("does not exist. You can set this with")); + } + } + + @Test + public void testGenSchemaXsdException() { + Store env = Mockito.mock(Store.class); + Mockito.doReturn(System.getProperty("user.dir")).when(env).get(null, EnvFactory.DEFAULT_SCHEMA_DIR); + String[] schemaFIles = new String[] {"../auth-client/src/main/xsd/aaf_2_0.xsd"}; + try { + BaseDataFactory.genSchema(env, schemaFIles); + } catch (APIException e) { + assertTrue(e.getLocalizedMessage().contains("for schema validation")); + } + } + + @Test + public void testGenSchemaNoException() { + Store env = Mockito.mock(Store.class); + Mockito.doReturn(System.getProperty("user.dir")).when(env).get(null, EnvFactory.DEFAULT_SCHEMA_DIR); + String[] schemaFIles = new String[] {"../../auth-client/src/main/xsd/aaf_2_0.xsd"}; + try { + BaseDataFactory.genSchema(env, schemaFIles); + } catch (APIException e) { + e.printStackTrace(); + } + } + + @Test + public void testGetQName() { + String[] schemaFIles = new String[] {"../../auth-client/src/main/xsd/aaf_2_0.xsd"}; + try { + BaseDataFactory.getQName(Api.class); + } catch (APIException e) { + assertTrue(e.getLocalizedMessage().contains("package-info does not have an XmlSchema annotation")); + } + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/JU_JavaUtilLogTarget.java b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_JavaUtilLogTarget.java new file mode 100644 index 00000000..2617559d --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_JavaUtilLogTarget.java @@ -0,0 +1,85 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.misc.env.impl; + +import static org.junit.Assert.assertFalse; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; + +public class JU_JavaUtilLogTarget { + + @Mock + Level level; + + @Mock + Logger log; + + @Before + public void setup() { + initMocks(this); + } + + @Test + public void testLoggable() { + JavaUtilLogTarget logObj = new JavaUtilLogTarget( log, level); + boolean retVal = logObj.isLoggable(); + + assertFalse(retVal); + } + + @Test + public void testLog() { + JavaUtilLogTarget logObj = new JavaUtilLogTarget( log, level); + Mockito.doReturn(false).when(log).isLoggable(level); + logObj.log(new Object[] {"test","test2",""}); + Mockito.doReturn(true).when(log).isLoggable(level); + logObj.log(new Object[] {"test","test2",""}); + + } + + @Test + public void testLogThrowable() { + JavaUtilLogTarget logObj = new JavaUtilLogTarget( log, level); + + Mockito.doReturn(true).when(log).isLoggable(level); + logObj.log(new Throwable("test exception"), new Object[] {"test","test2",""}); + logObj.log(new Throwable(), new Object[] {"test","test2",""}); + } + + @Test + public void testPrintf() { + JavaUtilLogTarget logObj = new JavaUtilLogTarget( log, level); + + Mockito.doReturn(true).when(log).isLoggable(level); + logObj.printf("test", new Object[] {"test","test2",""}); + + Mockito.doReturn(false).when(log).isLoggable(level); + logObj.printf("test", new Object[] {"test","test2",""}); + } +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/JU_NullLifeCycle.java b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_NullLifeCycle.java new file mode 100644 index 00000000..e685dfe6 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/JU_NullLifeCycle.java @@ -0,0 +1,43 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.misc.env.impl; + +import org.junit.Test; +import org.onap.aaf.misc.env.APIException; + +public class JU_NullLifeCycle { + + @Test + public void testServicePrestart() { + NullLifeCycle lifeCycleObj = new NullLifeCycle(); + try { + lifeCycleObj.servicePrestart(null); + lifeCycleObj.serviceDestroy(null); + lifeCycleObj.threadDestroy(null); + lifeCycleObj.threadPrestart(null); + lifeCycleObj.refresh(null); + }catch(APIException a) { + + } + } + +}
\ No newline at end of file diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_Log4JLogTargetTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_Log4JLogTargetTest.java index 9feaf3ef..2067e2ab 100644 --- a/misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_Log4JLogTargetTest.java +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/impl/JU_Log4JLogTargetTest.java @@ -21,42 +21,88 @@ package org.onap.aaf.misc.env.impl;
-import static org.junit.Assert.assertFalse;
-import static org.powermock.api.mockito.PowerMockito.when;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.MockitoAnnotations.initMocks;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.Mockito;
import org.onap.aaf.misc.env.APIException;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({ Log4JLogTarget.class, Logger.class })
public class JU_Log4JLogTargetTest {
+ @Mock
+ Level level;
+
@Mock
Logger log;
@Before
public void setup() {
- MockitoAnnotations.initMocks(this);
- PowerMockito.mockStatic(Logger.class);
- when(Logger.getLogger("Info")).thenReturn(log);
- when(log.isEnabledFor (Level.DEBUG)).thenReturn(false);
+ initMocks(this);
}
@Test
- public void test() throws APIException {
- Log4JLogTarget target = new Log4JLogTarget(null, Level.INFO);
- Log4JLogTarget target1 = new Log4JLogTarget("Info", Level.DEBUG);
+ public void testLoggable() {
+ Log4JLogTarget logObj = null;
+ try {
+ logObj = new Log4JLogTarget( "testLogger", Level.DEBUG);
+ } catch (APIException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ boolean retVal = logObj.isLoggable();
+ assertTrue(retVal);
+ }
+
+ @Test
+ public void testLog() {
+ Log4JLogTarget logObj = null;
+ try {
+ logObj = new Log4JLogTarget( null, Level.DEBUG);
+ } catch (APIException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ logObj.log(new Object[] {"test"});
+ }
+
+ @Test
+ public void testLogThrowable() {
+ Log4JLogTarget logObj = null;
+ try {
+ logObj = new Log4JLogTarget( null, Level.DEBUG);
+ } catch (APIException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ logObj.log(new Throwable("test exception"), new Object[] {"test","test2","",null});
+ }
+
+ @Test
+ public void testPrintf() {
+ Log4JLogTarget logObj = null;
+ try {
+ logObj = new Log4JLogTarget( "", level);
+ } catch (APIException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ logObj.printf("test", new Object[] {"test","test2",""});
- assertFalse(target1.isLoggable());
+ }
+
+ @Test
+ public void testSetEnv() {
+ try {
+ Log4JLogTarget.setLog4JEnv("test", Mockito.mock(BasicEnv.class));
+ } catch (APIException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
}
}
\ No newline at end of file diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JAXBObjectifier.java b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JAXBObjectifier.java new file mode 100644 index 00000000..ac0e859c --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JAXBObjectifier.java @@ -0,0 +1,135 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.misc.env.jaxb; + +import java.io.InputStream; +import java.io.Reader; + +import javax.xml.bind.JAXBException; +import javax.xml.validation.Schema; + +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.TimeTaken; +import org.onap.aaf.misc.env.old.IOObjectifier; + +/** + * Allow Extended IO interface usage without muddying up the Stringifier Interface + */ +public class JAXBObjectifier<T> implements IOObjectifier<T> { + private JAXBumar jumar; + + public JAXBObjectifier(Schema schema, Class<?>... classes) throws APIException { + try { + jumar = new JAXBumar(schema, classes); + } catch (JAXBException e) { + throw new APIException(e); + } + } + + public JAXBObjectifier(Class<?>... classes) throws APIException { + try { + jumar = new JAXBumar(classes); + } catch (JAXBException e) { + throw new APIException(e); + } + } + + // package on purpose + JAXBObjectifier(JAXBumar jumar) { + this.jumar = jumar; + } + + @SuppressWarnings("unchecked") + // @Override + public T objectify(Env env, String input) throws APIException { + TimeTaken tt = env.start("JAXB Unmarshal", Env.XML); + try { + tt.size(input.length()); + return (T)jumar.unmarshal(env.debug(), input); + } catch (JAXBException e) { + throw new APIException(e); + } finally { + tt.done(); + } + } + + @SuppressWarnings("unchecked") + // @Override + public T objectify(Env env, Reader rdr) throws APIException { + //TODO create a Reader that Counts? + TimeTaken tt = env.start("JAXB Unmarshal", Env.XML); + try { + return (T)jumar.unmarshal(env.debug(), rdr); + } catch (JAXBException e) { + throw new APIException(e); + } finally { + tt.done(); + } + } + + + @SuppressWarnings("unchecked") + // @Override + public T objectify(Env env, InputStream is) throws APIException { + //TODO create a Reader that Counts? + TimeTaken tt = env.start("JAXB Unmarshal", Env.XML); + try { + return (T)jumar.unmarshal(env.debug(), is); + } catch (JAXBException e) { + throw new APIException(e); + } finally { + tt.done(); + } + } + + + public void servicePrestart(Env env) throws APIException { + } + + public void threadPrestart(Env env) throws APIException { + } + + // // @Override + public void refresh(Env env) throws APIException { + } + + // // @Override + public void threadDestroy(Env env) throws APIException { + } + + // // @Override + public void serviceDestroy(Env env) throws APIException { + } + + + @SuppressWarnings("unchecked") + public T newInstance() throws APIException { + try { + return (T)jumar.newInstance(); + } catch (Exception e) { + throw new APIException(e); + } + } + +} + diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JAXBStringifier.java b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JAXBStringifier.java new file mode 100644 index 00000000..9b8a2c97 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JAXBStringifier.java @@ -0,0 +1,137 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.misc.env.jaxb; + +import java.io.OutputStream; +import java.io.StringWriter; +import java.io.Writer; + +import javax.xml.bind.JAXBException; +import javax.xml.namespace.QName; + +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.TimeTaken; +import org.onap.aaf.misc.env.old.IOStringifier; + +public class JAXBStringifier<T> implements IOStringifier<T> { + private JAXBmar jmar; + + public JAXBStringifier(Class<?>... classes) throws APIException { + try { + jmar = new JAXBmar(classes); + } catch (JAXBException e) { + throw new APIException(e); + } + } + + public JAXBStringifier(QName qname, Class<?>... classes) + throws APIException { + try { + jmar = new JAXBmar(qname, classes); + } catch (JAXBException e) { + throw new APIException(e); + } + } + + // package on purpose + JAXBStringifier(JAXBmar jmar) { + this.jmar = jmar; + } + + // // @Override + public void stringify(Env env, T input, Writer writer, boolean ... options) + throws APIException { + TimeTaken tt = env.start("JAXB Marshal", Env.XML); + try { + jmar.marshal(env.debug(), input, writer, options); + } catch (JAXBException e) { + throw new APIException(e); + } finally { + tt.done(); + } + } + + // @Override + public void stringify(Env env, T input, OutputStream os, boolean ... options) + throws APIException { + // TODO create an OutputStream that Counts? + TimeTaken tt = env.start("JAXB Marshal", Env.XML); + try { + jmar.marshal(env.debug(), input, os, options); + } catch (JAXBException e) { + throw new APIException(e); + } finally { + tt.done(); + } + } + + // @Override + public String stringify(Env env, T input, boolean ... options) throws APIException { + TimeTaken tt = env.start("JAXB Marshal", Env.XML); + StringWriter sw = new StringWriter(); + try { + jmar.marshal(env.debug(), input, sw, options); + String rv = sw.toString(); + tt.size(rv.length()); + return rv; + } catch (JAXBException e) { + tt.size(0); + throw new APIException(e); + } finally { + tt.done(); + } + } + + // // @Override + public void servicePrestart(Env env) throws APIException { + } + + // // @Override + public void threadPrestart(Env env) throws APIException { + } + + // // @Override + public void refresh(Env env) throws APIException { + } + + // // @Override + public void threadDestroy(Env env) throws APIException { + } + + // // @Override + public void serviceDestroy(Env env) throws APIException { + } + + // @Override + public JAXBStringifier<T> pretty(boolean pretty) { + jmar.pretty(pretty); + return this; + } + + // @Override + public JAXBStringifier<T> asFragment(boolean fragment) { + jmar.asFragment(fragment); + return this; + } + +} diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JU_JAXBDF.java b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JU_JAXBDF.java new file mode 100644 index 00000000..62c49032 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JU_JAXBDF.java @@ -0,0 +1,714 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.misc.env.jaxb; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.io.InputStream; +import java.io.OutputStream; +import java.io.StringReader; +import java.io.StringWriter; + +import javax.xml.bind.JAXBException; +import javax.xml.namespace.QName; +import javax.xml.validation.Schema; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Data; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.EnvJAXB; +import org.onap.aaf.misc.env.LogTarget; +import org.onap.aaf.misc.env.TimeTaken; + +public class JU_JAXBDF { + + @Mock + EnvJAXB primaryEnv; + + @Mock + JAXBumar jumar; + + @Mock + JAXBmar jmar; + + @Mock + Env env; + + TimeTaken tt,ttObjectify; + + @Before + public void setUp() { + initMocks(this); + tt=Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(env).start("JAXB Stringify", Env.XML); + Mockito.doNothing().when(tt).done(); + ttObjectify=Mockito.mock(TimeTaken.class); + Mockito.doReturn(ttObjectify).when(env).start("JAXB Objectify", Env.XML); + Mockito.doNothing().when(ttObjectify).done(); + } + + @Test + public void testNewInstance() { + JAXBDF<?> bdfObj = null; + try { + bdfObj = new JAXBDF( null, new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + Mockito.doThrow(new IllegalAccessException("Test Exception")).when(bdfObj.jumar).newInstance(); + Object retVal = bdfObj.newInstance(); + } catch (IllegalAccessException e) { + assertEquals("Test Exception", e.getLocalizedMessage()); + } catch (APIException e) { + assertTrue(e.getMessage().contains("Test Exception")); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testNewInstanceNoException() { + JAXBDF<?> bdfObj = null; + try { + bdfObj = new JAXBDF( null, new Class[] {this.getClass()}); + Object retVal = bdfObj.newInstance(); + assertTrue(retVal instanceof JU_JAXBDF); + } catch (APIException e) { + e.printStackTrace(); + } + + } + + @Test + public void testPrettyNoException() { + JAXBDF<?> bdfObj = null; + try { + bdfObj = new JAXBDF( null, Mockito.mock(Schema.class), new Class[] {this.getClass()}); + Object retVal = bdfObj.pretty(true); + assertTrue(retVal instanceof JAXBDF); + } catch (APIException e) { + e.printStackTrace(); + } + } + + @Test + public void testFragment() { + JAXBDF<?> bdfObj = null; + try { + bdfObj = new JAXBDF( null, Mockito.mock(QName.class), new Class[] {this.getClass()}); + Object retVal = bdfObj.asFragment(true); + assertTrue(retVal instanceof JAXBDF); + bdfObj.servicePrestart(null); + bdfObj.threadPrestart(null); + bdfObj.refresh(null); + bdfObj.threadDestroy(null); + bdfObj.serviceDestroy(null); + } catch (APIException e) { + e.printStackTrace(); + } + + } + + @Test + public void testNewData() { + JAXBDF<?> bdfObj = null; + try { + bdfObj = new JAXBDF( null, Mockito.mock(Schema.class),Mockito.mock(QName.class), new Class[] {this.getClass()}); + Data<?> retVal = bdfObj.newData(); + assertTrue(retVal instanceof JAXBData); + } catch (APIException e) { + e.printStackTrace(); + } + } + + @Test + public void testNewDataENV() { + JAXBDF<?> bdfObj = null; + try { + bdfObj = new JAXBDF( null, Mockito.mock(Schema.class),Mockito.mock(QName.class), new Class[] {this.getClass()}); + Data<?> retVal = bdfObj.newData(Mockito.mock(Env.class)); + assertTrue(retVal instanceof JAXBData); + } catch (APIException e) { + e.printStackTrace(); + } + } + + @Test + public void testNewDataType() { + JAXBDF<JAXBumar> bdfObj = null; + try { + bdfObj = new JAXBDF( null, Mockito.mock(Schema.class),Mockito.mock(QName.class), new Class[] {this.getClass()}); + Data<?> retVal = bdfObj.newData(new JAXBumar(new Class[] {this.getClass()})); + assertTrue(retVal instanceof JAXBData); + } catch (APIException e) { + e.printStackTrace(); + } catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testNewDataStream() { + JAXBDF<?> bdfObj = null; + try { + bdfObj = new JAXBDF( null, Mockito.mock(Schema.class),Mockito.mock(QName.class), new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + InputStream is = Mockito.mock(InputStream.class); + Mockito.doReturn(this.getClass()).when(bdfObj.jumar).unmarshal(logT, is); + Data<?> retVal = bdfObj.newDataFromStream(env, is); + assertTrue(retVal instanceof JAXBData); + } catch (APIException e) { + e.printStackTrace(); + } catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testNewDataStreamException() { + JAXBDF<?> bdfObj = null; + try { + bdfObj = new JAXBDF( null, Mockito.mock(Schema.class),Mockito.mock(QName.class), new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + InputStream is = Mockito.mock(InputStream.class); + Mockito.doThrow(new JAXBException("test")).when(bdfObj.jumar).unmarshal(logT, is); + Data<?> retVal = bdfObj.newDataFromStream(env, is); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + // TODO Auto-generated catch block + assertTrue(e.getMessage().contains("test")); + } + } + + @Test + public void testNewDataFromString() { + JAXBDF<?> bdfObj = null; + try { + bdfObj = new JAXBDF( null, Mockito.mock(Schema.class),Mockito.mock(QName.class), new Class[] {this.getClass()}); + Data<?> retVal = bdfObj.newDataFromString("test"); + assertTrue(retVal instanceof JAXBData); + } catch (APIException e) { + e.printStackTrace(); + } + } + + @Test + public void testStringify() { + JAXBDF<JAXBmar> bdfObj = null; + try { + JAXBmar typeObj = new JAXBmar(new Class[] {this.getClass()}); + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF( envJaxb, new Class[] {this.getClass()}); + bdfObj.jmar = Mockito.mock(JAXBmar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(envJaxb).debug(); + Mockito.doReturn(this.getClass()).when(bdfObj.jmar).marshal(logT, typeObj, Mockito.mock(StringWriter.class)); + String retVal = bdfObj.stringify(typeObj); + assertEquals("", retVal); + } catch (APIException e) { + e.printStackTrace(); + } catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testStringifyException() { + JAXBDF<JAXBmar> bdfObj = null; + try { + JAXBmar typeObj = new JAXBmar(new Class[] {this.getClass()}); + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(envJaxb).debug(); + bdfObj = new JAXBDF<JAXBmar>( envJaxb, new Class[] {this.getClass()}); + bdfObj.jmar = Mockito.mock(JAXBmar.class); + Mockito.doThrow(new JAXBException("test") ).when(bdfObj.jmar).marshal(logT, typeObj, Mockito.mock(StringWriter.class)); + String retVal = bdfObj.stringify(typeObj); + System.out.println(retVal); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + } + + @Test + public void testStringifyWriter() { + JAXBDF<JAXBmar> bdfObj = null; + try { + JAXBmar typeObj = new JAXBmar(new Class[] {this.getClass()}); + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF( envJaxb, new Class[] {this.getClass()}); + bdfObj.jmar = Mockito.mock(JAXBmar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(envJaxb).debug(); + Mockito.doReturn(this.getClass()).when(bdfObj.jmar).marshal(logT, typeObj, Mockito.mock(StringWriter.class)); + bdfObj.stringify(typeObj, Mockito.mock(StringWriter.class)); + } catch (APIException e) { + e.printStackTrace(); + } catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testStringifyWriterException() { + JAXBDF<JAXBmar> bdfObj = null; + try { + JAXBmar typeObj = new JAXBmar(new Class[] {this.getClass()}); + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF( envJaxb, new Class[] {this.getClass()}); + bdfObj.jmar = Mockito.mock(JAXBmar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(envJaxb).debug(); + StringWriter sw = Mockito.mock(StringWriter.class); + Mockito.doThrow(new JAXBException("test")).when(bdfObj.jmar).marshal(logT, typeObj, sw); + bdfObj.stringify(typeObj, sw); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + } + + @Test + public void testStringifyOS() { + JAXBDF<JAXBmar> bdfObj = null; + try { + JAXBmar typeObj = new JAXBmar(new Class[] {this.getClass()}); + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF( envJaxb, new Class[] {this.getClass()}); + bdfObj.jmar = Mockito.mock(JAXBmar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(envJaxb).debug(); + Mockito.doReturn(this.getClass()).when(bdfObj.jmar).marshal(logT, typeObj, Mockito.mock(OutputStream.class)); + bdfObj.stringify(typeObj, Mockito.mock(OutputStream.class)); + } catch (APIException e) { + e.printStackTrace(); + } catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testStringifyOsException() { + JAXBDF<JAXBmar> bdfObj = null; + try { + JAXBmar typeObj = new JAXBmar(new Class[] {this.getClass()}); + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF( envJaxb, new Class[] {this.getClass()}); + bdfObj.jmar = Mockito.mock(JAXBmar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(envJaxb).debug(); + OutputStream sw = Mockito.mock(OutputStream.class); + Mockito.doThrow(new JAXBException("test")).when(bdfObj.jmar).marshal(logT, typeObj, sw); + bdfObj.stringify(typeObj, sw); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + } + + @Test + public void testStringifyOptions() { + JAXBDF<JAXBmar> bdfObj = null; + try { + JAXBmar typeObj = new JAXBmar(new Class[] {this.getClass()}); + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF( envJaxb, new Class[] {this.getClass()}); + bdfObj.jmar = Mockito.mock(JAXBmar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(envJaxb).debug(); + Mockito.doReturn(this.getClass()).when(bdfObj.jmar).marshal(logT, typeObj, Mockito.mock(OutputStream.class)); + bdfObj.stringify(env, typeObj, true); + } catch (APIException e) { + e.printStackTrace(); + } catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testStringifyOSOptions() { + JAXBDF<JAXBmar> bdfObj = null; + try { + JAXBmar typeObj = new JAXBmar(new Class[] {this.getClass()}); + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF( envJaxb, new Class[] {this.getClass()}); + bdfObj.jmar = Mockito.mock(JAXBmar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + Mockito.doReturn(this.getClass()).when(bdfObj.jmar).marshal(logT, typeObj, Mockito.mock(OutputStream.class),true); + bdfObj.stringify(env, typeObj, Mockito.mock(OutputStream.class),true); + } catch (APIException e) { + e.printStackTrace(); + } catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testStringifyOsOptionsException() { + JAXBDF<JAXBmar> bdfObj = null; + try { + JAXBmar typeObj = new JAXBmar(new Class[] {this.getClass()}); + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF( envJaxb, new Class[] {this.getClass()}); + bdfObj.jmar = Mockito.mock(JAXBmar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + OutputStream sw = Mockito.mock(OutputStream.class); + Mockito.doThrow(new JAXBException("test")).when(bdfObj.jmar).marshal(logT, typeObj, sw,true); + bdfObj.stringify(env, typeObj, sw,true); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + } + @Test + public void testStringifySWOptions() { + JAXBDF<JAXBmar> bdfObj = null; + try { + JAXBmar typeObj = new JAXBmar(new Class[] {this.getClass()}); + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF( envJaxb, new Class[] {this.getClass()}); + bdfObj.jmar = Mockito.mock(JAXBmar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + Mockito.doReturn(this.getClass()).when(bdfObj.jmar).marshal(logT, typeObj, Mockito.mock(StringWriter.class),true); + bdfObj.stringify(env, typeObj, Mockito.mock(StringWriter.class),true); + } catch (APIException e) { + e.printStackTrace(); + } catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testStringifySWOptionsException() { + JAXBDF<JAXBmar> bdfObj = null; + try { + JAXBmar typeObj = new JAXBmar(new Class[] {this.getClass()}); + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF( envJaxb, new Class[] {this.getClass()}); + bdfObj.jmar = Mockito.mock(JAXBmar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + StringWriter sw = Mockito.mock(StringWriter.class); + Mockito.doThrow(new JAXBException("test")).when(bdfObj.jmar).marshal(logT, typeObj, sw,true); + bdfObj.stringify(env, typeObj, sw,true); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + } + + @Test + public void testObjectifyEnv() { + JAXBDF<JAXBumar> bdfObj = null; + try { + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF<JAXBumar>( envJaxb, new Class[] {this.getClass()}); + JAXBumar typeObj = new JAXBumar(new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + Mockito.doReturn(this.getClass()).when(bdfObj.jumar).unmarshal(logT, Mockito.mock(StringReader.class)); + + bdfObj.objectify(env, Mockito.mock(StringReader.class)); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + + } + @Test + public void testObjectifyEnvException() { + JAXBDF<JAXBumar> bdfObj = null; + try { + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF<JAXBumar>( envJaxb, new Class[] {this.getClass()}); + JAXBumar typeObj = new JAXBumar(new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + StringReader sr = Mockito.mock(StringReader.class); + Mockito.doThrow(new JAXBException("test")).when(bdfObj.jumar).unmarshal(logT, sr); + + bdfObj.objectify(env, sr); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + + } + + @Test + public void testObjectifyRdr() { + JAXBDF<JAXBumar> bdfObj = null; + try { + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF<JAXBumar>( envJaxb, new Class[] {this.getClass()}); + JAXBumar typeObj = new JAXBumar(new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + Mockito.doReturn(this.getClass()).when(bdfObj.jumar).unmarshal(logT, Mockito.mock(StringReader.class)); + + bdfObj.objectify( Mockito.mock(StringReader.class)); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + + } + @Test + public void testObjectifyRdrException() { + JAXBDF<JAXBumar> bdfObj = null; + try { + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF<JAXBumar>( envJaxb, new Class[] {this.getClass()}); + JAXBumar typeObj = new JAXBumar(new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(envJaxb).debug(); + StringReader sr = Mockito.mock(StringReader.class); + Mockito.doThrow(new JAXBException("test")).when(bdfObj.jumar).unmarshal(logT, sr); + + bdfObj.objectify(sr); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + + } + + @Test + public void testObjectifyEnvIS() { + JAXBDF<JAXBumar> bdfObj = null; + try { + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF<JAXBumar>( envJaxb, new Class[] {this.getClass()}); + JAXBumar typeObj = new JAXBumar(new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + Mockito.doReturn(this.getClass()).when(bdfObj.jumar).unmarshal(logT, Mockito.mock(InputStream.class)); + + bdfObj.objectify(env, Mockito.mock(InputStream.class)); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + + } + @Test + public void testObjectifyEnvISException() { + JAXBDF<JAXBumar> bdfObj = null; + try { + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF<JAXBumar>( envJaxb, new Class[] {this.getClass()}); + JAXBumar typeObj = new JAXBumar(new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + InputStream sr = Mockito.mock(InputStream.class); + Mockito.doThrow(new JAXBException("test")).when(bdfObj.jumar).unmarshal(logT, sr); + + bdfObj.objectify(env, sr); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + + } + + @Test + public void testObjectifyIs() { + JAXBDF<JAXBumar> bdfObj = null; + try { + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF<JAXBumar>( envJaxb, new Class[] {this.getClass()}); + JAXBumar typeObj = new JAXBumar(new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + Mockito.doReturn(this.getClass()).when(bdfObj.jumar).unmarshal(logT, Mockito.mock(InputStream.class)); + + bdfObj.objectify( Mockito.mock(InputStream.class)); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + + } + @Test + public void testObjectifyIsException() { + JAXBDF<JAXBumar> bdfObj = null; + try { + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF<JAXBumar>( envJaxb, new Class[] {this.getClass()}); + JAXBumar typeObj = new JAXBumar(new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(envJaxb).debug(); + InputStream sr = Mockito.mock(InputStream.class); + Mockito.doThrow(new JAXBException("test")).when(bdfObj.jumar).unmarshal(logT, sr); + + bdfObj.objectify(sr); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + + } + + @Test + public void testObjectifyEnvStr() { + JAXBDF<JAXBumar> bdfObj = null; + try { + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF<JAXBumar>( envJaxb, new Class[] {this.getClass()}); + JAXBumar typeObj = new JAXBumar(new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + Mockito.doReturn(this.getClass()).when(bdfObj.jumar).unmarshal(logT, "test"); + + bdfObj.objectify(env, "test"); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + + } + @Test + public void testObjectifyEnvStrException() { + JAXBDF<JAXBumar> bdfObj = null; + try { + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF<JAXBumar>( envJaxb, new Class[] {this.getClass()}); + JAXBumar typeObj = new JAXBumar(new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + InputStream sr = Mockito.mock(InputStream.class); + Mockito.doThrow(new JAXBException("test")).when(bdfObj.jumar).unmarshal(logT, "test"); + + bdfObj.objectify(env, "test"); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + + } + + @Test + public void testObjectifyStr() { + JAXBDF<JAXBumar> bdfObj = null; + try { + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF<JAXBumar>( envJaxb, new Class[] {this.getClass()}); + JAXBumar typeObj = new JAXBumar(new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(env).debug(); + Mockito.doReturn(this.getClass()).when(bdfObj.jumar).unmarshal(logT, "test"); + + bdfObj.objectify( "test"); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + + } + @Test + public void testObjectifyStrException() { + JAXBDF<JAXBumar> bdfObj = null; + try { + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF<JAXBumar>( envJaxb, new Class[] {this.getClass()}); + JAXBumar typeObj = new JAXBumar(new Class[] {this.getClass()}); + bdfObj.jumar = Mockito.mock(JAXBumar.class); + LogTarget logT = Mockito.mock(LogTarget.class); + Mockito.doReturn(logT).when(envJaxb).debug(); + InputStream sr = Mockito.mock(InputStream.class); + Mockito.doThrow(new JAXBException("test")).when(bdfObj.jumar).unmarshal(logT, "test"); + + bdfObj.objectify("test"); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } catch (JAXBException e) { + assertTrue(e.getMessage().contains("test")); + } + + } + + @Test + public void testTypeClass() { + JAXBDF<JAXBumar> bdfObj = null; + try { + EnvJAXB envJaxb = Mockito.mock(EnvJAXB.class); + bdfObj = new JAXBDF<JAXBumar>( envJaxb, new Class[] {this.getClass()}); + + Object obj = bdfObj.getTypeClass(); + assertFalse(obj instanceof JU_JAXBDF); + } catch (APIException e) { + assertTrue(e.getMessage().contains("test")); + } + + } +} |