diff --git a/client/src/main/java/client/utils/ServerUtilsExample.java b/client/src/main/java/client/utils/ServerUtilsExample.java deleted file mode 100644 index bc4a187..0000000 --- a/client/src/main/java/client/utils/ServerUtilsExample.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2021 Delft University of Technology - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package client.utils; - -import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; - -import java.net.ConnectException; -import java.util.List; - -import jakarta.ws.rs.core.GenericType; -import org.glassfish.jersey.client.ClientConfig; - -import commons.Quote; -import jakarta.ws.rs.ProcessingException; -import jakarta.ws.rs.client.ClientBuilder; -import jakarta.ws.rs.client.Entity; - -public class ServerUtilsExample { - - private static final String SERVER = "http://localhost:8080/"; - -// public void getQuotesTheHardWay() throws IOException, URISyntaxException { -// var url = new URI("http://localhost:8080/api/quotes").toURL(); -// var is = url.openConnection().getInputStream(); -// var br = new BufferedReader(new InputStreamReader(is)); -// String line; -// while ((line = br.readLine()) != null) { -// System.out.println(line); -// } -// } - - public List getQuotes() { - return ClientBuilder.newClient(new ClientConfig()) // - .target(SERVER).path("api/quotes") // - .request(APPLICATION_JSON) // - .get(new GenericType>() {}); - } - - public Quote addQuote(Quote quote) { - return ClientBuilder.newClient(new ClientConfig()) // - .target(SERVER).path("api/quotes") // - .request(APPLICATION_JSON) // - .post(Entity.entity(quote, APPLICATION_JSON), Quote.class); - } - - public boolean isServerAvailable() { - try { - ClientBuilder.newClient(new ClientConfig()) // - .target(SERVER) // - .request(APPLICATION_JSON) // - .get(); - } catch (ProcessingException e) { - if (e.getCause() instanceof ConnectException) { - return false; - } - } - return true; - } -} \ No newline at end of file diff --git a/commons/src/main/java/commons/Person.java b/commons/src/main/java/commons/Person.java deleted file mode 100644 index f7d2d31..0000000 --- a/commons/src/main/java/commons/Person.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2021 Delft University of Technology - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package commons; - -import static org.apache.commons.lang3.builder.ToStringStyle.MULTI_LINE_STYLE; - -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; -import org.apache.commons.lang3.builder.ToStringBuilder; - -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; - -@Entity -public class Person { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - public long id; - - public String firstName; - public String lastName; - - @SuppressWarnings("unused") - private Person() { - // for object mapper - } - - public Person(String firstName, String lastName) { - this.firstName = firstName; - this.lastName = lastName; - } - - @Override - public boolean equals(Object obj) { - return EqualsBuilder.reflectionEquals(this, obj); - } - - @Override - public int hashCode() { - return HashCodeBuilder.reflectionHashCode(this); - } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this, MULTI_LINE_STYLE); - } -} \ No newline at end of file diff --git a/commons/src/main/java/commons/Quote.java b/commons/src/main/java/commons/Quote.java deleted file mode 100644 index 60cb27d..0000000 --- a/commons/src/main/java/commons/Quote.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2021 Delft University of Technology - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package commons; - -import static org.apache.commons.lang3.builder.ToStringStyle.MULTI_LINE_STYLE; - -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; -import org.apache.commons.lang3.builder.ToStringBuilder; - -import jakarta.persistence.CascadeType; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.OneToOne; - -@Entity -public class Quote { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - public long id; - - @OneToOne(cascade = CascadeType.PERSIST) - public Person person; - public String quote; - - @SuppressWarnings("unused") - private Quote() { - // for object mappers - } - - public Quote(Person person, String quote) { - this.person = person; - this.quote = quote; - } - - @Override - public boolean equals(Object obj) { - return EqualsBuilder.reflectionEquals(this, obj); - } - - @Override - public int hashCode() { - return HashCodeBuilder.reflectionHashCode(this); - } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this, MULTI_LINE_STYLE); - } -} \ No newline at end of file diff --git a/commons/src/test/java/commons/PersonTest.java b/commons/src/test/java/commons/PersonTest.java deleted file mode 100644 index 93c545c..0000000 --- a/commons/src/test/java/commons/PersonTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2021 Delft University of Technology - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package commons; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.Test; - -public class PersonTest { - - @Test - public void checkConstructor() { - var p = new Person("f", "l"); - assertEquals("f", p.firstName); - assertEquals("l", p.lastName); - } - - @Test - public void equalsHashCode() { - var a = new Person("a", "b"); - var b = new Person("a", "b"); - assertEquals(a, b); - assertEquals(a.hashCode(), b.hashCode()); - } - - @Test - public void notEqualsHashCode() { - var a = new Person("a", "b"); - var b = new Person("a", "c"); - assertNotEquals(a, b); - assertNotEquals(a.hashCode(), b.hashCode()); - } - - @Test - public void hasToString() { - var actual = new Person("a", "b").toString(); - assertTrue(actual.contains(Person.class.getSimpleName())); - assertTrue(actual.contains("\n")); - assertTrue(actual.contains("firstName")); - } -} \ No newline at end of file diff --git a/commons/src/test/java/commons/QuoteTest.java b/commons/src/test/java/commons/QuoteTest.java deleted file mode 100644 index 85ee201..0000000 --- a/commons/src/test/java/commons/QuoteTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2021 Delft University of Technology - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package commons; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.Test; - -public class QuoteTest { - - private static final Person SOME_PERSON = new Person("a", "b"); - - @Test - public void checkConstructor() { - var q = new Quote(SOME_PERSON, "q"); - assertEquals(SOME_PERSON, q.person); - assertEquals("q", q.quote); - } - - @Test - public void equalsHashCode() { - var a = new Quote(new Person("a", "b"), "c"); - var b = new Quote(new Person("a", "b"), "c"); - assertEquals(a, b); - assertEquals(a.hashCode(), b.hashCode()); - } - - @Test - public void notEqualsHashCode() { - var a = new Quote(new Person("a", "b"), "c"); - var b = new Quote(new Person("a", "b"), "d"); - assertNotEquals(a, b); - assertNotEquals(a.hashCode(), b.hashCode()); - } - - @Test - public void hasToString() { - var actual = new Quote(new Person("a", "b"), "c").toString(); - assertTrue(actual.contains(Quote.class.getSimpleName())); - assertTrue(actual.contains("\n")); - assertTrue(actual.contains("person")); - } -} \ No newline at end of file diff --git a/server/src/main/java/server/api/PersonListingController.java b/server/src/main/java/server/api/PersonListingController.java deleted file mode 100644 index ea06a74..0000000 --- a/server/src/main/java/server/api/PersonListingController.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Copyright 2024 Sebastian Proksch - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package server.api; - -import java.util.LinkedList; -import java.util.List; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import commons.Person; - -@RestController -@RequestMapping("/api/people") -public class PersonListingController { - - private List people = new LinkedList<>(); - - public PersonListingController() { - people.add(new Person("Mickey", "Mouse")); - people.add(new Person("Donald", "Duck")); - } - - @GetMapping("/") - public List list() { - return people; - } - - @PostMapping("/") - public List add(@RequestBody Person p) { - if (!people.contains(p)) { - people.add(p); - } - return people; - } -} \ No newline at end of file diff --git a/server/src/main/java/server/api/QuoteController.java b/server/src/main/java/server/api/QuoteController.java deleted file mode 100644 index f68960e..0000000 --- a/server/src/main/java/server/api/QuoteController.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2021 Delft University of Technology - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package server.api; - -import java.util.List; -import java.util.Random; - -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import commons.Quote; -import server.database.QuoteRepository; - -@RestController -@RequestMapping("/api/quotes") -public class QuoteController { - - private final Random random; - private final QuoteRepository repo; - - public QuoteController(Random random, QuoteRepository repo) { - this.random = random; - this.repo = repo; - } - - @GetMapping(path = { "", "/" }) - public List getAll() { - return repo.findAll(); - } - - @GetMapping("/{id}") - public ResponseEntity getById(@PathVariable("id") long id) { - if (id < 0 || !repo.existsById(id)) { - return ResponseEntity.badRequest().build(); - } - return ResponseEntity.ok(repo.findById(id).get()); - } - - @PostMapping(path = { "", "/" }) - public ResponseEntity add(@RequestBody Quote quote) { - - if (quote.person == null || isNullOrEmpty(quote.person.firstName) || isNullOrEmpty(quote.person.lastName) - || isNullOrEmpty(quote.quote)) { - return ResponseEntity.badRequest().build(); - } - - Quote saved = repo.save(quote); - return ResponseEntity.ok(saved); - } - - private static boolean isNullOrEmpty(String s) { - return s == null || s.isEmpty(); - } - - @GetMapping("rnd") - public ResponseEntity getRandom() { - var quotes = repo.findAll(); - var idx = random.nextInt((int) repo.count()); - return ResponseEntity.ok(quotes.get(idx)); - } -} \ No newline at end of file diff --git a/server/src/main/java/server/database/QuoteRepository.java b/server/src/main/java/server/database/QuoteRepository.java deleted file mode 100644 index 2dd2c36..0000000 --- a/server/src/main/java/server/database/QuoteRepository.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2021 Delft University of Technology - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package server.database; - -import org.springframework.data.jpa.repository.JpaRepository; - -import commons.Quote; - -public interface QuoteRepository extends JpaRepository {} \ No newline at end of file diff --git a/server/src/test/java/server/api/PersonListingControllerTest.java b/server/src/test/java/server/api/PersonListingControllerTest.java deleted file mode 100644 index 97b15fe..0000000 --- a/server/src/test/java/server/api/PersonListingControllerTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright 2024 Sebastian Proksch - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package server.api; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.List; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import commons.Person; - -public class PersonListingControllerTest { - - private static final Person MICKEY = new Person("Mickey", "Mouse"); - private static final Person DONALD = new Person("Donald", "Duck"); - private static final Person SCROOGE = new Person("Scrooge", "McDuck"); - - private PersonListingController sut; - - @BeforeEach - public void setup() { - sut = new PersonListingController(); - } - - @Test - public void containsTwoDefaultNames() { - var actual = sut.list(); - var expected = List.of(MICKEY, DONALD); - assertEquals(expected, actual); - } - - @Test - public void canAddPeople() { - var actual = sut.add(SCROOGE); - var expected = List.of(MICKEY, DONALD, SCROOGE); - assertEquals(expected, actual); - } -} \ No newline at end of file diff --git a/server/src/test/java/server/api/QuoteControllerTest.java b/server/src/test/java/server/api/QuoteControllerTest.java deleted file mode 100644 index 12654da..0000000 --- a/server/src/test/java/server/api/QuoteControllerTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2021 Delft University of Technology - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package server.api; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.springframework.http.HttpStatus.BAD_REQUEST; - -import java.util.Random; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import commons.Person; -import commons.Quote; - -public class QuoteControllerTest { - - public int nextInt; - private MyRandom random; - private TestQuoteRepository repo; - - private QuoteController sut; - - @BeforeEach - public void setup() { - random = new MyRandom(); - repo = new TestQuoteRepository(); - sut = new QuoteController(random, repo); - } - - @Test - public void cannotAddNullPerson() { - var actual = sut.add(getQuote(null)); - assertEquals(BAD_REQUEST, actual.getStatusCode()); - } - - @Test - public void randomSelection() { - sut.add(getQuote("q1")); - sut.add(getQuote("q2")); - nextInt = 1; - var actual = sut.getRandom(); - - assertTrue(random.wasCalled); - assertEquals("q2", actual.getBody().quote); - } - - @Test - public void databaseIsUsed() { - sut.add(getQuote("q1")); - repo.calledMethods.contains("save"); - } - - private static Quote getQuote(String q) { - return new Quote(new Person(q, q), q); - } - - @SuppressWarnings("serial") - public class MyRandom extends Random { - - public boolean wasCalled = false; - - @Override - public int nextInt(int bound) { - wasCalled = true; - return nextInt; - } - } -} \ No newline at end of file diff --git a/server/src/test/java/server/api/TestQuoteRepository.java b/server/src/test/java/server/api/TestQuoteRepository.java deleted file mode 100644 index 9c605bd..0000000 --- a/server/src/test/java/server/api/TestQuoteRepository.java +++ /dev/null @@ -1,225 +0,0 @@ -/* - * Copyright 2021 Delft University of Technology - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package server.api; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.function.Function; - -import org.springframework.data.domain.Example; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Sort; -import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery; - -import commons.Quote; -import server.database.QuoteRepository; - -public class TestQuoteRepository implements QuoteRepository { - - public final List quotes = new ArrayList<>(); - public final List calledMethods = new ArrayList<>(); - - private void call(String name) { - calledMethods.add(name); - } - - @Override - public List findAll() { - calledMethods.add("findAll"); - return quotes; - } - - @Override - public List findAll(Sort sort) { - // TODO Auto-generated method stub - return null; - } - - @Override - public List findAllById(Iterable ids) { - // TODO Auto-generated method stub - return null; - } - - @Override - public List saveAll(Iterable entities) { - // TODO Auto-generated method stub - return null; - } - - @Override - public void flush() { - // TODO Auto-generated method stub - - } - - @Override - public S saveAndFlush(S entity) { - // TODO Auto-generated method stub - return null; - } - - @Override - public List saveAllAndFlush(Iterable entities) { - // TODO Auto-generated method stub - return null; - } - - @Override - public void deleteAllInBatch(Iterable entities) { - // TODO Auto-generated method stub - - } - - @Override - public void deleteAllByIdInBatch(Iterable ids) { - // TODO Auto-generated method stub - - } - - @Override - public void deleteAllInBatch() { - // TODO Auto-generated method stub - - } - - @Override - public Quote getOne(Long id) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Quote getById(Long id) { - call("getById"); - return find(id).get(); - } - - @Override - public Quote getReferenceById(Long id) { - call("getReferenceById"); - return find(id).get(); - } - - private Optional find(Long id) { - return quotes.stream().filter(q -> q.id == id).findFirst(); - } - - @Override - public List findAll(Example example) { - // TODO Auto-generated method stub - return null; - } - - @Override - public List findAll(Example example, Sort sort) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Page findAll(Pageable pageable) { - // TODO Auto-generated method stub - return null; - } - - @Override - public S save(S entity) { - call("save"); - entity.id = (long) quotes.size(); - quotes.add(entity); - return entity; - } - - @Override - public Optional findById(Long id) { - // TODO Auto-generated method stub - return null; - } - - @Override - public boolean existsById(Long id) { - call("existsById"); - return find(id).isPresent(); - } - - @Override - public long count() { - return quotes.size(); - } - - @Override - public void deleteById(Long id) { - // TODO Auto-generated method stub - - } - - @Override - public void delete(Quote entity) { - // TODO Auto-generated method stub - - } - - @Override - public void deleteAllById(Iterable ids) { - // TODO Auto-generated method stub - - } - - @Override - public void deleteAll(Iterable entities) { - // TODO Auto-generated method stub - - } - - @Override - public void deleteAll() { - // TODO Auto-generated method stub - - } - - @Override - public Optional findOne(Example example) { - // TODO Auto-generated method stub - return null; - } - - @Override - public Page findAll(Example example, Pageable pageable) { - // TODO Auto-generated method stub - return null; - } - - @Override - public long count(Example example) { - // TODO Auto-generated method stub - return 0; - } - - @Override - public boolean exists(Example example) { - // TODO Auto-generated method stub - return false; - } - - @Override - public R findBy(Example example, Function, R> queryFunction) { - // TODO Auto-generated method stub - return null; - } -} \ No newline at end of file