提交 46baa01f 编写于 作者: J Jason Song

Merge pull request #12 from yiming187/privilege_update

Add Privilege service,repo,controller and test in Portal
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 {
private static final long serialVersionUID = 7348554309210401557L;
@Id
private String id;
private String appId;
@Column(nullable = false)
private String name;
......@@ -36,12 +36,12 @@ public class App implements Serializable {
@Column
private Date lastUpdatedTimestamp;
public String getId() {
return id;
public String getAppId() {
return appId;
}
public void setId(String id) {
this.id = id;
public void setAppId(String appId) {
this.appId = appId;
}
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 {
@Test
public void testCreate() throws URISyntaxException {
App newApp = new App();
newApp.setId(String.valueOf(System.currentTimeMillis()));
newApp.setAppId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis());
URI uri = new URI("http://localhost:8080/apps");
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.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
public void testList() throws URISyntaxException {
App newApp = new App();
newApp.setId(String.valueOf(System.currentTimeMillis()));
newApp.setAppId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis());
appRepository.save(newApp);
......@@ -56,13 +56,13 @@ public class AppControllerTest extends AbstractPortalTest {
App[] apps = restTemplate.getForObject(uri, App[].class);
Assert.assertEquals(1, apps.length);
Assert.assertEquals(newApp.getId(), apps[0].getId());
Assert.assertEquals(newApp.getAppId(), apps[0].getAppId());
}
@Test
public void testListOutOfRange() throws URISyntaxException {
App newApp = new App();
newApp.setId(String.valueOf(System.currentTimeMillis()));
newApp.setAppId(String.valueOf(System.currentTimeMillis()));
newApp.setName("new app " + System.currentTimeMillis());
newApp.setOwner("owner " + System.currentTimeMillis());
appRepository.save(newApp);
......
......@@ -18,7 +18,7 @@ public class AppRepositoryTest extends AbstractPortalTest{
Assert.assertEquals(0, repository.count());
App ramdomApp = new App();
ramdomApp.setId(String.valueOf(System.currentTimeMillis()));
ramdomApp.setAppId(String.valueOf(System.currentTimeMillis()));
ramdomApp.setName("new app " + System.currentTimeMillis());
ramdomApp.setOwner("owner " + System.currentTimeMillis());
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 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.ctrip.framework</groupId>
<artifactId>framework-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>1.0</version>
<packaging>pom</packaging>
<name>Ctrip Framework Parent</name>
<description>Ctrip Framework Parent</description>
......@@ -15,7 +15,6 @@
<properties>
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<pluginManagement>
......@@ -26,12 +25,8 @@
<version>2.19.1</version>
<configuration>
<includes>
<include>**/*Tests.java</include>
<include>**/*Test.java</include>
<include>**/AllTests.java</include>
</includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
......@@ -104,7 +99,7 @@
<exclude>javax.servlet:servlet-api</exclude>
<exclude>org.mortbay.jetty:servlet-api-2.5</exclude>
</excludes>
<message>** There are some banned dependencies.</message>
<message>** prefer javax.servlet:javax.servlet-api</message>
</bannedDependencies>
</rules>
</configuration>
......@@ -156,12 +151,12 @@
</build>
<distributionManagement>
<repository>
<id>ctrip_fx_release</id>
<url>${ctrip.fx.release.repo}</url>
<id>releases</id>
<url>${releases.repo}</url>
</repository>
<snapshotRepository>
<id>ctrip_fx_snapshot</id>
<url>${ctrip.fx.snapshot.repo}</url>
<id>snapshots</id>
<url>${snapshots.repo}</url>
</snapshotRepository>
</distributionManagement>
</project>
</project>
\ No newline at end of file
......@@ -12,7 +12,7 @@
<parent>
<groupId>com.ctrip.framework</groupId>
<artifactId>framework-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>1.0</version>
<relativePath>framework-parent</relativePath>
</parent>
<organization>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册