Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #38 from TouristGuideApp/flickrTest
Browse files Browse the repository at this point in the history
Flickr test
  • Loading branch information
ErminaTrontzou authored Dec 11, 2022
2 parents a8a11aa + d69e776 commit 611aa94
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
testImplementation "org.mockito:mockito-core:3.+"
}


test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public ResponseEntity<?> getSpecificImage(@PathVariable Long id) {

@RequestMapping(value = "/getByTitle/{title}", method = RequestMethod.GET)
public ResponseEntity<?> imageByTitle(@PathVariable String title,
@RequestParam(value="page", defaultValue = "0") Integer page,
@RequestParam(value="size", defaultValue = "8") Integer size){
@RequestParam(value="page", defaultValue = "0") int page,
@RequestParam(value="size", defaultValue = "8") int size){
return imageService.getImageByTitle(title,page,size);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class FlickrService {
TagService tagService;
FlickrResponses flickrResponses = new FlickrResponses();

public FlickrService(){}

public ResponseEntity<?> getNewImagesForLocation(String location){
List<Image> imageList = getFlickr(location);
if(imageList.size()!=0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package gr.thegoodsideofe1.tourguide.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import gr.thegoodsideofe1.tourguide.entities.Image;
import gr.thegoodsideofe1.tourguide.services.FlickrService;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(FlickrController.class)
public class FlickrControllerTest {

@InjectMocks
FlickrController flickrController;

@Mock
FlickrService flickrService;

@Autowired
private MockMvc mockMvc;

@Autowired
ObjectMapper mapper;

ObjectMapper objectMapper = new ObjectMapper();
ObjectWriter objectWriter = objectMapper.writer();

Image firstImage = new Image(1,"url_l1","desc1","thessaloniki","12345","67890",1000,"owner1","dateTaken1","url_t1");
Image secondImage = new Image(2,"url_l2","desc2","athens","12345","67890",2000,"owner2","dateTaken2","url_t2");
Image thirdImage = new Image(3,"url_l3","desc3","thessaloniki","12345","67890",3000,"owner3","dateTaken3","url_t3");


@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(flickrController).build();
}

@Test
public void getAllImagesFromFlickrAPI() throws Exception {
List<Image> mockImage = new ArrayList<>();
mockImage.add(firstImage);
mockImage.add(secondImage);
mockImage.add(thirdImage);

when(flickrService.getNewImagesForLocation(firstImage.getTitle())).thenReturn(new ResponseEntity(firstImage, HttpStatus.OK));

mockMvc.perform(MockMvcRequestBuilders
.get("http://localhost:8080/api/v1/flickr/getByTitle/thessaloniki")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.title", Matchers.is("thessaloniki")));
}

@Test
public void saveAndImageViaFlickrController() throws Exception {
HashMap<String, String> returnedMessage = new HashMap<>();
when(flickrController.imageToSave(firstImage)).thenReturn(new ResponseEntity(returnedMessage, HttpStatus.OK));

String requestJson = objectWriter.writeValueAsString(firstImage);
mockMvc.perform(MockMvcRequestBuilders
.post("http://localhost:8080/api/v1/flickr/imageToSave")
.contentType(MediaType.APPLICATION_JSON)
.content(requestJson))
.andExpect(status().isOk());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package gr.thegoodsideofe1.tourguide.responses;

import org.junit.jupiter.api.Test;

import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.*;

class FlickrResponses_Test {
FlickrResponses flickrResponses = new FlickrResponses();

@Test
void problemOnImageImport() {
HashMap<String, String> returnedHashMap = flickrResponses.problemOnImageImport();
if (!returnedHashMap.containsKey("status")){
assertTrue(false, "Problem On HashMap");
}
if (!returnedHashMap.containsKey("message")){
assertTrue(false, "Problem On HashMap");
}
}

@Test
void imageSaved() {
HashMap<String, String> returnedHashMap = flickrResponses.imageSaved();
if (!returnedHashMap.containsKey("status")){
assertTrue(false, "Problem On HashMap");
}
if (!returnedHashMap.containsKey("message")){
assertTrue(false, "Problem On HashMap");
}
}
}

0 comments on commit 611aa94

Please sign in to comment.