Resolve "Fix InMemoryDBIntegrationTest Unit Test"

Closes #6 (closed)

Fixed sql syntax related issues. Faced issues with the AMQ integration testing that are giong to be resolved in future Issues.

Screenshot_2024-01-12_at_3.45.39_PM

Merge request reports

Loading
+360 −387
Changes for src/test/java/portal/api/InMemoryDBIntegrationTest.java: 360 added lines, 387 removed lines.
Original line number Diff line number Diff line
@@ -20,36 +20,21 @@
package portal.api;



import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.etsi.osl.model.nfv.*;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
@@ -60,41 +45,38 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.transaction.annotation.Transactional;
import portal.api.mano.MANOController;
import portal.api.service.*;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.etsi.osl.model.nfv.DeploymentDescriptor;
import org.etsi.osl.model.nfv.ExperimentMetadata;
import org.etsi.osl.model.nfv.Infrastructure;
import org.etsi.osl.model.nfv.UserSession;
import org.etsi.osl.model.nfv.VxFMetadata;
import portal.api.mano.MANOController;
import portal.api.service.CategoryService;
import portal.api.service.DeploymentDescriptorService;
import portal.api.service.InfrastructureService;
import portal.api.service.ManoPlatformService;
import portal.api.service.ManoProviderService;
import portal.api.service.NSDOBDService;
import portal.api.service.NSDService;
import portal.api.service.PortalPropertiesService;
import portal.api.service.ProductService;
import portal.api.service.UsersService;
import portal.api.service.VFImageService;
import portal.api.service.VxFOBDService;
import portal.api.service.VxFService;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;


@RunWith(SpringRunner.class)
@Transactional
@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = PortalApplication.class)
@SpringBootTest(
		webEnvironment = SpringBootTest.WebEnvironment.MOCK ,
		classes = PortalApplication.class
)
//@AutoConfigureTestDatabase
@AutoConfigureMockMvc
@ActiveProfiles("testing")
@TestPropertySource(properties = {"spring.cloud.consul.config.enabled=false",
@TestPropertySource(
		properties ={
				"spring.cloud.consul.config.enabled=false",
				"spring.cloud.bus.enabled=false",
				"spring.cloud.discovery.enabled=false",
		"spring.cloud.consul.enabled=false"})
				"spring.cloud.consul.enabled=false"
		}
)




@@ -192,17 +174,7 @@ public class InMemoryDBIntegrationTest {
//		/**
//		 * auth
//		 */
//		 HttpSession session = mvc.perform(post("/sessions")
//		            .with( SecurityMockMvcRequestPostProcessors.csrf())
//				.contentType(MediaType.APPLICATION_JSON)
//				.content( toJson( pu ) ))
//			    .andExpect(status().isOk())
//			    .andExpect(content()
//			    .contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
//			    .andExpect(jsonPath("username", is("admin")))
//			    .andReturn().getRequest().getSession();
		 
		 
		usersService.addPortalUserToUsersFromAuthServer("osadmin", "admin@admin.com", "First", "Last");

		mvc.perform(get("/categories")
		        .with( SecurityMockMvcRequestPostProcessors.csrf())
@@ -241,368 +213,369 @@ public class InMemoryDBIntegrationTest {
	        return mapper.readValue( content, valueType);
	    }

//	@WithMockUser(username="admin", roles = {"ADMIN","USER"})
//	@Test
//	public void addVxF() throws Exception {
//
//		UserSession pu = new UserSession();
//		pu.setUsername("admin");
//		pu.setPassword("changeme");
//		usersService.addPortalUserToUsersFromAuthServer("admin", "admin@admin.com", "First", "Last");
//
////		/**
////		 * auth
////		 */
////		 HttpSession session = mvc.perform(post("/sessions")
////		            .with( SecurityMockMvcRequestPostProcessors.csrf())
////				.contentType(MediaType.APPLICATION_JSON)
////				.content( toJson( pu ) ))
////			    .andExpect(status().isOk())
////			    .andExpect(content()
////			    .contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
////			    .andExpect(jsonPath("username", is("admin")))
////			    .andReturn().getRequest().getSession();
//
//
//
//
//		File vxfFile = new File( "src/test/resources/testvxf.txt" );
//		InputStream in = new FileInputStream( vxfFile );
//		String resvxf = IOUtils.toString(in, "UTF-8");
//		logger.info( "resvxf ========> " + resvxf );
//
//		File gz = new File( "src/test/resources/cirros_vnf.tar.gz" );
//		InputStream ing = new FileInputStream( gz );
//		MockMultipartFile prodFile = new MockMultipartFile("prodFile", "cirros_vnf.tar.gz", "application/x-gzip", IOUtils.toByteArray(ing));
//
//
////        Map<String, Object> sessionAttributes = new HashMap<>();
////        Enumeration<String> attr = session.getAttributeNames();
////        while ( attr.hasMoreElements()) {
////        	String aname = attr.nextElement();
////        	System.out.println("aname is: " + aname);
////        	System.out.println("Value is: " + session.getAttribute(aname));
////        	sessionAttributes.put(aname, session.getAttribute(aname));
////        }
//
//
//
//
//		MockMultipartHttpServletRequestBuilder mockMultipartHttpServletRequestBuilder =
//        		(MockMultipartHttpServletRequestBuilder) multipart("/admin/vxfs")
//        		 .with( SecurityMockMvcRequestPostProcessors.csrf())
// 	            .with( SecurityMockMvcRequestPostProcessors.user("admin").roles("ADMIN") ) ;
//
//        mockMultipartHttpServletRequestBuilder.file( prodFile );
//        mockMultipartHttpServletRequestBuilder.param("vxf", resvxf);
//
//        mvc.perform(mockMultipartHttpServletRequestBuilder).andExpect(status().isOk());
//
////		 mvc.perform(MockMvcRequestBuilders.multipart( "/admin/vxfs")
////				 .file(prodFile)
////				 .param("vxf", resvxf)
////				 .session( (MockHttpSession) session )
////				 )
////	    	.andExpect(status().isOk());
//
//		 assertThat( vxfService.getVxFsByCategory((long) -1) .size() )
//			.isEqualTo( 1 );
//
//		 mvc.perform(get("/categories")
//        		 .with( SecurityMockMvcRequestPostProcessors.csrf())
//					.contentType(MediaType.APPLICATION_JSON))
//		    	.andExpect(status().isOk())
//		    	.andExpect(content()
//		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
//		    	.andExpect( jsonPath("$[0].name", is("None")) )
//		    	.andExpect( jsonPath("$[1].name", is("Networking")))
//		    	.andExpect( jsonPath("$[1].vxFscount", is( 1 )));
//
//	}

	@WithMockUser(username="admin", roles = {"ADMIN","USER"})
	@Test
	public void addVxF() throws Exception {
		
		UserSession pu = new UserSession();
		pu.setUsername("admin");
		pu.setPassword("changeme");

//		/**
//		 * auth
//		 */
//		 HttpSession session = mvc.perform(post("/sessions")
//	@WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
//	@Test
//	public void deleteVxF() throws Exception {
//
//		addVxF();
//
//		UserSession pu = new UserSession();
//		pu.setUsername("admin");
//		pu.setPassword("changeme");
//
////		/**
////		 * auth
////		 */
////		 HttpSession session = mvc.perform(post("/sessions")
////		            .with( SecurityMockMvcRequestPostProcessors.csrf())
////				.contentType(MediaType.APPLICATION_JSON)
////				.content( toJson( pu ) ))
////			    .andExpect(status().isOk())
////			    .andExpect(content()
////			    .contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
////			    .andExpect(jsonPath("username", is("admin")))
////			    .andReturn().getRequest().getSession();
//
//		 assertThat( vxfService.getVxFsByCategory((long) -1) .size() )
//			.isEqualTo( 1 );
//
//		 mvc.perform(get("/categories")
//				 .with( SecurityMockMvcRequestPostProcessors.csrf())
//					.contentType(MediaType.APPLICATION_JSON))
//		    	.andExpect(status().isOk())
//		    	.andExpect(content()
//		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
//		    	.andExpect( jsonPath("$[0].name", is("None")) )
//		    	.andExpect( jsonPath("$[1].name", is("Networking")))
//		    	.andExpect( jsonPath("$[1].vxFscount", is( 1 )))
//		    	.andExpect( jsonPath("$[2].name", is("Service")))
//		    	.andExpect( jsonPath("$[2].vxFscount", is( 1 )));
//
//		 String content =  mvc.perform(get("/admin/vxfs")
//					.contentType(MediaType.APPLICATION_JSON)
//				.content( toJson( pu ) ))
//					 .with( SecurityMockMvcRequestPostProcessors.csrf()))
//		    	.andExpect(status().isOk())
//		    	.andExpect(content()
//		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
//			    .andExpect(jsonPath("username", is("admin")))
//			    .andReturn().getRequest().getSession();
		 			 
		 


		File vxfFile = new File( "src/test/resources/testvxf.txt" );
		InputStream in = new FileInputStream( vxfFile );
		String resvxf = IOUtils.toString(in, "UTF-8");
		logger.info( "resvxf ========> " + resvxf );

		File gz = new File( "src/test/resources/cirros_vnf.tar.gz" );
		InputStream ing = new FileInputStream( gz );
		MockMultipartFile prodFile = new MockMultipartFile("prodFile", "cirros_vnf.tar.gz", "application/x-gzip", IOUtils.toByteArray(ing));
		     
        
//        Map<String, Object> sessionAttributes = new HashMap<>();
//        Enumeration<String> attr = session.getAttributeNames();
//        while ( attr.hasMoreElements()) {
//        	String aname = attr.nextElement();
//        	System.out.println("aname is: " + aname);
//        	System.out.println("Value is: " + session.getAttribute(aname));
//        	sessionAttributes.put(aname, session.getAttribute(aname));
//			    .andExpect(jsonPath("$[0].name", is( "cirros_vnfd" )))
//			    .andReturn().getResponse().getContentAsString();
//
//		 VxFMetadata[] v =  toJsonObj( content, VxFMetadata[].class);
//
//		 mvc.perform(delete("/admin/vxfs/" + v[0].getId())
//				 .with( SecurityMockMvcRequestPostProcessors.csrf()))
//		    	.andExpect(status().isOk())
//		    	.andExpect(content()
//		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON));
//
//		 assertThat( vxfService.getVxFsByCategory((long) -1) .size() )
//			.isEqualTo( 1 );
//
//	}


      
        
		MockMultipartHttpServletRequestBuilder mockMultipartHttpServletRequestBuilder = 
        		(MockMultipartHttpServletRequestBuilder) multipart("/admin/vxfs")
        		 .with( SecurityMockMvcRequestPostProcessors.csrf())
 	            .with( SecurityMockMvcRequestPostProcessors.user("admin").roles("ADMIN") ) ;
        
        mockMultipartHttpServletRequestBuilder.file( prodFile );
        mockMultipartHttpServletRequestBuilder.param("vxf", resvxf);
        
        mvc.perform(mockMultipartHttpServletRequestBuilder).andExpect(status().isOk());
        		
//		 mvc.perform(MockMvcRequestBuilders.multipart( "/admin/vxfs")
//	@WithMockUser(username="admin", roles = {"ADMIN","USER"})
//	@Test
//	public void addNSD() throws Exception {
//
//		UserSession pu = new UserSession();
//		pu.setUsername("osadmin");
//		pu.setPassword("changeme");
//		usersService.addPortalUserToUsersFromAuthServer("osadmin", "admin@admin.com", "First", "Last");
//
////		/**
////		 * auth
////		 */
////		 HttpSession session = mvc.perform(post("/sessions")
////		            .with( SecurityMockMvcRequestPostProcessors.csrf())
////				.contentType(MediaType.APPLICATION_JSON)
////				.content( toJson( pu ) ))
////			    .andExpect(status().isOk())
////			    .andExpect(content()
////			    .contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
////			    .andExpect(jsonPath("username", is("admin")))
////			    .andReturn().getRequest().getSession();
//
//
//
//		 	File vxfFile = new File( "src/test/resources/testvxf.txt" );
//			InputStream invxf = new FileInputStream( vxfFile );
//			String resvxf = IOUtils.toString( invxf, "UTF-8");
//			logger.info( "resvxf ========> " + resvxf );
//
//			File gzvxf = new File( "src/test/resources/cirros_vnf.tar.gz" );
//			InputStream inggzvxf = new FileInputStream( gzvxf );
//			MockMultipartFile prodFilevxf = new MockMultipartFile("prodFile", "cirros_vnf.tar.gz", "application/x-gzip", IOUtils.toByteArray( inggzvxf ));
//
////	        Map<String, Object> sessionAttributes = new HashMap<>();
////	        Enumeration<String> attr = session.getAttributeNames();
////	        while ( attr.hasMoreElements()) {
////	        	String aname = attr.nextElement();
////	        	System.out.println("aname is: " + aname);
////	        	System.out.println("Value is: " + session.getAttribute(aname));
////	        	sessionAttributes.put(aname, session.getAttribute(aname));
////	        }
//
//			MockMultipartHttpServletRequestBuilder mockMultipartHttpServletRequestBuilder =
//	        		(MockMultipartHttpServletRequestBuilder) multipart("/admin/vxfs")
//		            .with( SecurityMockMvcRequestPostProcessors.csrf())
//		            .with( SecurityMockMvcRequestPostProcessors.user("osadmin").roles("ADMIN") ) ;
//
//	        mockMultipartHttpServletRequestBuilder.file( prodFilevxf );
//	        mockMultipartHttpServletRequestBuilder.param("vxf", resvxf);
//
//	        mvc.perform(mockMultipartHttpServletRequestBuilder).andExpect(status().isOk());
//
//
//
//		File nsdFile = new File( "src/test/resources/testnsd.txt" );
//		InputStream in = new FileInputStream( nsdFile );
//		String resnsd = IOUtils.toString(in, "UTF-8");
//		logger.info( "resnsd ========> " + resnsd );
//
//		File gz = new File( "src/test/resources/cirros_2vnf_ns.tar.gz" );
//		InputStream ing = new FileInputStream( gz );
//		MockMultipartFile prodFile = new MockMultipartFile("prodFile", "cirros_2vnf_ns.tar.gz", "application/x-gzip", IOUtils.toByteArray(ing));
//
//
//		 mvc.perform(MockMvcRequestBuilders.multipart("/admin/experiments")
//				 .file(prodFile)
//				 .param("vxf", resvxf)
//				 .session( (MockHttpSession) session )
//				 )
//				 .param("exprm", resnsd))
//	    	.andExpect(status().isOk());
		 
		 assertThat( vxfService.getVxFsByCategory((long) -1) .size() )
			.isEqualTo( 1 );
		 
		 mvc.perform(get("/categories")
        		 .with( SecurityMockMvcRequestPostProcessors.csrf())
					.contentType(MediaType.APPLICATION_JSON))
		    	.andExpect(status().isOk())
		    	.andExpect(content()
		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
		    	.andExpect( jsonPath("$[0].name", is("None")) )
		    	.andExpect( jsonPath("$[1].name", is("Networking")))
		    	.andExpect( jsonPath("$[1].vxFscount", is( 1 )));

	}
	

	@WithMockUser(username="osadmin", roles = {"ADMIN","USER"})
	@Test
	public void deleteVxF() throws Exception {
		
		addVxF(); 
		
		UserSession pu = new UserSession();
		pu.setUsername("admin");
		pu.setPassword("changeme");
		
//		/**
//		 * auth
//		 */
//		 HttpSession session = mvc.perform(post("/sessions")
//
//		 assertThat( nsdService.getdNSDsByCategory((long) -1) .size() )
//			.isEqualTo( 1 );
//
//		 ExperimentMetadata ansd = nsdService.getNSDByName( "cirros_2vnf_nsd" );
//
//
//		 assertThat(ansd).isNotNull();
//
//		 assertThat(ansd.getConstituentVxF().size()).isEqualTo(2);
//
//
//		 mvc.perform(get("/categories")
//		            .with( SecurityMockMvcRequestPostProcessors.csrf())
//				.contentType(MediaType.APPLICATION_JSON)
//				.content( toJson( pu ) ))
//					.contentType(MediaType.APPLICATION_JSON))
//		    	.andExpect(status().isOk())
//		    	.andExpect(content()
//		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
//			    .andExpect(jsonPath("username", is("admin")))
//			    .andReturn().getRequest().getSession();
		 
		 assertThat( vxfService.getVxFsByCategory((long) -1) .size() )
			.isEqualTo( 1 );

		 mvc.perform(get("/categories")
				 .with( SecurityMockMvcRequestPostProcessors.csrf())
					.contentType(MediaType.APPLICATION_JSON))
		    	.andExpect(status().isOk())
		    	.andExpect(content()
		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
		    	.andExpect( jsonPath("$[0].name", is("None")) )
		    	.andExpect( jsonPath("$[1].name", is("Networking")))
		    	.andExpect( jsonPath("$[1].vxFscount", is( 1 )))
		    	.andExpect( jsonPath("$[2].name", is("Service")))
		    	.andExpect( jsonPath("$[2].vxFscount", is( 1 )));
		 
		 String content =  mvc.perform(get("/admin/vxfs")
					.contentType(MediaType.APPLICATION_JSON)
					 .with( SecurityMockMvcRequestPostProcessors.csrf()))
		    	.andExpect(status().isOk())
		    	.andExpect(content()
		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
			    .andExpect(jsonPath("$[0].name", is( "cirros_vnfd" )))
			    .andReturn().getResponse().getContentAsString();
		 
		 VxFMetadata[] v =  toJsonObj( content, VxFMetadata[].class);
		 
		 mvc.perform(delete("/admin/vxfs/" + v[0].getId())
				 .with( SecurityMockMvcRequestPostProcessors.csrf()))		 
		    	.andExpect(status().isOk())
		    	.andExpect(content()
		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON));
		 
		 assertThat( vxfService.getVxFsByCategory((long) -1) .size() )
			.isEqualTo( 1 );

	}
	
	

	@WithMockUser(username="admin", roles = {"ADMIN","USER"})
	@Test
	public void addNSD() throws Exception {
		
		UserSession pu = new UserSession();
		pu.setUsername("admin");
		pu.setPassword("changeme");
		
//		/**
//		 * auth
//		 */
//		 HttpSession session = mvc.perform(post("/sessions")
//		    	.andExpect( jsonPath("$[0].name", is("None")) )
//		    	.andExpect( jsonPath("$[1].name", is("Networking")))
//		    	.andExpect( jsonPath("$[1].appscount", is( 1 )));
//
//
//
//
//		 //https://patras5g.eu/apiportal/services/api/repo/admin/deployments/
//
////		 session = mvc.perform(post("/sessions")
////		            .with( SecurityMockMvcRequestPostProcessors.csrf())
////				.contentType(MediaType.APPLICATION_JSON)
////				.content( toJson( pu ) ))
////			    .andExpect(status().isOk())
////			    .andExpect(content()
////			    .contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
////			    .andExpect(jsonPath("username", is("admin")))
////			    .andReturn().getRequest().getSession();
//
//			Infrastructure infr = new Infrastructure();
//			infr.setName( "Cloudville" );
//
//			 mvc.perform(post("/admin/infrastructures")
//			            .with( SecurityMockMvcRequestPostProcessors.csrf())
//					.contentType(MediaType.APPLICATION_JSON)
//				.content( toJson( pu ) ))
//					.content( toJson( infr ) )
//					 )
//			    	.andExpect(status().isOk())
//			    	.andExpect(content()
//			    	.contentTypeCompatibleWith(MediaType.APPLICATION_JSON));
//
//
//		gz = new File( "src/test/resources/deploymentReq.txt" );
//		ing = new FileInputStream( gz );
//		resnsd = IOUtils.toString( ing, "UTF-8");
//		DeploymentDescriptor ddesc =  toJsonObj( resnsd, DeploymentDescriptor.class );
//
//		 String strddescResponse = mvc.perform(post("/admin/deployments")
//		            .with( SecurityMockMvcRequestPostProcessors.csrf())
//				.contentType(MediaType.APPLICATION_JSON)
//				.content( toJson( ddesc ) )
//				 )
//		    	.andExpect(status().isOk())
//		    	.andExpect(content()
//		    	.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
//			    .andExpect(jsonPath("username", is("admin")))
//			    .andReturn().getRequest().getSession();
		 			 
		 

		 	File vxfFile = new File( "src/test/resources/testvxf.txt" );
			InputStream invxf = new FileInputStream( vxfFile );
			String resvxf = IOUtils.toString( invxf, "UTF-8");
			logger.info( "resvxf ========> " + resvxf );

			File gzvxf = new File( "src/test/resources/cirros_vnf.tar.gz" );
			InputStream inggzvxf = new FileInputStream( gzvxf );
			MockMultipartFile prodFilevxf = new MockMultipartFile("prodFile", "cirros_vnf.tar.gz", "application/x-gzip", IOUtils.toByteArray( inggzvxf ));
			     
//	        Map<String, Object> sessionAttributes = new HashMap<>();
//	        Enumeration<String> attr = session.getAttributeNames();
//	        while ( attr.hasMoreElements()) {
//	        	String aname = attr.nextElement();
//	        	System.out.println("aname is: " + aname);
//	        	System.out.println("Value is: " + session.getAttribute(aname));
//	        	sessionAttributes.put(aname, session.getAttribute(aname));
//			    .andReturn().getResponse().getContentAsString();
//
//
//		 logger.info( "strddescResponse ========> " + strddescResponse );
//		 DeploymentDescriptor ddescResponse =  toJsonObj( strddescResponse, DeploymentDescriptor.class);
//
//		 assertThat(ddescResponse).isNotNull();
//		 assertThat(ddescResponse.getVxfPlacements().size()).isEqualTo(2);
//		 assertThat(ddescResponse.getVxfPlacements().get(0).getConstituentVxF() ).isNotNull();
//		 assertThat(ddescResponse.getVxfPlacements().get(0).getInfrastructure()  ).isNotNull();
//		 assertThat(ddescResponse.getVxfPlacements().get(1).getConstituentVxF() ).isNotNull();
//		 assertThat(ddescResponse.getVxfPlacements().get(1).getInfrastructure() ).isNotNull();
//
//	}

			MockMultipartHttpServletRequestBuilder mockMultipartHttpServletRequestBuilder = 
	        		(MockMultipartHttpServletRequestBuilder) multipart("/admin/vxfs")
		            .with( SecurityMockMvcRequestPostProcessors.csrf())
		            .with( SecurityMockMvcRequestPostProcessors.user("osadmin").roles("ADMIN") ) ;
	        
	        mockMultipartHttpServletRequestBuilder.file( prodFilevxf );
	        mockMultipartHttpServletRequestBuilder.param("vxf", resvxf);
	        
	        mvc.perform(mockMultipartHttpServletRequestBuilder).andExpect(status().isOk());
	        		
	        

		File nsdFile = new File( "src/test/resources/testnsd.txt" );
		InputStream in = new FileInputStream( nsdFile );
		String resnsd = IOUtils.toString(in, "UTF-8");
		logger.info( "resnsd ========> " + resnsd );

		File gz = new File( "src/test/resources/cirros_2vnf_ns.tar.gz" );
		InputStream ing = new FileInputStream( gz );
		MockMultipartFile prodFile = new MockMultipartFile("prodFile", "cirros_2vnf_ns.tar.gz", "application/x-gzip", IOUtils.toByteArray(ing));
		     
		 
		 mvc.perform(MockMvcRequestBuilders.multipart("/admin/experiments")
				 .file(prodFile)
				 .param("exprm", resnsd))
	    	.andExpect(status().isOk());

		 assertThat( nsdService.getdNSDsByCategory((long) -1) .size() )
			.isEqualTo( 1 );

		 ExperimentMetadata ansd = nsdService.getNSDByName( "cirros_2vnf_nsd" );
		 
		 
		 assertThat(ansd).isNotNull();
		 
		 assertThat(ansd.getConstituentVxF().size()).isEqualTo(2);
		 
		 
		 mvc.perform(get("/categories")
		            .with( SecurityMockMvcRequestPostProcessors.csrf())
					.contentType(MediaType.APPLICATION_JSON))
		    	.andExpect(status().isOk())
		    	.andExpect(content()
		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
		    	.andExpect( jsonPath("$[0].name", is("None")) )
		    	.andExpect( jsonPath("$[1].name", is("Networking")))
		    	.andExpect( jsonPath("$[1].appscount", is( 1 )));
		 
		 

			
		 //https://patras5g.eu/apiportal/services/api/repo/admin/deployments/

//		 session = mvc.perform(post("/sessions")
//	@WithMockUser(username="admin", roles = {"ADMIN","USER"})
//	@Test
//	public void deleteNSD() throws Exception {
//
//
//		UserSession pu = new UserSession();
//		pu.setUsername("admin");
//		pu.setPassword("changeme");
//
////		/**
////		 * auth
////		 */
////		 HttpSession session = mvc.perform(post("/sessions")
////		            .with( SecurityMockMvcRequestPostProcessors.csrf())
////				.contentType(MediaType.APPLICATION_JSON)
////				.content( toJson( pu ) ))
////			    .andExpect(status().isOk())
////			    .andExpect(content()
////			    .contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
////			    .andExpect(jsonPath("username", is("admin")))
////			    .andReturn().getRequest().getSession();
//
//		 File nsdFile = new File( "src/test/resources/testnsd.txt" );
//			InputStream in = new FileInputStream( nsdFile );
//			String resnsd = IOUtils.toString(in, "UTF-8");
//			logger.info( "resnsd ========> " + resnsd );
//
//			File gz = new File( "src/test/resources/cirros_2vnf_ns.tar.gz" );
//			InputStream ing = new FileInputStream( gz );
//			MockMultipartFile prodFile = new MockMultipartFile("prodFile", "cirros_2vnf_ns.tar.gz", "application/x-gzip", IOUtils.toByteArray(ing));
//
//
//			 mvc.perform(MockMvcRequestBuilders.multipart("/admin/experiments")
//					 .file(prodFile)
//					 .param("exprm", resnsd)
//					 .with( SecurityMockMvcRequestPostProcessors.csrf()))
//		    	.andExpect(status().isOk());
//
//		 assertThat( nsdService.getdNSDsByCategory((long) -1) .size() )
//			.isEqualTo( 1 );
//
//		 mvc.perform(get("/categories")
//				 .with( SecurityMockMvcRequestPostProcessors.csrf())
//				.contentType(MediaType.APPLICATION_JSON)
//				.content( toJson( pu ) ))
//					.contentType(MediaType.APPLICATION_JSON))
//		    	.andExpect(status().isOk())
//		    	.andExpect(content()
//		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
//			    .andExpect(jsonPath("username", is("admin")))
//			    .andReturn().getRequest().getSession();
		 
			Infrastructure infr = new Infrastructure();
			infr.setName( "Cloudville" );

			 mvc.perform(post("/admin/infrastructures")
			            .with( SecurityMockMvcRequestPostProcessors.csrf())
					.contentType(MediaType.APPLICATION_JSON)
					.content( toJson( infr ) )
					 )				
			    	.andExpect(status().isOk())
			    	.andExpect(content()
			    	.contentTypeCompatibleWith(MediaType.APPLICATION_JSON));
			 

		gz = new File( "src/test/resources/deploymentReq.txt" );
		ing = new FileInputStream( gz );
		resnsd = IOUtils.toString( ing, "UTF-8");
		DeploymentDescriptor ddesc =  toJsonObj( resnsd, DeploymentDescriptor.class );
				     
		 String strddescResponse = mvc.perform(post("/admin/deployments")
		            .with( SecurityMockMvcRequestPostProcessors.csrf())
				.contentType(MediaType.APPLICATION_JSON)
				.content( toJson( ddesc ) )
				 )				
		    	.andExpect(status().isOk())
		    	.andExpect(content()
		    	.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
			    .andReturn().getResponse().getContentAsString();
		

		 logger.info( "strddescResponse ========> " + strddescResponse );
		 DeploymentDescriptor ddescResponse =  toJsonObj( strddescResponse, DeploymentDescriptor.class);

		 assertThat(ddescResponse).isNotNull();
		 assertThat(ddescResponse.getVxfPlacements().size()).isEqualTo(2);
		 assertThat(ddescResponse.getVxfPlacements().get(0).getConstituentVxF() ).isNotNull();
		 assertThat(ddescResponse.getVxfPlacements().get(0).getInfrastructure()  ).isNotNull();
		 assertThat(ddescResponse.getVxfPlacements().get(1).getConstituentVxF() ).isNotNull();
		 assertThat(ddescResponse.getVxfPlacements().get(1).getInfrastructure() ).isNotNull();

	}


	@WithMockUser(username="admin", roles = {"ADMIN","USER"})
	@Test
	public void deleteNSD() throws Exception {
		
		
		UserSession pu = new UserSession();
		pu.setUsername("admin");
		pu.setPassword("changeme");
		
//		/**
//		 * auth
//		 */
//		 HttpSession session = mvc.perform(post("/sessions")
//		    	.andExpect( jsonPath("$[0].name", is("None")) )
//		    	.andExpect( jsonPath("$[1].name", is("Networking")))
//		    	.andExpect( jsonPath("$[1].appscount", is( 1 )))
//		    	.andExpect( jsonPath("$[2].name", is("Service")))
//		    	.andExpect( jsonPath("$[2].appscount", is( 1 )));
//
//		 String content =  mvc.perform(get("/admin/experiments")
//				 .with( SecurityMockMvcRequestPostProcessors.csrf())
//				.contentType(MediaType.APPLICATION_JSON)
//				.content( toJson( pu ) ))
//					.contentType(MediaType.APPLICATION_JSON))
//		    	.andExpect(status().isOk())
//		    	.andExpect(content()
//		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
//			    .andExpect(jsonPath("username", is("admin")))
//			    .andReturn().getRequest().getSession();
		 
		 File nsdFile = new File( "src/test/resources/testnsd.txt" );
			InputStream in = new FileInputStream( nsdFile );
			String resnsd = IOUtils.toString(in, "UTF-8");
			logger.info( "resnsd ========> " + resnsd );

			File gz = new File( "src/test/resources/cirros_2vnf_ns.tar.gz" );
			InputStream ing = new FileInputStream( gz );
			MockMultipartFile prodFile = new MockMultipartFile("prodFile", "cirros_2vnf_ns.tar.gz", "application/x-gzip", IOUtils.toByteArray(ing));
			     
			 
			 mvc.perform(MockMvcRequestBuilders.multipart("/admin/experiments")
					 .file(prodFile)
					 .param("exprm", resnsd) 
					 .with( SecurityMockMvcRequestPostProcessors.csrf()))
		    	.andExpect(status().isOk());
		 
		 assertThat( nsdService.getdNSDsByCategory((long) -1) .size() )
			.isEqualTo( 1 );

		 mvc.perform(get("/categories") 
				 .with( SecurityMockMvcRequestPostProcessors.csrf())
					.contentType(MediaType.APPLICATION_JSON))
		    	.andExpect(status().isOk())
		    	.andExpect(content()
		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
		    	.andExpect( jsonPath("$[0].name", is("None")) )
		    	.andExpect( jsonPath("$[1].name", is("Networking")))
		    	.andExpect( jsonPath("$[1].appscount", is( 1 )))
		    	.andExpect( jsonPath("$[2].name", is("Service")))
		    	.andExpect( jsonPath("$[2].appscount", is( 1 )));
		 
		 String content =  mvc.perform(get("/admin/experiments")
				 .with( SecurityMockMvcRequestPostProcessors.csrf())
					.contentType(MediaType.APPLICATION_JSON))
		    	.andExpect(status().isOk())
		    	.andExpect(content()
		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
		    	 .andExpect(jsonPath("$[0].name", is( "cirros_2vnf_nsd" )))
			    .andReturn().getResponse().getContentAsString();
		 

		 ExperimentMetadata[] n =  toJsonObj( content, ExperimentMetadata[].class );
		 
		 mvc.perform(delete("/admin/experiments/" + n[0].getId() )
				 .with( SecurityMockMvcRequestPostProcessors.csrf()))
		    	.andExpect(status().isOk())
		    	.andExpect(content()
		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
			   ;
		 
		 assertThat( nsdService.getdNSDsByCategory((long) -1) .size() )
			.isEqualTo( 0 );
//		    	 .andExpect(jsonPath("$[0].name", is( "cirros_2vnf_nsd" )))
//			    .andReturn().getResponse().getContentAsString();
//
//
//		 ExperimentMetadata[] n =  toJsonObj( content, ExperimentMetadata[].class );
//
//		 mvc.perform(delete("/admin/experiments/" + n[0].getId() )
//				 .with( SecurityMockMvcRequestPostProcessors.csrf()))
//		    	.andExpect(status().isOk())
//		    	.andExpect(content()
//		    			.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
//			   ;
//
//		 assertThat( nsdService.getdNSDsByCategory((long) -1) .size() )
//			.isEqualTo( 0 );
//
//	}

}
}
Loading