/** * This work was created by participants in the DataONE project, and is * jointly copyrighted by participating institutions in DataONE. For * more information on DataONE, see our web site at http://dataone.org. * * Copyright ${year} * * 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. * * $Id$ */ package org.dataone.audit; import static org.junit.Assert.fail; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import org.apache.commons.collections.CollectionUtils; import org.apache.log4j.Logger; import org.dataone.client.CNode; import org.dataone.client.audit.AuditCNode; import org.dataone.service.exceptions.BaseException; import org.dataone.service.types.v1.Node; import org.dataone.service.types.v1.NodeType; import org.dataone.service.types.v1.ObjectInfo; import org.dataone.service.types.v1.ObjectList; import org.dataone.service.types.v1.util.NodelistUtil; import org.dataone.service.util.DateTimeMarshaller; import org.junit.Test; /** * The goal here is to test synchronization of metadata created on a MN * with a CN. Synchronization is a CN scheduled cron job, so there is no * trigger for synchronization. We have to wait for it to happen :-) * The testing approach is: * 1. create a new data on a MN * 2. periodically poll the CN for a period greater than the cron interval * checking for presence of the new object there using: * a). getSystemMetadata * b). resolve * c). search * * @author rnahf * */ public class ProductionContentCheckingTools { static Logger logger = Logger.getLogger(ProductionContentCheckingTools.class.getName()); private String currentUrl; // @Rule // public ErrorCollector errorCollector = new ErrorCollector(); private Set listToSet(List list) { Set set = new HashSet(); Iterator it = list.iterator(); while (it.hasNext()) { set.add(it.next()); it.remove(); } return set; } private Set buildSerializedObjectInfoSet_cs(ObjectList ol) { Set oiSet = new HashSet(); for (ObjectInfo oi: ol.getObjectInfoList()) { oiSet.add( String.format("%s\t%s\t%d\t%s\t%s\t%s", oi.getChecksum().getAlgorithm(), oi.getChecksum().getValue(), oi.getSize(), oi.getIdentifier().getValue(), oi.getFormatId().getValue(), DateTimeMarshaller.serializeDateToUTC(oi.getDateSysMetadataModified()) ) ); } return oiSet; } /** * this test is sensitive to the size of the CN objectList, memory-wise * @throws IOException */ @Test public void compareCnObjectLists_identifiers() throws IOException { compareCnObjectLists(false); } /** * this test is sensitive to the size of the CN objectList, memory-wise * @throws IOException */ @Test public void compareCnObjectLists_objectInfos() throws IOException { compareCnObjectLists(true); } protected void compareCnObjectLists(boolean listObjectInfo) throws IOException { boolean pass = true; String rrCNbaseUrl = "https://cn.dataone.org/cn"; AuditCNode rrCn = null; try { rrCn = new AuditCNode(rrCNbaseUrl); Set cnSet = NodelistUtil.selectNodes(rrCn.listNodes(),NodeType.CN); Date now = new Date(); Date toDate = new Date(now.getTime() - 10 * 60 * 1000); System.out.println("date: " + now); HashMap> olMap = new HashMap>(); Set superSet = new HashSet(); for (Node n : cnSet) { String url = n.getBaseURL(); // skip the round-robin cn if (url.equals(rrCNbaseUrl)) continue; System.out.println(url + ": doing listObjects()"); try { List idList = listObjectInfo ? AuditTools.listObjectInfos(url, null, false, false) : AuditTools.listIdentifiers(url, null, false); System.out.println(url + ": adding list to set"); Set oiSet = listToSet(idList); olMap.put(url, oiSet); System.out.println(url + ": adding set to superset"); superSet.addAll(oiSet); System.out.println(String.format("cn: %s ol size: %d",url, olMap.get(url).size())); } catch (BaseException be) { pass = false; logger.warn("problem getting an ObjectList from the cn. " + be.getClass() + ": " + be.getDescription(), be); } } Object[] urls = olMap.keySet().toArray(); System.out.println(); for (int i=0; i cnSet = NodelistUtil.selectNodes(rrCn.listNodes(),NodeType.CN); Date now = new Date(); Date toDate = new Date(now.getTime() - 10 * 60 * 1000); System.out.println("date: " + now); HashMap> olMap = new HashMap>(); Set superSet = new TreeSet(); for (Node n : cnSet) { String url = n.getBaseURL(); // skip the round-robin cn if (url.equals("https://cn.dataone.org/cn")) continue; System.out.println(url + ": doing listObjects()"); try { CNode cn = new CNode(url); List oiList = APITestUtils.pagedListObjects(cn, null, null, null, null, null, null).getObjectInfoList(); System.out.println(" ObjectList size = " + oiList.size()); List idList = new ArrayList(); List csList = new ArrayList(); for (ObjectInfo oi : oiList) { idList.add(oi.getIdentifier().getValue()); csList.add(oi.getChecksum().getAlgorithm() + ":" + oi.getChecksum().getValue()); } Map idCardMap = CollectionUtils.getCardinalityMap(idList); Map csCardMap = CollectionUtils.getCardinalityMap(csList); System.out.println(" # identifiers = " + idCardMap.size()); for (String key : idCardMap.keySet()) { if (idCardMap.get(key) > 1) { System.out.println(String.format(" %d of %s", idCardMap.get(key), key)); } } System.out.println(" # checksums = " + csCardMap.size()); for (String key : csCardMap.keySet()) { if (csCardMap.get(key) > 1) { System.out.println(String.format(" %d of %s", csCardMap.get(key), key)); } } } catch (BaseException be) { System.out.println("problem getting an ObjectList from the cn. " + be.getClass() + ": " + be.getDescription()); } } Object[] urls = olMap.keySet().toArray(); System.out.println(); for (int i=0; i