Search in sources :

Example 1 with UserDTO

use of org.c4sg.dto.UserDTO in project c4sg-services by Code4SocialGood.

the class UserController method createUser.

@RequestMapping(method = RequestMethod.POST)
@ApiOperation(value = "Add a new user")
public UserDTO createUser(@ApiParam(value = "User object to return", required = true) @RequestBody CreateUserDTO createUserDTO) {
    //calculate lat and long
    try {
        UserDTO userDTO = userService.createUser(createUserDTO);
        GeoCodeUtil geoCodeUtil = new GeoCodeUtil(userDTO);
        Map<String, BigDecimal> geoCode = geoCodeUtil.getGeoCode();
        userDTO.setLatitude(geoCode.get("lat"));
        userDTO.setLongitude(geoCode.get("lon"));
        return userService.saveUser(userDTO);
    } catch (Exception e) {
        throw new NotFoundException("Error getting geocode");
    }
}
Also used : GeoCodeUtil(org.c4sg.util.GeoCodeUtil) CreateUserDTO(org.c4sg.dto.CreateUserDTO) UserDTO(org.c4sg.dto.UserDTO) NotFoundException(org.c4sg.exception.NotFoundException) BigDecimal(java.math.BigDecimal) NotFoundException(org.c4sg.exception.NotFoundException) IOException(java.io.IOException)

Example 2 with UserDTO

use of org.c4sg.dto.UserDTO in project c4sg-services by Code4SocialGood.

the class UserControllerTest method getMockUsers.

/*
	 //Testing POST
	 @Test	 
	 public void testCreateUser() throws Exception {
	 
		 UserDTO user = new UserDTO();
		 user.setEmail("test1Email@gmail.com");
		 
		 when(userServiceMock.saveUser(user)).thenReturn(user);
		 
		 this.mockMvc.perform(post("/api/users")
				 .contentType(MediaType.APPLICATION_JSON)
				 .content(asJsonString(user)))
		 .andExpect(status().isCreated())
		 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
		 .andExpect(jsonPath("$.email",is("test1Email@gmail.com")))
		 ;
		 
		 verify(userServiceMock, times(1)).saveUser(user);
	     verifyNoMoreInteractions(userServiceMock);	 
	 }
	 
	 @Test	 
	 public void testGetActiveUsers() throws Exception {
	 
		 List<UserDTO> activeUsers = getMockUsers("Active");
		 
		 when(userServiceMock.findActiveUsers()).thenReturn(activeUsers);
		 
		 this.mockMvc.perform(get("/api/users/active")
				 .accept(MediaType.parseMediaType("application/json;charset=UTF-8")))	
		 .andExpect(status().isOk())
		 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
		 .andExpect(jsonPath("$", hasSize(2)))
		 .andExpect(jsonPath("$[0].id",is(1)))
		 .andExpect(jsonPath("$[0].github",is(1)))
		 .andExpect(jsonPath("$[0].userName",is("test1UserName")))
		 .andExpect(jsonPath("$[0].firstName",is("test1FirstName")))
		 .andExpect(jsonPath("$[0].lastName",is("test1LastName")))
		 .andExpect(jsonPath("$[0].email",is("test1Email@gmail.com")))
		 .andExpect(jsonPath("$[0].phone",is("9542234567")))		 
		 .andExpect(jsonPath("$[0].status",is("A")))
		 .andExpect(jsonPath("$[0].role",is("V")))
		 .andExpect(jsonPath("$[0].state",is("CA")))
		 .andExpect(jsonPath("$[0].country",is("USA")))
		 .andExpect(jsonPath("$[1].id",is(3)))
		 .andExpect(jsonPath("$[1].github",is(3)))
		 .andExpect(jsonPath("$[1].userName",is("dev3UserName")))
		 .andExpect(jsonPath("$[1].firstName",is("dev3FirstName")))
		 .andExpect(jsonPath("$[1].lastName",is("dev3LastName")))
		 .andExpect(jsonPath("$[1].email",is("dev3Email@gmail.com")))
		 .andExpect(jsonPath("$[1].phone",is("9542234567")))		 
		 .andExpect(jsonPath("$[1].status",is("A")))
		 .andExpect(jsonPath("$[1].role",is("D")))
		 .andExpect(jsonPath("$[1].state",is("CA")))
		 .andExpect(jsonPath("$[1].country",is("USA")));
		 
		 verify(userServiceMock, times(1)).findActiveUsers();
	     verifyNoMoreInteractions(userServiceMock);
		 
	 }
	 
	 @Test	
	 public void testGetUserById() throws Exception {
	 
		//Mock user data - user with id = 1
 		 UserDTO userById = getMockUsers("ById").get(0);
		 
		 when(userServiceMock.findById(1)).thenReturn(userById);
		 
		 this.mockMvc.perform(get("/api/users/{id}",1)
				 .accept(MediaType.parseMediaType("application/json;charset=UTF-8")))	
		 .andExpect(status().isOk())
		 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
		 .andExpect(jsonPath("$.id",is(1)))
		 .andExpect(jsonPath("$.github",is(1)))
		 .andExpect(jsonPath("$.userName",is("test1UserName")))
		 .andExpect(jsonPath("$.firstName",is("test1FirstName")))
		 .andExpect(jsonPath("$.lastName",is("test1LastName")))
		 .andExpect(jsonPath("$.email",is("test1Email@gmail.com")))
		 .andExpect(jsonPath("$.phone",is("9542234567")))		 
		 .andExpect(jsonPath("$.status",is("A")))
		 .andExpect(jsonPath("$.role",is("V")))
		 .andExpect(jsonPath("$.state",is("CA")))
		 .andExpect(jsonPath("$.country",is("USA")));
		 
		 verify(userServiceMock, times(1)).findById(1);
	     verifyNoMoreInteractions(userServiceMock);
		 
	 }
	 
	 @Test	 
	 public void testGetDevelopers()
	 {
		 //code is commented because findDevelopers() method in UserService returns User instead of UserDTO
		 
		//get mock users
		 List<UserDTO> developers = getMockUsers("Developers");
		 
		 when(userServiceMock.findDevelopers()).thenReturn(developers);
		 
		 this.mockMvc.perform(get("/api/users/developers")
				 .accept(MediaType.parseMediaType("application/json;charset=UTF-8")))	
		 .andExpect(status().isOk())
		 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
		 .andExpect(jsonPath("$",hasSize(1)))
		 .andExpect(jsonPath("$[0].github",is(3)))
		 .andExpect(jsonPath("$[0].userName",is("dev3UserName")))
		 .andExpect(jsonPath("$[0].firstName",is("dev3FirstName")))
		 .andExpect(jsonPath("$[0].lastName",is("dev3LastName")))
		 .andExpect(jsonPath("$[0].email",is("dev3Email@gmail.com")))
		 .andExpect(jsonPath("$[0].phone",is("9542234567")))		 
		 .andExpect(jsonPath("$[0].status",is("A")))
		 .andExpect(jsonPath("$[0].role",is("D")))
		 .andExpect(jsonPath("$[0].state",is("CA")))
		 .andExpect(jsonPath("$[0].country",is("USA")));
		 
		 verify(userServiceMock, times(1)).findDevelopers();
	     verifyNoMoreInteractions(userServiceMock);
	 }

	 //Testing PUT	 
	 @Test
	 public void testUpdateUser() throws Exception {
		 
		 //get mock data
		 UserDTO user = getMockUsers("Update").get(0);
		 
		 when(userServiceMock.saveUser(user)).thenReturn(user);
		 
		 this.mockMvc.perform(put("/api/users")
				 .contentType(MediaType.APPLICATION_JSON)
				 .content(asJsonString(user)))
		 .andExpect(status().isOk())
		 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
		 .andExpect(jsonPath("$.id",is(1)))
		 .andExpect(jsonPath("$.github",is(1)))
		 .andExpect(jsonPath("$.userName",is("test1UserName")))
		 .andExpect(jsonPath("$.firstName",is("test1FirstName")))
		 .andExpect(jsonPath("$.lastName",is("test1LastName")))
		 .andExpect(jsonPath("$.email",is("test1Email@gmail.com")))
		 .andExpect(jsonPath("$.phone",is("9542234567")))		 
		 .andExpect(jsonPath("$.status",is("A")))
		 .andExpect(jsonPath("$.role",is("V")))
		 .andExpect(jsonPath("$.state",is("CA")))
		 .andExpect(jsonPath("$.country",is("USA")));
		 
		 verify(userServiceMock, times(1)).saveUser(user);
	     verifyNoMoreInteractions(userServiceMock);
				 
	 }
	 
	 //Testing DELETE
	 @Test	 
	 public void testDeleteUser() throws Exception
	 {
		 //get mock data
		 UserDTO user = getMockUsers("Delete").get(0);
		 
		 doNothing().when(userServiceMock).deleteUser(user.getId());
		 
		 this.mockMvc.perform(delete("/api/users/{id}",user.getId())
				 .accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
		 .andExpect(status().isOk());
		 
		 verify(userServiceMock, times(1)).deleteUser(user.getId());
	     verifyNoMoreInteractions(userServiceMock);
	 }
	 
*/
//Private methods	 
List<UserDTO> getMockUsers(String condition) {
    //creating mock data
    //Role - Volunteer and Status - Active
    UserDTO userDto = new UserDTO();
    /*
		 userDto.setCountry("USA");
		 userDto.setDisplayFlag("");
		 userDto.setEmail("test1Email@gmail.com");
		 userDto.setFirstName("test1FirstName");
		 userDto.setGithub(1);
		 userDto.setId(1);
		 userDto.setIntroduction("Full stack developer with 3 years of industry experience");
		 userDto.setLastName("test1LastName");
		 userDto.setLatitude("40.366633");
		 userDto.setLinked_inurl("https://www.linkedin.com/feed/");
		 userDto.setLongitude("74.640832");
		 userDto.setPersonal_web_site("www.mtest.blogspot.com");
		 userDto.setPhone("9542234567");
		 userDto.setResume("Thi$ is a s@mple resume with sPecial ch@r@cters!");
		 userDto.setRole("V");
		 userDto.setSkill1("java");
		 userDto.setSkill2("ruby");
		 userDto.setSkill3("perl");
		 userDto.setSkill4("spring");
		 userDto.setSkill5("struts");
		 userDto.setState("CA");
		 userDto.setStatus("A");
		 userDto.setUserName("test1UserName");
		 userDto.setZip("70701");*/
    //Role - Volunteer and Status - pending
    UserDTO userDto2 = new UserDTO();
    /*
		 userDto2.setCountry("USA");
		 userDto2.setDisplayFlag("");
		 userDto2.setEmail("test2Email@gmail.com");
		 userDto2.setFirstName("test2FirstName");
		 userDto2.setGithub(2);
		 userDto2.setId(2);
		 userDto2.setIntroduction("Full stack developer with 3 years of industry experience");
		 userDto2.setLastName("test2LastName");
		 userDto2.setLatitude("40.366633");
		 userDto2.setLinked_inurl("https://www.linkedin.com/feed/");
		 userDto2.setLongitude("74.640832");
		 userDto2.setPersonal_web_site("www.mtest.blogspot.com");
		 userDto2.setPhone("9542234567");
		 userDto2.setResume("Thi$ is a s@mple resume with sPecial ch@r@cters!");
		 userDto2.setRole("V");
		 userDto2.setSkill1("java");
		 userDto2.setSkill2("spring");
		 userDto2.setSkill3("REST");
		 userDto2.setSkill4("MySql");
		 userDto2.setSkill5("Git");
		 userDto2.setState("CA");
		 userDto2.setStatus("P");
		 userDto2.setUserName("test2UserName");
		 userDto2.setZip("70701");*/
    //Role - Developer and Status - Active
    UserDTO userDto3 = new UserDTO();
    /*
		 userDto3.setCountry("USA");
		 userDto3.setDisplayFlag("");
		 userDto3.setEmail("dev3Email@gmail.com");
		 userDto3.setFirstName("dev3FirstName");
		 userDto3.setGithub(3);
		 userDto3.setId(3);
		 userDto3.setIntroduction("Full stack developer with 3 years of industry experience");
		 userDto3.setLastName("dev3LastName");
		 userDto3.setLatitude("40.366633");
		 userDto3.setLinked_inurl("https://www.linkedin.com/feed/");
		 userDto3.setLongitude("74.640832");
		 userDto3.setPersonal_web_site("www.mtest.blogspot.com");
		 userDto3.setPhone("9542234567");
		 userDto3.setResume("Thi$ is a s@mple resume with sPecial ch@r@cters!");
		 userDto3.setRole("D");
		 userDto3.setSkill1("java");
		 userDto3.setSkill2("spring");
		 userDto3.setSkill3("REST");
		 userDto3.setSkill4("MySql");
		 userDto3.setSkill5("Git");
		 userDto3.setState("CA");
		 userDto3.setStatus("A");
		 userDto3.setUserName("dev3UserName");
		 userDto3.setZip("70701");*/
    //Role - Volunteer and Status - Deleted
    UserDTO userDto4 = new UserDTO();
    if (condition == "Active") {
        return Arrays.asList(userDto, userDto3);
    } else if (condition == "Developers") {
        return Arrays.asList(userDto3);
    } else if (condition == "ById") {
        return Arrays.asList(userDto);
    } else if (condition == "Create") {
        return Arrays.asList(userDto);
    } else if (condition == "Update") {
        return Arrays.asList(userDto);
    } else if (condition == "Delete") {
        return Arrays.asList(userDto);
    } else {
        //returns all users
        return Arrays.asList(userDto, userDto2, userDto3, userDto4);
    }
}
Also used : UserDTO(org.c4sg.dto.UserDTO)

Aggregations

UserDTO (org.c4sg.dto.UserDTO)2 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 CreateUserDTO (org.c4sg.dto.CreateUserDTO)1 NotFoundException (org.c4sg.exception.NotFoundException)1 GeoCodeUtil (org.c4sg.util.GeoCodeUtil)1