package org.sagebionetworks.audit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import org.sagebionetworks.repo.model.ACCESS_TYPE;
import org.sagebionetworks.repo.model.UserProfile;
import org.sagebionetworks.repo.model.audit.AccessRecord;
import org.sagebionetworks.repo.model.audit.ObjectRecord;
import org.sagebionetworks.repo.model.audit.ResourceAccessRecord;
import org.sagebionetworks.schema.adapter.JSONObjectAdapterException;
import org.sagebionetworks.schema.adapter.org.json.EntityFactory;

/**
 * 
 * @author jmhill
 *
 */
public class AuditTestUtils {

	/**
	 * Create a list for use in testing that is sorted on timestamp
	 * @param count
	 * @param startTimestamp
	 * @return
	 */
	public static List<AccessRecord> createAccessRecordList(int count, long startTimestamp){
		List<AccessRecord> list = new LinkedList<AccessRecord>();
		for(int i=0; i<count; i++){
			AccessRecord ar = new AccessRecord();
			ar.setUserId((long) i);
			ar.setElapseMS((long) (10*i));
			ar.setTimestamp(startTimestamp+i);
			ar.setMethod(Method.values()[i%4].toString());
			if(i%2 > 0){
				ar.setSuccess(true);
				ar.setResponseStatus(201L);
			}else{
				ar.setSuccess(false);
				ar.setResponseStatus(401L);
			}
			ar.setRequestURL("/url/"+i);
			ar.setSessionId(UUID.randomUUID().toString());
			ar.setHost("localhost:8080");
			ar.setOrigin("http://www.example-social-network.com");
			ar.setUserAgent("The bat-mobile OS");
			ar.setThreadId(Thread.currentThread().getId());
			ar.setVia("1 then two");
			ar.setStack("stack");
			ar.setInstance("0001");
			ar.setVmId("vmId");
			ar.setQueryString("value=bar");
			ar.setReturnObjectId("syn123");
			list.add(ar);
		}
		return list;
	}
	
	enum Method{
		GET,POST,PUT,DELETE
	}

	/**
	 * create and return numberOfRecords ResourceAccessRecords
	 */
	public static List<ResourceAccessRecord> createResourceAccessRecordList(int numberOfRecords) {
		List<ResourceAccessRecord> list = new ArrayList<ResourceAccessRecord>();
		for (int i = 0; i < numberOfRecords; i++) {
			ResourceAccessRecord newRecord = new ResourceAccessRecord();
			newRecord.setAccessType(ACCESS_TYPE.READ);
			newRecord.setChangeNumber(-1L);
			newRecord.setPrincipalId(-1L);

			list.add(newRecord);
		}
		return list;
	}

	public static List<ObjectRecord> createUserProfileObjectRecordList(int numberOfRecords) {
		UserProfile up = new UserProfile();
		up.setCompany("Sage");
		up.setEmail("employee@sagebase.org");
		up.setEmails(Arrays.asList("employee@sagebase.org", "employee@gmail.com", "employee@yahoo.com"));
		up.setLocation("Seattle");
		List<ObjectRecord> list = new ArrayList<ObjectRecord>();
		for (int i = 0; i < numberOfRecords; i++) {
			ObjectRecord newRecord = new ObjectRecord();
			try {
				newRecord.setJsonString(EntityFactory.createJSONStringForEntity(up));
			} catch (JSONObjectAdapterException e) {
				newRecord.setJsonString("");
			}
			newRecord.setJsonClassName(up.getClass().getSimpleName());
			newRecord.setTimestamp(System.currentTimeMillis());
			list.add(newRecord);
		}
		return list;
	}
}

@@ -170,60 +170,8 @@
 %0D%0A%0D%0A
-import org.sagebionetworks.repo.model.ACCESS_TYPE;%0D%0A
 impo
@@ -340,75 +340,8 @@
 d;%0D%0A
-import org.sagebionetworks.repo.model.audit.ResourceAccessRecord;%0D%0A
 impo
@@ -1758,538 +1758,8 @@
 %0D%0A%0D%0A
-%09/**%0D%0A%09 * create and return numberOfRecords ResourceAccessRecords%0D%0A%09 */%0D%0A%09public static List%3CResourceAccessRecord%3E createResourceAccessRecordList(int numberOfRecords) %7B%0D%0A%09%09List%3CResourceAccessRecord%3E list = new ArrayList%3CResourceAccessRecord%3E();%0D%0A%09%09for (int i = 0; i %3C numberOfRecords; i++) %7B%0D%0A%09%09%09ResourceAccessRecord newRecord = new ResourceAccessRecord();%0D%0A%09%09%09newRecord.setAccessType(ACCESS_TYPE.READ);%0D%0A%09%09%09newRecord.setChangeNumber(-1L);%0D%0A%09%09%09newRecord.setPrincipalId(-1L);%0D%0A%0D%0A%09%09%09list.add(newRecord);%0D%0A%09%09%7D%0D%0A%09%09return list;%0D%0A%09%7D%0D%0A%0D%0A
 %09pub

remove unused method