Skip to content

Commit

Permalink
Add Privilege service,repo,controller and test in Portal
Browse files Browse the repository at this point in the history
  • Loading branch information
yiming187 committed Mar 14, 2016
1 parent fa1c4fe commit 157a51f
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 27 deletions.
@@ -0,0 +1,10 @@
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 {

}
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
@@ -0,0 +1,62 @@
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;
}
}
@@ -0,0 +1,14 @@
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);
}
@@ -0,0 +1,41 @@
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);
}
}
17 changes: 17 additions & 0 deletions apollo-portal/src/test/java/com/ctrip/apollo/portal/AllTests.java
@@ -0,0 +1,17 @@
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 {

}
Expand Up @@ -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);
Expand All @@ -56,13 +56,13 @@ public void testList() throws URISyntaxException {

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);
Expand Down
Expand Up @@ -18,7 +18,7 @@ public void testCreate() {
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);
Expand Down
@@ -0,0 +1,64 @@
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));
}
}
21 changes: 8 additions & 13 deletions framework-parent/pom.xml
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -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>
Expand Down

0 comments on commit 157a51f

Please sign in to comment.