Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
♻️ 📝 Done refactor and updated the documentation
  • Loading branch information
gniadeck committed Sep 30, 2022
commit 9aefb8516b50e8d76160497720eb4dd077ed0765
6 changes: 3 additions & 3 deletions src/main/java/dev/wms/pwrapi/api/ParkingAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public class ParkingAPI {
private ParkingService parkingService;

@GetMapping
@Operation(summary = "Returns processed data from iparking.pwr.edu.pl", description = "You can use it to get data from iparking.pwr.edu.pl in simple format")
@Operation(summary = "Returns processed data from skd.pwr.edu.pl", description = "You can use it to get data from skd.pwr.edu.pl in simple format")
public ResponseEntity<List<Parking>> getProcessedParkingInfo() throws JsonProcessingException, IOException{
List<Parking> result = parkingService.getParkingData();
return ResponseEntity.status(HttpStatus.OK).body(result);

}

@GetMapping("/raw")
@Operation(summary = "Returns raw request data from iparking.pwr.edu.pl",
description = "You can use it to get raw, unprocessed data from iparking.pwr.edu.pl Especially useful when you want to graph parking usage")
@Operation(summary = "Returns history data from skd.pwr.edu.pl",
description = "You can use it to get parking history data from last 24h")
public ResponseEntity<List<ParkingWithHistory>> getRawParkingInfo() throws IOException{
return ResponseEntity.status(HttpStatus.OK).body(parkingService.getRawParkingData());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/wms/pwrapi/dao/parking/ParkingDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface ParkingDAO {
List<Parking> getProcessedParkingInfo() throws IOException;

/**
* Returns unprocessed response from server. Works like proxy
* Returns parking information and array containing history of parking places for the last 24 hours
* @return JSON Response from server
* @throws IOException When deserialization goes wrong
*/
Expand Down
52 changes: 22 additions & 30 deletions src/main/java/dev/wms/pwrapi/dao/parking/ParkingDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@
import java.util.regex.Pattern;

import dev.wms.pwrapi.dto.parking.ParkingWithHistory;
import dev.wms.pwrapi.utils.http.HttpUtils;
import org.jetbrains.annotations.NotNull;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Repository;

import dev.wms.pwrapi.dto.parking.Parking;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

@Repository
public class ParkingDAOImpl implements ParkingDAO {

public static final String PARKING_WRONSKIEGO = "Parking Wrońskiego";
public static final String C_13 = "C13";
public static final String D_20 = "D20";
public static final String GEOCENTRUM = "Geocentrum";
public static final String ARCHITEKTURA = "Architektura";

@Override
public List<Parking> getProcessedParkingInfo() throws IOException{
return parseProcessed(fetchParkingWebsite());
Expand All @@ -36,38 +40,22 @@ public List<ParkingWithHistory> getRawParkingData() throws IOException{
private Document fetchParkingWebsite() throws IOException {
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
Request request = new Request.Builder()
.url("https://skd.pwr.edu.pl/")
.addHeader("sec-ch-ua", "\"Google Chrome\";v=\"105\", \"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"105\"")
.addHeader("sec-ch-ua-mobile", "?0")
.addHeader("sec-ch-ua-platform", "\"macOS\"")
.addHeader("Upgrade-Insecure-Requests", "1")
.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36")
.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
.addHeader("Sec-Fetch-Site", "same-origin")
.addHeader("Sec-Fetch-Mode", "navigate")
.addHeader("Sec-Fetch-User", "?1")
.addHeader("Sec-Fetch-Dest", "document")
.addHeader("host", "skd.pwr.edu.pl")
.build();
Response response = client.newCall(request).execute();
return Jsoup.parse(response.body().string());
return HttpUtils.makeRequestWithClientAndGetDocument(client, "https://skd.pwr.edu.pl/");
}

private List<Parking> parseProcessed(Element page){
List<Parking> result = new ArrayList<>();
Matcher matcher = Pattern.compile("\"type\":\"put\",\"key\":\"text\",\"feat\":7,\"value\":\"\\d+").matcher(page.html());
matcher.find();

result.add(new Parking("Parking Wrońskiego", getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
result.add(new Parking(PARKING_WRONSKIEGO, getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
matcher.find();
result.add(new Parking("C13", getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
result.add(new Parking(C_13, getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
matcher.find();
result.add(new Parking("D20", getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
result.add(new Parking(D_20, getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
matcher.find();
result.add(new Parking("Geocentrum", getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
result.add(new Parking(GEOCENTRUM, getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
matcher.find();
result.add(new Parking("Architektura", getMeasurmentTime(), getPlacesFromResponse(matcher), 0));
result.add(new Parking(ARCHITEKTURA, getMeasurmentTime(), getPlacesFromResponse(matcher), 0));

return result;
}
Expand All @@ -76,19 +64,23 @@ private List<ParkingWithHistory> parseWithDetails(Element page){
List<ParkingWithHistory> result = new ArrayList<>();
Matcher matcher = Pattern.compile("(?<=\\\\\"data\\\\\":\\[)(.*?)(?=\\])").matcher(page.html());
matcher.find();
result.add(new ParkingWithHistory("Parking Wrońskiego", getMeasurmentTime(), matcher.group()));
result.add(new ParkingWithHistory(PARKING_WRONSKIEGO, getMeasurmentTime(), sanitizeArray(matcher.group())));
matcher.find();
result.add(new ParkingWithHistory("C13", getMeasurmentTime(), matcher.group()));
result.add(new ParkingWithHistory(C_13, getMeasurmentTime(), sanitizeArray(matcher.group())));
matcher.find();
result.add(new ParkingWithHistory("D20", getMeasurmentTime(), matcher.group()));
result.add(new ParkingWithHistory(D_20, getMeasurmentTime(), sanitizeArray(matcher.group())));
matcher.find();
result.add(new ParkingWithHistory("Geocentrum", getMeasurmentTime(), matcher.group()));
result.add(new ParkingWithHistory(GEOCENTRUM, getMeasurmentTime(), sanitizeArray(matcher.group())));
matcher.find();
result.add(new ParkingWithHistory("Architektura", getMeasurmentTime(), matcher.group()));
result.add(new ParkingWithHistory(ARCHITEKTURA, getMeasurmentTime(), sanitizeArray(matcher.group())));

return result;
}

private String sanitizeArray(String array){
return "[" + array + "]";
}

@NotNull
private String getMeasurmentTime() {
return LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
Expand Down

This file was deleted.

16 changes: 14 additions & 2 deletions src/test/java/dev/wms/pwrapi/parking/ParkingDAOTest.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
package dev.wms.pwrapi.parking;

import dev.wms.pwrapi.dao.parking.ParkingDAO;
import dev.wms.pwrapi.dto.parking.Parking;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

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

@SpringBootTest
public class ParkingDAOTest {

@Autowired
private ParkingDAO parkingDAO;

/**
* Because of migration to SKD service, trend feature is no longer supported, and trend should be equal
* to zero
* @throws IOException
*/
@Test
public void getProcessedParkingInfoShouldWork() throws IOException {
System.out.println(parkingDAO.getRawParkingData());
public void trendShouldAlwaysBeZero() throws IOException {
List<Parking> result = parkingDAO.getProcessedParkingInfo();
for(Parking parking : result){
assertEquals(0, parking.getTrend());
}
}

}
31 changes: 26 additions & 5 deletions src/test/java/dev/wms/pwrapi/parking/ParkingTests.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package dev.wms.pwrapi.parking;

import dev.wms.pwrapi.api.ParkingAPI;
import io.restassured.http.ContentType;
import io.restassured.module.mockmvc.RestAssuredMockMvc;

import org.junit.jupiter.api.BeforeAll;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import static org.hamcrest.Matchers.*;

Expand Down Expand Up @@ -51,10 +48,34 @@ public void allParkingNamesShouldBeAccessible(){

@Test
public void responseTypeShouldBeJSON(){

get("api/parking").then()
.contentType(ContentType.JSON);
}

@Test
public void detailsEndpointShouldContainAllParkingNames(){

get("api/parking/raw").then()
.body("[0].name", oneOf("D20", "Parking Wrońskiego", "C13", "Geocentrum", "Architektura"))
.body("[1].name", oneOf("D20", "Parking Wrońskiego", "C13", "Geocentrum", "Architektura"))
.body("[2].name", oneOf("D20", "Parking Wrońskiego", "C13", "Geocentrum", "Architektura"))
.body("[3].name", oneOf("D20", "Parking Wrońskiego", "C13", "Geocentrum", "Architektura"))
.body("[4].name", oneOf("D20", "Parking Wrońskiego", "C13", "Geocentrum", "Architektura"));

}

@Test
public void historyEndpointShouldBeAnArrayWrappedInString(){
get("api/parking/raw").then()
.body("[0].history", getArrayMatcher())
.body("[1].history", getArrayMatcher())
.body("[2].history", getArrayMatcher())
.body("[3].history", getArrayMatcher())
.body("[4].history", getArrayMatcher());
}

private Matcher<String> getArrayMatcher(){
return allOf(startsWith("["), endsWith("]"), not(containsString("(")), not(containsString(")")));
}


Expand Down
41 changes: 0 additions & 41 deletions src/test/java/dev/wms/pwrapi/parking/ParkingUtilsTests.java

This file was deleted.