#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, os, shutil, time, requests, json


USERDIR = "/mnt2/users/"
URL = "http://10.17.0.85:8091/"
_HEADERS = {'Content-type': 'application/json'}
TOOL = 'wcrft2'
_SAMPLEDIR = './testdata/txt/sample01.txt'
def go():
	print "Test 1 Booking"

	r = requests.post(URL+"booking", data=json.dumps({"username" : "default"}), headers=_HEADERS)
	response = json.loads(r.text)
	if response['status'] != 'OK':
		print "Test 1 faild "+response['value']
	else:
		print "Test 1 success"
	

	print "Test 2 Confirmation"
	sampleFile = open(_SAMPLEDIR, 'ra')
	content = sampleFile.read()
	sampleFile.close()
	files = [USERDIR+"default/"+TOOL+"_testfile_"+str(num) for num in range (1,2)]
	for f in files:
		print f
		openFile = open(f, 'a')
		openFile.write(content)
		openFile.close()
	r = requests.post(URL+"confirmation", data=json.dumps({"data": {"tool":TOOL,"files":files,"options":{"model":"xyz"}}}), headers=_HEADERS)
	response = json.loads(r.text)
	if response['status'] != 'OK':
		print "Test 2 faild "+response['value']
	else:
		print "Test 2 success"

	print "Test 3 Checking"
	for token in response['value']:
		r = requests.post(URL+"checking", data=json.dumps({"token": token, "tool": TOOL}), headers=_HEADERS)
		response = json.loads(r.text)
		while response['status'] != 'DONE':
			time.sleep(1)
			print str(token) +" "+ str(r.text)
			r = requests.post(URL+"checking", data=json.dumps({"token": token, "tool": TOOL}), headers=_HEADERS)
                	response = json.loads(r.text)		

if __name__ == '__main__':
	go()
