Skip to content

Commit

Permalink
Move justification to result package.
Browse files Browse the repository at this point in the history
This package is a location of all the data that has been added from all
the data loaded in and should be separated from the regular data in lib/
  • Loading branch information
James C. Scott committed Oct 3, 2016
1 parent 1f65776 commit f898b26
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 60 deletions.
36 changes: 17 additions & 19 deletions commands/docs/gitbook/gitbookCertification.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"io/ioutil"
"path/filepath"

"github.com/opencontrol/compliance-masonry/lib"
"github.com/opencontrol/compliance-masonry/lib/common"
"github.com/opencontrol/compliance-masonry/lib/result"
)

func (openControl *OpenControlGitBook) getResponsibleRole(text string, component common.Component) string {
Expand Down Expand Up @@ -51,7 +51,7 @@ func (openControl *OpenControlGitBook) getParameter(text string, parameter commo
return text
}

func (openControl *OpenControlGitBook) getCoveredBy(text string, justification lib.Verification) string {
func (openControl *OpenControlGitBook) getCoveredBy(text string, justification result.Verification) string {
if len(justification.SatisfiesData.GetCoveredBy()) > 0 {
text += "Covered By:\n"
}
Expand Down Expand Up @@ -89,27 +89,25 @@ func (openControl *OpenControlGitBook) getControlOrigin(text string, controlOrig
func (openControl *OpenControlGitBook) exportControl(control *ControlGitbook) (string, string) {
key := replaceParentheses(fmt.Sprintf("%s-%s", control.standardKey, control.controlKey))
text := fmt.Sprintf("#%s\n##%s\n", key, control.GetName())
openControl.Justifications.GetAndApply(control.standardKey, control.controlKey, func(selectJustifications lib.Verifications) {
// In the case that no information was found period for the standard and control
if len(selectJustifications) == 0 {
errorText := fmt.Sprintf("No information found for the combination of standard %s and control %s", control.standardKey, control.controlKey)
text = fmt.Sprintf("%s%s\n", text, errorText)
return
}
for _, justification := range selectJustifications {
component := openControl.Components.Get(justification.ComponentKey)
text = fmt.Sprintf("%s\n#### %s\n", text, component.GetName())
selectJustifications := openControl.Justifications.Get(control.standardKey, control.controlKey)
// In the case that no information was found period for the standard and control
if len(selectJustifications) == 0 {
errorText := fmt.Sprintf("No information found for the combination of standard %s and control %s", control.standardKey, control.controlKey)
text = fmt.Sprintf("%s%s\n", text, errorText)
}
for _, justification := range selectJustifications {
component := openControl.Components.Get(justification.ComponentKey)
text = fmt.Sprintf("%s\n#### %s\n", text, component.GetName())

text = openControl.getResponsibleRole(text, component)
text = openControl.getResponsibleRole(text, component)

text = openControl.getParameters(text, justification.SatisfiesData.GetParameters())
text = openControl.getParameters(text, justification.SatisfiesData.GetParameters())

text = openControl.getControlOrigin(text, justification.SatisfiesData.GetControlOrigin())
text = openControl.getControlOrigin(text, justification.SatisfiesData.GetControlOrigin())

text = openControl.getNarratives(justification.SatisfiesData.GetNarratives(), text, control)
text = openControl.getCoveredBy(text, justification)
}
})
text = openControl.getNarratives(justification.SatisfiesData.GetNarratives(), text, control)
text = openControl.getCoveredBy(text, justification)
}
return filepath.Join(control.exportPath, key+".md"), text
}

Expand Down
3 changes: 2 additions & 1 deletion lib/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/opencontrol/compliance-masonry/lib/common/mocks"
"github.com/stretchr/testify/assert"
"path/filepath"
"github.com/opencontrol/compliance-masonry/lib/result"
)

func TestAddComponent(t *testing.T) {
Expand Down Expand Up @@ -40,7 +41,7 @@ func TestCompareAndAddComponent(t *testing.T) {
}

func TestLoadSameComponentTwice(t *testing.T) {
ws := LocalWorkspace{Components: newComponents(), Justifications: NewJustifications()}
ws := LocalWorkspace{Components: newComponents(), Justifications: result.NewJustifications()}
componentPath := filepath.Join("..", "fixtures", "component_fixtures", "v3_1_0", "EC2")
err := ws.LoadComponent(componentPath)
// Should load the component without a problem.
Expand Down
29 changes: 11 additions & 18 deletions lib/justifications.go → lib/result/justifications.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lib
package result

import (
"sync"
Expand All @@ -17,7 +17,7 @@ type Verifications []Verification

// Justifications struct contains the mapping that links controls to specific components
type Justifications struct {
mapping map[string]map[string]Verifications
Mapping map[string]map[string]Verifications
sync.RWMutex
}

Expand All @@ -38,23 +38,23 @@ func (slice Verifications) Swap(i, j int) {

// NewJustifications creates a new justification
func NewJustifications() *Justifications {
return &Justifications{mapping: make(map[string]map[string]Verifications)}
return &Justifications{Mapping: make(map[string]map[string]Verifications)}
}

// Add methods adds a new mapping to the justification while locking
func (justifications *Justifications) Add(standardKey string, controlKey string, componentKey string, satisfies common.Satisfies) {
justifications.Lock()
newVerification := Verification{componentKey, satisfies}
_, standardKeyExists := justifications.mapping[standardKey]
_, standardKeyExists := justifications.Mapping[standardKey]
if !standardKeyExists {
justifications.mapping[standardKey] = make(map[string]Verifications)
justifications.Mapping[standardKey] = make(map[string]Verifications)
}
_, controlKeyExists := justifications.mapping[standardKey][controlKey]
_, controlKeyExists := justifications.Mapping[standardKey][controlKey]
if !controlKeyExists {
justifications.mapping[standardKey][controlKey] = Verifications{}
justifications.Mapping[standardKey][controlKey] = Verifications{}
}
justifications.mapping[standardKey][controlKey] = append(
justifications.mapping[standardKey][controlKey], newVerification,
justifications.Mapping[standardKey][controlKey] = append(
justifications.Mapping[standardKey][controlKey], newVerification,
)
justifications.Unlock()
}
Expand All @@ -68,20 +68,13 @@ func (justifications *Justifications) LoadMappings(component common.Component) {

// Get retrieves justifications for a specific standard and control
func (justifications *Justifications) Get(standardKey string, controlKey string) Verifications {
_, standardKeyExists := justifications.mapping[standardKey]
_, standardKeyExists := justifications.Mapping[standardKey]
if !standardKeyExists {
return nil
}
controlJustifications, controlKeyExists := justifications.mapping[standardKey][controlKey]
controlJustifications, controlKeyExists := justifications.Mapping[standardKey][controlKey]
if !controlKeyExists {
return nil
}
return controlJustifications
}

//GetAndApply get a justification set and apply a generic function
func (justifications *Justifications) GetAndApply(standardKey string, controlKey string, callback func(selectJustifications Verifications)) {
justifications.Lock()
callback(justifications.Get(standardKey, controlKey))
justifications.Unlock()
}
22 changes: 3 additions & 19 deletions lib/justifications_test.go → lib/result/justifications_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lib
package result

import "testing"

Expand Down Expand Up @@ -37,8 +37,8 @@ func TestJustificationAdd(t *testing.T) {
just.Add(mapping.standardKey, mapping.controlKey, mapping.componentKey, nil)
}
// Check that the expected stored standards are the actual standards
if example.expectedCount != len(just.mapping) {
t.Errorf("Expected %d, Actual: %d", example.expectedCount, len(just.mapping))
if example.expectedCount != len(just.Mapping) {
t.Errorf("Expected %d, Actual: %d", example.expectedCount, len(just.Mapping))
}
}
}
Expand Down Expand Up @@ -66,22 +66,6 @@ func TestJustificationGet(t *testing.T) {
}
}

func TestJustificationGetAndApply(t *testing.T) {
for _, example := range justificationsGetTests {
just := NewJustifications()
for _, mapping := range example.mappings {
just.Add(mapping.standardKey, mapping.controlKey, mapping.componentKey, nil)
}
just.GetAndApply("a", "b", func(actualVerificaitons Verifications) {
numberofABs := actualVerificaitons.Len()
// Check that the number of controls stored is the expected number
if example.expectedCount != numberofABs {
t.Errorf("Expected %d, Actual: %d", example.expectedCount, numberofABs)
}
})
}
}

var verificationsLenTests = []verificationsLenTest{
// Check that the number of verifications stored is 0
{Verifications{}, 0},
Expand Down
5 changes: 3 additions & 2 deletions lib/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"sync"
"github.com/codegangsta/cli"
"github.com/opencontrol/compliance-masonry/lib/common"
"github.com/opencontrol/compliance-masonry/lib/result"
)

// LocalWorkspace struct combines components, standards, and a certification data
// For more information on the opencontrol schema visit: https://github.com/opencontrol/schemas
type LocalWorkspace struct {
Components *componentsMap
Standards *standardsMap
Justifications *Justifications
Justifications *result.Justifications
Certification common.Certification
}

Expand All @@ -28,7 +29,7 @@ func getKey(filePath string) string {
// NewWorkspace initializes an empty OpenControl struct
func NewWorkspace() *LocalWorkspace {
return &LocalWorkspace{
Justifications: NewJustifications(),
Justifications: result.NewJustifications(),
Components: newComponents(),
Standards: newStandards(),
}
Expand Down
2 changes: 1 addition & 1 deletion lib/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestLoadData(t *testing.T) {
t.Errorf("Expected: `%d`, Actual: `%d`", example.expectedComponents, actualComponentNum)
}
// Check the number of justifications
actualJustificationNum := len(actual.Justifications.mapping)
actualJustificationNum := len(actual.Justifications.Mapping)
if actualJustificationNum != example.expectedJustificationNum {
t.Errorf("Expected: `%d`, Actual: `%d`", example.expectedComponents, actualComponentNum)
}
Expand Down

0 comments on commit f898b26

Please sign in to comment.