Skip to content

Commit

Permalink
Merge branch 'develop' into LANG_012b
Browse files Browse the repository at this point in the history
  • Loading branch information
eder13 authored Jun 3, 2020
2 parents 1d2071d + 4905fe3 commit 8791772
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 30 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
-->
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.2</version>
</dependency>

<!--
<dependency>
<groupId>junit</groupId>
Expand Down Expand Up @@ -90,7 +96,7 @@
<artifactId>jackson-core</artifactId>
</dependency>


<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class Beans implements WebMvcConfigurer{
public class InterfaceLangConfig implements WebMvcConfigurer{

@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
Expand All @@ -23,7 +24,7 @@ public LocaleResolver localeResolver() {
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
lci.setParamName("lang"); // change interface language
return lci;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.vocabularytrainer.project.db.VocabularyEntries;
import com.vocabularytrainer.project.db.VocabularyRepository; // Repository Interface

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -34,13 +36,35 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
/**
* Presentation Layer with functionality as RESTful web services
*
* We don't have a "standalone" service layer -> do directly in Repository using CRUD
*
* As soon as you need to deal with access rights, it's no longer a matter of routing requests
* from the controller directly to the repository, but checking access and filtering data as well.
* Requests may need validation and consistency checks before hitting the database, rules and
*
* When to use @Service Layers?
* If you have an API which performs some logic before querying your repository then this should be in a separate service class.
* Because as the "Single responsibility principle" states, the controller's single responsibility should only handle requests.
* The Service layer's single responsibility is to do any logic required with the data received by the Controller.
* Example of implementing @Service class: https://howtodoinjava.com/spring-boot2/spring-boot-crud-hibernate/
* Service classes are used to write business logic.
*
* We are building behind REST (representational state transfer) -> Programming paradigm for distributed systems
* A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data.
*
* */

@Controller //@Controller also makes it automatically a bean
public class MVCController {

/* Setup access Database Repository Interface */
// constructor based dependency injection -> from import com.vocabularytrainer.project.db.VocabularyRepository
VocabularyRepository vocabularyRepository;
public MVCController(VocabularyRepository vocabularyRepository) {
// client service
this.vocabularyRepository = vocabularyRepository;
}

Expand Down Expand Up @@ -323,7 +347,7 @@ public String getEnglishStartPage() {
public String getFrenchStartPage() {
return "?lang=fr";
}

/*
@GetMapping("/user/?lang=de")
public String getGermanPage() {
return "user/?lang=de";
Expand All @@ -336,6 +360,7 @@ public String getEnglishPage() {
public String getFrenchPage() {
return "user/?lang=fr";
}
*/

//Testing Mode
@GetMapping("/user/test")
Expand Down Expand Up @@ -457,7 +482,6 @@ public String userTestRepetition(Model model) {
return "user/test/testingmode_repetition";
}


/* Export and Download vocabularies as CSV */
@GetMapping("user/export_vocabularies")
public ResponseEntity<?> export()
Expand Down Expand Up @@ -531,11 +555,14 @@ public ResponseEntity<?> export()

/* Importing CSV */
/// TODO: GetMapping for import_vocab.html
@GetMapping("user/import")
//@GetMapping("user/import")
/*
public String showImportView()
{
return "user/import_vocab";
}
*/

/// TODO: PostMapping for import_vocab.html - the Read-Process (import CSV to Database)

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,25 @@

import java.util.List;

/**
* JpaRepository = Sub Class of CrudRepository
* CrudRepository - "connection layer" - from spring data representation
* This allows us to use CRUD Operations (Create, Read, Update and Delete)
*
* Alternatively, we could've setup our own "connection layer" by applying DAO principles (data access objects)
*
* Persistence = JPA -> ORM (object relative mapping) standard in a relational database
*
* The repository's single responsibility is to query the data base.
*
* */

// with this, we can make the connection to the database - "connection layer"
// JPA = Java Persistence API for managing relational data (=Database)
// We use JPQL (="SQL" queries in Java) to make queries

// JPA repositories are not picked up by component scans since they are just
// interfaces whos concrete classes are created dynamically as beans by Spring Data provided -> Bean
public interface VocabularyRepository extends JpaRepository<VocabularyEntries, Integer> {

/* Queries */
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db_vocabularytrainer?useLegacyDatetimeCode=false&serverTimezone=CET
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_vocabularytrainer?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username=student
spring.datasource.password=student
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id;user;german_word;engl_trans;rating;tag
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
id;user;german_word;engl_trans;rating;tag
2;user;essen;eat;0;sda
3;user;schön;beautiful;0;adjective
4;user;Kraftwerk;"power plant";4;noun
5;user;sjdalsdj;asdasd;0;
4 changes: 4 additions & 0 deletions src/main/resources/templates/user/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@
<li class="nav-item">
<a class="nav-link" href="/user/export_vocabularies"><span th:text="#{downloadvocab}"></span></a>
</li>

<!--
<li class="nav-item">
<a class="nav-link" href="user/import"><span th:text="#{importvocab}"></span></a>
</li>
-->

<li class="nav-item">
<a class="nav-link" href="/logout"><span th:text="#{logout}"></span></a>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@
<li class="nav-item">
<a class="nav-link" href="/user/export_vocabularies"><span th:text="#{downloadvocab}"></span></a>
</li>
<!--
<li class="nav-item">
<a class="nav-link" href="user/import"><span th:text="#{importvocab}"></span></a>
</li>
-->
<li class="nav-item">
<a class="nav-link" href="/logout"><span th:text="#{logout}"></span></a>
</li>
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/templates/user/study_interface_german.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@
<li class="nav-item">
<a class="nav-link" href="/user/export_vocabularies"><span th:text="#{downloadvocab}"></span></a>
</li>
<!--
<li class="nav-item">
<a class="nav-link" href="user/import"><span th:text="#{importvocab}"></span></a>
</li>
-->
<li class="nav-item">
<a class="nav-link" href="/logout"><span th:text="#{logout}"></span></a>
</li>
Expand Down
72 changes: 72 additions & 0 deletions src/test/java/com/vocabularytrainer/project/CSVWriterTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.vocabularytrainer.project;


import com.vocabularytrainer.project.CSVParser.CSVWriter;
import com.vocabularytrainer.project.db.VocabularyRepository;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

@SpringBootTest
public class CSVWriterTests {

@Mock
private VocabularyRepository vocabularyRepository; // MVCController is dependent on VocabularyRepository -> MockBean VocabularyRepository

@Mock
private UserDetails userDetails;

@InjectMocks
private CSVWriter csvWriter;

private String savedFilePath;

@Before
public void before() {

MockitoAnnotations.initMocks(this);

when(this.userDetails.getUsername()).thenReturn("TestUser");

String filepath = "src/main/resources/templates/user/csv-output/";
String filename = filepath + "export_vocabularies_" + userDetails.getUsername() + ".csv";

File file = new File(filename);
savedFilePath = file.getAbsolutePath();
}

@Test
public void testPath() {

//System.out.println(savedFilePath);

assertTrue(savedFilePath.endsWith("/Users/Simon/git-repos/ASD-Morning-4/src/main/resources/templates/user/csv-output/export_vocabularies_TestUser.csv"));

}

@Test
public void testFileCreation()
{

List<Integer> ids = Arrays.asList();

File file = csvWriter.writeAllEntriesToCSV(vocabularyRepository, userDetails, savedFilePath, ids);

assertNotNull(file);

//System.out.print(file.toString());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
@SpringBootTest
class VocabularyTrainerApplicationTests {

/** A simple sanity check test that will fail if the application context cannot start (build in) */

@Test
void contextLoads() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
* Tests if Spring is configured and all dependencies (db, etc.) are accessible.
* Test 1: If Spring is able to create an MVCController and the VocabularyRepository
* Test 2: If the Security Context succeeds
*/

// Diese Annotation markiert, dass diese Klasse SpringBoot-spezifische Tests
Expand All @@ -22,7 +22,8 @@ public class VocabularyTrainerConfigTests {
// Eine Autowired-Annotationen bewirkt eine automatische Verdrahtung - wie der Name bereits verrät.
// D. h. Diese Methode wird ausgelöst, wenn eine Instanz von der Klasse erzeugt wird, in der sie
// enthalten ist.
@Autowired

@Autowired // = dependency injection
private MVCController controller;

@Autowired
Expand All @@ -33,13 +34,13 @@ public class VocabularyTrainerConfigTests {
// wird eine auftretende Exception nicht in dieser Methode behandelt, sondern
// an die aufrunfende Methode übergeben.
public void testLoadContext() throws Exception {

// "assertNotNull" überprüft, ob ein Objekt null "ist" oder nicht. Wenn es den Wert null
// enthält, wirft (throws) es einen "AssertionError".
assertNotNull(this.controller);
assertNotNull(this.vocabularyRepository);
}


@Test
public void testSecurityContext() throws Exception {
assertNotNull(SecurityContextHolder.getContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/** test: testGetUserAddVocabulary */

@SpringBootTest
public class MVCControllerNoSecurityTest {

Expand All @@ -32,6 +34,7 @@ public class MVCControllerNoSecurityTest {
private Model model;



// Ein globaler Test-String
private static final String username = "John Test";

Expand Down
Loading

0 comments on commit 8791772

Please sign in to comment.