Merge branch 'eugenp/master'

This commit is contained in:
iaforek 2017-05-19 22:49:49 +01:00
commit a3ca171be3
151 changed files with 68659 additions and 490 deletions

View File

@ -1,9 +1,15 @@
language: java
before_install:
- export MAVEN_OPTS="-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit"
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
install: travis_wait 60 mvn -q test -fae
sudo: required
before_script:
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:MaxPermSize=2048M -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC -XX:+TieredCompilation -XX:TieredStopAtLevel=1 -XX:-UseGCOverheadLimit'" > ~/.mavenrc
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
jdk:
- oraclejdk8

View File

@ -0,0 +1,35 @@
package com.baeldung.concurrent.synchronize;
public class BaeldungSynchronizedBlocks {
private int count = 0;
private static int staticCount = 0;
public void performSynchronisedTask() {
synchronized (this) {
setCount(getCount() + 1);
}
}
public static void performStaticSyncTask() {
synchronized (BaeldungSynchronizedBlocks.class) {
setStaticCount(getStaticCount() + 1);
}
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public static int getStaticCount() {
return staticCount;
}
public static void setStaticCount(int staticCount) {
BaeldungSynchronizedBlocks.staticCount = staticCount;
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.concurrent.synchronize;
public class BaeldungSynchronizedMethods {
private int sum = 0;
private int syncSum = 0;
public static int staticSum = 0;
public void calculate() {
setSum(getSum() + 1);
}
public synchronized void synchronisedCalculate() {
setSyncSum(getSyncSum() + 1);
}
public static synchronized void syncStaticCalculate() {
staticSum = staticSum + 1;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public int getSyncSum() {
return syncSum;
}
public void setSyncSum(int syncSum) {
this.syncSum = syncSum;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesLoader {
public static Properties loadProperties(String resourceFileName) throws IOException {
Properties configuration = new Properties();
InputStream inputStream = PropertiesLoader.class
.getClassLoader()
.getResourceAsStream(resourceFileName);
configuration.load(inputStream);
inputStream.close();
return configuration;
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.concurrent.synchronize;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.junit.Test;
public class BaeldungSychronizedBlockTest {
@Test
public void givenMultiThread_whenBlockSync() throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(3);
BaeldungSynchronizedBlocks synchronizedBlocks = new BaeldungSynchronizedBlocks();
IntStream.range(0, 1000)
.forEach(count -> service.submit(synchronizedBlocks::performSynchronisedTask));
service.awaitTermination(100, TimeUnit.MILLISECONDS);
assertEquals(1000, synchronizedBlocks.getCount());
}
@Test
public void givenMultiThread_whenStaticSyncBlock() throws InterruptedException {
ExecutorService service = Executors.newCachedThreadPool();
IntStream.range(0, 1000)
.forEach(count -> service.submit(BaeldungSynchronizedBlocks::performStaticSyncTask));
service.awaitTermination(100, TimeUnit.MILLISECONDS);
assertEquals(1000, BaeldungSynchronizedBlocks.getStaticCount());
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.concurrent.synchronize;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.junit.Ignore;
import org.junit.Test;
public class BaeldungSynchronizeMethodsTest {
@Test
@Ignore
public void givenMultiThread_whenNonSyncMethod() throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(3);
BaeldungSynchronizedMethods method = new BaeldungSynchronizedMethods();
IntStream.range(0, 1000)
.forEach(count -> service.submit(method::calculate));
service.awaitTermination(100, TimeUnit.MILLISECONDS);
assertEquals(1000, method.getSum());
}
@Test
public void givenMultiThread_whenMethodSync() throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(3);
BaeldungSynchronizedMethods method = new BaeldungSynchronizedMethods();
IntStream.range(0, 1000)
.forEach(count -> service.submit(method::synchronisedCalculate));
service.awaitTermination(100, TimeUnit.MILLISECONDS);
assertEquals(1000, method.getSyncSum());
}
@Test
public void givenMultiThread_whenStaticSyncMethod() throws InterruptedException {
ExecutorService service = Executors.newCachedThreadPool();
IntStream.range(0, 1000)
.forEach(count -> service.submit(BaeldungSynchronizedMethods::syncStaticCalculate));
service.awaitTermination(100, TimeUnit.MILLISECONDS);
assertEquals(1000, BaeldungSynchronizedMethods.staticSum);
}
}

View File

@ -47,10 +47,9 @@ public class SplitUnitTest {
}
@Test
public void givenString_whenSplit_thenReturnsIterable_Splitter() {
public void givenString_whenSplit_thenReturnsList_Splitter() {
//given
Iterable<String> result = Splitter.on(',').trimResults().omitEmptyStrings().split("car,jeep,, scooter");
List<String> resultList = Lists.newArrayList(result);
List<String> resultList = Splitter.on(',').trimResults().omitEmptyStrings().splitToList("car,jeep,, scooter");
assertThat(resultList)
.containsExactly("car", "jeep", "scooter");

View File

@ -0,0 +1,34 @@
package com.baeldung.util;
import org.junit.Test;
import java.io.IOException;
import java.util.Properties;
import static org.junit.Assert.assertEquals;
public class PropertiesLoaderTest {
@Test
public void loadProperties_whenPropertyReaded_thenSuccess() throws IOException {
//given
final String RESOURCE_FILE_NAME = "configuration.properties";
final String SAMPLE_CONF_ENTRY = "sampleConfEntry";
final String COLON_SEPARATED_CONF_ENTRY = "colonSeparatedEntry";
final String GIVEN_CONF_ENTRY_VALUE = "sample String value";
final String COLON_SEPARATED_CONF_ENTRY_VALUE = "colon separated entry value";
//when
Properties config = PropertiesLoader.loadProperties(RESOURCE_FILE_NAME);
String sampleConfEntryValue = config.getProperty(SAMPLE_CONF_ENTRY);
String colonSeparatedConfEntryValue = config.getProperty(COLON_SEPARATED_CONF_ENTRY);
//then
assertEquals(GIVEN_CONF_ENTRY_VALUE, sampleConfEntryValue);
assertEquals(COLON_SEPARATED_CONF_ENTRY_VALUE, colonSeparatedConfEntryValue);
}
}

View File

@ -0,0 +1,99 @@
package org.baeldung.java.io;
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
public class JavaFilePathUnitTest {
private static String userDir;
@BeforeClass
public static void createFilesAndFolders() throws IOException {
userDir = System.getProperty("user.dir");
new File(userDir + "/baeldung/foo").mkdirs();
new File(userDir + "/baeldung/bar/baz").mkdirs();
new File(userDir + "/baeldung/foo/foo-one.txt").createNewFile();
new File(userDir + "/baeldung/foo/foo-two.txt").createNewFile();
new File(userDir + "/baeldung/bar/bar-one.txt").createNewFile();
new File(userDir + "/baeldung/bar/bar-two.txt").createNewFile();
new File(userDir + "/baeldung/bar/baz/baz-one.txt").createNewFile();
new File(userDir + "/baeldung/bar/baz/baz-two.txt").createNewFile();
}
@Test
public void whenPathResolved_thenSuccess() {
File file = new File("baeldung/foo/foo-one.txt");
String expectedPath = isWindows() ? "baeldung\\foo\\foo-one.txt" : "baeldung/foo/foo-one.txt";
String actualPath = file.getPath();
assertEquals(expectedPath, actualPath);
}
@Test
public void whenAbsolutePathResolved_thenSuccess() {
File file = new File("baeldung/foo/foo-one.txt");
String expectedPath = isWindows() ? userDir + "\\baeldung\\foo\\foo-one.txt" : userDir + "/baeldung/foo/foo-one.txt";
String actualPath = file.getAbsolutePath();
assertEquals(expectedPath, actualPath);
}
@Test
public void whenAbsolutePathWithShorthandResolved_thenSuccess() {
File file = new File("baeldung/bar/baz/../bar-one.txt");
String expectedPath = isWindows() ? userDir + "\\baeldung\\bar\\baz\\..\\bar-one.txt" : userDir + "/baeldung/bar/baz/../bar-one.txt";
String actualPath = file.getAbsolutePath();
assertEquals(expectedPath, actualPath);
}
@Test
public void whenCanonicalPathWithShorthandResolved_thenSuccess() throws IOException {
File file = new File("baeldung/bar/baz/../bar-one.txt");
String expectedPath = isWindows() ? userDir + "\\baeldung\\bar\\bar-one.txt" : userDir + "/baeldung/bar/bar-one.txt";
String actualPath = file.getCanonicalPath();
assertEquals(expectedPath, actualPath);
}
@Test
public void whenCanonicalPathWithDotShorthandResolved_thenSuccess() throws IOException {
File file = new File("baeldung/bar/baz/./baz-one.txt");
String expectedPath = isWindows() ? userDir + "\\baeldung\\bar\\baz\\baz-one.txt" : userDir + "/baeldung/bar/baz/baz-one.txt";
String actualPath = file.getCanonicalPath();
assertEquals(expectedPath, actualPath);
}
@Test(expected = IOException.class)
public void givenWindowsOs_whenCanonicalPathWithWildcard_thenIOException() throws IOException {
Assume.assumeTrue(isWindows());
new File("*").getCanonicalPath();
}
@AfterClass
public static void deleteFilesAndFolders() {
File baeldungDir = new File(userDir + "/baeldung");
deleteRecursively(baeldungDir);
}
private static void deleteRecursively(File dir) {
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
deleteRecursively(f);
}
f.delete();
}
dir.delete();
}
private static boolean isWindows() {
String osName = System.getProperty("os.name");
return osName.contains("Windows");
}
}

View File

@ -0,0 +1,4 @@
# this is sample property file for PropertiesLoaderTest configuration needs
! this is also a comment
sampleConfEntry = sample String value
colonSeparatedEntry : colon separated entry value

View File

@ -9,7 +9,7 @@ import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
public class GuavaStreamsTests {
public class GuavaStreamsUnitTest {
List<Integer> numbers;

4
guest/spring-boot-app/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/target/
.settings/
.classpath
.project

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

View File

@ -0,0 +1,61 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-boot-app</groupId>
<artifactId>spring-boot-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<tomcat.version>8.0.43</tomcat.version>
</properties>
</project>

View File

@ -0,0 +1,13 @@
package com.stackify;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,21 @@
package com.stackify.config;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
@Configuration
public class PersistenceConfig {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.H2).addScript("mySchema.sql").addScript("myData.sql").build();
return db;
}
}

View File

@ -0,0 +1,34 @@
package com.stackify.controller;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import com.stackify.model.Employee;
import com.stackify.repository.EmployeeRepository;
@RestController
public class EmployeeController {
private EmployeeRepository employeeRepository;
public EmployeeController(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
@PostMapping("/employees")
@ResponseStatus(HttpStatus.CREATED)
public void addEmployee(@RequestBody Employee employee) {
employeeRepository.save(employee);
}
@GetMapping("/employees")
public List<Employee> getEmployees() {
return employeeRepository.findAll();
}
}

View File

@ -0,0 +1,41 @@
package com.stackify.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
public Employee() {
}
public Employee(long id, String name) {
super();
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,9 @@
package com.stackify.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.stackify.model.Employee;
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}

View File

@ -0,0 +1,5 @@
server.port=8081
server.contextPath=/springbootapp
logging.level.org.springframework.web: INFO
spring.jpa.hibernate.ddl-auto=update

View File

@ -0,0 +1 @@
insert into employee(name) values ('ana');

View File

@ -0,0 +1 @@
create table employee(id int identity primary key, name varchar(30));

View File

@ -0,0 +1,46 @@
package com.stackify.test;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import com.stackify.Application;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class EmployeeControllerTest {
private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setup() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void whenCreateGetEmployee_thenOk() throws Exception {
String employeeJson = "{\"name\":\"john\"}";
this.mockMvc.perform(post("/employees").contentType(CONTENT_TYPE).content(employeeJson)).andExpect(status().isCreated());
this.mockMvc.perform(get("/employees")).andExpect(status().isOk()).andExpect(content().contentType(CONTENT_TYPE)).andExpect(jsonPath("$", hasSize(2))).andExpect(jsonPath("$[0].name", is("ana"))).andExpect(jsonPath("$[1].name", is("john")));
}
}

View File

@ -1,5 +0,0 @@
### Relevant Articles:
- [Introduction to Javaslang](http://www.baeldung.com/javaslang)
- [Guide to Try in Javaslang](http://www.baeldung.com/javaslang-try)
- [Guide to Pattern Matching in Javaslang](http://www.baeldung.com/javaslang-pattern-matching)
- [Property Testing Example With Javaslang](http://www.baeldung.com/javaslang-property-testing)

View File

@ -1,18 +0,0 @@
package com.baeldung.javaslang.exception.handling;
import com.baeldung.javaslang.exception.handling.client.HttpClient;
import com.baeldung.javaslang.exception.handling.client.Response;
import javaslang.control.Try;
public class JavaslangTry {
private final HttpClient httpClient;
public JavaslangTry(HttpClient httpClient) {
this.httpClient = httpClient;
}
public Try<Response> getResponse() {
return Try.of(httpClient::call);
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.arquillian;
import javax.ejb.Stateless;
@Stateless
public class CapsConvertor {
public ConvertToLowerCase getLowerCase(){
return new ConvertToLowerCase();
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.arquillian;
import javax.ejb.Stateless;
import javax.inject.Inject;
@Stateless
public class CapsService {
@Inject
private CapsConvertor capsConvertor;
public String getConvertedCaps(final String word){
return capsConvertor.getLowerCase().convert(word);
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.arquillian;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
@Entity
public class Car {
@Id
@GeneratedValue
private Long id;
@NotNull
private String name;
public Long getId() {
return id;
}
public void setId(final Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
@Override
public String toString() {
return "Car [id=" + id + ", name=" + name + "]";
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.arquillian;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class CarEJB {
@PersistenceContext(unitName = "defaultPersistenceUnit")
private EntityManager em;
public Car saveCar(final Car car) {
em.persist(car);
return car;
}
@SuppressWarnings("unchecked")
public List<Car> findAllCars() {
final Query query = em.createQuery("SELECT b FROM Car b ORDER BY b.name ASC");
List<Car> entries = query.getResultList();
if (entries == null) {
entries = new ArrayList<Car>();
}
return entries;
}
public void deleteCar(Car car) {
car = em.merge(car);
em.remove(car);
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.arquillian;
import java.io.PrintStream;
public class Component {
public void sendMessage(PrintStream to, String msg) {
to.println(message(msg));
}
public String message(String msg) {
return "Message, " + msg;
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.arquillian;
public class ConvertToLowerCase {
public String convert(String word){
return word.toLowerCase();
}
}

View File

@ -0,0 +1,13 @@
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="defaultPersistenceUnit" transaction-type="JTA">
<class>com.baeldung.arquillian.Car</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,73 @@
package com.baeldung.arquillan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import javax.inject.Inject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.baeldung.arquillian.CapsConvertor;
import com.baeldung.arquillian.CapsService;
import com.baeldung.arquillian.Car;
import com.baeldung.arquillian.CarEJB;
import com.baeldung.arquillian.Component;
import com.baeldung.arquillian.ConvertToLowerCase;
@RunWith(Arquillian.class)
public class ArquillianTest {
@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class).addClasses(Component.class, CapsService.class, CapsConvertor.class, ConvertToLowerCase.class, Car.class, CarEJB.class).addAsResource("META-INF/persistence.xml").addAsManifestResource(EmptyAsset.INSTANCE,
"beans.xml");
}
@Inject
Component component;
@Test
public void givenMessage_WhenComponentSendMessage_ThenMessageReceived() {
Assert.assertEquals("Message, MESSAGE", component.message(("MESSAGE")));
component.sendMessage(System.out, "MESSAGE");
}
@Inject
private CapsService capsService;
@Test
public void givenWord_WhenUppercase_ThenLowercase() {
assertTrue("capitalize".equals(capsService.getConvertedCaps("CAPITALIZE")));
assertEquals("capitalize", capsService.getConvertedCaps("CAPITALIZE"));
}
@Inject
private CarEJB carEJB;
@Test
public void testCars() {
assertTrue(carEJB.findAllCars().isEmpty());
Car c1 = new Car();
c1.setName("Impala");
Car c2 = new Car();
c2.setName("Maverick");
Car c3 = new Car();
c3.setName("Aspen");
Car c4 = new Car();
c4.setName("Lincoln");
carEJB.saveCar(c1);
carEJB.saveCar(c2);
carEJB.saveCar(c3);
carEJB.saveCar(c4);
assertEquals(4, carEJB.findAllCars().size());
carEJB.deleteCar(c4);
assertEquals(3, carEJB.findAllCars().size());
}
}

View File

@ -0,0 +1,13 @@
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="defaultPersistenceUnit" transaction-type="JTA">
<class>com.baeldung.arquillian.Car</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>

View File

@ -27,7 +27,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
**/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GatewayApp.class)
public class ProfileInfoResourceIntTest {
public class ProfileInfoResourceIntegrationTest {
@Mock
private Environment environment;

View File

@ -27,7 +27,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
**/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BaeldungApp.class)
public class ProfileInfoResourceIntTest {
public class ProfileInfoResourceIntegrationTest {
@Mock
private Environment environment;

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,129 @@
package com.baeldung;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.function.ThrowingConsumer;
public class DynamicTestsExample {
@TestFactory
Collection<DynamicTest> dynamicTestsWithCollection() {
return Arrays.asList(
DynamicTest.dynamicTest("Add test", () -> assertEquals(2, Math.addExact(1, 1))),
DynamicTest.dynamicTest("Multiply Test", () -> assertEquals(4, Math.multiplyExact(2, 2))));
}
@TestFactory
Iterable<DynamicTest> dynamicTestsWithIterable() {
return Arrays.asList(
DynamicTest.dynamicTest("Add test", () -> assertEquals(2, Math.addExact(1, 1))),
DynamicTest.dynamicTest("Multiply Test", () -> assertEquals(4, Math.multiplyExact(2, 2))));
}
@TestFactory
Iterator<DynamicTest> dynamicTestsWithIterator() {
return Arrays.asList(
DynamicTest.dynamicTest("Add test", () -> assertEquals(2, Math.addExact(1, 1))),
DynamicTest.dynamicTest("Multiply Test", () -> assertEquals(4, Math.multiplyExact(2, 2))))
.iterator();
}
@TestFactory
Stream<DynamicTest> dynamicTestsFromIntStream() {
return IntStream.iterate(0, n -> n + 2).limit(10).mapToObj(
n -> DynamicTest.dynamicTest("test" + n, () -> assertTrue(n % 2 == 0)));
}
@TestFactory
Stream<DynamicTest> dynamicTestsFromStream() {
// sample input and output
List<String> inputList =
new ArrayList<>(Arrays.asList("www.somedomain.com", "www.anotherdomain.com", "www.yetanotherdomain.com"));
List<String> outputList =
new ArrayList<>(Arrays.asList("154.174.10.56", "211.152.104.132", "178.144.120.156"));
// input generator that generates inputs using inputList
Iterator<String> inputGenerator = new Iterator<String>() {
String current;
int size = inputList.size();
int index = 0;
@Override
public boolean hasNext() {
if(index == size) {
return false;
}
current = inputList.get(index);
index++;
return true;
}
@Override
public String next() {
return current;
}
};
// a display name generator that creates a different name based on the input
Function<String, String> displayNameGenerator = (input) -> "Resolving: " + input;
// the test executor, which actually has the logic of how to execute the test case
DomainNameResolver resolver = new DomainNameResolver();
ThrowingConsumer<String> testExecutor = (input) -> {
int id = inputList.indexOf(input);
assertEquals(outputList.get(id), resolver.resolveDomain(input));
};
// combine everything and return a Stream of DynamicTest
return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor);
}
@TestFactory
Stream<DynamicTest> dynamicTestsFromStreamInJava8() {
DomainNameResolver resolver = new DomainNameResolver();
List<String> inputList =
Arrays.asList("www.somedomain.com", "www.anotherdomain.com", "www.yetanotherdomain.com");
List<String> outputList =
Arrays.asList("154.174.10.56", "211.152.104.132", "178.144.120.156");
return inputList.stream().map(dom -> DynamicTest.dynamicTest("Resolving: " + dom, () -> {
int id = inputList.indexOf(dom);
assertEquals(outputList.get(id), resolver.resolveDomain(dom));
}));
}
class DomainNameResolver {
private Map<String, String> ipByDomainName = new HashMap<>();
DomainNameResolver() {
this.ipByDomainName.put("www.somedomain.com", "154.174.10.56");
this.ipByDomainName.put("www.anotherdomain.com", "211.152.104.132");
this.ipByDomainName.put("www.yetanotherdomain.com", "178.144.120.156");
}
public String resolveDomain(String domainName) {
return ipByDomainName.get(domainName);
}
}
}

View File

@ -236,7 +236,6 @@
<artifactId>datanucleus-xml</artifactId>
<version>5.0.0-release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
@ -254,21 +253,25 @@
<version>3.0.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.0.0</version>
</dependency>
<dependency>
<groupId>org.multiverse</groupId>
<artifactId>multiverse-core</artifactId>
<version>${multiverse.version}</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.195</version>
</dependency>
</dependencies>
<properties>
<multiverse.version>0.7.0</multiverse.version>
<cglib.version>3.2.4</cglib.version>
<commons-lang.version>3.5</commons-lang.version>
<jasypt.version>1.9.2</jasypt.version>

View File

@ -1,44 +1,42 @@
package com.baeldung.hikaricp;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class DataSource {
private static HikariConfig config = new HikariConfig();
private static HikariDataSource ds;
static {
/* config = new HikariConfig("datasource.properties");
// config = new HikariConfig("datasource.properties");
Properties props = new Properties();
props.setProperty("dataSourceClassName", "org.postgresql.ds.PGSimpleDataSource");
props.setProperty("dataSource.user", "postgres");
props.setProperty("dataSource.password", "postgres");
props.setProperty("dataSource.databaseName", "postgres");
props.setProperty("dataSource.portNumber", "5432");
props.setProperty("dataSource.serverName", "localhost");
props.put("dataSource.logWriter", new PrintWriter(System.out));
config = new HikariConfig(props);*/
// Properties props = new Properties();
// props.setProperty("dataSourceClassName", "org.h2.Driver");
// props.setProperty("dataSource.user", "");
// props.setProperty("dataSource.password", "");
// props.put("dataSource.logWriter", new PrintWriter(System.out));
// config = new HikariConfig(props);
config.setJdbcUrl("jdbc:postgresql://localhost:5432/postgres");
config.setUsername("postgres");
config.setPassword("postgres");
config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:/db.sql'");
config.setUsername("");
config.setPassword("");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
ds = new HikariDataSource(config);
/* ds.setJdbcUrl("jdbc:postgresql://localhost:5432/postgres");
ds.setUsername("postgres");
ds.setPassword("postgres");*/
// ds.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:/db.sql'");
// ds.setUsername("");
// ds.setPassword("");
}
private DataSource() {
}
private DataSource() {}
public static Connection getConnection() throws SQLException {
return ds.getConnection();

View File

@ -9,11 +9,13 @@ import java.util.List;
public class HikariCPDemo {
public static List<Employee> fetchData() throws SQLException {
public static List<Employee> fetchData() {
final String SQL_QUERY = "select * from emp";
List<Employee> employees;
try (Connection con = DataSource.getConnection(); PreparedStatement pst = con.prepareStatement(SQL_QUERY); ResultSet rs = pst.executeQuery()) {
employees = new ArrayList<>();
List<Employee> employees = null;
try (Connection con = DataSource.getConnection();
PreparedStatement pst = con.prepareStatement(SQL_QUERY);
ResultSet rs = pst.executeQuery();) {
employees = new ArrayList<Employee>();
Employee employee;
while (rs.next()) {
employee = new Employee();
@ -27,8 +29,14 @@ public class HikariCPDemo {
employee.setDeptno(rs.getInt("deptno"));
employees.add(employee);
}
} catch (SQLException e) {
e.printStackTrace();
}
return employees;
}
public static void main(String[] args) {
fetchData();
}
}

View File

@ -0,0 +1,50 @@
package com.baeldung.stm;
import org.multiverse.api.StmUtils;
import org.multiverse.api.callables.TxnCallable;
import org.multiverse.api.references.TxnInteger;
import org.multiverse.api.references.TxnLong;
public class Account {
private final TxnLong lastUpdate;
private final TxnInteger balance;
public Account(final int balance) {
this.lastUpdate = StmUtils.newTxnLong(System.currentTimeMillis());
this.balance = StmUtils.newTxnInteger(balance);
}
public Integer getBalance() {
return balance.atomicGet();
}
public void adjustBy(final int amount) {
adjustBy(amount, System.currentTimeMillis());
}
public void adjustBy(final int amount, final long date) {
StmUtils.atomic(() -> {
balance.increment(amount);
lastUpdate.set(date);
if (balance.get() < 0) {
throw new IllegalArgumentException("Not enough money");
}
});
}
public void transferTo(final Account other, final int amount) {
StmUtils.atomic(() -> {
final long date = System.currentTimeMillis();
adjustBy(-amount, date);
other.adjustBy(amount, date);
});
}
@Override
public String toString() {
return StmUtils.atomic((TxnCallable<String>)
txn -> "Balance: " + balance.get(txn) + " lastUpdateDate: " + lastUpdate.get(txn));
}
}

View File

@ -7,7 +7,7 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ProxyTest {
public class ProxyIntegrationTest {
@Test
public void givenPersonService_whenSayHello_thenReturnResult() {
//given

View File

@ -6,7 +6,7 @@ import org.apache.commons.math3.analysis.solvers.UnivariateSolver;
import org.junit.Assert;
import org.junit.Test;
public class RootFindingTests {
public class RootFindingUnitTest {
@Test
public void whenUnivariateSolverSolver_thenCorrect() {

View File

@ -2,17 +2,14 @@ package com.baeldung.hikaricp;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import java.sql.SQLException;
import static org.junit.Assert.assertEquals;
public class HikariCPTest {
public class HikariCPUnitTest {
@Test
@Ignore
public void givenConnection_thenFetchDbData() throws SQLException {
public void givenConnection_thenFetchDbData() {
List<Employee> employees = HikariCPDemo.fetchData();
assertEquals(4, employees.size());
}

View File

@ -20,9 +20,9 @@ import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER
@RunWith(Suite.class)
@Suite.SuiteClasses({
KonamCheatClassDirtiesContextTest.DirtiesContextTest.class, KonamCheatClassDirtiesContextTest.AnotherDirtiesContextTest.class
KonamCheatClassDirtiesContextIntegrationTest.DirtiesContextTest.class, KonamCheatClassDirtiesContextIntegrationTest.AnotherDirtiesContextTest.class
})
public class KonamCheatClassDirtiesContextTest {
public class KonamCheatClassDirtiesContextIntegrationTest {
@RunWith(SerenityRunner.class)
@ContextConfiguration(classes = KonamiCodeService.class)

View File

@ -20,7 +20,7 @@ import org.springframework.test.context.ContextConfiguration;
@RunWith(SerenityRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration(classes = KonamiCodeService.class)
public class KonamCheatWithDirtyActionTest {
public class KonamCheatWithDirtyActionIntegrationTest {
private KonamiCodeConstructorDependencySteps cheatSteps;

View File

@ -19,7 +19,7 @@ import org.springframework.test.context.ContextConfiguration;
@RunWith(SerenityRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration(classes = KonamiCodeService.class)
public class KonamCheatWithDirtyActionWithImplicitInjectionTest {
public class KonamCheatWithDirtyActionWithImplicitInjectionIntegrationTest {
@Steps private KonamiCodeServiceInjectionSteps cheatSteps;

View File

@ -11,7 +11,7 @@ import org.junit.runner.RunWith;
* @author aiet
*/
@RunWith(SerenityRunner.class)
public class KonamCheatWithServiceTest {
public class KonamCheatWithServiceIntegrationTest {
@Steps private KonamiCodeServiceInjectionSteps cheatSteps;

View File

@ -17,9 +17,9 @@ import org.springframework.test.context.ContextConfiguration;
*/
@RunWith(SerenityRunner.class)
@ContextConfiguration(locations = "classpath:konami-cheat-beans.xml")
public class KonamiCheatWithIntegrationMethodRulesTest {
public class KonamiCheatWithIntegrationMethodRulesIntegrationTest {
private static Logger LOG = LoggerFactory.getLogger(KonamiCheatWithIntegrationMethodRulesTest.class);
private static Logger LOG = LoggerFactory.getLogger(KonamiCheatWithIntegrationMethodRulesIntegrationTest.class);
@BeforeClass
public static void initClass() {

View File

@ -16,9 +16,9 @@ import org.springframework.test.context.ContextConfiguration;
*/
@RunWith(SpringIntegrationSerenityRunner.class)
@ContextConfiguration(locations = "classpath:konami-cheat-beans.xml")
public class KonamiCheatWithIntegrationRunnerTest {
public class KonamiCheatWithIntegrationRunnerIntegrationTest {
private static Logger LOG = LoggerFactory.getLogger(KonamiCheatWithIntegrationRunnerTest.class);
private static Logger LOG = LoggerFactory.getLogger(KonamiCheatWithIntegrationRunnerIntegrationTest.class);
@BeforeClass
public static void initClass() {

View File

@ -12,7 +12,7 @@ import org.junit.runner.RunWith;
* @author aiet
*/
@RunWith(SerenityRunner.class)
public class KonamiCodeMockMvcTest {
public class KonamiCodeMockMvcIntegrationTest {
@Before
public void init() {

View File

@ -0,0 +1,145 @@
package com.baeldung.stm;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.junit.Assert.assertTrue;
public class AccountTest {
@Test
public void givenAccount_whenDecrement_thenShouldReturnProperValue() {
//given
Account a = new Account(10);
//when
a.adjustBy(-5);
//then
assertThat(a.getBalance()).isEqualTo(5);
}
@Test(expected = IllegalArgumentException.class)
public void givenAccount_whenDecrementTooMuch_thenShouldThrow() {
//given
Account a = new Account(10);
//when
a.adjustBy(-11);
}
@Test
public void givenTwoThreads_whenBothApplyOperation_thenShouldThrow() throws InterruptedException {
//given
ExecutorService ex = Executors.newFixedThreadPool(2);
Account a = new Account(10);
CountDownLatch countDownLatch = new CountDownLatch(1);
AtomicBoolean exceptionThrown = new AtomicBoolean(false);
//when
ex.submit(() -> {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
a.adjustBy(-6);
} catch (IllegalArgumentException e) {
exceptionThrown.set(true);
}
});
ex.submit(() -> {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
a.adjustBy(-5);
} catch (IllegalArgumentException e) {
exceptionThrown.set(true);
}
});
countDownLatch.countDown();
ex.awaitTermination(1, TimeUnit.SECONDS);
ex.shutdown();
//then
assertTrue(exceptionThrown.get());
}
@Test
public void givenTwoAccounts_whenFailedWhileTransferring_thenShouldRollbackTransaction() {
//given
final Account a = new Account(10);
final Account b = new Account(10);
//when
a.transferTo(b, 5);
//then
assertThat(a.getBalance()).isEqualTo(5);
assertThat(b.getBalance()).isEqualTo(15);
//and
try {
a.transferTo(b, 20);
} catch (final IllegalArgumentException e) {
System.out.println("failed to transfer money");
}
//then
assertThat(a.getBalance()).isEqualTo(5);
assertThat(b.getBalance()).isEqualTo(15);
}
@Test
public void givenTwoThreads_whenBothTryToTransfer_thenShouldNotDeadlock() throws InterruptedException {
//given
ExecutorService ex = Executors.newFixedThreadPool(2);
final Account a = new Account(10);
final Account b = new Account(10);
CountDownLatch countDownLatch = new CountDownLatch(1);
//when
ex.submit(() -> {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
a.transferTo(b, 10);
});
ex.submit(() -> {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
b.transferTo(a, 1);
});
countDownLatch.countDown();
ex.awaitTermination(1, TimeUnit.SECONDS);
ex.shutdown();
//then
assertThat(a.getBalance()).isEqualTo(1);
assertThat(b.getBalance()).isEqualTo(19);
}
}

View File

@ -6,7 +6,7 @@ import org.junit.Test;
import com.codahale.metrics.*;
public class ReporterTest {
public class ReporterIntegrationTest {
@Test
public void whenConsoleReporter_thenOutputReport() {
MetricRegistry metricRegistry = new MetricRegistry();

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -70,7 +70,7 @@
<module>jackson</module>
<!-- <module>java-cassandra</module> -->
<module>javaslang</module>
<module>vavr</module>
<module>javax-servlets</module>
<module>javaxval</module>
<module>jaxb</module>

View File

@ -13,7 +13,7 @@ import java.util.Random;
import static org.junit.Assert.assertEquals;
public class ProtobufTest {
public class ProtobufUnitTest {
private final String filePath = "address_book";
@After

View File

@ -20,7 +20,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static org.junit.Assert.assertEquals;
public class ProgrammaticallyManaged {
public class ProgrammaticallyManagedLiveTest {
private static final String BAELDUNG_PATH = "/baeldung";

View File

@ -13,7 +13,7 @@ import java.util.concurrent.TimeUnit;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
public class RxJavaTesting {
public class RxJavaUnitTest {
@Test
public void givenObservable_whenZip_shouldAssertBlockingInASameThread() {
// given

View File

@ -6,7 +6,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ScopesTest {
public class ScopesIntegrationTest {
private static final String NAME = "John Smith";
private static final String NAME_OTHER = "Anna Jones";

View File

@ -59,6 +59,56 @@
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<configuration>
<tasks>
<exec executable="cmd" osfamily="windows" dir="${project.basedir}/src/main/angular/ui">
<arg value="/c"/>
<arg value="ng"/>
<arg value="build"/>
</exec>
<exec executable="/bin/sh" osfamily="mac" dir="${project.basedir}/src/main/angular/ui">
<arg value="-c"/>
<arg value="ng build"/>
</exec>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<spring-cloud-dependencies.version>Brixton.SR7</spring-cloud-dependencies.version>
</properties>

View File

@ -0,0 +1,58 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"project": {
"name": "ui"
},
"apps": [
{
"root": "src",
"outDir": "../../resources/static/home",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app",
"styles": [
"styles.css",
"../node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [],
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}
],
"e2e": {
"protractor": {
"config": "./protractor.conf.js"
}
},
"lint": [
{
"project": "src/tsconfig.app.json"
},
{
"project": "src/tsconfig.spec.json"
},
{
"project": "e2e/tsconfig.e2e.json"
}
],
"test": {
"karma": {
"config": "./karma.conf.js"
}
},
"defaults": {
"styleExt": "css",
"component": {}
}
}

View File

@ -0,0 +1,13 @@
# Editor configuration, see http://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
max_line_length = off
trim_trailing_whitespace = false

View File

@ -0,0 +1,42 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.
# compiled output
/dist
/tmp
/out-tsc
# dependencies
/node_modules
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
# misc
/.sass-cache
/connect.lock
/coverage
/libpeerconnection.log
npm-debug.log
testem.log
/typings
# e2e
/e2e/*.js
/e2e/*.map
# System Files
.DS_Store
Thumbs.db

View File

@ -0,0 +1,28 @@
# Ui
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 1.0.0.
## Development server
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
## Code scaffolding
Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive/pipe/service/class/module`.
## Build
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `-prod` flag for a production build.
## Running unit tests
Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).
## Running end-to-end tests
Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/).
Before running the tests make sure you are serving the app via `ng serve`.
## Further help
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).

View File

@ -0,0 +1,14 @@
import {UiPage} from "./app.po";
describe('ui App', () => {
let page: UiPage;
beforeEach(() => {
page = new UiPage();
});
it('should display message saying app works', () => {
page.navigateTo();
expect(page.getParagraphText()).toEqual('app works!');
});
});

View File

@ -0,0 +1,11 @@
import {browser, element, by} from "protractor";
export class UiPage {
navigateTo() {
return browser.get('/');
}
getParagraphText() {
return element(by.css('app-root h1')).getText();
}
}

View File

@ -0,0 +1,12 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/e2e",
"module": "commonjs",
"target": "es5",
"types":[
"jasmine",
"node"
]
}
}

View File

@ -0,0 +1,44 @@
// Karma configuration file, see link for more information
// https://karma-runner.github.io/0.13/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular/cli'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular/cli/plugins/karma')
],
client:{
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
files: [
{ pattern: './src/test.ts', watched: false }
],
preprocessors: {
'./src/test.ts': ['@angular/cli']
},
mime: {
'text/x-typescript': ['ts','tsx']
},
coverageIstanbulReporter: {
reports: [ 'html', 'lcovonly' ],
fixWebpackSourcePaths: true
},
angularCli: {
environment: 'dev'
},
reporters: config.angularCli && config.angularCli.codeCoverage
? ['progress', 'coverage-istanbul']
: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};

View File

@ -0,0 +1,47 @@
{
"name": "ui",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/forms": "^4.0.0",
"@angular/http": "^4.0.0",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/router": "^4.0.0",
"bootstrap": "^4.0.0-alpha.6",
"core-js": "^2.4.1",
"rxjs": "^5.1.0",
"zone.js": "^0.8.4"
},
"devDependencies": {
"@angular/cli": "1.0.0",
"@angular/compiler-cli": "^4.0.0",
"@types/jasmine": "2.5.38",
"@types/node": "~6.0.60",
"codelyzer": "~2.0.0",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.0.0",
"karma-cli": "~1.0.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-coverage-istanbul-reporter": "^0.2.0",
"protractor": "~5.1.0",
"ts-node": "~2.0.0",
"tslint": "~4.5.0",
"typescript": "~2.2.0"
}
}

View File

@ -0,0 +1,30 @@
// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts
const { SpecReporter } = require('jasmine-spec-reporter');
exports.config = {
allScriptsTimeout: 11000,
specs: [
'./e2e/**/*.e2e-spec.ts'
],
capabilities: {
'browserName': 'chrome'
},
directConnect: true,
baseUrl: 'http://localhost:4200/',
framework: 'jasmine',
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000,
print: function() {}
},
beforeLaunch: function() {
require('ts-node').register({
project: 'e2e/tsconfig.e2e.json'
});
},
onPrepare() {
jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
}
};

View File

@ -0,0 +1,3 @@
.custom-close {
float:right;
}

View File

@ -0,0 +1,37 @@
<nav class="navbar navbar-toggleable-md navbar-inverse fixed-top bg-inverse">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Book Rater <span *ngIf="principal.isAdmin()">Admin</span></a>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav mr-auto">
</ul>
<button *ngIf="principal.authenticated" type="button" class="btn btn-link" (click)="onLogout()">Logout</button>
</div>
</nav>
<div class="jumbotron">
<div class="container">
<h1>Book Rater App</h1>
<p *ngIf="!principal.authenticated" class="lead">Anyone can view the books.</p>
<p *ngIf="principal.authenticated && !principal.isAdmin()" class="lead">Users can view and create ratings</p>
<p *ngIf="principal.isAdmin()" class="lead">Admins can do anything!</p>
</div>
</div>
<section class="books">
<div class="container">
<div class="row">
<div class="col-md">
<div class="row">
<div class="col-md-12">
<app-book-list [principal]="principal" (onBookSelected)="selectBook($event)"></app-book-list>
</div>
</div>
</div>
<div *ngIf="selectedBook != null" class="col-md-3">
<app-book-detail [selectedBook]="selectedBook" [principal]="principal" (closeBook)="closeBookDetail()"></app-book-detail>
</div>
</div>
</div>
</section>

View File

@ -0,0 +1,50 @@
import {Component} from "@angular/core";
import {Principal} from "./principal";
import {Response} from "@angular/http";
import {Book} from "./book";
import {HttpService} from "./http.service";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
selectedBook: Book = null;
principal: Principal = new Principal(false, []);
loginFailed: boolean = false;
constructor(private httpService: HttpService){}
ngOnInit(): void {
this.httpService.me()
.subscribe((response: Response) => {
let principalJson = response.json();
this.principal = new Principal(principalJson.authenticated, principalJson.authorities);
}, (error) => {
console.log(error);
});
}
onLogout() {
this.httpService.logout()
.subscribe((response: Response) => {
if (response.status === 200) {
this.loginFailed = false;
this.principal = new Principal(false, []);
window.location.replace(response.url);
}
}, (error) => {
console.log(error);
});
}
closeBookDetail() {
this.selectedBook = null;
}
selectBook(book: Book) {
this.selectedBook = book;
}
}

View File

@ -0,0 +1,28 @@
import {BrowserModule} from "@angular/platform-browser";
import {NgModule} from "@angular/core";
import {FormsModule} from "@angular/forms";
import {HttpModule} from "@angular/http";
import {AppComponent} from "./app.component";
import {RatingComponent} from "./rating/rating.component";
import {ClickStopPropagationDirective} from "./click-stop-propagation.directive";
import {BookDetailComponent} from "./book/book-detail/book-detail.component";
import {BookListComponent} from "./book/book-list/book-list.component";
import {HttpService} from "./http.service";
@NgModule({
declarations: [
AppComponent,
RatingComponent,
ClickStopPropagationDirective,
BookDetailComponent,
BookListComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [HttpService],
bootstrap: [AppComponent]
})
export class AppModule { }

View File

@ -0,0 +1,10 @@
export class Book {
id: number;
author: String;
title: String;
constructor(id: number, author: String, title: String){
this.id = id;
this.author = author;
this.title = title;
}
}

View File

@ -0,0 +1,11 @@
<div class="card">
<div class="card-block">
<button type="button" class="close" aria-label="Close" (click)="closeBookDetail()">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="card-title">Title: {{selectedBook.title}}</h4>
<h6 class="card-subtitle mb-2 text-muted">Author: {{selectedBook.author}}</h6>
<p class="card-text">A quick summary of the book</p>
<app-rating *ngIf="principal.authenticated" [bookId]="selectedBook.id" [principal]="principal"></app-rating>
</div>
</div>

View File

@ -0,0 +1,26 @@
import {Component, OnInit, Input, Output, EventEmitter} from "@angular/core";
import {Book} from "../../book";
import {Principal} from "../../principal";
@Component({
selector: 'app-book-detail',
templateUrl: './book-detail.component.html',
styleUrls: ['./book-detail.component.css']
})
export class BookDetailComponent implements OnInit {
@Input() selectedBook: Book = null;
@Input() principal: Principal = null;
@Output() closeBook: EventEmitter<any> = new EventEmitter<any>();
constructor() { }
ngOnInit() {
}
closeBookDetail() {
this.closeBook.emit(null);
}
}

View File

@ -0,0 +1,54 @@
<div class="col-md-12" *ngFor="let book of books; let i = index;" (click)="selectBook(book)">
<div class="card">
<div class="card-block">
<div *ngIf="booksToEdit.indexOf(i) === -1 ; then bookView else bookEdit"></div>
<ng-template #bookView>
<button appClickStopPropagation *ngIf="principal.isAdmin()" type="button" class="btn btn-danger custom-close" (click)="delete(i)">Delete</button>
<button appClickStopPropagation *ngIf="principal.isAdmin()" type="button" class="btn btn-warning custom-close" (click)="editBook(i)">Edit</button>
<h4 class="card-title">Title: {{book.title}}</h4>
<h6 class="card-subtitle mb-2 text-muted">Author: {{book.author}}</h6>
</ng-template>
<ng-template #bookEdit>
<button appClickStopPropagation type="button" class="btn btn-secondary custom-close" (click)="cancelEditBook(i)">Cancel</button>
<form appClickStopPropagation (ngSubmit)="saveBook(i, newBooks[i])" class="mt-2 mt-md-0" #f1="ngForm">
<div class="form-group">
<label for="title">Title:</label>
<input id="title" name="title" [(ngModel)]="newBooks[i].title" required class="form-control mr-sm-2" type="text">
</div>
<div class="form-group">
<label for="author">Author:</label>
<input id="author" name="author" [(ngModel)]="newBooks[i].author" required class="form-control mr-sm-2" type="text">
</div>
<button class="btn btn-outline-success my-2 my-sm-0" type="submit" [disabled]="!f1.valid">Save</button>
</form>
</ng-template>
</div>
</div>
</div>
<div *ngIf="principal.isAdmin()" class="col-md-12">
<div class="card">
<div class="card-block">
<div *ngIf="!isAddNewBook; then bookPlaceHolder else bookAdd"></div>
<ng-template #bookPlaceHolder>
<h4 (click)="activateAddNewBook()" class="card-title center-block">Add New Book</h4>
</ng-template>
<ng-template #bookAdd>
<button appClickStopPropagation type="button" class="btn btn-secondary custom-close" (click)="cancelAddBook()">Cancel</button>
<form appClickStopPropagation (ngSubmit)="addNewBook(newBook, titleNewBook)" class="mt-2 mt-md-0" #f2="ngForm">
<div class="form-group">
<label for="titleNewBook">Title:</label>
<input id="titleNewBook" name="title" [(ngModel)]="newBook.title" required class="form-control mr-sm-2" type="text" #titleNewBook>
</div>
<div class="form-group">
<label for="authorNewBook">Author:</label>
<input id="authorNewBook" name="author" [(ngModel)]="newBook.author" required class="form-control mr-sm-2" type="text">
</div>
<button class="btn btn-outline-success my-2 my-sm-0" type="submit" [disabled]="!f2.valid">Save</button>
</form>
</ng-template>
</div>
</div>
</div>

View File

@ -0,0 +1,122 @@
import {Component, OnInit, Input, Output, EventEmitter} from "@angular/core";
import {Principal} from "../../principal";
import {Book} from "../../book";
import {Response} from "@angular/http";
import {HttpService} from "../../http.service";
@Component({
selector: 'app-book-list',
templateUrl: './book-list.component.html',
styleUrls: ['./book-list.component.css']
})
export class BookListComponent implements OnInit {
@Input() principal: Principal = null;
@Output() onBookSelected: EventEmitter<Book> = new EventEmitter<Book>();
books: Book[] = [];
newBooks: Book[] = [];
newBook: Book = new Book(Math.floor(Math.random() * 1000), '', '');
booksToEdit: number[] = [];
isAddNewBook: boolean = false;
selectedBook: Book = null;
constructor(private httpService: HttpService) { }
ngOnInit() {
this.loadBooks();
}
loadBooks() {
this.httpService.getBooks()
.subscribe((response: Response) => {
let booksJson: any[] = response.json()
booksJson.forEach(book => {
this.books.push(new Book(book.id, book.author, book.title));
this.newBooks.push(new Book(book.id, book.author, book.title));
})
}, (error) => {
console.log(error);
});
}
cancelEditBook(bookIndex: number) {
if (this.booksToEdit.indexOf(bookIndex) !== -1) {
this.booksToEdit.splice(this.booksToEdit.indexOf(bookIndex), 1); //remove the index of the book to edit
//get the original book
let bookCopy: Book = new Book(this.books[bookIndex].id, this.books[bookIndex].author, this.books[bookIndex].title);
this.newBooks.splice(bookIndex,1,bookCopy); //replace the edited book with the old book
}
}
editBook(bookIndex: number) {
this.booksToEdit.push(bookIndex);
}
saveBook(bookIndex: number, newBook: Book) {
console.log(newBook);
//save the book to the database
this.httpService.updateBook(newBook)
.subscribe((response: Response) => {
let bookJson = response.json();
let book: Book = new Book(bookJson.id, bookJson.author, bookJson.title);
//update the current array of books
let bookArr: Book = this.books.find(b => b.id === book.id);
bookArr.title = book.title;
bookArr.author = book.author;
this.booksToEdit.splice(this.booksToEdit.indexOf(bookIndex), 1); //remove the index of the book to edit
}, (error) => {
console.log(error);
});
}
delete(bookIndex: number) {
let book: Book = this.books[bookIndex];
this.httpService.deleteBook(book)
.subscribe(() => {
if (this.selectedBook !== null && this.books[bookIndex].id === this.selectedBook.id) {
this.selectedBook = null;
this.onBookSelected.emit(this.selectedBook);
}
this.books.splice(bookIndex, 1); //remove the book at this index;
this.newBooks.splice(bookIndex, 1); //remove the editing book at this index
}, (error) => {
console.log(error);
});
}
activateAddNewBook() {
this.isAddNewBook = true;
this.newBook = new Book(null, '', '');
}
addNewBook(newBook: Book, element: any) {
//write new book to db
this.httpService.createBook(newBook)
.subscribe((response: Response) => {
let bookJson = response.json();
let book: Book = new Book(bookJson.id, bookJson.author, bookJson.title);
console.log(book);
this.books.push(book);
this.newBooks.push(book);
this.newBook = new Book(Math.floor(Math.random() * 1000), '', '');
element.focus();
}, (error) => {
console.log(error);
});
}
cancelAddBook() {
this.isAddNewBook = false;
}
selectBook(book: Book) {
this.selectedBook = book;
this.onBookSelected.emit(book);
}
}

View File

@ -0,0 +1,13 @@
import {Directive, HostListener} from "@angular/core";
@Directive({
selector: '[appClickStopPropagation]'
})
export class ClickStopPropagationDirective
{
@HostListener("click", ["$event"])
public onClick(event: any): void
{
event.stopPropagation();
}
}

View File

@ -0,0 +1,56 @@
import {Injectable} from "@angular/core";
import {Observable} from "rxjs";
import {Response, Http, Headers, RequestOptions} from "@angular/http";
import {Book} from "./book";
import {Rating} from "./rating";
@Injectable()
export class HttpService {
constructor(private http: Http) { }
me(): Observable<Response> {
return this.http.get("/me", this.makeOptions())
}
logout(): Observable<Response> {
return this.http.post("/logout", '', this.makeOptions())
}
getBooks(): Observable<Response> {
return this.http.get("/book-service/books", this.makeOptions())
}
updateBook(newBook: Book): Observable<Response> {
return this.http.put("/book-service/books/" + newBook.id, newBook, this.makeOptions())
}
deleteBook(book: Book): Observable<Response> {
return this.http.delete("/book-service/books/" + book.id, this.makeOptions())
}
createBook(newBook: Book): Observable<Response> {
return this.http.post("/book-service/books", newBook, this.makeOptions())
}
getRatings(bookId: number): Observable<Response> {
return this.http.get("/rating-service/ratings?bookId=" + bookId, this.makeOptions())
}
createRating(rating: Rating): Observable<Response> {
return this.http.post("/rating-service/ratings", rating, this.makeOptions())
}
deleteRating(ratingId: number) {
return this.http.delete("/rating-service/ratings/" + ratingId, this.makeOptions())
}
updateRating(rating: Rating) {
return this.http.put("/rating-service/ratings/" + rating.id, rating, this.makeOptions())
}
private makeOptions(): RequestOptions {
let headers = new Headers({'Content-Type': 'application/json'});
return new RequestOptions({headers: headers});
}
}

View File

@ -0,0 +1,21 @@
export class Principal {
public authenticated: boolean;
public authorities: Authority[] = [];
constructor(authenticated: boolean, authorities: any[]) {
this.authenticated = authenticated;
authorities.map(auth => this.authorities.push(new Authority(auth.authority)))
}
isAdmin() {
return this.authorities.some((auth: Authority) => auth.authority.indexOf('ADMIN') > -1)
}
}
export class Authority {
public authority: String;
constructor(authority: String) {
this.authority = authority;
}
}

View File

@ -0,0 +1,11 @@
export class Rating{
id: number;
bookId: number;
stars: number;
constructor(id: number, bookId: number, stars: number) {
this.id = id;
this.bookId = bookId;
this.stars = stars;
}
}

View File

@ -0,0 +1,11 @@
div.progress {
margin-top: 5px;
}
.rating:hover {
border: solid blue;
}
.selected {
border: solid blue;
}

View File

@ -0,0 +1,25 @@
Ratings:
<div *ngFor="let rating of ratings; let i = index;" class="row">
<div class="col-md-10">
<div class="progress" [ngClass]="{'selected': principal.isAdmin() && rating === newRating, 'rating': principal.isAdmin()}" (click)="selectRating(rating)">
<div class="progress-bar bg-success" role="progressbar" [style.width]="findWidth(rating)" [attr.aria-valuenow]="rating.stars" aria-valuemin="0" aria-valuemax="5"></div>
</div>
</div>
<div class="col-md-1">
<button *ngIf="principal?.isAdmin()" type="button" class="close" aria-label="Close" (click)="deleteRating(i)">
<span aria-hidden="true">&times;</span>
</button>
</div>
</div>
<form (ngSubmit)="onSaveRating(f)" #f="ngForm">
<div class="form-check form-check-inline" *ngFor="let star of stars; let i = index;">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="star" [(ngModel)]="newRating.stars" [value]="star">{{star}}
</label>
</div>
<button *ngIf="newRating.id === null" type="submit" class="btn btn-secondary" [disabled]="!f.valid">Add Rating</button>
<button *ngIf="principal.isAdmin() && newRating.id !== null" type="button" class="btn btn-secondary" (click)="updateRating()">Save</button>
<button *ngIf="principal.isAdmin() && newRating.id !== null" type="button" class="btn btn-secondary" (click)="cancelSelection()">Cancel</button>
</form>

View File

@ -0,0 +1,90 @@
import {Component, OnInit, Input, OnChanges} from "@angular/core";
import {Rating} from "../rating";
import {Principal} from "../principal";
import {HttpService} from "../http.service";
import {Response} from "@angular/http";
@Component({
selector: 'app-rating',
templateUrl: './rating.component.html',
styleUrls: ['./rating.component.css']
})
export class RatingComponent implements OnInit, OnChanges {
@Input() bookId: number;
@Input() principal: Principal = null;
ratings: Rating[] = [];
stars: number[] = [1,2,3,4,5];
newRating: Rating = null;
constructor(private httpService: HttpService) { }
ngOnInit() {}
ngOnChanges() {
this.newRating = new Rating(null, this.bookId, 1);
this.ratings = [];
this.loadRatings();
}
findWidth(rating: Rating): String {
let percent: number = (rating.stars/5)*100;
return percent.toString() + '%';
}
private loadRatings() {
this.httpService.getRatings(this.bookId)
.subscribe((response: Response) => {
let responseJson: any[] = response.json();
responseJson.forEach(rating => this.ratings.push(new Rating(rating.id, rating.bookId, rating.stars)))
}, (error) => {
console.log(error);
});
}
onSaveRating() {
console.log(this.newRating);
let ratingCopy: Rating = Object.assign({}, this.newRating);
this.httpService.createRating(ratingCopy)
.subscribe((response: Response) => {
let ratingJson = response.json()
this.ratings.push(new Rating(ratingJson.id, ratingJson.bookId, ratingJson.stars))
}, (error) => {
console.log(error);
});
}
updateRating() {
this.httpService.updateRating(this.newRating)
.subscribe(() => {
this.newRating = new Rating(null, this.bookId, 1);
}, (error) => {
console.log(error);
});
}
selectRating(rating: Rating) {
if (this.principal.isAdmin()) {
this.newRating = rating;
}
}
cancelSelection() {
this.newRating = new Rating(null, this.bookId, 1);
}
deleteRating(index: number) {
let rating = this.ratings[index];
this.httpService.deleteRating(rating.id)
.subscribe(() => {
if (this.ratings[index] === this.newRating) {
this.newRating = new Rating(null, this.bookId, 1);
}
this.ratings.splice(index, 1);
}, (error) => {
console.log(error);
});
}
}

View File

@ -0,0 +1,3 @@
export const environment = {
production: true
};

View File

@ -0,0 +1,8 @@
// The file contents for the current environment will overwrite these during build.
// The build system defaults to the dev environment which uses `environment.ts`, but if you do
// `ng build --env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `.angular-cli.json`.
export const environment = {
production: false
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

View File

@ -0,0 +1,14 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Ui</title>
<base href="/home/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root>Loading...</app-root>
</body>
</html>

View File

@ -0,0 +1,10 @@
import {enableProdMode} from "@angular/core";
import {platformBrowserDynamic} from "@angular/platform-browser-dynamic";
import {AppModule} from "./app/app.module";
import {environment} from "./environments/environment";
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule);

View File

@ -0,0 +1,15 @@
import "core-js/es6/reflect";
import "core-js/es7/reflect";
import "zone.js/dist/zone";
/***************************************************************************************************
* APPLICATION IMPORTS
*/
/**
* Date, currency, decimal and percent pipes.
* Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10
*/
// import 'intl'; // Run `npm install --save intl`.

View File

@ -0,0 +1 @@
/* You can add global styles to this file, and also import other style files */

View File

@ -0,0 +1,27 @@
import "zone.js/dist/long-stack-trace-zone";
import "zone.js/dist/proxy.js";
import "zone.js/dist/sync-test";
import "zone.js/dist/jasmine-patch";
import "zone.js/dist/async-test";
import "zone.js/dist/fake-async-test";
import {getTestBed} from "@angular/core/testing";
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from "@angular/platform-browser-dynamic/testing";
// Unfortunately there's no typing for the `__karma__` variable. Just declare it as any.
declare var __karma__: any;
declare var require: any;
// Prevent Karma from running prematurely.
__karma__.loaded = function () {};
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
// Finally, start Karma to run the tests.
__karma__.start();

View File

@ -0,0 +1,13 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"module": "es2015",
"baseUrl": "",
"types": []
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}

Some files were not shown because too many files have changed in this diff Show More