Commit 157a51f6 authored by Yiming Liu's avatar Yiming Liu

Add Privilege service,repo,controller and test in Portal

parent fa1c4fe3
package com.ctrip.apollo.portal.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/privileges")
public class PrivilegeController {
}
...@@ -16,7 +16,7 @@ public class App implements Serializable { ...@@ -16,7 +16,7 @@ public class App implements Serializable {
private static final long serialVersionUID = 7348554309210401557L; private static final long serialVersionUID = 7348554309210401557L;
@Id @Id
private String id; private String appId;
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
...@@ -36,12 +36,12 @@ public class App implements Serializable { ...@@ -36,12 +36,12 @@ public class App implements Serializable {
@Column @Column
private Date lastUpdatedTimestamp; private Date lastUpdatedTimestamp;
public String getId() { public String getAppId() {
return id; return appId;
} }
public void setId(String id) { public void setAppId(String appId) {
this.id = id; this.appId = appId;
} }
public String getName() { public String getName() {
......
package com.ctrip.apollo.portal.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Privilege implements Serializable {
/**
*
*/
private static final long serialVersionUID = -430087307622435118L;
@Id
@GeneratedValue
private long id;
@Column
private String name;
@Column
private String privilType;
@Column
private String appId;
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;
}
public String getPrivilType() {
return privilType;
}
public void setPrivilType(String privilType) {
this.privilType = privilType;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
}
package com.ctrip.apollo.portal.repository;
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.ctrip.apollo.portal.entity.Privilege;
public interface PrivilegeRepository extends PagingAndSortingRepository<Privilege, Long> {
List<Privilege> findByAppId(String appId);
Privilege findByAppIdAndPrivilType(String appId, String privilType);
}
package com.ctrip.apollo.portal.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctrip.apollo.portal.entity.Privilege;
import com.ctrip.apollo.portal.repository.PrivilegeRepository;
@Service
public class PrivilegeService {
enum PrivilType {
EDIT, REVIEW, RELEASE
}
@Autowired
private PrivilegeRepository privilRepo;
public boolean hasPrivilege(String appId, String name, PrivilType privilType) {
Privilege privil = privilRepo.findByAppIdAndPrivilType(appId, privilType.name());
if (privil != null && privil.getName().equals(name)) return true;
return false;
}
public List<Privilege> listPrivileges(String appId) {
return privilRepo.findByAppId(appId);
}
public Privilege setPrivilege(String appId, String name, PrivilType privilType) {
Privilege privil = privilRepo.findByAppIdAndPrivilType(appId, privilType.name());
if (privil == null) {
privil = new Privilege();
privil.setAppId(appId);
privil.setPrivilType(privilType.name());
}
privil.setName(name);
return privilRepo.save(privil);
}
}
package com.ctrip.apollo.portal;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import com.ctrip.apollo.portal.controller.AppControllerTest;
import com.ctrip.apollo.portal.repository.AppRepositoryTest;
import com.ctrip.apollo.portal.service.PrivilegeServiceTest;
@RunWith(Suite.class)
@SuiteClasses({AppControllerTest.class, AppRepositoryTest.class, PrivilegeServiceTest.class,
})
public class AllTests {
}
...@@ -28,26 +28,26 @@ public class AppControllerTest extends AbstractPortalTest { ...@@ -28,26 +28,26 @@ public class AppControllerTest extends AbstractPortalTest {
@Test @Test
public void testCreate() throws URISyntaxException { public void testCreate() throws URISyntaxException {
App newApp = new App(); App newApp = new App();
newApp.setId(String.valueOf(System.currentTimeMillis())); newApp.setAppId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis()); newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis()); newApp.setOwner("owner " + System.currentTimeMillis());
URI uri = new URI("http://localhost:8080/apps"); URI uri = new URI("http://localhost:8080/apps");
App createdApp = restTemplate.postForObject(uri, newApp, App.class); App createdApp = restTemplate.postForObject(uri, newApp, App.class);
Assert.assertEquals(newApp.getId(), createdApp.getId()); Assert.assertEquals(newApp.getAppId(), createdApp.getAppId());
Assert.assertNull(newApp.getCreateTimestamp()); Assert.assertNull(newApp.getCreateTimestamp());
Assert.assertNotNull(createdApp.getCreateTimestamp()); Assert.assertNotNull(createdApp.getCreateTimestamp());
App foundApp = appRepository.findOne(newApp.getId()); App foundApp = appRepository.findOne(newApp.getAppId());
Assert.assertEquals(newApp.getId(), foundApp.getId()); Assert.assertEquals(newApp.getAppId(), foundApp.getAppId());
} }
@Test @Test
public void testList() throws URISyntaxException { public void testList() throws URISyntaxException {
App newApp = new App(); App newApp = new App();
newApp.setId(String.valueOf(System.currentTimeMillis())); newApp.setAppId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis()); newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis()); newApp.setOwner("owner " + System.currentTimeMillis());
appRepository.save(newApp); appRepository.save(newApp);
...@@ -56,13 +56,13 @@ public class AppControllerTest extends AbstractPortalTest { ...@@ -56,13 +56,13 @@ public class AppControllerTest extends AbstractPortalTest {
App[] apps = restTemplate.getForObject(uri, App[].class); App[] apps = restTemplate.getForObject(uri, App[].class);
Assert.assertEquals(1, apps.length); Assert.assertEquals(1, apps.length);
Assert.assertEquals(newApp.getId(), apps[0].getId()); Assert.assertEquals(newApp.getAppId(), apps[0].getAppId());
} }
@Test @Test
public void testListOutOfRange() throws URISyntaxException { public void testListOutOfRange() throws URISyntaxException {
App newApp = new App(); App newApp = new App();
newApp.setId(String.valueOf(System.currentTimeMillis())); newApp.setAppId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis()); newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis()); newApp.setOwner("owner " + System.currentTimeMillis());
appRepository.save(newApp); appRepository.save(newApp);
......
...@@ -18,7 +18,7 @@ public class AppRepositoryTest extends AbstractPortalTest{ ...@@ -18,7 +18,7 @@ public class AppRepositoryTest extends AbstractPortalTest{
Assert.assertEquals(0, repository.count()); Assert.assertEquals(0, repository.count());
App ramdomApp = new App(); App ramdomApp = new App();
ramdomApp.setId(String.valueOf(System.currentTimeMillis())); ramdomApp.setAppId(String.valueOf(System.currentTimeMillis()));
ramdomApp.setName("new app " + System.currentTimeMillis()); ramdomApp.setName("new app " + System.currentTimeMillis());
ramdomApp.setOwner("owner " + System.currentTimeMillis()); ramdomApp.setOwner("owner " + System.currentTimeMillis());
repository.save(ramdomApp); repository.save(ramdomApp);
......
package com.ctrip.apollo.portal.service;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctrip.apollo.portal.AbstractPortalTest;
import com.ctrip.apollo.portal.entity.App;
import com.ctrip.apollo.portal.entity.Privilege;
public class PrivilegeServiceTest extends AbstractPortalTest {
@Autowired
AppService appService;
@Autowired
PrivilegeService privilService;
@Test
public void testAddPrivilege() {
App newApp = new App();
newApp.setAppId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis());
appService.save(newApp);
privilService.setPrivilege(newApp.getAppId(), newApp.getOwner(),
PrivilegeService.PrivilType.EDIT);
List<Privilege> privileges = privilService.listPrivileges(newApp.getAppId());
Assert.assertEquals(1, privileges.size());
Assert.assertEquals(PrivilegeService.PrivilType.EDIT.name(), privileges.get(0).getPrivilType());
Assert.assertEquals(newApp.getOwner(), privileges.get(0).getName());
}
@Test
public void testCheckPrivilege() {
App newApp = new App();
newApp.setAppId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis());
appService.save(newApp);
privilService.setPrivilege(newApp.getAppId(), newApp.getOwner(),
PrivilegeService.PrivilType.EDIT);
Assert.assertTrue(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(),
PrivilegeService.PrivilType.EDIT));
Assert.assertFalse(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(),
PrivilegeService.PrivilType.REVIEW));
Assert.assertFalse(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(),
PrivilegeService.PrivilType.RELEASE));
privilService.setPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.EDIT);
Assert.assertTrue(
privilService.hasPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.EDIT));
Assert.assertFalse(privilService.hasPrivilege(newApp.getAppId(), newApp.getOwner(),
PrivilegeService.PrivilType.EDIT));
privilService.setPrivilege(newApp.getAppId(), "nobody", PrivilegeService.PrivilType.RELEASE);
Assert.assertTrue(privilService.hasPrivilege(newApp.getAppId(), "nobody",
PrivilegeService.PrivilType.RELEASE));
}
}
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.ctrip.framework</groupId> <groupId>com.ctrip.framework</groupId>
<artifactId>framework-parent</artifactId> <artifactId>framework-parent</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>1.0</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>Ctrip Framework Parent</name> <name>Ctrip Framework Parent</name>
<description>Ctrip Framework Parent</description> <description>Ctrip Framework Parent</description>
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
<properties> <properties>
<java.version>1.7</java.version> <java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties> </properties>
<build> <build>
<pluginManagement> <pluginManagement>
...@@ -26,12 +25,8 @@ ...@@ -26,12 +25,8 @@
<version>2.19.1</version> <version>2.19.1</version>
<configuration> <configuration>
<includes> <includes>
<include>**/*Tests.java</include> <include>**/AllTests.java</include>
<include>**/*Test.java</include>
</includes> </includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
...@@ -104,7 +99,7 @@ ...@@ -104,7 +99,7 @@
<exclude>javax.servlet:servlet-api</exclude> <exclude>javax.servlet:servlet-api</exclude>
<exclude>org.mortbay.jetty:servlet-api-2.5</exclude> <exclude>org.mortbay.jetty:servlet-api-2.5</exclude>
</excludes> </excludes>
<message>** There are some banned dependencies.</message> <message>** prefer javax.servlet:javax.servlet-api</message>
</bannedDependencies> </bannedDependencies>
</rules> </rules>
</configuration> </configuration>
...@@ -156,12 +151,12 @@ ...@@ -156,12 +151,12 @@
</build> </build>
<distributionManagement> <distributionManagement>
<repository> <repository>
<id>ctrip_fx_release</id> <id>releases</id>
<url>${ctrip.fx.release.repo}</url> <url>${releases.repo}</url>
</repository> </repository>
<snapshotRepository> <snapshotRepository>
<id>ctrip_fx_snapshot</id> <id>snapshots</id>
<url>${ctrip.fx.snapshot.repo}</url> <url>${snapshots.repo}</url>
</snapshotRepository> </snapshotRepository>
</distributionManagement> </distributionManagement>
</project> </project>
\ No newline at end of file
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<parent> <parent>
<groupId>com.ctrip.framework</groupId> <groupId>com.ctrip.framework</groupId>
<artifactId>framework-parent</artifactId> <artifactId>framework-parent</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>1.0</version>
<relativePath>framework-parent</relativePath> <relativePath>framework-parent</relativePath>
</parent> </parent>
<organization> <organization>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment