Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
Add cost center example
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekaterina Gavrilova committed Sep 14, 2017
1 parent 3c2d43a commit 2e7a336
Show file tree
Hide file tree
Showing 48 changed files with 1,928 additions and 10 deletions.
31 changes: 31 additions & 0 deletions .gitignore
@@ -0,0 +1,31 @@
**/target/
**/.settings/
**/.project
**/.classpath
**/.DS_Store
**/*.pyc
**/*.pmd
**/*.fpr
**/sdkdist.zip
**/*.ipr
**/*.iws
**/bin/
**/*.iml
**/*.idea
**/.cache
**/.cache-main
**/WebContent
**/META-INF/application.xml
**/*.sublime-project
**/*.sublime-workspace
**/pom.xml.versionsBackup
*.sh
**/sdkdist*
**/example*
**/tmprepo
**/scp/
**/node_modules
**/credentials.json
**/reports
/e2e-tests/*.log
.org.chromium.Chromium.*
12 changes: 12 additions & 0 deletions Costcenter-Controller-CF/CreateReport.js
@@ -0,0 +1,12 @@
const report = require('cucumber-html-report');
report.create({
source: 'reports/e2e/cucumber.json', // source json
dest: 'reports/e2e/prod', // target directory (will create if not exists)
name: 'report.html', // report file name (will be index.html if not exists)
title: 'Cucumber Report', // Title for default template. (default is Cucumber Report)
component: 'My Component', // Subtitle for default template. (default is empty)
screenshots: 'reports/e2e/screenshots', // Path to the directory of screenshots. Optional.
maxScreenshots: 10 // Max number of screenshots to save (default is 1000)
})
.then(console.log)
.catch(console.error);
7 changes: 7 additions & 0 deletions Costcenter-Controller-CF/Jenkinsfile
@@ -0,0 +1,7 @@
#!/usr/bin/env groovy

node {
deleteDir()
sh "git clone --depth 1 https://github.com/SAP/cloud-s4-sdk-pipeline.git pipelines"
load './pipelines/s4sdk-pipeline.groovy'
}
61 changes: 61 additions & 0 deletions Costcenter-Controller-CF/README.md
@@ -0,0 +1,61 @@
# CostCenter Controller CF
[![Build Status](http://mo-878278750.mo.sap.corp:8080/buildStatus/icon?job=partner/Costcenter-Controller-CF/master)](http://mo-878278750.mo.sap.corp:8080/job/partner/job/Costcenter-Controller-CF/job/master/)

## Build
```shell
mvn clean compile
```

## Test
```shell
mvn test
```

### Start Local Server
```shell
mvn spring-boot:run
```

## Run E2E Tests against Local Server
```shell
npm install
ENV_LAUNCH_URL=http://localhost:8080 npm run ci-e2e
```

## Shutdown local Server
```shell
curl -X POST localhost:8080/shutdown
```

## Deploy to CF
```shell
cf api https://api.cf.eu10.hana.ondemand.com
cf login
mvn clean package && cf push
```

## E2E Tests
Configuration reads hosts from environment, so set URL like this:
```shell
export ENV_LAUNCH_URL=https://costcenter-cf-spring-test.cfapps.eu10.hana.ondemand.com
```

- Run Tests
```shell
npm install
npm run ci-e2e
```

## Edit manifest.yml

- Host
- Endpoints

```
https://costcenter-cf-spring-<space>.cfapps.sap.hana.ondemand.com/index.html
```

## Useful CF Commands
```shell
cf logs costcenter-cf-spring --recent
```
5 changes: 5 additions & 0 deletions Costcenter-Controller-CF/application.properties
@@ -0,0 +1,5 @@
#No auth protected
endpoints.shutdown.sensitive=false

#Enable shutdown endpoint
endpoints.shutdown.enabled=true
83 changes: 83 additions & 0 deletions Costcenter-Controller-CF/application/pom.xml
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>

<name>Cost Center Controller - Application</name>
<description>costcenter-cf-spring - Application</description>

<artifactId>costcenter-cf-spring-application</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>com.sap.cloud.sdk.showcase</groupId>
<artifactId>costcenter-cf-spring</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<dependencies>
<!-- SDK dependencies -->
<dependency>
<groupId>com.sap.cloud.s4hana.cloudplatform</groupId>
<artifactId>scp-cf</artifactId>
</dependency>
<dependency>
<groupId>com.sap.cloud.s4hana</groupId>
<artifactId>s4hana-all</artifactId>
</dependency>

<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.2.RELEASE</version>
<configuration>
<attach>false</attach>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,23 @@
package com.sap.cloud.sdk.tutorial;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
@ServletComponentScan( "com.sap.cloud.sdk" )
public class Application extends SpringBootServletInitializer
{
@Override
protected SpringApplicationBuilder configure( final SpringApplicationBuilder application )
{
return application.sources(Application.class);
}

public static void main( final String[] args )
{
SpringApplication.run(Application.class, args);
}
}
@@ -0,0 +1,70 @@
package com.sap.cloud.sdk.tutorial.command;

import com.netflix.config.ConfigurationManager;

import java.util.List;

import com.sap.cloud.sdk.s4hana.config.S4HanaConfig;
import com.sap.cloud.sdk.s4hana.connectivity.ErpCommand;
import com.sap.cloud.sdk.s4hana.connectivity.ErpConfigContext;
import com.sap.cloud.sdk.s4hana.datamodel.bapi.services.CostCenterService;
import com.sap.cloud.sdk.s4hana.datamodel.bapi.structures.CostCenterCreateInput;
import com.sap.cloud.sdk.s4hana.datamodel.bapi.structures.ReturnParameter;
import com.sap.cloud.sdk.s4hana.datamodel.bapi.types.CostCenter;
import com.sap.cloud.sdk.s4hana.datamodel.bapi.types.CostCenterManager;
import com.sap.cloud.sdk.s4hana.datamodel.bapi.types.CurrencyKey;
import com.sap.cloud.sdk.s4hana.datamodel.bapi.types.IndicatorForCostCenterType;
import com.sap.cloud.sdk.s4hana.datamodel.bapi.types.SetId;
import com.sap.cloud.sdk.s4hana.serialization.ErpBoolean;
import com.sap.cloud.sdk.tutorial.models.CostCenterDetails;

import static com.sap.cloud.sdk.s4hana.config.S4HanaConfig.BAPI_SERIALIZATION_STRATEGY;

public class CreateCostCenterCommand extends ErpCommand<List<ReturnParameter>>
{
private final CostCenterDetails details;
private final boolean testRun;

public CreateCostCenterCommand( final ErpConfigContext configContext, final CostCenterDetails costCenterDetails, final boolean isTestRun)
{
super(CreateCostCenterCommand.class, configContext);
this.details = costCenterDetails;
this.testRun = isTestRun;

// for now use JSON
ConfigurationManager.getConfigInstance().setProperty(
BAPI_SERIALIZATION_STRATEGY,
S4HanaConfig.RemoteFunctionSerializationStrategy.JSON);
}

@Override
protected List<ReturnParameter> run()
throws Exception
{
final List<ReturnParameter> returnValues =
CostCenterService
.createMultiple(
// Required parameter: ControllingArea
details.getControllingArea(),
// Required parameter: CostCenter Input
CostCenterCreateInput
.builder()
.validFrom(CostCenterDetails.asLocalDate(details.getValidFrom()))
.validTo(CostCenterDetails.asLocalDate(details.getValidTo()))
.costcenter(new CostCenter(details.getId().getValue()))
.name("SAP dummy")
.currency(CurrencyKey.of("EUR"))
.descript(details.getDescription())
.costcenterType(IndicatorForCostCenterType.of(details.getCategory()))
.personInCharge(CostCenterManager.of(details.getPersonResponsible()))
.costctrHierGrp(SetId.of(details.getCostCenterGroup()))
.compCode(details.getCompanyCode())
.profitCtr(details.getProfitCenter())
.build())
.testRun(new ErpBoolean(testRun))
.execute(getConfigContext())
.getMessages();

return returnValues;
}
}
@@ -0,0 +1,64 @@
package com.sap.cloud.sdk.tutorial.command;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.slf4j.Logger;

import java.util.List;

import com.sap.cloud.sdk.cloudplatform.cache.CacheKey;
import com.sap.cloud.sdk.cloudplatform.logging.CloudLoggerFactory;
import com.sap.cloud.sdk.odatav2.connectivity.ODataQuery;
import com.sap.cloud.sdk.odatav2.connectivity.ODataQueryBuilder;
import com.sap.cloud.sdk.odatav2.connectivity.ODataQueryResult;
import com.sap.cloud.sdk.s4hana.connectivity.CachingErpCommand;
import com.sap.cloud.sdk.s4hana.connectivity.ErpConfigContext;
import com.sap.cloud.sdk.s4hana.connectivity.exception.QueryExecutionException;
import com.sap.cloud.sdk.tutorial.models.CostCenterDetails;


public class GetCostCenterCommand extends CachingErpCommand<List<CostCenterDetails>> {
private static final Logger logger = CloudLoggerFactory.getLogger(GetCostCenterCommand.class);

public GetCostCenterCommand(final ErpConfigContext configContext) {
super(GetCostCenterCommand.class, configContext);
}

private static final Cache<CacheKey, List<CostCenterDetails>> cache =
CacheBuilder.newBuilder().concurrencyLevel(10).build();

@Override
protected Cache<CacheKey, List<CostCenterDetails>> getCache() {
return cache;
}

@Override
protected CacheKey getCommandCacheKey() {
return super.getCommandCacheKey().append(getConfigContext());
}

@Override
protected List<CostCenterDetails> runCacheable() throws QueryExecutionException {
try {
final ODataQuery query = ODataQueryBuilder
.withEntity("/sap/opu/odata/sap/FCO_PI_COST_CENTER", "CostCenterCollection")
.select("CostCenterID",
"CostCenterDescription",
"Status",
"CompanyCode",
"Category",
"ValidityStartDate",
"ValidityEndDate")
.build();

final ODataQueryResult result = query.execute(getErpEndpoint());
return result.asList(CostCenterDetails.class);
} catch (final Exception e) {
throw new QueryExecutionException("Failed to get CostCenters from OData by using cached command.", e);
}
}

public void resetCache() {
cache.invalidate(getCommandCacheKey());
}
}

0 comments on commit 2e7a336

Please sign in to comment.