/**
 * Copyright Microsoft Corporation
 * 
 * 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.
 */
package com.microsoft.windowsazure.management.sql;


import com.microsoft.windowsazure.management.sql.models.ServerCreateParameters;
import com.microsoft.windowsazure.management.sql.models.ServerCreateResponse;
import com.microsoft.windowsazure.management.sql.models.ServerListResponse;
import com.microsoft.windowsazure.management.sql.models.ServerListResponse.Server;

import java.io.IOException;
import java.util.Iterator;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.junit.*;
import org.xml.sax.SAXException;
import static org.junit.Assert.*;

import com.microsoft.windowsazure.exception.ServiceException;

public class SqlServerIntegrationTest extends SqlManagementIntegrationTestBase {

	@Before
	public void setup() throws Exception
	{
		createService();
	}
	
	@After
	public void tearDown() throws Exception 
	{
		ServerOperations serverOperations = sqlManagementClient.getServersOperations();
		ServerListResponse serverListResponse = serverOperations.list();
        Iterator<Server> serverList = serverListResponse.iterator();
        while (serverList.hasNext())
        {
        	Server nextServer = serverList.next();
        	serverOperations.delete(nextServer.getName());
        }
	}
	
    @Test
    public void createSqlServerWithRequiredParameters() throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException 
    {
    	//arrange 
    	String testAdministratorUserName = "testadminname";
    	String testPassword = "testpassword8!";
    	String testLocation = "West US";
    	
    	// act
    	ServerOperations serverOperations = sqlManagementClient.getServersOperations();
    	ServerCreateParameters serverCreateParameters = new ServerCreateParameters();
    	serverCreateParameters.setAdministratorUserName(testAdministratorUserName);
    	serverCreateParameters.setAdministratorPassword(testPassword);
    	serverCreateParameters.setLocation(testLocation);
        ServerCreateResponse serverCreateResponse = serverOperations.create(serverCreateParameters);
        String serverName = serverCreateResponse.getServerName();
        
        
        //assert
        
        ServerListResponse serverListResponse = serverOperations.list();
        Iterator<Server> serverList = serverListResponse.iterator();
        Server createdServer = null;
        while (serverList.hasNext())
        {
            Server nextServer = serverList.next();
            if (nextServer.getName().equals(serverName))
            {
            	createdServer = nextServer;
            }
        }
        assertNotNull(createdServer);
        assertEquals(testAdministratorUserName, createdServer.getAdministratorUserName());
        assertEquals(testLocation, createdServer.getLocation());
        
    }
    
    @Test
    public void deleteServerSuccess() throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException
    {
    	//arrange 
    	String testAdministratorUserName = "testadminname";
    	String testPassword = "testpassword8!";
    	String testLocation = "West US";
    	
    	// act
    	ServerOperations serverOperations = sqlManagementClient.getServersOperations();
    	ServerCreateParameters serverCreateParameters = new ServerCreateParameters();
    	serverCreateParameters.setAdministratorUserName(testAdministratorUserName);
    	serverCreateParameters.setAdministratorPassword(testPassword);
    	serverCreateParameters.setLocation(testLocation);
        ServerCreateResponse serverCreateResponse = serverOperations.create(serverCreateParameters);
        String serverName = serverCreateResponse.getServerName();
        serverOperations.delete(serverName);
        
        // assert
        ServerListResponse serverListResponse = serverOperations.list();
        Iterator<Server> serverList = serverListResponse.iterator();
        Server createdServer = null;
        while (serverList.hasNext())
        {
            Server nextServer = serverList.next();
            if (nextServer.getName().equals(serverName))
            {
            	createdServer = nextServer;
            }
        }
        assertNull(createdServer);
        
    }
    
    
    
    
    
    
}
@@ -1009,17 +1009,69 @@
 til.
-Iterator;
+ArrayList;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0A
 %0Aimp
@@ -1410,446 +1410,367 @@
 %7B%0A%0A%09
-@Before%0A%09public void setup() throws Exception%0A%09%7B%0A%09%09createService();%0A%09%7D%0A%09%0A%09@After%0A%09public void tearDown() throws Exception %0A%09%7B%0A%09%09ServerOperations serverOperations = sqlManagementClient.getServersOperations();%0A%09%09ServerListResponse serverListResponse = serverOperations.list();%0A        Iterator%3CServer%3E serverList = serverListResponse.iterator();%0A        while (serverList.hasNext())%0A        %7B%0A        %09Server nextServer = serverList.next();
+private static List%3CString%3E serverToBeRemoved = new ArrayList%3CString%3E();%0A%09private static ServerOperations serverOperations = sqlManagementClient.getServersOperations();%0A%09%0A%09@Before%0A%09public void setup() throws Exception%0A%09%7B%0A%09%09createService();%0A%09%7D%0A%09%0A%09@After%0A%09public void tearDown() throws Exception %0A%09%7B%0A        for (String serverName : serverToBeRemoved)%0A        %7B
 %0A   
@@ -1799,36 +1799,26 @@
 .delete(
-nextS
+s
 erver
-.get
 Name
-()
 );%0A     
@@ -2181,93 +2181,8 @@
 act%0A
-    %09ServerOperations serverOperations = sqlManagementClient.getServersOperations();%0A
     
@@ -2631,32 +2631,66 @@
 Name();%0A        
+serverToBeRemoved.add(serverName);
 %0A        %0A      

check in server integration tests.