github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/indexer/test/data.go (about)

     1  // Copyright 2018-2022 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package test
    20  
    21  import (
    22  	"encoding/json"
    23  	"os"
    24  	"path"
    25  )
    26  
    27  // User is a user.
    28  type User struct {
    29  	ID, UserName, Email string
    30  	UID                 int
    31  }
    32  
    33  // Pet is a pet.
    34  type Pet struct {
    35  	ID, Kind, Color, Name string
    36  	UID                   int
    37  }
    38  
    39  // Account mocks an ocis account.
    40  type Account struct {
    41  	ID                       string
    42  	OnPremisesSamAccountName string
    43  	Mail                     string
    44  }
    45  
    46  // Data mock data.
    47  var Data = map[string][]interface{}{
    48  	"users": {
    49  		User{ID: "abcdefg-123", UserName: "mikey", Email: "mikey@example.com"},
    50  		User{ID: "hijklmn-456", UserName: "frank", Email: "frank@example.com"},
    51  		User{ID: "ewf4ofk-555", UserName: "jacky", Email: "jacky@example.com"},
    52  		User{ID: "rulan54-777", UserName: "jones", Email: "jones@example.com"},
    53  	},
    54  	"pets": {
    55  		Pet{ID: "rebef-123", Kind: "Dog", Color: "Brown", Name: "Waldo"},
    56  		Pet{ID: "wefwe-456", Kind: "Cat", Color: "White", Name: "Snowy"},
    57  		Pet{ID: "goefe-789", Kind: "Hog", Color: "Green", Name: "Dicky"},
    58  		Pet{ID: "xadaf-189", Kind: "Hog", Color: "Green", Name: "Ricky"},
    59  	},
    60  	"accounts": {
    61  		Account{ID: "ba5b6e54-e29d-4b2b-8cc4-0a0b958140d2", Mail: "spooky@skeletons.org", OnPremisesSamAccountName: "MrDootDoot"},
    62  	},
    63  }
    64  
    65  // WriteIndexTestData writes mock data to disk.
    66  func WriteIndexTestData(m map[string][]interface{}, privateKey, dir string) (string, error) {
    67  	rootDir, err := getRootDir(dir)
    68  	if err != nil {
    69  		return "", err
    70  	}
    71  
    72  	err = writeData(m, privateKey, rootDir)
    73  	if err != nil {
    74  		return "", err
    75  	}
    76  
    77  	return rootDir, nil
    78  }
    79  
    80  // getRootDir allows for some minimal behavior on destination on disk. Testing the cs3 api behavior locally means
    81  // keeping track of where the cs3 data lives on disk, this function allows for multiplexing whether or not to use a
    82  // temporary folder or an already defined one.
    83  func getRootDir(dir string) (string, error) {
    84  	var rootDir string
    85  	var err error
    86  
    87  	if dir != "" {
    88  		rootDir = dir
    89  	} else {
    90  		rootDir, err = CreateTmpDir()
    91  		if err != nil {
    92  			return "", err
    93  		}
    94  	}
    95  	return rootDir, nil
    96  }
    97  
    98  // writeData writes test data to disk on rootDir location Marshaled as json.
    99  func writeData(m map[string][]interface{}, privateKey string, rootDir string) error {
   100  	for dirName := range m {
   101  		fileTypePath := path.Join(rootDir, dirName)
   102  
   103  		if err := os.MkdirAll(fileTypePath, 0755); err != nil {
   104  			return err
   105  		}
   106  		for _, u := range m[dirName] {
   107  			data, err := json.Marshal(u)
   108  			if err != nil {
   109  				return err
   110  			}
   111  
   112  			pkVal := ValueOf(u, privateKey)
   113  			if err := os.WriteFile(path.Join(fileTypePath, pkVal), data, 0600); err != nil {
   114  				return err
   115  			}
   116  		}
   117  	}
   118  	return nil
   119  }